For launching time performance, patch to not exec web process.
[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
39 #include <EWebKit2.h>
40
41
42 IMPLEMENT_SINGLETON(WRT::CoreModule)
43
44 namespace { //Anonymous
45
46 const char * const bundlePath = "/usr/lib/wrt-wk2-bundles/libwrt-wk2-bundle.so";
47
48 std::string cutOffFileName(const std::string& path)
49 {
50     size_t found = path.find_last_of("/");
51     if (found == std::string::npos) {
52         return path;
53     } else {
54         return path.substr(0, found);
55     }
56 }
57
58 bool isDir(const std::string& path)
59 {
60     struct stat st;
61     if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
62         return true;
63     }
64     LogError("Cannot access directory [ " << path << " ]");
65     return false;
66 }
67
68 bool checkPaths()
69 {
70     using namespace WrtDB;
71     using namespace WrtDB::GlobalConfig;
72
73     bool if_ok = true;
74     if_ok &= (isDir(cutOffFileName(GetWrtDatabaseFilePath())));
75     if (!if_ok) {
76         LogError("Path <" << GetWrtDatabaseFilePath() << "> does not exist.");
77     }
78
79     if_ok &= (isDir(GetDevicePluginPath()));
80     if (!if_ok) {
81         LogError("Path <" << GetDevicePluginPath() << "> does not exist.");
82     }
83
84     if_ok &= (isDir(GetUserInstalledWidgetPath()));
85     if (!if_ok) {
86         LogError("Path <" << GetUserInstalledWidgetPath() <<
87             "> does not exist.");
88     }
89     return if_ok;
90 }
91 }// namespace anonymous
92
93 namespace WRT {
94
95 class CoreModuleImpl
96 {
97 public:
98
99     CoreModuleImpl() : m_initialized(false), m_ewkContext(NULL)
100     {
101         LogDebug("enter");
102     }
103
104     ~CoreModuleImpl()
105     {
106         LogDebug("Core module implementation destroyed");
107     }
108
109     bool Init()
110     {
111         if (!m_initialized) {
112             ADD_PROFILING_POINT("CoreModule::Init", "point");
113             DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
114             LogDebug("Initialize");
115             if (!checkPaths()) {
116                 LogError("Required path does not exist");
117                 return false;
118             }
119             Try
120             {
121                 if(!m_ewkContext)
122                 {
123                     // Needed settings for WKContext are located here
124                     // create Ewk_Context
125                     Ewk_Context* newEwkContext =
126                         ewk_context_new_with_injected_bundle_path(bundlePath);
127                     if (!newEwkContext) {
128                         LogError("Failed to create Ewk_Context");
129                         ThrowMsg(DPL::Exception, "Failed to create ewk context");
130                     }
131
132                     m_ewkContext = newEwkContext;
133                 }
134
135                 // cache model setting
136                 ewk_context_cache_model_set(m_ewkContext,
137                                             EWK_CACHE_MODEL_DOCUMENT_BROWSER);
138                 ADD_PROFILING_POINT("WebProcess fork", "start");
139
140                 // To fork a Webprocess as soon as possible,
141                 // the following ewk_api is called explicitly.
142                 Ewk_Cookie_Manager *ewkCookieManager;
143                 ewkCookieManager =
144                     ewk_context_cookie_manager_get(m_ewkContext);
145                 ewk_cookie_manager_accept_policy_set(ewkCookieManager,
146                                                EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
147                 ADD_PROFILING_POINT("WebProcess fork", "stop");
148
149                 ADD_PROFILING_POINT("attach databases", "start");
150                 MainThreadSingleton::Instance().AttachDatabases();
151                 ADD_PROFILING_POINT("attach databases", "stop");
152
153                 LogDebug("Initialize finished");
154             } catch (const DPL::Exception& ex) {
155                 LogError("Internal Error during screen preparation:");
156                 DPL::Exception::DisplayKnownException(ex);
157                 /* TODO:
158                  * Do deinitialization: check on which step exception occured
159                  * and deinitialize only initialized parts.
160                 */
161                 return false;
162             }
163             m_initialized = true;
164         }
165         return true;
166     }
167
168     bool Init(Ewk_Context* ewk_context)
169     {
170         if(ewk_context)
171         {
172             m_ewkContext = ewk_context;
173         }
174
175         return Init();
176     }
177
178     void Terminate()
179     {
180         if (m_ewkContext) {
181             LogInfo("finalizeEwkContext called");
182             ewk_context_delete(m_ewkContext);
183             m_ewkContext = 0;
184         }
185         MainThreadSingleton::Instance().DetachDatabases();
186
187         m_initialized = false;
188     }
189
190     RunnableWidgetObjectPtr getRunnableWidgetObject(
191             const std::string& tizenId)
192     {
193         try {
194             RunnableWidgetObjectPtr runnable;
195             WidgetModelPtr model = Domain::deserializeWidgetModel(tizenId);
196             if (!!model) {
197                 runnable.reset(new RunnableWidgetObject(model, m_ewkContext));
198             }
199             return runnable;
200         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
201             LogDebug("Widget not found.");
202             return RunnableWidgetObjectPtr();
203         }
204     }
205
206     CoreModule::NetworkAccessMode homeNetworkAccess()
207     {
208         switch (WrtDB::GlobalDAOReadOnly::GetHomeNetworkDataUsage()) {
209         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
210             return CoreModule::NEVER_CONNECT;
211         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
212             return CoreModule::ALWAYS_ASK;
213         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
214             return CoreModule::CONNECT_AUTOMATICALLY;
215         default:
216             break;
217         }
218         LogWarning("using default value");
219         return CoreModule::ALWAYS_ASK;
220     }
221
222     CoreModule::NetworkAccessMode roamingNetworkAccess()
223     {
224         switch (WrtDB::GlobalDAOReadOnly::GetRoamingDataUsage()) {
225         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
226             return CoreModule::NEVER_CONNECT;
227         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
228             return CoreModule::ALWAYS_ASK;
229         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
230             return CoreModule::CONNECT_AUTOMATICALLY;
231         default:
232             break;
233         }
234         LogWarning("using default value");
235         return CoreModule::ALWAYS_ASK;
236     }
237
238     bool developerMode()
239     {
240         return WrtDB::GlobalDAOReadOnly::GetDeveloperMode();
241     }
242
243 private:
244     bool m_initialized;
245     Ewk_Context* m_ewkContext;
246 };
247
248 CoreModule::CoreModule() : m_impl(new CoreModuleImpl())
249 {
250 }
251
252 CoreModule::~CoreModule()
253 {
254 }
255
256 bool CoreModule::Init()
257 {
258     return m_impl->Init();
259 }
260
261 bool CoreModule::Init(Ewk_Context* ewk_context)
262 {
263     return m_impl->Init(ewk_context);
264 }
265
266 void CoreModule::Terminate()
267 {
268     return m_impl->Terminate();
269 }
270
271 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(
272         const std::string& tizenId)
273 {
274     return m_impl->getRunnableWidgetObject(tizenId);
275 }
276
277 CoreModule::NetworkAccessMode CoreModule::homeNetworkAccess()
278 {
279     return m_impl->homeNetworkAccess();
280 }
281
282 CoreModule::NetworkAccessMode CoreModule::roamingNetworkAccess()
283 {
284     return m_impl->roamingNetworkAccess();
285 }
286
287 bool CoreModule::developerMode()
288 {
289     return m_impl->developerMode();
290 }
291
292 } /* namespace WRT */