[Common] Moved StringToArray function to utils and applied auto-format
[platform/core/api/webapi-plugins.git] / src / common / extension.h
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #ifndef COMMON_EXTENSION_H_
7 #define COMMON_EXTENSION_H_
8
9 // This is a C++ wrapper over Crosswalk Extension C API. It implements once the
10 // boilerplate for the common case of mapping XW_Extension and XW_Instance to
11 // objects of their own. The wrapper deals automatically with creating and
12 // destroying the objects.
13 //
14 // Extension object lives during the lifetime of the extension, and when the
15 // extension process is properly shutdown, it's destructor will be
16 // called. Instance objects (there can be many) live during the lifetime of a
17 // script context associated with a frame in the page. These objects serves as
18 // storage points for extension specific objects, use them for that.
19
20 #include <sys/types.h>
21
22 #include <functional>
23 #include <map>
24 #include <mutex>
25 #include <string>
26 #include <unordered_set>
27
28 #include "common/XW_Extension.h"
29 #include "common/XW_Extension_EntryPoints.h"
30 #include "common/XW_Extension_Permissions.h"
31 #include "common/XW_Extension_Runtime.h"
32 #include "common/XW_Extension_SyncMessage.h"
33 #include "common/platform_exception.h"
34 #include "common/platform_result.h"
35
36 namespace common {
37
38 class Instance;
39 class Extension;
40
41 }  // namespace common
42
43 // This function should be implemented by each extension and should return
44 // an appropriate Extension subclass.
45 common::Extension* CreateExtension();
46
47 namespace common {
48
49 // implemented in XW_Extension.cc
50 // can be called only after the extension is fully created
51 //    (CreateExtension() has been called)
52 Extension* GetCurrentExtension();
53
54 class Extension {
55  public:
56   Extension();
57   virtual ~Extension();
58
59   // These should be called in the subclass constructor.
60   void SetExtensionName(const char* name);
61   void SetJavaScriptAPI(const char* api);
62   void SetExtraJSEntryPoints(const char** entry_points);
63   bool RegisterPermissions(const char* perm_table);
64
65   // This API should be called in the message handler of extension
66   bool CheckAPIAccessControl(const char* api_name);
67
68   virtual Instance* CreateInstance();
69
70   std::string GetRuntimeVariable(const char* var_name, unsigned len);
71
72  private:
73   friend int32_t(::XW_Initialize)(XW_Extension extension, XW_GetInterface get_interface);
74
75   static int32_t XW_Initialize(XW_Extension extension, XW_GetInterface get_interface,
76                                XW_Initialize_Func initialize,
77                                XW_CreatedInstanceCallback created_instance,
78                                XW_ShutdownCallback shutdown);
79
80   // XW_Extension callbacks.
81   static void OnInstanceCreated(XW_Instance xw_instance, Instance* instance);  // modified
82   static void OnInstanceDestroyed(XW_Instance xw_instance);
83   static void HandleMessage(XW_Instance xw_instance, const char* msg);
84   static void HandleSyncMessage(XW_Instance xw_instance, const char* msg);
85
86   XW_Extension xw_extension_;
87
88   class Detail;
89 };
90
91 class Instance {
92  public:
93   Instance();
94   virtual ~Instance();
95
96   static void DoAndPostMessage(Instance* that, const std::function<void()>& work,
97                                const picojson::value& json);
98   static void PostMessage(Instance* that, const char* msg);
99   static void PostMessage(Instance* that, const picojson::value& json);
100   void PostMessage(const char* msg);
101   void SendSyncReply(const char* reply);
102
103   virtual void Initialize() {
104   }
105   virtual void HandleMessage(const char* msg) = 0;
106   virtual void HandleSyncMessage(const char* msg) {
107   }
108
109   XW_Instance xw_instance() const {
110     return xw_instance_;
111   }
112
113  private:
114   friend class Extension;
115
116   static std::mutex instances_mutex_;
117   static std::unordered_set<Instance*> all_instances_;
118
119   XW_Instance xw_instance_;
120 };
121
122 typedef std::function<void(const picojson::value&, picojson::object&)> NativeHandler;
123
124 class ParsedInstance : public Instance {
125  public:
126   ParsedInstance();
127   virtual ~ParsedInstance();
128
129  protected:
130   void RegisterHandler(const std::string& name, const NativeHandler& func);
131   void RegisterSyncHandler(const std::string& name, const NativeHandler& func);
132
133   static void ReportSuccess(picojson::object& out);
134   static void ReportSuccess(const picojson::value& result, picojson::object& out);
135   static void ReportError(picojson::object& out);
136   static void ReportError(const PlatformException& ex, picojson::object& out);
137   static void ReportError(const PlatformResult& error, picojson::object* out);
138
139  private:
140   void HandleMessage(const char* msg);
141   void HandleSyncMessage(const char* msg);
142
143   void HandleMessage(const char* msg, bool is_sync);
144   void HandleException(const PlatformException& ex);
145   void HandleError(const PlatformResult& error);
146
147   std::map<std::string, NativeHandler> handler_map_;
148 };
149
150 }  // namespace common
151
152 #endif  // COMMON_EXTENSION_H_