Profiling points updated, script fixed
[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 <global_context.h>
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
31 #include <dpl/exception.h>
32 #include <dpl/popup/popup_controller.h>
33 #include <dpl/singleton_impl.h>
34 #include "localization_setting.h"
35 #include <dpl/wrt-dao-ro/global_config.h>
36 #include <profiling_util.h>
37 #include <widget_deserialize_model.h>
38 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
39
40 #include <EWebKit2.h>
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(GetFactoryInstalledWidgetPath()));
85     if (!if_ok) {
86         LogError("Path <" << GetFactoryInstalledWidgetPath() <<
87             "> does not exist.");
88     }
89
90     if_ok &= (isDir(GetUserInstalledWidgetPath()));
91     if (!if_ok) {
92         LogError("Path <" << GetUserInstalledWidgetPath() <<
93             "> does not exist.");
94     }
95     return if_ok;
96 }
97 }// namespace anonymous
98
99 namespace WRT {
100
101 class CoreModuleImpl
102 {
103 public:
104
105     CoreModuleImpl() : m_initialized(false), m_ewkContext(NULL)
106     {
107         LogDebug("enter");
108     }
109
110     ~CoreModuleImpl()
111     {
112         LogDebug("Core module implementation destroyed");
113     }
114
115     bool Init()
116     {
117         if (!m_initialized) {
118             ADD_PROFILING_POINT("CoreModule::Init", "point");
119             DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
120             LogDebug("Initialize");
121             if (!checkPaths()) {
122                 LogError("Required path does not exist");
123                 return false;
124             }
125             Try
126             {
127
128                 // Needed settings for WKContext are located here
129                 // create Ewk_Context
130                 Ewk_Context* newEwkContext =
131                     ewk_context_new_with_injected_bundle_path(bundlePath);
132                 if (!newEwkContext) {
133                     LogError("Failed to create Ewk_Context");
134                     ThrowMsg(DPL::Exception, "Failed to create ewk context");
135                 }
136                     // cache model setting
137                 ewk_context_cache_model_set(newEwkContext,
138                                             EWK_CACHE_MODEL_DOCUMENT_BROWSER);
139                 m_ewkContext = newEwkContext;
140                 ADD_PROFILING_POINT("WebProcess fork", "start");
141
142                 // To fork a Webprocess as soon as possible,
143                 // the following ewk_api is called explicitly.
144                 ewk_context_cookies_policy_set(m_ewkContext,
145                                                EWK_COOKIE_JAR_ACCEPT_ALWAYS);
146                 ADD_PROFILING_POINT("WebProcess fork", "stop");
147
148                 GlobalContext::TouchArchitecture();
149
150                 ADD_PROFILING_POINT("attach databases", "start");
151                 MainThreadSingleton::Instance().AttachDatabases();
152                 ADD_PROFILING_POINT("attach databases", "stop");
153
154                 // Initialize popup manager
155                 DPL::Popup::PopupManagerSingleton::Instance().Initialize(
156                     DPL::Popup::PopupRendererPtr(new DPL::Popup::PopupRenderer));
157
158                 LogDebug("Initialize finished");
159             } catch (const DPL::Exception& ex) {
160                 LogError("Internal Error during screen preparation:");
161                 DPL::Exception::DisplayKnownException(ex);
162                 /* TODO:
163                  * Do deinitialization: check on which step exception occured
164                  * and deinitialize only initialized parts.
165                 */
166                 return false;
167             }
168             m_initialized = true;
169         }
170         return true;
171     }
172
173     void Terminate()
174     {
175         if (m_ewkContext) {
176             LogInfo("finalizeEwkContext called");
177             ewk_context_delete(m_ewkContext);
178             m_ewkContext = 0;
179         }
180         MainThreadSingleton::Instance().DetachDatabases();
181         // Deinitialize popup manager
182         DPL::Popup::PopupManagerSingleton::Instance().Deinitialize();
183
184         m_initialized = false;
185     }
186
187     RunnableWidgetObjectPtr getRunnableWidgetObject(
188             const std::string& tizenId)
189     {
190         try {
191             RunnableWidgetObjectPtr runnable;
192             WidgetModelPtr model = Domain::deserializeWidgetModel(tizenId);
193             if (!!model) {
194                 runnable.reset(new RunnableWidgetObject(model, m_ewkContext));
195             }
196             return runnable;
197         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
198             LogDebug("Widget not found.");
199             return RunnableWidgetObjectPtr();
200         }
201     }
202
203 private:
204     bool m_initialized;
205     Ewk_Context* m_ewkContext;
206 };
207
208 CoreModule::CoreModule() : m_impl(new CoreModuleImpl())
209 {
210 }
211
212 CoreModule::~CoreModule()
213 {
214 }
215
216 bool CoreModule::Init()
217 {
218     return m_impl->Init();
219 }
220
221 void CoreModule::Terminate()
222 {
223     return m_impl->Terminate();
224 }
225
226 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(
227         const std::string& tizenId)
228 {
229     return m_impl->getRunnableWidgetObject(tizenId);
230 }
231
232 } /* namespace WRT */