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