Temporary fix for asking geolocation permission popup crash.
[platform/framework/web/wrt.git] / src / view / common / application_launcher.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    application_launcher.cpp
18  * @author  Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for application launcher
21  */
22
23 #include "application_launcher.h"
24
25 #include <dpl/log/secure_log.h>
26 #include <dpl/singleton_impl.h>
27
28 #include <app.h>
29 #include <app_manager.h>
30 #include <appsvc.h>
31 #include <download.h>
32 #include <notification.h>
33 #include <widget_string.h>
34
35 IMPLEMENT_SINGLETON(ApplicationLauncher)
36
37 namespace {
38 const char * const SERVICE_EXTRA_DATA_KEY_PATH = "path";
39 const char * const SERVICE_EXTRA_DATA_KEY_COOKIE = "cookie";
40 const char * const SERVICE_EXTRA_DATA_KEY_MODE = "mode";
41 const char * const SERVICE_EXTRA_DATA_VALUE_SLIENT = "silent";
42
43 const char * const SCHEME_TYPE_RTSP = "rtsp";
44 const char * const SCHEME_TYPE_HTML5_VIDEO = "html5video";
45 }
46
47 ApplicationLauncher::ApplicationLauncher() :
48     m_windowHandle(0)
49 {
50 }
51
52 ApplicationLauncher::~ApplicationLauncher()
53 {}
54
55 void ApplicationLauncher::OnEventReceived(
56     const ApplicationLauncherEvents::LaunchApplicationByAppService &event)
57 {
58     int result;
59     service_h serviceHandle = event.GetArg0();
60     service_reply_cb responseCallback = event.GetArg1();
61     void *userData = event.GetArg2();
62
63     if (m_windowHandle) {
64         service_set_window(serviceHandle, m_windowHandle);
65     }
66
67     result = service_send_launch_request(serviceHandle, responseCallback, userData);
68     if (result != SERVICE_ERROR_NONE) {
69         service_destroy(serviceHandle);
70         _E("Failed to run service : %d", result);
71         return;
72     }
73     service_destroy(serviceHandle);
74     _D("Success to run service");
75 }
76
77 void ApplicationLauncher::OnEventReceived(
78     const ApplicationLauncherEvents::LaunchApplicationByPkgname &event)
79 {
80     using namespace ApplicationLauncherPkgname;
81     std::string pkgName(event.GetArg0());
82
83     if (PKG_NAME_DOWNLOAD_PROVIDER == pkgName) {
84         std::string url(event.GetArg1());
85         // This value needs for checking video, music contents later.
86         //std::string mime_type(event.GetArg2());
87         std::string cookie(event.GetArg3());
88
89         if ("null" == url) {
90             _E("url is empty");
91             return;
92         }
93
94         service_h serviceHandle = NULL;
95         int ret = SERVICE_ERROR_NONE;
96
97         // create service
98         ret = service_create(&serviceHandle);
99         if (SERVICE_ERROR_NONE != ret && NULL == serviceHandle) {
100             _E("Fail to create service");
101             return;
102         }
103
104         // set service operation
105         ret = service_set_operation(serviceHandle, SERVICE_OPERATION_DOWNLOAD);
106         if (SERVICE_ERROR_NONE != ret) {
107             _E("Fail to set operation [%d]", ret);
108             service_destroy(serviceHandle);
109             return;
110         }
111
112         // set service uri
113         ret = service_set_uri(serviceHandle, url.c_str());
114         if (SERVICE_ERROR_NONE != ret) {
115             _E("Fail to set uri [%d]");
116             service_destroy(serviceHandle);
117             return;
118         }
119
120         // set cookie
121         if (cookie != "null") {
122             ret = service_add_extra_data(serviceHandle,
123                                          SERVICE_EXTRA_DATA_KEY_COOKIE,
124                                          cookie.c_str());
125             if (SERVICE_ERROR_NONE != ret) {
126                 _D("Fail to add cookie [%d]", ret);
127                 service_destroy(serviceHandle);
128                 return;
129             }
130         }
131
132         ret = service_add_extra_data(serviceHandle,
133                                      SERVICE_EXTRA_DATA_KEY_MODE,
134                                      SERVICE_EXTRA_DATA_VALUE_SLIENT);
135         if (SERVICE_ERROR_NONE != ret) {
136             _E("Fail to set service extra data [%d]", ret);
137             service_destroy(serviceHandle);
138             return;
139         }
140
141         if (m_windowHandle) {
142             service_set_window(serviceHandle, m_windowHandle);
143         }
144
145         //launch service
146         ret = service_send_launch_request(serviceHandle, NULL, NULL);
147         if (SERVICE_ERROR_NONE != ret) {
148             _E("Fail to launch service [%d]", ret);
149             service_destroy(serviceHandle);
150             return;
151         }
152         service_destroy(serviceHandle);
153         notification_status_message_post(WRT_POP_STARTING_DOWNLOADING);
154         return;
155     } else if (PKG_NAME_VIDEO_PLAYER == pkgName) {
156         bool isRunning = false;
157         if (APP_MANAGER_ERROR_NONE !=
158             app_manager_is_running(PKG_NAME_VT_MAIN.c_str(), &isRunning))
159         {
160             _E("Fail to get app running information");
161             return;
162         }
163         if (true == isRunning) {
164             _E("video-call is running");
165             return;
166         }
167
168         std::string scheme(event.GetArg1());
169         std::string uri(event.GetArg2());
170         std::string cookie(event.GetArg3());
171         const char* url;
172
173         if ("null" == scheme) {
174             _E("scheme is empty");
175             return;
176         }
177         if ("null" == uri) {
178             _E("uri is empty");
179             return;
180         }
181
182         if (SCHEME_TYPE_RTSP == scheme ||
183             SCHEME_TYPE_HTML5_VIDEO == scheme)
184         {
185             url = uri.c_str();
186         } else {
187             _E("scheme is invalid!!");
188             return;
189         }
190
191         service_h serviceHandle = NULL;
192         int ret = SERVICE_ERROR_NONE;
193
194         // create service
195         ret = service_create(&serviceHandle);
196         if (SERVICE_ERROR_NONE != ret && NULL == serviceHandle) {
197             _E("Fail to create service");
198             return;
199         }
200
201         // set url
202         if (!url || strlen(url) == 0) {
203             _E("Fail to get url");
204             service_destroy(serviceHandle);
205             return;
206         }
207         ret = service_add_extra_data(serviceHandle,
208                                      SERVICE_EXTRA_DATA_KEY_PATH,
209                                      url);
210         if (SERVICE_ERROR_NONE != ret) {
211             _E("Fail to set url [%d]", ret);
212             service_destroy(serviceHandle);
213             return;
214         }
215
216         // set cookie
217         if (SCHEME_TYPE_HTML5_VIDEO == scheme) {
218             if ("null" != cookie) {
219                 ret = service_add_extra_data(serviceHandle,
220                                              SERVICE_EXTRA_DATA_KEY_COOKIE,
221                                              cookie.c_str());
222                 if (SERVICE_ERROR_NONE != ret) {
223                     _E("Fail to add cookie [%d]", ret);
224                     service_destroy(serviceHandle);
225                     return;
226                 }
227             }
228         }
229
230         // set package
231         ret = service_set_package(serviceHandle, PKG_NAME_VIDEO_PLAYER.c_str());
232         if (SERVICE_ERROR_NONE != ret) {
233             _E("Fail to set package service [%d]", ret);
234             service_destroy(serviceHandle);
235             return;
236         }
237
238         // set window handle when available
239         if (m_windowHandle) {
240             service_set_window(serviceHandle, m_windowHandle);
241         }
242
243         //launch service
244         ret = service_send_launch_request(serviceHandle, NULL, NULL);
245         if (SERVICE_ERROR_NONE != ret) {
246             _E("Fail to launch service [%d]", ret);
247             service_destroy(serviceHandle);
248             return;
249         }
250         service_destroy(serviceHandle);
251         return;
252     } else {
253         _E("Not implemented application : ", pkgName.c_str());
254     }
255
256     _D("Success to launch application : ", pkgName.c_str());
257     return;
258 }
259
260 void ApplicationLauncher::setWidgetTizenId(const std::string& tizenId)
261 {
262     m_tizenId = tizenId;
263 }
264
265 void ApplicationLauncher::setWindowHandle(unsigned windowHandle)
266 {
267     m_windowHandle = windowHandle;
268 }