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.
6 #ifndef COMMON_EXTENSION_H_
7 #define COMMON_EXTENSION_H_
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.
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.
20 #include <sys/types.h>
26 #include <unordered_set>
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"
43 // This function should be implemented by each extension and should return
44 // an appropriate Extension subclass.
45 common::Extension* CreateExtension();
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();
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);
65 // This API should be called in the message handler of extension
66 bool CheckAPIAccessControl(const char* api_name);
68 virtual Instance* CreateInstance();
70 std::string GetRuntimeVariable(const char* var_name, unsigned len);
73 friend int32_t(::XW_Initialize)(XW_Extension extension, XW_GetInterface get_interface);
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);
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);
86 XW_Extension xw_extension_;
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);
103 virtual void Initialize() {
105 virtual void HandleMessage(const char* msg) = 0;
106 virtual void HandleSyncMessage(const char* msg) {
109 XW_Instance xw_instance() const {
114 friend class Extension;
116 static std::mutex instances_mutex_;
117 static std::unordered_set<Instance*> all_instances_;
119 XW_Instance xw_instance_;
122 typedef std::function<void(const picojson::value&, picojson::object&)> NativeHandler;
124 class ParsedInstance : public Instance {
127 virtual ~ParsedInstance();
130 void RegisterHandler(const std::string& name, const NativeHandler& func);
131 void RegisterSyncHandler(const std::string& name, const NativeHandler& func);
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);
140 void HandleMessage(const char* msg);
141 void HandleSyncMessage(const char* msg);
143 void HandleMessage(const char* msg, bool is_sync);
144 void HandleException(const PlatformException& ex);
145 void HandleError(const PlatformResult& error);
147 std::map<std::string, NativeHandler> handler_map_;
150 } // namespace common
152 #endif // COMMON_EXTENSION_H_