8debb430c6efeaa953cbc377344f2a95c08e2aa5
[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 #include <app.h>
25 #include <app_manager.h>
26 #include <download.h>
27 #include <youtubehd.h>
28 #include <dpl/singleton_impl.h>
29 #include <appsvc.h>
30
31 IMPLEMENT_SINGLETON(ApplicationLauncher)
32
33 using namespace WrtDB;
34
35 namespace {
36 const char * const SERVICE_EXTRA_DATA_KEY_PATH = "path";
37 const char * const SERVICE_EXTRA_DATA_KEY_COOKIE = "cookie";
38
39 const char * const SCHEME_TYPE_YOUTUBE = "vnd.youtube";
40 const char * const SCHEME_TYPE_RTSP = "rtsp";
41 const char * const SCHEME_TYPE_HTML5_VIDEO = "html5video";
42 }
43
44 ApplicationLauncher::ApplicationLauncher()
45 {
46 }
47
48 ApplicationLauncher::~ApplicationLauncher()
49 {
50 }
51
52 void ApplicationLauncher::OnEventReceived(
53         const ApplicationLauncherEvents::LaunchApplicationByAppService &event)
54 {
55     int result;
56     bundle *args = event.GetArg0();
57     appsvc_res_fn responseCallback = event.GetArg1();
58     void *userData = event.GetArg2();
59     result = appsvc_run_service(args, 0, responseCallback, userData);
60
61     if (result < 0)
62         LogError("Failed to run AppService : " << result);
63 }
64
65 void ApplicationLauncher::OnEventReceived(
66         const ApplicationLauncherEvents::LaunchApplicationByPkgname &event)
67 {
68     using namespace ApplicationLauncherPkgname;
69     LogDebug("LaunchApplicationByPkgname");
70     std::string pkgName(event.GetArg0());
71
72     if (PKG_NAME_DOWNLOAD_PROVIDER == pkgName) {
73         std::string url(event.GetArg1());
74         // This value needs for checking video, music contents later.
75         //std::string mime_type(event.GetArg2());
76         std::string cookie(event.GetArg3());
77
78         if ("null" == url) {
79             LogError("url is empty");
80             return;
81         }
82
83         service_h serviceHandle = NULL;
84         int ret = SERVICE_ERROR_NONE;
85
86         // create service
87         ret = service_create(&serviceHandle);
88         if (SERVICE_ERROR_NONE != ret && NULL == serviceHandle) {
89             LogError("Fail to create service");
90             return;
91         }
92
93         // set service operation
94         ret = service_set_operation(serviceHandle, SERVICE_OPERATION_DOWNLOAD);
95         if (SERVICE_ERROR_NONE != ret) {
96             LogError("Fail to set operation [" << ret << "]");
97             service_destroy(serviceHandle);
98             return;
99         }
100
101         // set service uri
102         ret = service_set_uri(serviceHandle, url.c_str());
103         if (SERVICE_ERROR_NONE != ret) {
104             LogError("Fail to set uri [" << ret << "]");
105             service_destroy(serviceHandle);
106             return;
107         }
108
109         // set cookie
110         if (cookie != "null") {
111             ret = service_add_extra_data(serviceHandle,
112                                          SERVICE_EXTRA_DATA_KEY_COOKIE,
113                                          cookie.c_str());
114             if (SERVICE_ERROR_NONE != ret) {
115                 LogError("Fail to add cookie [" << ret << "]");
116                 service_destroy(serviceHandle);
117                 return;
118             }
119         }
120
121         //launch service
122         ret = service_send_launch_request(serviceHandle, NULL, NULL);
123         if (SERVICE_ERROR_NONE != ret) {
124             LogError("Fail to launch service [" << ret << "]");
125             service_destroy(serviceHandle);
126             return;
127         }
128
129         LogDebug("Success launch " << SERVICE_OPERATION_DOWNLOAD);
130         service_destroy(serviceHandle);
131         return;
132     } else if (PKG_NAME_VIDEO_PLAYER == pkgName) {
133         bool isRunning = false;
134         if (APP_MANAGER_ERROR_NONE !=
135             app_manager_is_running(PKG_NAME_VT_MAIN.c_str(), &isRunning))
136         {
137             LogError("Fail to get app running information");
138             return;
139         }
140         if (true == isRunning) {
141             LogError("video-call is running");
142             return;
143         }
144
145         std::string scheme(event.GetArg1());
146         std::string uri(event.GetArg2());
147         std::string cookie(event.GetArg3());
148         const char* url;
149
150         if ("null" == scheme) {
151             LogError("scheme is empty");
152             return;
153         }
154         if ("null" == uri) {
155             LogError("uri is empty");
156             return;
157         }
158
159         LogDebug("scheme: " << scheme);
160         if (SCHEME_TYPE_YOUTUBE == scheme) {
161             YouTubeHD *youtube = new YouTubeHD(m_tizenId);
162             url = youtube->getYouTubeHD(uri.c_str());
163             delete youtube;
164         } else if (SCHEME_TYPE_RTSP == scheme ||
165                     (SCHEME_TYPE_HTML5_VIDEO == scheme))
166         {
167             url = uri.c_str();
168         } else {
169             LogError("scheme is invalid!!");
170             return;
171         }
172
173         service_h serviceHandle = NULL;
174         int ret = SERVICE_ERROR_NONE;
175
176         // create service
177         ret = service_create(&serviceHandle);
178         if (SERVICE_ERROR_NONE != ret && NULL == serviceHandle) {
179             LogError("Fail to create service");
180             return;
181         }
182
183         // set url
184         if (!url || strlen(url) == 0) {
185             LogError("Fail to get url");
186             service_destroy(serviceHandle);
187             return;
188         }
189         ret = service_add_extra_data(serviceHandle,
190                                      SERVICE_EXTRA_DATA_KEY_PATH,
191                                      url);
192         if (SERVICE_ERROR_NONE != ret) {
193             LogError("Fail to set url [" << ret << "]");
194             service_destroy(serviceHandle);
195             return;
196         }
197
198         // set cookie
199         if (SCHEME_TYPE_HTML5_VIDEO == scheme) {
200             if ("null" != cookie) {
201                 ret = service_add_extra_data(serviceHandle,
202                                              SERVICE_EXTRA_DATA_KEY_COOKIE,
203                                              cookie.c_str());
204                 if (SERVICE_ERROR_NONE != ret) {
205                     LogError("Fail to add cookie [" << ret << "]");
206                     service_destroy(serviceHandle);
207                     return;
208                 }
209             }
210         }
211
212         // set package
213         ret = service_set_package(serviceHandle, PKG_NAME_VIDEO_PLAYER.c_str());
214         if (SERVICE_ERROR_NONE != ret) {
215             LogError("Fail to set package [" << ret << "]");
216             service_destroy(serviceHandle);
217             return;
218         }
219
220         //launch service
221         ret = service_send_launch_request(serviceHandle, NULL, NULL);
222         if (SERVICE_ERROR_NONE != ret) {
223             LogError("Fail to launch service [" << ret << "]");
224             service_destroy(serviceHandle);
225             return;
226         }
227
228         LogDebug("Success launch " << PKG_NAME_VIDEO_PLAYER);
229         service_destroy(serviceHandle);
230         return;
231     }
232 }
233
234 void ApplicationLauncher::setWidgetTizenId(const std::string& tizenId)
235 {
236     LogDebug("tizen id: " << tizenId);
237     m_tizenId = tizenId;
238 }