[Engine] Replace widget handle with tizen id in widgetModel
[platform/framework/web/wrt.git] / src / view / common / view_logic_apps_support.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    view_logic_apps_support.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @brief   Implementation file of AppsSupport class used by ViewLogic
20  */
21
22 #include "view_logic_apps_support.h"
23 #include <dpl/assert.h>
24 #include <dpl/log/log.h>
25
26 #include <appsvc.h>
27 #include <widget_model.h>
28 #include <application_launcher.h>
29
30 namespace ViewModule {
31
32 namespace {
33 const char* const SCHEME_TYPE_HTML5_VIDEO = "html5video";
34 const char* const HTTP_STREAMING_MPEG_MIMETYPE = "application/x-mpegurl";
35 const char* const HTTP_STREAMING_APPLE_MIMETYPE =
36     "application/vnd.apple.mpegurl";
37 }
38
39 //Implementation class
40 class AppsSupportImplementation
41 {
42   private:
43     WidgetModel *m_widgetModel;
44     bool m_initialized;
45
46     struct HTML5Video {
47         const char* path;
48         const char* cookie;
49     };
50
51   public:
52     AppsSupportImplementation() :
53         m_widgetModel(NULL),
54         m_initialized(false)
55     {
56     }
57
58     ~AppsSupportImplementation()
59     {
60         Assert(!m_initialized &&
61                "AppsSupport has to be deinitialized prior destroying!");
62     }
63
64     void initialize(WidgetModel *widgetModel)
65     {
66         Assert(!m_initialized && "Already initialized!");
67
68         LogDebug("Initializing Apps Support");
69         Assert(widgetModel && "Passed widgetModel is NULL!");
70         m_widgetModel = widgetModel;
71
72         ApplicationLauncherSingleton::Instance().Touch();
73
74         ApplicationLauncherSingleton::Instance().setWidgetTizenId(
75                 DPL::ToUTF8String(m_widgetModel->TizenId));
76
77         LogDebug("Initialized");
78         m_initialized = true;
79     }
80
81     void deinitialize()
82     {
83         Assert(m_initialized && "Not initialized!");
84         LogDebug("Deinitialized");
85         m_widgetModel = NULL;
86         m_initialized = false;
87     }
88
89     bool httpMultimediaRequest(std::string mimeType, std::string uri)
90     {
91         LogInfo("httpMultimediaRequest called");
92
93         if ("null" == mimeType || "null" == uri) {
94             LogError("uri/mimeType is null");
95             return false;
96         }
97
98         bundle *args = NULL;
99         args = bundle_create();
100         void *userData = NULL;
101         appsvc_res_fn responseCallback = NULL;
102
103         // ignore case match of string of mime type
104         // if needed, define appsvc response callback
105         // and its user data per mimetype
106         if (!strcasecmp(mimeType.c_str(), HTTP_STREAMING_APPLE_MIMETYPE) ||
107             !strcasecmp(mimeType.c_str(), HTTP_STREAMING_MPEG_MIMETYPE))
108         {
109             appsvc_set_operation(args, APPSVC_OPERATION_VIEW);
110             appsvc_set_mime(args, mimeType.c_str());
111             appsvc_set_uri(args, uri.c_str());
112         } else if (!strcasecmp(mimeType.c_str(), "video/mp4") ||
113                    !strcasecmp(mimeType.c_str(), "vidoe/3gp"))
114         {
115             CONTROLLER_POST_EVENT(
116                 ApplicationLauncher,
117                 ApplicationLauncherEvents::LaunchApplicationByPkgname(
118                     ApplicationLauncherPkgname::PKG_NAME_VIDEO_PLAYER,
119                     SCHEME_TYPE_HTML5_VIDEO,
120                     uri,
121                     "null"));
122         } else {
123             LogInfo("Not Supported MIME type in WRT");
124             bundle_free(args);
125             return false;
126         }
127
128         CONTROLLER_POST_EVENT(
129             ApplicationLauncher,
130             ApplicationLauncherEvents::LaunchApplicationByAppService(
131                 args,
132                 responseCallback,
133                 userData));
134
135         return true;
136     }
137
138     void downloadRequest(const char *url,
139                          const char *mimeType,
140                          const char *userParam)
141     {
142         LogInfo("Download info : " << url << "(" <<
143                 mimeType << ", " << userParam << ")");
144
145         // ignore case match of string of mime type
146         int isAppServiceable = httpMultimediaRequest(
147             mimeType ? std::string(mimeType) : "null",
148             url ? std::string(url) : "null");
149
150         if (isAppServiceable) {
151             LogInfo("Application Service start");
152             return;
153         }
154
155         CONTROLLER_POST_EVENT(
156             ApplicationLauncher,
157             ApplicationLauncherEvents::LaunchApplicationByPkgname(
158                 ApplicationLauncherPkgname::PKG_NAME_DOWNLOAD_PROVIDER,
159                 url ? std::string(url) : "null",
160                 mimeType ? mimeType : "null",
161                 userParam ? userParam : "null"));
162     }
163
164     void html5VideoRequest(void* event_info)
165     {
166         LogInfo("html5VideoRequestCallback called");
167         Assert(event_info);
168         HTML5Video* video = static_cast<HTML5Video*>(event_info);
169
170         LogDebug("video->path : " << video->path);
171         LogDebug("video->cookie : " << video->cookie);
172         if (NULL == video->path) {
173             LogError("path is null");
174             return;
175         }
176         CONTROLLER_POST_EVENT(
177             ApplicationLauncher,
178             ApplicationLauncherEvents::LaunchApplicationByPkgname(
179                 ApplicationLauncherPkgname::PKG_NAME_VIDEO_PLAYER,
180                 SCHEME_TYPE_HTML5_VIDEO,
181                 video->path ? video->path : "null",
182                 video->cookie ? video->cookie : "null"));
183     }
184 };
185
186 AppsSupport::AppsSupport(): m_impl(new AppsSupportImplementation)
187 {
188 }
189
190 AppsSupport::~AppsSupport()
191 {
192 }
193
194 void AppsSupport::initialize(WidgetModel *widgetModel)
195 {
196     m_impl->initialize(widgetModel);
197 }
198
199 void AppsSupport::deinitialize()
200 {
201     m_impl->deinitialize();
202 }
203
204 void AppsSupport::html5VideoRequest(void* event_info)
205 {
206     m_impl->html5VideoRequest(event_info);
207 }
208
209 void AppsSupport::downloadRequest(const char *url,
210                                   const char *mimeType,
211                                   const char *userParam)
212 {
213     m_impl->downloadRequest(url, mimeType, userParam);
214 }
215
216 }//namespace