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 #include "extensions/renderer/xwalk_extension_renderer_controller.h"
13 #include "common/logger.h"
14 #include "common/profiler.h"
15 #include "extensions/renderer/object_tools_module.h"
16 #include "extensions/renderer/widget_module.h"
17 #include "extensions/renderer/xwalk_extension_client.h"
18 #include "extensions/renderer/xwalk_extension_module.h"
19 #include "extensions/renderer/xwalk_module_system.h"
20 #include "extensions/renderer/xwalk_v8tools_module.h"
21 #include "extensions/renderer/runtime_ipc_client.h"
23 namespace extensions {
26 int XWalkExtensionRendererController::plugin_session_count = 0;
30 void CreateExtensionModules(XWalkExtensionClient* client,
31 XWalkModuleSystem* module_system) {
32 const XWalkExtensionClient::ExtensionAPIMap& extensions =
33 client->extension_apis();
35 for (auto it = extensions.begin(); it != extensions.end(); ++it) {
36 XWalkExtensionClient::ExtensionCodePoints* codepoint = it->second;
37 std::unique_ptr<XWalkExtensionModule> module(
38 new XWalkExtensionModule(client, module_system,
39 it->first, codepoint->api));
40 module_system->RegisterExtensionModule(std::move(module),
41 codepoint->entry_points);
47 XWalkExtensionRendererController&
48 XWalkExtensionRendererController::GetInstance() {
49 static XWalkExtensionRendererController instance;
53 XWalkExtensionRendererController::XWalkExtensionRendererController()
54 : exit_requested(false),
55 extensions_client_(new XWalkExtensionClient()) {
58 XWalkExtensionRendererController::~XWalkExtensionRendererController() {
61 void XWalkExtensionRendererController::DidCreateScriptContext(
62 v8::Handle<v8::Context> context) {
65 // Skip plugin loading after application exit request.
70 XWalkModuleSystem* module_system = new XWalkModuleSystem(context);
71 XWalkModuleSystem::SetModuleSystemInContext(
72 std::unique_ptr<XWalkModuleSystem>(module_system), context);
73 module_system->RegisterNativeModule(
75 std::unique_ptr<XWalkNativeModule>(new XWalkV8ToolsModule));
76 module_system->RegisterNativeModule(
78 std::unique_ptr<XWalkNativeModule>(new WidgetModule));
79 module_system->RegisterNativeModule(
81 std::unique_ptr<XWalkNativeModule>(new ObjectToolsModule));
83 extensions_client_->Initialize();
84 CreateExtensionModules(extensions_client_.get(), module_system);
86 module_system->Initialize();
87 plugin_session_count++;
88 LOGGER(DEBUG) << "plugin_session_count : " << plugin_session_count;
91 void XWalkExtensionRendererController::WillReleaseScriptContext(
92 v8::Handle<v8::Context> context) {
93 v8::Context::Scope contextScope(context);
94 XWalkModuleSystem::ResetModuleSystemFromContext(context);
95 plugin_session_count--;
96 LOGGER(DEBUG) << "plugin_session_count : " << plugin_session_count;
99 void XWalkExtensionRendererController::OnReceivedIPCMessage(
100 const Ewk_IPC_Wrt_Message_Data* data) {
102 Eina_Stringshare* type = ewk_ipc_wrt_message_data_type_get(data);
104 #define TYPE_BEGIN(x) (!strncmp(type, x, strlen(x)))
105 if (TYPE_BEGIN("xwalk://")) {
106 Eina_Stringshare* id = ewk_ipc_wrt_message_data_id_get(data);
107 Eina_Stringshare* msg = ewk_ipc_wrt_message_data_value_get(data);
108 extensions_client_->OnReceivedIPCMessage(id, msg);
109 eina_stringshare_del(id);
110 eina_stringshare_del(msg);
112 RuntimeIPCClient* ipc = RuntimeIPCClient::GetInstance();
113 ipc->HandleMessageFromRuntime(data);
117 eina_stringshare_del(type);
120 void XWalkExtensionRendererController::InitializeExtensionClient() {
121 extensions_client_->Initialize();
124 void XWalkExtensionRendererController::LoadUserExtensions(
125 const std::string app_path) {
126 extensions_client_->LoadUserExtensions(app_path);
129 } // namespace extensions