Patch for PluginLogic pre-initializing.
[platform/framework/web/wrt.git] / src / api_new / core_module.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    core_module.cpp
18  * @author  Przemyslaw Ciezkowski (p.ciezkowski@samsung.com)
19  * @author  Andrzej Surdej (a.surdej@samsung.com)
20  * @version 1.0
21  * @brief   File contains definitions of wrt core module.
22  */
23
24 #include "core_module.h"
25 #include "runnable_widget_object.h"
26 #include <string>
27 #include <main_thread.h>
28 #include <dpl/log/log.h>
29 #include <dpl/assert.h>
30 #include <dpl/exception.h>
31 #include <dpl/singleton_impl.h>
32 #include "localization_setting.h"
33 #include <dpl/wrt-dao-ro/global_config.h>
34 #include <profiling_util.h>
35 #include <widget_deserialize_model.h>
36 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
37 #include <dpl/wrt-dao-ro/global_dao_read_only.h>
38 #include "webkit/bundles/plugin_module_support.h"
39
40 #include <EWebKit2.h>
41
42
43 IMPLEMENT_SINGLETON(WRT::CoreModule)
44
45 namespace { //Anonymous
46
47 const char * const bundlePath = "/usr/lib/wrt-wk2-bundles/libwrt-wk2-bundle.so";
48
49 std::string cutOffFileName(const std::string& path)
50 {
51     size_t found = path.find_last_of("/");
52     if (found == std::string::npos) {
53         return path;
54     } else {
55         return path.substr(0, found);
56     }
57 }
58
59 bool isDir(const std::string& path)
60 {
61     struct stat st;
62     if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
63         return true;
64     }
65     LogError("Cannot access directory [ " << path << " ]");
66     return false;
67 }
68
69 bool checkPaths()
70 {
71     using namespace WrtDB;
72     using namespace WrtDB::GlobalConfig;
73
74     bool if_ok = true;
75     if_ok &= (isDir(cutOffFileName(GetWrtDatabaseFilePath())));
76     if (!if_ok) {
77         LogError("Path <" << GetWrtDatabaseFilePath() << "> does not exist.");
78     }
79
80     if_ok &= (isDir(GetDevicePluginPath()));
81     if (!if_ok) {
82         LogError("Path <" << GetDevicePluginPath() << "> does not exist.");
83     }
84
85     if_ok &= (isDir(GetUserInstalledWidgetPath()));
86     if (!if_ok) {
87         LogError("Path <" << GetUserInstalledWidgetPath() <<
88             "> does not exist.");
89     }
90     return if_ok;
91 }
92 }// namespace anonymous
93
94 namespace WRT {
95
96 class CoreModuleImpl
97 {
98 public:
99
100     CoreModuleImpl() : m_initialized(false), m_ewkContext(NULL)
101     {
102         LogDebug("enter");
103     }
104
105     ~CoreModuleImpl()
106     {
107         LogDebug("Core module implementation destroyed");
108     }
109
110     bool Init()
111     {
112         if (!m_initialized) {
113             ADD_PROFILING_POINT("CoreModule::Init", "point");
114             DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
115             LogDebug("Initialize");
116             if (!checkPaths()) {
117                 LogError("Required path does not exist");
118                 return false;
119             }
120             Try
121             {
122                 if(!m_ewkContext)
123                 {
124                     // Needed settings for WKContext are located here
125                     // create Ewk_Context
126                     Ewk_Context* newEwkContext =
127                         ewk_context_new_with_injected_bundle_path(bundlePath);
128                     if (!newEwkContext) {
129                         LogError("Failed to create Ewk_Context");
130                         ThrowMsg(DPL::Exception, "Failed to create ewk context");
131                     }
132
133                     m_ewkContext = newEwkContext;
134                 }
135
136                 // cache model setting
137                 ewk_context_cache_model_set(m_ewkContext,
138                                             EWK_CACHE_MODEL_DOCUMENT_BROWSER);
139                 ADD_PROFILING_POINT("WebProcess fork", "start");
140
141                 // To fork a Webprocess as soon as possible,
142                 // the following ewk_api is called explicitly.
143                 Ewk_Cookie_Manager *ewkCookieManager;
144                 ewkCookieManager =
145                     ewk_context_cookie_manager_get(m_ewkContext);
146                 ewk_cookie_manager_accept_policy_set(ewkCookieManager,
147                                                EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
148                 ADD_PROFILING_POINT("WebProcess fork", "stop");
149
150                 ADD_PROFILING_POINT("attach databases", "start");
151                 MainThreadSingleton::Instance().AttachDatabases();
152                 ADD_PROFILING_POINT("attach databases", "stop");
153
154                 LogDebug("Initialize finished");
155             } catch (const DPL::Exception& ex) {
156                 LogError("Internal Error during screen preparation:");
157                 DPL::Exception::DisplayKnownException(ex);
158                 /* TODO:
159                  * Do deinitialization: check on which step exception occured
160                  * and deinitialize only initialized parts.
161                 */
162                 return false;
163             }
164             m_initialized = true;
165         }
166         return true;
167     }
168
169     bool Init(Ewk_Context* ewk_context)
170     {
171         if(ewk_context)
172         {
173             m_ewkContext = ewk_context;
174         }
175
176         return Init();
177     }
178
179     void Terminate()
180     {
181         if (m_ewkContext) {
182             LogInfo("finalizeEwkContext called");
183             ewk_context_delete(m_ewkContext);
184             m_ewkContext = 0;
185         }
186         MainThreadSingleton::Instance().DetachDatabases();
187
188         m_initialized = false;
189     }
190
191     RunnableWidgetObjectPtr getRunnableWidgetObject(
192             const std::string& tizenId)
193     {
194         PluginModuleSupport::init(m_ewkContext, tizenId);
195
196         try {
197             RunnableWidgetObjectPtr runnable;
198             WidgetModelPtr model = Domain::deserializeWidgetModel(tizenId);
199             if (!!model) {
200                 runnable.reset(new RunnableWidgetObject(model, m_ewkContext));
201             }
202             return runnable;
203         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
204             LogDebug("Widget not found.");
205             return RunnableWidgetObjectPtr();
206         }
207     }
208
209     CoreModule::NetworkAccessMode homeNetworkAccess()
210     {
211         switch (WrtDB::GlobalDAOReadOnly::GetHomeNetworkDataUsage()) {
212         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
213             return CoreModule::NEVER_CONNECT;
214         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
215             return CoreModule::ALWAYS_ASK;
216         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
217             return CoreModule::CONNECT_AUTOMATICALLY;
218         default:
219             break;
220         }
221         LogWarning("using default value");
222         return CoreModule::ALWAYS_ASK;
223     }
224
225     CoreModule::NetworkAccessMode roamingNetworkAccess()
226     {
227         switch (WrtDB::GlobalDAOReadOnly::GetRoamingDataUsage()) {
228         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
229             return CoreModule::NEVER_CONNECT;
230         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
231             return CoreModule::ALWAYS_ASK;
232         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
233             return CoreModule::CONNECT_AUTOMATICALLY;
234         default:
235             break;
236         }
237         LogWarning("using default value");
238         return CoreModule::ALWAYS_ASK;
239     }
240
241     bool developerMode()
242     {
243         return WrtDB::GlobalDAOReadOnly::GetDeveloperMode();
244     }
245
246 private:
247     bool m_initialized;
248     Ewk_Context* m_ewkContext;
249 };
250
251 CoreModule::CoreModule() : m_impl(new CoreModuleImpl())
252 {
253 }
254
255 CoreModule::~CoreModule()
256 {
257 }
258
259 bool CoreModule::Init()
260 {
261     return m_impl->Init();
262 }
263
264 bool CoreModule::Init(Ewk_Context* ewk_context)
265 {
266     return m_impl->Init(ewk_context);
267 }
268
269 void CoreModule::Terminate()
270 {
271     return m_impl->Terminate();
272 }
273
274 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(
275         const std::string& tizenId)
276 {
277     return m_impl->getRunnableWidgetObject(tizenId);
278 }
279
280 CoreModule::NetworkAccessMode CoreModule::homeNetworkAccess()
281 {
282     return m_impl->homeNetworkAccess();
283 }
284
285 CoreModule::NetworkAccessMode CoreModule::roamingNetworkAccess()
286 {
287     return m_impl->roamingNetworkAccess();
288 }
289
290 bool CoreModule::developerMode()
291 {
292     return m_impl->developerMode();
293 }
294
295 } /* namespace WRT */