[Release] wrt_0.8.213
[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
40 #include <EWebKit2.h>
41
42 IMPLEMENT_SINGLETON(WRT::CoreModule)
43
44 namespace {
45 std::string cutOffFileName(const std::string& path)
46 {
47     size_t found = path.find_last_of("/");
48     if (found == std::string::npos) {
49         return path;
50     } else {
51         return path.substr(0, found);
52     }
53 }
54
55 bool isDir(const std::string& path)
56 {
57     struct stat st;
58     if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
59         return true;
60     }
61     LogError("Cannot access directory [ " << path << " ]");
62     return false;
63 }
64
65 bool checkPaths()
66 {
67     using namespace WrtDB;
68     using namespace WrtDB::GlobalConfig;
69
70     bool if_ok = true;
71     if_ok &= (isDir(cutOffFileName(GetWrtDatabaseFilePath())));
72     if_ok &= (isDir(GetDevicePluginPath()));
73     if_ok &= (isDir(GetUserInstalledWidgetPath()));
74     return if_ok;
75 }
76 } // namespace anonymous
77
78 namespace WRT {
79 class CoreModuleImpl
80 {
81   public:
82
83     CoreModuleImpl() : m_initialized(false), m_ewkContext(NULL)
84     {
85         LogDebug("enter");
86     }
87
88     ~CoreModuleImpl()
89     {
90         LogDebug("Core module implementation destroyed");
91     }
92
93     bool Init()
94     {
95         if (!m_initialized) {
96             ADD_PROFILING_POINT("CoreModule::Init", "point");
97             DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
98             LogDebug("Initialize");
99             if (!checkPaths()) {
100                 LogError("Required path does not exist");
101                 return false;
102             }
103             Try
104             {
105                 ADD_PROFILING_POINT("attach databases", "start");
106                 MainThreadSingleton::Instance().AttachDatabases();
107                 ADD_PROFILING_POINT("attach databases", "stop");
108                 LogDebug("Initialize finished");
109             } catch (const DPL::Exception& ex) {
110                 LogError("Internal Error during screen preparation:");
111                 DPL::Exception::DisplayKnownException(ex);
112                 /* TODO:
113                  * Do deinitialization: check on which step exception occured
114                  * and deinitialize only initialized parts.
115                  */
116                 return false;
117             }
118             m_initialized = true;
119         }
120         return true;
121     }
122
123     void Terminate()
124     {
125         MainThreadSingleton::Instance().DetachDatabases();
126         m_initialized = false;
127     }
128
129     RunnableWidgetObjectPtr getRunnableWidgetObject(
130         const std::string& tizenId,
131         DPL::OptionalUInt appControlIndex)
132     {
133         try {
134             RunnableWidgetObjectPtr runnable;
135             WidgetModelPtr model =
136                 Domain::deserializeWidgetModel(tizenId, appControlIndex);
137             if (!!model) {
138                 runnable.reset(new RunnableWidgetObject(model));
139             }
140             return runnable;
141         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
142             LogDebug("Widget not found.");
143             return RunnableWidgetObjectPtr();
144         }
145     }
146
147     CoreModule::NetworkAccessMode homeNetworkAccess()
148     {
149         switch (WrtDB::GlobalDAOReadOnly::GetHomeNetworkDataUsage()) {
150         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
151             return CoreModule::NEVER_CONNECT;
152         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
153             return CoreModule::ALWAYS_ASK;
154         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
155             return CoreModule::CONNECT_AUTOMATICALLY;
156         default:
157             break;
158         }
159         LogWarning("using default value");
160         return CoreModule::ALWAYS_ASK;
161     }
162
163     CoreModule::NetworkAccessMode roamingNetworkAccess()
164     {
165         switch (WrtDB::GlobalDAOReadOnly::GetRoamingDataUsage()) {
166         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
167             return CoreModule::NEVER_CONNECT;
168         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
169             return CoreModule::ALWAYS_ASK;
170         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
171             return CoreModule::CONNECT_AUTOMATICALLY;
172         default:
173             break;
174         }
175         LogWarning("using default value");
176         return CoreModule::ALWAYS_ASK;
177     }
178
179   private:
180     bool m_initialized;
181     Ewk_Context* m_ewkContext;
182 };
183
184 CoreModule::CoreModule() : m_impl(new CoreModuleImpl())
185 {}
186
187 CoreModule::~CoreModule()
188 {}
189
190 bool CoreModule::Init()
191 {
192     return m_impl->Init();
193 }
194
195 void CoreModule::Terminate()
196 {
197     return m_impl->Terminate();
198 }
199
200 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(
201     const std::string& tizenId,
202     DPL::OptionalUInt appControlIndex)
203 {
204     return m_impl->getRunnableWidgetObject(tizenId, appControlIndex);
205 }
206
207 CoreModule::NetworkAccessMode CoreModule::homeNetworkAccess()
208 {
209     return m_impl->homeNetworkAccess();
210 }
211
212 CoreModule::NetworkAccessMode CoreModule::roamingNetworkAccess()
213 {
214     return m_impl->roamingNetworkAccess();
215 }
216
217 } /* namespace WRT */