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 <JSWebAPIErrorFactory.h>
26 #include <ArgumentValidator.h>
28 #include "JSDownloadRequest.h"
29 #include "DownloadRequest.h"
31 #define TIZEN_DOWNLOAD_REQUEST_URL "url"
32 #define TIZEN_DOWNLOAD_REQUEST_DESTINATION "destination"
33 #define TIZEN_DOWNLOAD_REQUEST_FILE_NAME "fileName"
34 #define TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE "networkType"
35 #define TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER "httpHeader"
37 using namespace DeviceAPI::Common;
42 JSClassDefinition JSDownloadRequest::m_classInfo = {
44 kJSClassAttributeNone,
48 NULL, //staticFunctions,
53 setProperty, //setProperty,
54 NULL, //deleteProperty,
55 NULL, //getPropertyNames,
56 NULL, //callAsFunction,
59 NULL, //convertToType,
63 JSClassRef JSDownloadRequest::m_jsClassRef = JSClassCreate(JSDownloadRequest::getClassInfo());
65 const JSClassDefinition* JSDownloadRequest::getClassInfo()
67 return &(m_classInfo);
70 JSClassRef JSDownloadRequest::getClassRef()
73 m_jsClassRef = JSClassCreate(&m_classInfo);
78 void JSDownloadRequest::initialize(JSContextRef context, JSObjectRef object)
80 if (!JSObjectGetPrivate(object)) {
81 DownloadRequest *priv = new DownloadRequest();
82 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
88 void JSDownloadRequest::finalize(JSObjectRef object)
90 DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
92 JSObjectSetPrivate(object, NULL);
97 bool JSDownloadRequest::setProperty(JSContextRef context, JSObjectRef object,
98 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
100 if (JSStringIsEqualToUTF8CString(propertyName, "networkType")) {
101 if (JSUtil::JSValueToString(context, value).compare("CELLULAR") &&
102 JSUtil::JSValueToString(context, value).compare("WIFI") &&
103 JSUtil::JSValueToString(context, value).compare("ALL"))
111 JSObjectRef JSDownloadRequest::constructor(JSContextRef context,
112 JSObjectRef constructor,
113 size_t argumentCount,
114 const JSValueRef arguments[],
115 JSValueRef* exception)
117 ArgumentValidator validator(context, argumentCount, arguments);
119 JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
122 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
123 JSObjectSetProperty(context, obj, ctorName, constructor,
124 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
125 JSStringRelease(ctorName);
127 DownloadRequest *priv = new DownloadRequest();
130 priv->setUrl(validator.toString(0, true, ""));
131 } catch (const BasePlatformException& err) {
132 LoggerW("url convertion is failed. %s", err.getMessage().c_str());
136 priv->setDestination(validator.toString(1, true, ""));
137 } catch (const BasePlatformException& err) {
138 LoggerW("destination convertion is failed. %s", err.getMessage().c_str());
142 priv->setFileName(validator.toString(2, true, ""));
143 } catch (const BasePlatformException& err) {
144 LoggerW("fileName convertion is failed. %s", err.getMessage().c_str());
148 std::string networkType = validator.toString(3, true, "ALL");
149 if (networkType.compare("CELLULAR") && networkType.compare("WIFI") && networkType.compare("ALL")) {
151 TypeMismatchException err("networkType is invalid.");
152 JSObjectRef errObj = JSWebAPIErrorFactory::makeErrorObject(context, err);
156 priv->setNetworkType(networkType);
157 } catch (const BasePlatformException& err) {
158 LoggerW("networkType convertion is failed. %s", err.getMessage().c_str());
162 priv->setHttpHeader(validator.toStringMap(4, true));
163 } catch (const BasePlatformException& err) {
164 LoggerW("httpHeader convertion is failed. %s", err.getMessage().c_str());
167 setPrivateObject(context, obj, priv);
172 DownloadRequest* JSDownloadRequest::getPrivateObject(JSContextRef context, JSObjectRef object)
174 DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
176 throw TypeMismatchException("DownloadRequest's private object is NULL.");
180 JSValueRef url = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL);
181 priv->setUrl(JSUtil::JSValueToString(context, url));
184 JSValueRef destination = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION);
185 priv->setDestination(JSUtil::JSValueToString(context, destination));
188 JSValueRef fileName = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME);
189 priv->setFileName(JSUtil::JSValueToString(context, fileName));
192 JSValueRef networkType = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE);
193 priv->setNetworkType(JSUtil::JSValueToString(context, networkType));
196 JSValueRef httpHeader = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER);
197 priv->setHttpHeader(JSUtil::JSValueToStringMap(context, httpHeader));
202 void JSDownloadRequest::setPrivateObject(JSContextRef context, JSObjectRef object, DownloadRequest* priv)
205 throw TypeMismatchException("DownloadRequest's private object is NULL.");
208 JSObjectSetPrivate(object, static_cast<void*>(priv));
211 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL,
212 JSUtil::toJSValueRef(context, priv->getUrl()), kJSPropertyAttributeNone);
215 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION,
216 JSUtil::toJSValueRef(context, priv->getDestination()), kJSPropertyAttributeNone);
219 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME,
220 JSUtil::toJSValueRef(context, priv->getFileName()), kJSPropertyAttributeNone);
223 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE,
224 JSUtil::toJSValueRef(context, priv->getNetworkType()), kJSPropertyAttributeNone);
227 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER,
228 JSUtil::toJSValueRef(context, priv->getHttpHeader()), kJSPropertyAttributeNone);