[Release] wrt_0.8.215
[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 const char* const TEXT_DOMAIN = "wrt";
46 const char* const TEXT_LOCALE_PATH = "/usr/share/wrt-engine/locale";
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             bindtextdomain(TEXT_DOMAIN, TEXT_LOCALE_PATH);
122             m_initialized = true;
123         }
124         return true;
125     }
126
127     void Terminate()
128     {
129         MainThreadSingleton::Instance().DetachDatabases();
130         m_initialized = false;
131     }
132
133     RunnableWidgetObjectPtr getRunnableWidgetObject(
134         const std::string& tizenId,
135         DPL::OptionalUInt appControlIndex)
136     {
137         try {
138             RunnableWidgetObjectPtr runnable;
139             WidgetModelPtr model =
140                 Domain::deserializeWidgetModel(tizenId, appControlIndex);
141             if (!!model) {
142                 runnable.reset(new RunnableWidgetObject(model));
143             }
144             return runnable;
145         } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
146             LogDebug("Widget not found.");
147             return RunnableWidgetObjectPtr();
148         }
149     }
150
151     CoreModule::NetworkAccessMode homeNetworkAccess()
152     {
153         switch (WrtDB::GlobalDAOReadOnly::GetHomeNetworkDataUsage()) {
154         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
155             return CoreModule::NEVER_CONNECT;
156         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
157             return CoreModule::ALWAYS_ASK;
158         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
159             return CoreModule::CONNECT_AUTOMATICALLY;
160         default:
161             break;
162         }
163         LogWarning("using default value");
164         return CoreModule::ALWAYS_ASK;
165     }
166
167     CoreModule::NetworkAccessMode roamingNetworkAccess()
168     {
169         switch (WrtDB::GlobalDAOReadOnly::GetRoamingDataUsage()) {
170         case WrtDB::GlobalDAOReadOnly::NEVER_CONNECT:
171             return CoreModule::NEVER_CONNECT;
172         case WrtDB::GlobalDAOReadOnly::ALWAYS_ASK:
173             return CoreModule::ALWAYS_ASK;
174         case WrtDB::GlobalDAOReadOnly::CONNECT_AUTOMATICALLY:
175             return CoreModule::CONNECT_AUTOMATICALLY;
176         default:
177             break;
178         }
179         LogWarning("using default value");
180         return CoreModule::ALWAYS_ASK;
181     }
182
183   private:
184     bool m_initialized;
185     Ewk_Context* m_ewkContext;
186 };
187
188 CoreModule::CoreModule() : m_impl(new CoreModuleImpl())
189 {}
190
191 CoreModule::~CoreModule()
192 {}
193
194 bool CoreModule::Init()
195 {
196     return m_impl->Init();
197 }
198
199 void CoreModule::Terminate()
200 {
201     return m_impl->Terminate();
202 }
203
204 RunnableWidgetObjectPtr CoreModule::getRunnableWidgetObject(
205     const std::string& tizenId,
206     DPL::OptionalUInt appControlIndex)
207 {
208     return m_impl->getRunnableWidgetObject(tizenId, appControlIndex);
209 }
210
211 CoreModule::NetworkAccessMode CoreModule::homeNetworkAccess()
212 {
213     return m_impl->homeNetworkAccess();
214 }
215
216 CoreModule::NetworkAccessMode CoreModule::roamingNetworkAccess()
217 {
218     return m_impl->roamingNetworkAccess();
219 }
220
221 } /* namespace WRT */