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.
20 #include <JavaScriptCore/JavaScript.h>
24 #include <JSWebAPIError.h>
25 #include <ArgumentValidator.h>
27 #include "JSDownloadRequest.h"
28 #include "DownloadRequest.h"
30 #define TIZEN_DOWNLOAD_REQUEST_URL "url"
31 #define TIZEN_DOWNLOAD_REQUEST_DESTINATION "destination"
32 #define TIZEN_DOWNLOAD_REQUEST_FILE_NAME "fileName"
33 #define TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE "networkType"
34 #define TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER "httpHeader"
36 using namespace DeviceAPI::Common;
41 JSClassDefinition JSDownloadRequest::m_classInfo = {
43 kJSClassAttributeNone,
47 NULL, //staticFunctions,
53 NULL, //deleteProperty,
54 NULL, //getPropertyNames,
55 NULL, //callAsFunction,
58 NULL, //convertToType,
62 JSClassRef JSDownloadRequest::m_jsClassRef = JSClassCreate(JSDownloadRequest::getClassInfo());
64 const JSClassDefinition* JSDownloadRequest::getClassInfo()
66 return &(m_classInfo);
69 JSClassRef JSDownloadRequest::getClassRef()
72 m_jsClassRef = JSClassCreate(&m_classInfo);
77 void JSDownloadRequest::initialize(JSContextRef context, JSObjectRef object)
79 if (!JSObjectGetPrivate(object)) {
80 DownloadRequest *priv = new DownloadRequest();
81 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
87 void JSDownloadRequest::finalize(JSObjectRef object)
89 DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
91 JSObjectSetPrivate(object, NULL);
96 JSObjectRef JSDownloadRequest::constructor(JSContextRef context,
97 JSObjectRef constructor,
99 const JSValueRef arguments[],
100 JSValueRef* exception)
102 ArgumentValidator validator(context, argumentCount, arguments);
104 JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
107 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
108 JSObjectSetProperty(context, obj, ctorName, constructor,
109 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
110 JSStringRelease(ctorName);
112 DownloadRequest *priv = new DownloadRequest();
115 priv->setUrl(validator.toString(0, true, ""));
116 } catch (const BasePlatformException& err) {
117 LogWarning("url convertion is failed. %s", err.getMessage().c_str());
121 priv->setDestination(validator.toString(1, true, ""));
122 } catch (const BasePlatformException& err) {
123 LogWarning("url destination is failed. %s", err.getMessage().c_str());
127 priv->setFileName(validator.toString(2, true, ""));
128 } catch (const BasePlatformException& err) {
129 LogWarning("url fileName is failed. %s", err.getMessage().c_str());
133 priv->setNetworkType(validator.toString(3, true, ""));
134 } catch (const BasePlatformException& err) {
135 LogWarning("url networkType is failed. %s", err.getMessage().c_str());
139 priv->setHttpHeader(validator.toStringMap(4, true));
140 } catch (const BasePlatformException& err) {
141 LogWarning("url networkType is failed. %s", err.getMessage().c_str());
144 setPrivateObject(context, obj, priv);
149 DownloadRequest* JSDownloadRequest::getPrivateObject(JSContextRef context, JSObjectRef object)
151 DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
153 throw TypeMismatchException("DownloadRequest's private object is NULL.");
157 JSValueRef url = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL);
158 priv->setUrl(JSUtil::JSValueToString(context, url));
161 JSValueRef destination = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION);
162 priv->setDestination(JSUtil::JSValueToString(context, destination));
165 JSValueRef fileName = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME);
166 priv->setFileName(JSUtil::JSValueToString(context, fileName));
169 JSValueRef networkType = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE);
170 priv->setNetworkType(JSUtil::JSValueToString(context, networkType));
173 JSValueRef httpHeader = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER);
174 priv->setHttpHeader(JSUtil::JSValueToStringMap(context, httpHeader));
179 void JSDownloadRequest::setPrivateObject(JSContextRef context, JSObjectRef object, DownloadRequest* priv)
182 JSObjectSetPrivate(object, static_cast<void*>(priv));
186 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL,
187 JSUtil::toJSValueRef(context, priv->getUrl()), kJSPropertyAttributeNone);
190 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION,
191 JSUtil::toJSValueRef(context, priv->getDestination()), kJSPropertyAttributeNone);
194 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME,
195 JSUtil::toJSValueRef(context, priv->getFileName()), kJSPropertyAttributeNone);
198 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE,
199 JSUtil::toJSValueRef(context, priv->getNetworkType()), kJSPropertyAttributeNone);
202 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER,
203 JSUtil::toJSValueRef(context, priv->getHttpHeader()), kJSPropertyAttributeNone);