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