44c23fe4d1db47a39684426a3af44b19f7dc2d69
[platform/framework/web/wrt.git] / src / api_new / runnable_widget_object.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 defitinions of RunnableWidgetObject implementation.
21  */
22
23 #include <runnable_widget_object.h>
24 #include <privilege-control.h>
25 #include <dpl/exception.h>
26 #include <dpl/wrt-dao-ro/global_config.h>
27 #include <dpl/utils/wrt_global_settings.h>
28 #include <appcore-common.h>
29 #include <profiling_util.h>
30 #include <signal.h>
31 #include "webkit/bundles/plugin_module_support.h"
32 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
33 #include <runnable_widget_object_state.h>
34 #include <wrt_ocsp_api.h>
35 #include <popup-runner/PopupInvoker.h>
36 #include "ewk_context_manager.h"
37
38 namespace { //Anonymous
39 const std::string ACCESS_DENIED = _("IDS_BR_POP_ACCESS_DENIED");
40 const std::string ALREADY_RUNNING = _("IDS_BR_POP_ALREADY_RUNNING");
41 const std::string INVALID_LOCALE = _("IDS_IM_POP_INVALID_WIDGET_LOCALE");
42 const std::string STILL_AUTHORIZING = _("IDS_IM_POP_AUTHORIZING_ING_ATNT");
43 const unsigned int UID_ROOT = 0;
44 } // namespace anonymous
45
46 namespace WRT {
47 RunnableWidgetObject::RunnableWidgetObject(WidgetModelPtr &model) :
48         m_widgetModel(model),
49         m_view(ViewModule::createView())
50 {
51     //set initial state of runnable object
52     m_guardstate = std::shared_ptr<State::RunnableWidgetObjectState>(
53             new State::InitialState(*this));
54     // If current uid is 'root', change privilege to apps
55     if (UID_ROOT == getuid()) {
56         // Set privilege by tizen id
57         // this code prevent that widget launch with "root" permission,
58         // when developers launch by command in the shell
59         set_privilege(DPL::ToUTF8String(m_widgetModel->TizenId).c_str());
60     }
61 }
62
63 bool RunnableWidgetObject::CheckBeforeLaunch()
64 {
65     State::StateChange change = m_guardstate->allowCheckBeforeLaunch();
66     Assert(m_widgetModel);
67
68 #ifdef WRT_SMACK_ENABLED
69     // TODO - this should be in the very first line of the process's main()
70     // for security reasons; but for now it is easier to place here because
71     // here the pkg name is already known; we don't struggle to move it
72     // near the start of main() because we don't know yet if this will
73     // stay in this process at all: it may be moved to AUL altogether
74     set_process_config(DPL::ToUTF8String(widgetModel->TizenId).c_str());
75 #endif
76
77     // Inform view controller about new widget model displaying
78     ADD_PROFILING_POINT("OCSPCheck", "start");
79
80     wrt_ocsp_widget_verification_status_t response;
81     if (m_widgetModel->Type.Get().appType == WrtDB::APP_TYPE_WAC20) {
82         wrt_ocsp_initialize();
83         wrt_ocsp_verify_widget(WrtDB::WidgetDAOReadOnly::getHandle(
84                                    m_widgetModel->TizenId), &response);
85         wrt_ocsp_shutdown();
86     } else {
87         response = WRT_OCSP_WIDGET_VERIFICATION_STATUS_GOOD;
88     }
89     ADD_PROFILING_POINT("OCSPCheck", "stop");
90
91     LogDebug("WRT OCSP response " << static_cast<int>(response));
92     if (response == WRT_OCSP_WIDGET_VERIFICATION_STATUS_GOOD) {
93         change.commit();
94         return true;
95     } else {
96         if (!GlobalSettings::PopupsTestModeEnabled()) {
97             Wrt::Popup::PopupInvoker().showInfo(
98                 _("IDS_IM_POP_WIDGET_REVOKED_TITLE"),
99                 _("IDS_IM_POP_WIDGET_REVOKED_ERROR"),
100                 _("IDS_IM_BUTTON_OK"));
101         }
102         return false;
103     }
104 }
105
106 bool RunnableWidgetObject::PrepareView(const std::string &startUrl,
107         Evas_Object *window, Ewk_Context* ewkContext)
108 {
109     State::StateChange change = m_guardstate->allowPrepareView();
110     if (!window) {
111         return false;
112     }
113
114     std::string appId = DPL::ToUTF8String(m_widgetModel->TizenId);
115     Try {
116         if(!m_ewkContextManager) {
117             m_ewkContextManager =
118                 EwkContextManager::create(appId, ewkContext, m_view);
119         } else {
120             if (ewkContext && 
121                     ewkContext != m_ewkContextManager->getEwkContext()) 
122             {
123                 m_ewkContextManager =
124                     EwkContextManager::create(appId, ewkContext, m_view);
125             }
126         }
127     } Catch (DPL::Exception) {
128         LogError("Internal Error during create or initialize Ewk Context");
129         return false;
130     }
131
132     ADD_PROFILING_POINT("view_logic_init", "start");
133     Ewk_Context* context = m_ewkContextManager->getEwkContext();
134
135     // plugin init
136     PluginModuleSupport::init(context, DPL::ToUTF8String(m_widgetModel->TizenId));
137
138     // view init
139     if(!m_view->createWebView(context, window)) {
140         return false;
141     }
142     m_view->prepareView(m_widgetModel.get(), startUrl);
143     ADD_PROFILING_POINT("view_logic_init", "stop");
144
145     change.commit();
146     return true;
147 }
148
149 void RunnableWidgetObject::Show()
150 {
151     State::StateChange change = m_guardstate->allowShow();
152
153     m_view->showWidget();
154
155     change.commit();
156 }
157
158 void RunnableWidgetObject::Hide()
159 {
160     State::StateChange change = m_guardstate->allowHide();
161
162     m_view->hideWidget();
163
164     change.commit();
165 }
166
167 void RunnableWidgetObject::Suspend()
168 {
169     LogDebug("Suspending widget");
170     State::StateChange change = m_guardstate->allowSuspend();
171     m_view->suspendWidget();
172
173     change.commit();
174 }
175
176 void RunnableWidgetObject::Resume()
177 {
178     LogDebug("Resuming widget");
179     State::StateChange change = m_guardstate->allowResume();
180     m_view->resumeWidget();
181
182     change.commit();
183 }
184
185 void RunnableWidgetObject::Reset()
186 {
187     LogDebug("Reseting widget");
188     State::StateChange change = m_guardstate->allowReset();
189     m_view->resetWidget();
190
191     change.commit();
192 }
193
194 void RunnableWidgetObject::ReloadStartPage()
195 {
196     m_view->reloadStartPage();
197 }
198
199 Evas_Object* RunnableWidgetObject::GetCurrentWebview()
200 {
201     State::StateChange change = m_guardstate->allowGetCurrentWebview();
202
203     Evas_Object* cww = m_view->getCurrentWebview();
204
205     change.commit();
206     return cww;
207 }
208
209 void RunnableWidgetObject::SetUserDelegates(const UserDelegatesPtr& cbs)
210 {
211     State::StateChange change = m_guardstate->allowSetUserDelegates();
212     m_view->setUserCallbacks(cbs);
213     change.commit();
214 }
215
216 void RunnableWidgetObject::Backward()
217 {
218     State::StateChange change = m_guardstate->allowBackward();
219     m_view->backward();
220
221     change.commit();
222 }
223
224 void RunnableWidgetObject::FireJavascriptEvent(int event, void* data)
225 {
226     State::StateChange change = m_guardstate->allowFireJavascriptEvent();
227     m_view->fireJavascriptEvent(event, data);
228
229     change.commit();
230 }
231
232 void RunnableWidgetObject::setNewState(
233     std::shared_ptr<State::RunnableWidgetObjectState> sptr)
234 {
235     LogInfo("RunnableWidgetObject changes state to: " << sptr->toString());
236     m_guardstate = sptr;
237 }
238
239 RunnableWidgetObject::~RunnableWidgetObject()
240 {
241     LogDebug("");
242     PluginModuleSupport::shutdown(m_ewkContextManager->getEwkContext());
243 }
244 } /* namespace WRT */