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