76406a4b0a3c14702267f3922f478d34d10775df
[platform/framework/web/crosswalk-tizen.git] / src / bundle / extension_renderer_controller.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "bundle/extension_renderer_controller.h"
6
7 #include <v8/v8.h>
8 #include <string>
9 #include <utility>
10
11 #include "common/logger.h"
12 #include "bundle/extension_client.h"
13 #include "bundle/extension_module.h"
14 #include "bundle/module_system.h"
15 #include "bundle/xwalk_v8tools_module.h"
16
17 namespace wrt {
18
19 namespace {
20
21 void CreateExtensionModules(ExtensionClient* client,
22                             ModuleSystem* module_system) {
23   const ExtensionClient::ExtensionAPIMap& extensions =
24       client->extension_apis();
25   auto it = extensions.begin();
26   for (; it != extensions.end(); ++it) {
27     ExtensionClient::ExtensionCodePoints* codepoint = it->second;
28     if (codepoint->api.empty())
29       continue;
30
31     std::unique_ptr<ExtensionModule> module(
32         new ExtensionModule(client, module_system, it->first, codepoint->api));
33     module_system->RegisterExtensionModule(
34         std::move(module), codepoint->entry_points);
35   }
36 }
37
38 }  // namespace
39
40 ExtensionRendererController& ExtensionRendererController::GetInstance() {
41   static ExtensionRendererController instance;
42   return instance;
43 }
44
45 ExtensionRendererController::ExtensionRendererController()
46     : extensions_client_(new ExtensionClient()) {
47 }
48
49 ExtensionRendererController::~ExtensionRendererController() {
50 }
51
52 void ExtensionRendererController::DidCreateScriptContext(
53     v8::Handle<v8::Context> context) {
54   ModuleSystem* module_system = new ModuleSystem(context);
55   ModuleSystem::SetModuleSystemInContext(
56       std::unique_ptr<ModuleSystem>(module_system), context);
57
58   module_system->RegisterNativeModule(
59         "v8tools", std::unique_ptr<NativeModule>(new XWalkV8ToolsModule));
60
61   CreateExtensionModules(extensions_client_.get(), module_system);
62   module_system->Initialize();
63 }
64
65 void ExtensionRendererController::WillReleaseScriptContext(
66     v8::Handle<v8::Context> context) {
67   v8::Context::Scope contextScope(context);
68   ModuleSystem::ResetModuleSystemFromContext(context);
69 }
70
71 bool ExtensionRendererController::InitializeExtensions(
72     const std::string& uuid) {
73   return extensions_client_->Initialize(uuid);
74 }
75
76 }  // namespace wrt