Update wrt_0.8.85
[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  * @version 1.0
20  * @brief   File contains definitions of wrt core module.
21  */
22
23 #include "core_module.h"
24 #include "runnable_widget_object.h"
25 #include <string>
26 #include <main_thread.h>
27 #include <global_context.h>
28 #include <dpl/log/log.h>
29 #include <dpl/assert.h>
30 #include <dpl/exception.h>
31 #include <dpl/popup/popup_controller.h>
32 #include <libxml/parser.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
39 namespace { //Anonymous
40
41 std::string cutOffFileName(const std::string& path)
42 {
43     size_t found = path.find_last_of("/");
44     if (found == std::string::npos) {
45         return path;
46     } else {
47         return path.substr(0, found);
48     }
49 }
50
51 bool isDir(const std::string& path)
52 {
53     struct stat st;
54     if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
55         return true;
56     }
57     LogError("Cannot access directory [ " << path << " ]");
58     return false;
59 }
60
61 bool checkPaths()
62 {
63     using namespace WrtDB;
64     using namespace WrtDB::GlobalConfig;
65
66     bool if_ok = true;
67     if_ok &= (isDir(cutOffFileName(GetWrtDatabaseFilePath())));
68     if (!if_ok) {
69         LogError("Path <" << GetWrtDatabaseFilePath() << "> does not exist.");
70     }
71
72     if_ok &= (isDir(GetDevicePluginPath()));
73     if (!if_ok) {
74         LogError("Path <" << GetDevicePluginPath() << "> does not exist.");
75     }
76
77     if_ok &= (isDir(GetFactoryInstalledWidgetPath()));
78     if (!if_ok) {
79         LogError("Path <" << GetFactoryInstalledWidgetPath() <<
80             "> does not exist.");
81     }
82
83     if_ok &= (isDir(GetUserInstalledWidgetPath()));
84     if (!if_ok) {
85         LogError("Path <" << GetUserInstalledWidgetPath() <<
86             "> does not exist.");
87     }
88     return if_ok;
89 }
90 }// namespace anonymous
91
92 namespace WRT {
93
94 namespace CoreModule {
95
96 bool Init()
97 {
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         GlobalContext::TouchArchitecture();
107
108         ADD_PROFILING_POINT("attach databases", "start");
109         MainThreadSingleton::Instance().AttachDatabases();
110         ADD_PROFILING_POINT("attach databases", "stop");
111
112         ADD_PROFILING_POINT("xml_parser_init", "start");
113         xmlInitParser();
114         ADD_PROFILING_POINT("xml_parser_init", "stop");
115
116         // Initialize popup manager
117         ADD_PROFILING_POINT("popup_manager_init", "start");
118         DPL::Popup::PopupManagerSingleton::Instance().Initialize(
119             DPL::Popup::PopupRendererPtr(new DPL::Popup::PopupRenderer));
120         ADD_PROFILING_POINT("popup_manager_init", "stop");
121
122         // Initialize Language Subtag registry
123         ADD_PROFILING_POINT("language_rst_init", "start");
124         LocalizationSetting::Initialize();
125         ADD_PROFILING_POINT("language_rst_init", "stop");
126
127         LogDebug("Initialize finished");
128     } catch (const DPL::Exception& ex) {
129         LogError("Internal Error during screen preparation:");
130         DPL::Exception::DisplayKnownException(ex);
131         /* TODO:
132          * Do deinitialization: check on which step exception occured
133          * and deinitialize only initialized parts.
134         */
135         return false;
136     }
137     return true;
138 }
139
140 void Terminate()
141 {
142     MainThreadSingleton::Instance().DetachDatabases();
143     // Deinitialize popup manager
144     DPL::Popup::PopupManagerSingleton::Instance().Deinitialize();
145
146     LogInfo("Cleanup libxml2 global values.");
147     xmlCleanupParser();
148 }
149
150 RunnableWidgetObjectPtr getRunnableWidgetObject(
151     const WidgetHandle& handle)
152 {
153     RunnableWidgetObjectPtr runnable;
154     WidgetModelPtr model = Domain::deserializeWidgetModel(handle);
155     if (!!model) {
156         runnable.reset(new RunnableWidgetObject(model));
157     }
158     return runnable;
159 }
160
161 RunnableWidgetObjectPtr getRunnableWidgetObject(const std::string& packageName)
162 {
163     DPL::OptionalString pkgName(DPL::FromUTF8String(packageName));
164     try {
165         return CoreModule::getRunnableWidgetObject(
166                 WrtDB::WidgetDAOReadOnly::getHandle(pkgName));
167     } catch (WrtDB::WidgetDAOReadOnly::Exception::WidgetNotExist) {
168         LogDebug("Widget not found.");
169         return RunnableWidgetObjectPtr();
170     }
171 }
172
173 } //namespace CoreModule
174
175 } /* namespace WRT */