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