Update change log and spec for wrt-plugins-tizen_0.4.25-1
[platform/framework/web/wrt-plugins-tizen.git] / src / Download / JSDownloadManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24
25 #include "plugin_config.h"
26
27 #include "JSDownloadManager.h"
28 #include "JSDownloadRequest.h"
29 #include "DownloadManager.h"
30 #include "DownloadRequest.h"
31 #include "DownloadCallback.h"
32
33 #include <TimeTracer.h>
34 #include <Logger.h>
35
36 using namespace WrtDeviceApis::Commons;
37 using namespace DeviceAPI::Common;
38
39 namespace DeviceAPI {
40 namespace Download {
41
42 JSClassDefinition JSDownloadManager::m_classInfo = {
43     0,
44     kJSClassAttributeNone,
45     "DownloadManager",
46     NULL, //ParentClass
47     NULL, //StaticValues
48     m_function,
49     initialize,
50     finalize,
51     NULL, //HasProperty,
52     NULL, //GetProperty,
53     NULL, //SetProperty,
54     NULL, //DeleteProperty,
55     NULL, //GetPropertyNames,
56     NULL, //CallAsFunction,
57     NULL, //CallAsConstructor,
58     NULL, //HasInstance,
59     NULL //ConvertToType
60 };
61
62 JSStaticFunction JSDownloadManager::m_function[] = {
63     { DOWNLOAD_FUNCTION_API_START, startDownload, kJSPropertyAttributeNone },
64     { DOWNLOAD_FUNCTION_API_CANCEL, cancelDownload, kJSPropertyAttributeNone },
65     { DOWNLOAD_FUNCTION_API_PAUSE, pauseDownload, kJSPropertyAttributeNone },
66     { DOWNLOAD_FUNCTION_API_RESUME, resumeDownload, kJSPropertyAttributeNone },
67     { DOWNLOAD_FUNCTION_API_GET_STATE, getState, kJSPropertyAttributeNone },
68     { DOWNLOAD_FUNCTION_API_GET_DOWNLOAD_REQUEST, getDownloadRequest, kJSPropertyAttributeNone },
69     { DOWNLOAD_FUNCTION_API_GET_MIME_TYPE, getMIMEType, kJSPropertyAttributeNone },
70     { DOWNLOAD_FUNCTION_API_SET_LISTENER, setListener, kJSPropertyAttributeNone },
71
72     { 0, 0, 0 }
73 };
74
75 JSClassRef JSDownloadManager::m_jsClassRef = JSClassCreate(JSDownloadManager::getClassInfo());
76
77 const JSClassRef JSDownloadManager::getClassRef()
78 {
79     if (!m_jsClassRef) {
80         m_jsClassRef = JSClassCreate(&m_classInfo);
81     }
82     return m_jsClassRef;
83 }
84
85 const JSClassDefinition* JSDownloadManager::getClassInfo()
86 {
87     return &m_classInfo;
88 }
89
90 void JSDownloadManager::initialize(JSContextRef context, JSObjectRef object)
91 {
92         if (!JSObjectGetPrivate(object)) {
93                 DownloadManager *downloadManager = new DownloadManager();
94                 if (!JSObjectSetPrivate(object, static_cast<void*>(downloadManager))) {
95                         delete downloadManager;
96                 }
97         }
98 }
99
100 void JSDownloadManager::finalize(JSObjectRef object)
101 {
102         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(object));
103         if (downloadManager) {
104                 JSObjectSetPrivate(object, NULL);
105                 delete downloadManager;
106         }
107 }
108
109 JSValueRef JSDownloadManager::startDownload(JSContextRef context,
110         JSObjectRef object,
111         JSObjectRef thisObject,
112         size_t argumentCount,
113         const JSValueRef arguments[],
114         JSValueRef* exception)
115 {
116         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
117     // Access Check
118     AceSecurityStatus status = DOWNLOAD_CHECK_ACCESS(DOWNLOAD_FUNCTION_API_START);
119     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
120
121     try {
122         // Private Object
123         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
124         if (!downloadManager) {
125             throw TypeMismatchException("Private object is NULL.");
126         }
127
128         ArgumentValidator validator(context, argumentCount, arguments);
129
130         // downloadRequest
131         JSObjectRef downloadRequestObj = validator.toObject(0, JSDownloadRequest::getClassRef());
132         DownloadRequest *downloadRequest = JSDownloadRequest::getPrivateObject(context, downloadRequestObj);
133         if (!downloadRequest) {
134             throw TypeMismatchException("DownloadRequest's private object is NULL.");
135         }
136
137         // downloadCallback
138         DownloadCallback *downloadCallback = NULL;
139         JSObjectRef downloadCallbackObj = validator.toObject(1, true);
140         if (downloadCallbackObj) {
141             downloadCallback = new DownloadCallback(GlobalContextManager::getInstance()->getGlobalContext(context), downloadCallbackObj);
142         }
143
144         // perform
145         long downloadId = downloadManager->start(downloadRequest, downloadCallback);
146                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
147         return JSUtil::toJSValueRef(context, downloadId);
148     } catch (const BasePlatformException &err) {
149         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
150         return JSWebAPIException::throwException(context, exception, err);
151     } catch (...) {
152         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.start().");
153         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
154         return JSWebAPIException::throwException(context, exception, err);
155     }
156 }
157
158 JSValueRef JSDownloadManager::setListener(JSContextRef context,
159         JSObjectRef object,
160         JSObjectRef thisObject,
161         size_t argumentCount,
162         const JSValueRef arguments[],
163         JSValueRef* exception)
164 {
165         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
166     try {
167         // Private Object
168         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
169         if (!downloadManager) {
170             throw TypeMismatchException("Private object is NULL.");
171         }
172
173         ArgumentValidator validator(context, argumentCount, arguments);
174
175         // downloadId
176         long downloadId = validator.toLong(0);
177
178         // downloadCallback
179         JSObjectRef downloadCallbackObj = validator.toObject(1);
180         DownloadCallback *downloadCallback =
181                     new DownloadCallback(GlobalContextManager::getInstance()->getGlobalContext(context), downloadCallbackObj);
182
183         // perform
184         downloadManager->setListener(downloadId, downloadCallback);
185                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
186         return JSValueMakeUndefined(context);
187     } catch (const BasePlatformException &err) {
188         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
189         return JSWebAPIException::throwException(context, exception, err);
190     } catch (...) {
191         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.setListener().");
192         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
193         return JSWebAPIException::throwException(context, exception, err);
194     }
195 }
196
197 JSValueRef JSDownloadManager::cancelDownload(JSContextRef context,
198         JSObjectRef object,
199         JSObjectRef thisObject,
200         size_t argumentCount,
201         const JSValueRef arguments[],
202         JSValueRef* exception)
203 {
204         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
205     try {
206         // Private Object
207         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
208         if (!downloadManager) {
209             throw TypeMismatchException("Private object is NULL.");
210         }
211
212         ArgumentValidator validator(context, argumentCount, arguments);
213
214         // downloadId
215         long downloadId = validator.toLong(0);
216
217         // perform
218         downloadManager->cancel(downloadId);
219                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
220         return JSValueMakeUndefined(context);
221     } catch (const BasePlatformException &err) {
222         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
223         return JSWebAPIException::throwException(context, exception, err);
224     } catch (...) {
225         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.cancel().");
226         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
227         return JSWebAPIException::throwException(context, exception, err);
228     }
229 }
230
231 JSValueRef JSDownloadManager::pauseDownload(JSContextRef context,
232         JSObjectRef object,
233         JSObjectRef thisObject,
234         size_t argumentCount,
235         const JSValueRef arguments[],
236         JSValueRef* exception)
237 {
238         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
239     try {
240         // Private Object
241         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
242         if (!downloadManager) {
243             throw TypeMismatchException("Private object is NULL.");
244         }
245
246         ArgumentValidator validator(context, argumentCount, arguments);
247
248         // downloadId
249         long downloadId = validator.toLong(0);
250
251         // perform
252         downloadManager->pause(downloadId);
253                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
254         return JSValueMakeUndefined(context);
255     } catch (const BasePlatformException &err) {
256         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
257         return JSWebAPIException::throwException(context, exception, err);
258     } catch (...) {
259         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.pause().");
260         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
261         return JSWebAPIException::throwException(context, exception, err);
262     }
263 }
264
265 JSValueRef JSDownloadManager::resumeDownload(JSContextRef context,
266         JSObjectRef object,
267         JSObjectRef thisObject,
268         size_t argumentCount,
269         const JSValueRef arguments[],
270         JSValueRef* exception)
271 {
272         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
273     try {
274         // Private Object
275         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
276         if (!downloadManager) {
277             throw TypeMismatchException("Private object is NULL.");
278         }
279
280         ArgumentValidator validator(context, argumentCount, arguments);
281
282         // downloadId
283         long downloadId = validator.toLong(0);
284
285         // perform
286         downloadManager->resume(downloadId);
287                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
288         return JSValueMakeUndefined(context);
289     } catch (const BasePlatformException &err) {
290         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
291         return JSWebAPIException::throwException(context, exception, err);
292     } catch (...) {
293         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.resume().");
294         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
295         return JSWebAPIException::throwException(context, exception, err);
296     }
297 }
298
299
300 JSValueRef JSDownloadManager::getState(JSContextRef context,
301         JSObjectRef object,
302         JSObjectRef thisObject,
303         size_t argumentCount,
304         const JSValueRef arguments[],
305         JSValueRef* exception)
306 {
307         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
308     try {
309         // Private Object
310         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
311         if (!downloadManager) {
312             throw TypeMismatchException("Private object is NULL.");
313         }
314
315         ArgumentValidator validator(context, argumentCount, arguments);
316
317         // downloadId
318         long downloadId = validator.toLong(0);
319
320         // perform
321         std::string ret = downloadManager->getState(downloadId);
322                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
323         return JSUtil::toJSValueRef(context, ret);
324     } catch (const BasePlatformException &err) {
325         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
326         return JSWebAPIException::throwException(context, exception, err);
327     } catch (...) {
328         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.resume().");
329         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
330         return JSWebAPIException::throwException(context, exception, err);
331     }
332 }
333
334 JSValueRef JSDownloadManager::getDownloadRequest(JSContextRef context,
335         JSObjectRef object,
336         JSObjectRef thisObject,
337         size_t argumentCount,
338         const JSValueRef arguments[],
339         JSValueRef* exception)
340 {
341         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
342     try {
343         // Private Object
344         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
345         if (!downloadManager) {
346             throw TypeMismatchException("Private object is NULL.");
347         }
348
349         ArgumentValidator validator(context, argumentCount, arguments);
350
351         // downloadId
352         long downloadId = validator.toLong(0);
353
354         // perform
355         JSObjectRef obj = JSObjectMake(context, JSDownloadRequest::getClassRef(), NULL);
356         DownloadRequest *priv = downloadManager->getDownloadRequest(downloadId);
357         JSDownloadRequest::setPrivateObject(context, obj, priv);
358                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
359         return obj;
360     } catch (const BasePlatformException &err) {
361         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
362         return JSWebAPIException::throwException(context, exception, err);
363     } catch (...) {
364         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.resume().");
365         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
366         return JSWebAPIException::throwException(context, exception, err);
367     }
368 }
369
370 JSValueRef JSDownloadManager::getMIMEType(JSContextRef context,
371         JSObjectRef object,
372         JSObjectRef thisObject,
373         size_t argumentCount,
374         const JSValueRef arguments[],
375         JSValueRef* exception)
376 {
377         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
378     try {
379         // Private Object
380         DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
381         if (!downloadManager) {
382             throw TypeMismatchException("Private object is NULL.");
383         }
384
385         ArgumentValidator validator(context, argumentCount, arguments);
386
387         // downloadId
388         long downloadId = validator.toLong(0);
389
390         // perform
391         std::string ret = downloadManager->getMIMEType(downloadId);
392                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
393         return JSUtil::toJSValueRef(context, ret);
394     } catch (const BasePlatformException &err) {
395         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
396         return JSWebAPIException::throwException(context, exception, err);
397     } catch (...) {
398         DeviceAPI::Common::UnknownException err("Unknown Error in tizen.download.resume().");
399         LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
400         return JSWebAPIException::throwException(context, exception, err);
401     }
402 }
403
404 } // Download
405 } // DeviceAPI