5a1cf624205d72f3e142122ee6ed7c12e56def2c
[platform/framework/web/crosswalk-tizen.git] / src / bundle / injected_bundle.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 <unistd.h>
6 #include <v8.h>
7 #include <ewk_ipc_message.h>
8 #include <Ecore.h>
9 #include <string>
10 #include <memory>
11
12 #include "common/logger.h"
13 #include "common/string_utils.h"
14 #include "common/application_data.h"
15 #include "common/resource_manager.h"
16 #include "common/locale_manager.h"
17 #include "bundle/runtime_ipc_client.h"
18 #include "bundle/extension_renderer_controller.h"
19
20 namespace wrt {
21 class BundleGlobalData {
22  public :
23   static BundleGlobalData* GetInstance() {
24     static BundleGlobalData instance;
25     return &instance;
26   }
27   void Initialize(const std::string& app_id) {
28     app_data_.reset(new ApplicationData(app_id));
29     app_data_->LoadManifestData();
30     locale_manager_.reset(new LocaleManager);
31     locale_manager_->EnableAutoUpdate(true);
32     if (app_data_->widget_info() != NULL &&
33         !app_data_->widget_info()->default_locale().empty()) {
34       locale_manager_->SetDefaultLocale(
35           app_data_->widget_info()->default_locale());
36     }
37     resource_manager_.reset(new ResourceManager(app_data_.get(),
38                                                 locale_manager_.get()));
39     resource_manager_->set_base_resource_path(
40         app_data_->application_path());
41   }
42
43   ResourceManager* resource_manager() {
44     return resource_manager_.get();
45   }
46
47  private:
48   BundleGlobalData() {}
49   ~BundleGlobalData() {}
50   std::unique_ptr<ResourceManager> resource_manager_;
51   std::unique_ptr<LocaleManager> locale_manager_;
52   std::unique_ptr<ApplicationData> app_data_;
53 };
54 }  //  namespace wrt
55
56 extern "C" void DynamicSetWidgetInfo(const char* tizen_id) {
57   LOGGER(DEBUG) << "InjectedBundle::DynamicSetWidgetInfo !!" << tizen_id;
58   ecore_init();
59   wrt::BundleGlobalData::GetInstance()->Initialize(tizen_id);
60 }
61
62 extern "C" void DynamicPluginStartSession(const char* tizen_id,
63                                           v8::Handle<v8::Context> context,
64                                           int routing_handle,
65                                           double /*scale*/,
66                                           const char* uuid,
67                                           const char* /*theme*/,
68                                           const char* base_url) {
69   LOGGER(DEBUG) << "InjectedBundle::DynamicPluginStartSession !!" << tizen_id;
70   if (base_url == NULL || wrt::utils::StartsWith(base_url, "http")) {
71     LOGGER(ERROR) << "External url not allowed plugin loading.";
72     return;
73   }
74
75   // Initialize RuntimeIPCClient
76   wrt::RuntimeIPCClient* rc = wrt::RuntimeIPCClient::GetInstance();
77   rc->set_routing_id(routing_handle);
78
79   // Initialize ExtensionRendererController
80   wrt::ExtensionRendererController& controller =
81       wrt::ExtensionRendererController::GetInstance();
82   controller.InitializeExtensions(uuid);
83   controller.DidCreateScriptContext(context);
84 }
85
86 extern "C" void DynamicPluginStopSession(
87     const char* tizen_id, v8::Handle<v8::Context> context) {
88   LOGGER(DEBUG) << "InjectedBundle::DynamicPluginStopSession !!" << tizen_id;
89
90   wrt::ExtensionRendererController& controller =
91       wrt::ExtensionRendererController::GetInstance();
92   controller.WillReleaseScriptContext(context);
93 }
94
95 extern "C" void DynamicUrlParsing(
96     std::string* old_url, std::string* new_url, const char* /*tizen_id*/) {
97   auto res_manager = wrt::BundleGlobalData::GetInstance()->resource_manager();
98   if (res_manager == NULL) {
99     LOGGER(ERROR) << "Widget Info was not set, Resource Manager is NULL";
100     *new_url = *old_url;
101     return;
102   }
103   *new_url = res_manager->GetLocalizedPath(*old_url);
104 }
105
106 extern "C" void DynamicDatabaseAttach(int /*attach*/) {
107   // LOGGER(DEBUG) << "InjectedBundle::DynamicDatabaseAttach !!";
108 }
109
110 extern "C" void DynamicOnIPCMessage(const Ewk_IPC_Wrt_Message_Data& data) {
111   LOGGER(DEBUG) << "InjectedBundle::DynamicOnIPCMessage !!";
112   wrt::RuntimeIPCClient* rc = wrt::RuntimeIPCClient::GetInstance();
113   rc->HandleMessageFromRuntime(&data);
114 }
115
116 extern "C" void DynamicPreloading() {
117   // LOGGER(DEBUG) << "InjectedBundle::DynamicPreloading !!";
118 }