Update wrt_0.8.90
[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 <libxml/parser.h>
35 #include "localization_setting.h"
36 #include <dpl/wrt-dao-ro/global_config.h>
37 #include <profiling_util.h>
38 #include <widget_deserialize_model.h>
39 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
40
41 #include <EWebKit2.h>
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(GetFactoryInstalledWidgetPath()));
86     if (!if_ok) {
87         LogError("Path <" << GetFactoryInstalledWidgetPath() <<
88             "> does not exist.");
89     }
90
91     if_ok &= (isDir(GetUserInstalledWidgetPath()));
92     if (!if_ok) {
93         LogError("Path <" << GetUserInstalledWidgetPath() <<
94             "> does not exist.");
95     }
96     return if_ok;
97 }
98 }// namespace anonymous
99
100 namespace WRT {
101
102 class CoreModuleImpl
103 {
104 public:
105
106     CoreModuleImpl() : m_initialized(false), m_ewkContext(NULL)
107     {
108         LogDebug("enter");
109     }
110
111     ~CoreModuleImpl()
112     {
113         LogDebug("Core module implementation destroyed");
114     }
115
116     bool Init()
117     {
118         if (!m_initialized) {
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                 GlobalContext::TouchArchitecture();
128
129                 ADD_PROFILING_POINT("attach databases", "start");
130                 MainThreadSingleton::Instance().AttachDatabases();
131                 ADD_PROFILING_POINT("attach databases", "stop");
132
133                 ADD_PROFILING_POINT("xml_parser_init", "start");
134                 xmlInitParser();
135                 ADD_PROFILING_POINT("xml_parser_init", "stop");
136
137                 // Initialize popup manager
138                 ADD_PROFILING_POINT("popup_manager_init", "start");
139                 DPL::Popup::PopupManagerSingleton::Instance().Initialize(
140                     DPL::Popup::PopupRendererPtr(new DPL::Popup::PopupRenderer));
141                 ADD_PROFILING_POINT("popup_manager_init", "stop");
142
143                 // Initialize Language Subtag registry
144                 ADD_PROFILING_POINT("language_rst_init", "start");
145                 LocalizationSetting::SetLocalization();
146                 ADD_PROFILING_POINT("language_rst_init", "stop");
147
148
149                 // Needed settings for WKContext are located here
150                 // create Ewk_Context
151                 Ewk_Context* newEwkContext =
152                     ewk_context_new_with_injected_bundle_path(bundlePath);
153                 if (!newEwkContext) {
154                     LogError("Failed to create Ewk_Context");
155                     ThrowMsg(DPL::Exception, "Failed to create ewk context");
156                 }
157                     // cache model setting
158                 ewk_context_cache_model_set(newEwkContext,
159                                             EWK_CACHE_MODEL_PRIMARY_WEBBROWSER);
160                 m_ewkContext = newEwkContext;
161
162                 LogDebug("Initialize finished");
163             } catch (const DPL::Exception& ex) {
164                 LogError("Internal Error during screen preparation:");
165                 DPL::Exception::DisplayKnownException(ex);
166                 /* TODO:
167                  * Do deinitialization: check on which step exception occured
168                  * and deinitialize only initialized parts.
169                 */
170                 return false;
171             }
172             m_initialized = true;
173         }
174         return true;
175     }
176
177     void Terminate()
178     {
179         if (m_ewkContext) {
180             LogInfo("finalizeEwkContext called");
181             ewk_context_delete(m_ewkContext);
182             m_ewkContext = 0;
183         }
184         MainThreadSingleton::Instance().DetachDatabases();
185         // Deinitialize popup manager
186         DPL::Popup::PopupManagerSingleton::Instance().Deinitialize();
187
188         LogInfo("Cleanup libxml2 global values.");
189         xmlCleanupParser();
190         m_initialized = false;
191     }
192
193     RunnableWidgetObjectPtr getRunnableWidgetObject(const WidgetHandle& handle)
194     {
195         RunnableWidgetObjectPtr runnable;
196         WidgetModelPtr model = Domain::deserializeWidgetModel(handle);
197         if (!!model) {
198             runnable.reset(new RunnableWidgetObject(model, m_ewkContext));
199         }
200         return runnable;
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(const WidgetHandle& handle)
227 {
228     return m_impl->getRunnableWidgetObject(handle);
229 }
230
231 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(const std::string& packageName)
232 {
233     DPL::OptionalString pkgName(DPL::FromUTF8String(packageName));
234     try {
235         return m_impl->getRunnableWidgetObject(
236                 WrtDB::WidgetDAOReadOnly::getHandle(*pkgName));
237     } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
238         LogDebug("Widget not found.");
239         return RunnableWidgetObjectPtr();
240     }
241 }
242
243 } /* namespace WRT */