[Release] wrt_0.8.185
[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_ok &= (isDir(GetDevicePluginPath()));
75     if_ok &= (isDir(GetUserInstalledWidgetPath()));
76     return if_ok;
77 }
78 } // namespace anonymous
79
80 namespace WRT {
81 class CoreModuleImpl
82 {
83   public:
84
85     CoreModuleImpl() : m_initialized(false), m_ewkContext(NULL)
86     {
87         LogDebug("enter");
88     }
89
90     ~CoreModuleImpl()
91     {
92         LogDebug("Core module implementation destroyed");
93     }
94
95     bool Init()
96     {
97         if (!m_initialized) {
98             ADD_PROFILING_POINT("CoreModule::Init", "point");
99             DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
100             LogDebug("Initialize");
101             if (!checkPaths()) {
102                 LogError("Required path does not exist");
103                 return false;
104             }
105             Try
106             {
107                 ADD_PROFILING_POINT("attach databases", "start");
108                 MainThreadSingleton::Instance().AttachDatabases();
109                 ADD_PROFILING_POINT("attach databases", "stop");
110                 LogDebug("Initialize finished");
111             } catch (const DPL::Exception& ex) {
112                 LogError("Internal Error during screen preparation:");
113                 DPL::Exception::DisplayKnownException(ex);
114                 /* TODO:
115                  * Do deinitialization: check on which step exception occured
116                  * and deinitialize only initialized parts.
117                  */
118                 return false;
119             }
120             m_initialized = true;
121         }
122         return true;
123     }
124
125     void Terminate()
126     {
127         MainThreadSingleton::Instance().DetachDatabases();
128         m_initialized = false;
129     }
130 #ifdef MULTIPROCESS_SERVICE_SUPPORT
131     RunnableWidgetObjectPtr getRunnableWidgetObject(
132         const std::string& tizenId,
133         DPL::Optional<unsigned> serviceIndex)
134 #else
135     RunnableWidgetObjectPtr getRunnableWidgetObject(
136         const std::string& tizenId)
137 #endif
138     {
139         try {
140             RunnableWidgetObjectPtr runnable;
141             WidgetModelPtr model =
142 #ifdef MULTIPROCESS_SERVICE_SUPPORT
143                 Domain::deserializeWidgetModel(tizenId, serviceIndex);
144 #else
145                 Domain::deserializeWidgetModel(tizenId);
146 #endif
147             if (!!model) {
148                 runnable.reset(new RunnableWidgetObject(model));
149             }
150             return runnable;
151         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
152             LogDebug("Widget not found.");
153             return RunnableWidgetObjectPtr();
154         }
155     }
156
157     CoreModule::NetworkAccessMode homeNetworkAccess()
158     {
159         switch (WrtDB::GlobalDAOReadOnly::GetHomeNetworkDataUsage()) {
160         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
161             return CoreModule::NEVER_CONNECT;
162         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
163             return CoreModule::ALWAYS_ASK;
164         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
165             return CoreModule::CONNECT_AUTOMATICALLY;
166         default:
167             break;
168         }
169         LogWarning("using default value");
170         return CoreModule::ALWAYS_ASK;
171     }
172
173     CoreModule::NetworkAccessMode roamingNetworkAccess()
174     {
175         switch (WrtDB::GlobalDAOReadOnly::GetRoamingDataUsage()) {
176         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
177             return CoreModule::NEVER_CONNECT;
178         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
179             return CoreModule::ALWAYS_ASK;
180         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
181             return CoreModule::CONNECT_AUTOMATICALLY;
182         default:
183             break;
184         }
185         LogWarning("using default value");
186         return CoreModule::ALWAYS_ASK;
187     }
188
189     bool developerMode()
190     {
191         return WrtDB::GlobalDAOReadOnly::GetDeveloperMode();
192     }
193
194   private:
195     bool m_initialized;
196     Ewk_Context* m_ewkContext;
197 };
198
199 CoreModule::CoreModule() : m_impl(new CoreModuleImpl())
200 {}
201
202 CoreModule::~CoreModule()
203 {}
204
205 bool CoreModule::Init()
206 {
207     return m_impl->Init();
208 }
209
210 void CoreModule::Terminate()
211 {
212     return m_impl->Terminate();
213 }
214 #ifdef MULTIPROCESS_SERVICE_SUPPORT
215 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(
216     const std::string& tizenId,
217     DPL::Optional<unsigned> serviceIndex)
218 #else
219 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(
220     const std::string& tizenId)
221 #endif
222 {
223 #ifdef MULTIPROCESS_SERVICE_SUPPORT
224     return m_impl->getRunnableWidgetObject(tizenId, serviceIndex);
225 #else
226     return m_impl->getRunnableWidgetObject(tizenId);
227 #endif
228 }
229
230 CoreModule::NetworkAccessMode CoreModule::homeNetworkAccess()
231 {
232     return m_impl->homeNetworkAccess();
233 }
234
235 CoreModule::NetworkAccessMode CoreModule::roamingNetworkAccess()
236 {
237     return m_impl->roamingNetworkAccess();
238 }
239
240 bool CoreModule::developerMode()
241 {
242     return m_impl->developerMode();
243 }
244 } /* namespace WRT */