Assure proper termination using context session
[platform/framework/web/crosswalk-tizen.git] / extensions / renderer / xwalk_extension_renderer_controller.cc
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 #include "extensions/renderer/xwalk_extension_renderer_controller.h"
7
8 #include <Ecore.h>
9 #include <v8/v8.h>
10 #include <string>
11 #include <utility>
12
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"
22
23 namespace extensions {
24
25 // static
26 int XWalkExtensionRendererController::plugin_session_count = 0;
27
28 namespace {
29
30 void CreateExtensionModules(XWalkExtensionClient* client,
31                             XWalkModuleSystem* module_system) {
32   const XWalkExtensionClient::ExtensionAPIMap& extensions =
33       client->extension_apis();
34
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);
42   }
43 }
44
45 }  // namespace
46
47 XWalkExtensionRendererController&
48 XWalkExtensionRendererController::GetInstance() {
49   static XWalkExtensionRendererController instance;
50   return instance;
51 }
52
53 XWalkExtensionRendererController::XWalkExtensionRendererController()
54     : exit_requested(false),
55       extensions_client_(new XWalkExtensionClient()) {
56 }
57
58 XWalkExtensionRendererController::~XWalkExtensionRendererController() {
59 }
60
61 void XWalkExtensionRendererController::DidCreateScriptContext(
62     v8::Handle<v8::Context> context) {
63   SCOPE_PROFILE();
64
65   // Skip plugin loading after application exit request.
66   if (exit_requested) {
67     return;
68   }
69
70   XWalkModuleSystem* module_system = new XWalkModuleSystem(context);
71   XWalkModuleSystem::SetModuleSystemInContext(
72       std::unique_ptr<XWalkModuleSystem>(module_system), context);
73   module_system->RegisterNativeModule(
74         "v8tools",
75         std::unique_ptr<XWalkNativeModule>(new XWalkV8ToolsModule));
76   module_system->RegisterNativeModule(
77         "WidgetModule",
78         std::unique_ptr<XWalkNativeModule>(new WidgetModule));
79   module_system->RegisterNativeModule(
80         "objecttools",
81         std::unique_ptr<XWalkNativeModule>(new ObjectToolsModule));
82
83   extensions_client_->Initialize();
84   CreateExtensionModules(extensions_client_.get(), module_system);
85
86   module_system->Initialize();
87   plugin_session_count++;
88   LOGGER(DEBUG) << "plugin_session_count : " << plugin_session_count;
89 }
90
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;
97 }
98
99 void XWalkExtensionRendererController::OnReceivedIPCMessage(
100     const Ewk_IPC_Wrt_Message_Data* data) {
101
102   Eina_Stringshare* type = ewk_ipc_wrt_message_data_type_get(data);
103
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);
111   } else {
112     RuntimeIPCClient* ipc = RuntimeIPCClient::GetInstance();
113     ipc->HandleMessageFromRuntime(data);
114   }
115 #undef TYPE_BEGIN
116
117   eina_stringshare_del(type);
118 }
119
120 void XWalkExtensionRendererController::InitializeExtensionClient() {
121   extensions_client_->Initialize();
122 }
123
124 void XWalkExtensionRendererController::LoadUserExtensions(
125   const std::string app_path) {
126   extensions_client_->LoadUserExtensions(app_path);
127 }
128
129 }  // namespace extensions