2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <SecurityExceptions.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
25 #include "plugin_config.h"
27 #include "JSDownloadManager.h"
28 #include "JSDownloadRequest.h"
29 #include "DownloadManager.h"
30 #include "DownloadRequest.h"
31 #include "DownloadCallback.h"
33 #include <TimeTracer.h>
36 using namespace WrtDeviceApis::Commons;
37 using namespace DeviceAPI::Common;
42 JSClassDefinition JSDownloadManager::m_classInfo = {
44 kJSClassAttributeNone,
54 NULL, //DeleteProperty,
55 NULL, //GetPropertyNames,
56 NULL, //CallAsFunction,
57 NULL, //CallAsConstructor,
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 },
75 JSClassRef JSDownloadManager::m_jsClassRef = JSClassCreate(JSDownloadManager::getClassInfo());
77 const JSClassRef JSDownloadManager::getClassRef()
80 m_jsClassRef = JSClassCreate(&m_classInfo);
85 const JSClassDefinition* JSDownloadManager::getClassInfo()
90 void JSDownloadManager::initialize(JSContextRef context, JSObjectRef object)
92 if (!JSObjectGetPrivate(object)) {
93 DownloadManager *downloadManager = new DownloadManager();
94 if (!JSObjectSetPrivate(object, static_cast<void*>(downloadManager))) {
95 delete downloadManager;
100 void JSDownloadManager::finalize(JSObjectRef object)
102 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(object));
103 if (downloadManager) {
104 JSObjectSetPrivate(object, NULL);
105 delete downloadManager;
109 JSValueRef JSDownloadManager::startDownload(JSContextRef context,
111 JSObjectRef thisObject,
112 size_t argumentCount,
113 const JSValueRef arguments[],
114 JSValueRef* exception)
116 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
118 AceSecurityStatus status = DOWNLOAD_CHECK_ACCESS(DOWNLOAD_FUNCTION_API_START);
119 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
123 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
124 if (!downloadManager) {
125 throw TypeMismatchException("Private object is NULL.");
128 ArgumentValidator validator(context, argumentCount, arguments);
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.");
138 DownloadCallback *downloadCallback = NULL;
139 JSObjectRef downloadCallbackObj = validator.toObject(1, true);
140 if (downloadCallbackObj) {
141 downloadCallback = new DownloadCallback(GlobalContextManager::getInstance()->getGlobalContext(context), downloadCallbackObj);
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);
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);
158 JSValueRef JSDownloadManager::setListener(JSContextRef context,
160 JSObjectRef thisObject,
161 size_t argumentCount,
162 const JSValueRef arguments[],
163 JSValueRef* exception)
165 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
168 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
169 if (!downloadManager) {
170 throw TypeMismatchException("Private object is NULL.");
173 ArgumentValidator validator(context, argumentCount, arguments);
176 long downloadId = validator.toLong(0);
179 JSObjectRef downloadCallbackObj = validator.toObject(1);
180 DownloadCallback *downloadCallback =
181 new DownloadCallback(GlobalContextManager::getInstance()->getGlobalContext(context), downloadCallbackObj);
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);
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);
197 JSValueRef JSDownloadManager::cancelDownload(JSContextRef context,
199 JSObjectRef thisObject,
200 size_t argumentCount,
201 const JSValueRef arguments[],
202 JSValueRef* exception)
204 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
207 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
208 if (!downloadManager) {
209 throw TypeMismatchException("Private object is NULL.");
212 ArgumentValidator validator(context, argumentCount, arguments);
215 long downloadId = validator.toLong(0);
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);
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);
231 JSValueRef JSDownloadManager::pauseDownload(JSContextRef context,
233 JSObjectRef thisObject,
234 size_t argumentCount,
235 const JSValueRef arguments[],
236 JSValueRef* exception)
238 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
241 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
242 if (!downloadManager) {
243 throw TypeMismatchException("Private object is NULL.");
246 ArgumentValidator validator(context, argumentCount, arguments);
249 long downloadId = validator.toLong(0);
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);
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);
265 JSValueRef JSDownloadManager::resumeDownload(JSContextRef context,
267 JSObjectRef thisObject,
268 size_t argumentCount,
269 const JSValueRef arguments[],
270 JSValueRef* exception)
272 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
275 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
276 if (!downloadManager) {
277 throw TypeMismatchException("Private object is NULL.");
280 ArgumentValidator validator(context, argumentCount, arguments);
283 long downloadId = validator.toLong(0);
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);
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);
300 JSValueRef JSDownloadManager::getState(JSContextRef context,
302 JSObjectRef thisObject,
303 size_t argumentCount,
304 const JSValueRef arguments[],
305 JSValueRef* exception)
307 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
310 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
311 if (!downloadManager) {
312 throw TypeMismatchException("Private object is NULL.");
315 ArgumentValidator validator(context, argumentCount, arguments);
318 long downloadId = validator.toLong(0);
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);
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);
334 JSValueRef JSDownloadManager::getDownloadRequest(JSContextRef context,
336 JSObjectRef thisObject,
337 size_t argumentCount,
338 const JSValueRef arguments[],
339 JSValueRef* exception)
341 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
344 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
345 if (!downloadManager) {
346 throw TypeMismatchException("Private object is NULL.");
349 ArgumentValidator validator(context, argumentCount, arguments);
352 long downloadId = validator.toLong(0);
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);
360 } catch (const BasePlatformException &err) {
361 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
362 return JSWebAPIException::throwException(context, exception, err);
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);
370 JSValueRef JSDownloadManager::getMIMEType(JSContextRef context,
372 JSObjectRef thisObject,
373 size_t argumentCount,
374 const JSValueRef arguments[],
375 JSValueRef* exception)
377 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
380 DownloadManager *downloadManager = static_cast<DownloadManager*>(JSObjectGetPrivate(thisObject));
381 if (!downloadManager) {
382 throw TypeMismatchException("Private object is NULL.");
385 ArgumentValidator validator(context, argumentCount, arguments);
388 long downloadId = validator.toLong(0);
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);
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);