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 <JSWebAPIErrorFactory.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,
52 setProperty, //setProperty,
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 bool JSDownloadRequest::setProperty(JSContextRef context, JSObjectRef object,
97 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
101 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_URL)) {
102 JSUtil::JSValueToString(context, value);
106 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_DESTINATION)) {
107 JSUtil::JSValueToString(context, value);
111 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_FILE_NAME)) {
112 JSUtil::JSValueToString(context, value);
116 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE)) {
117 if (!JSValueIsNull(context, value)) {
118 std::string networkType = JSUtil::JSValueToString(context, value);
119 if (networkType != "CELLULAR" && networkType != "WIFI" && networkType != "ALL")
121 throw TypeMismatchException("Invalid networkType");
127 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER)) {
128 if (!JSValueIsNull(context, value)) {
129 if (!JSValueIsObject(context, value)) {
130 throw TypeMismatchException("Value is not Object");
132 JSUtil::JSValueToObject(context, value);
135 } catch (const BasePlatformException &err) {
136 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
143 JSObjectRef JSDownloadRequest::constructor(JSContextRef context,
144 JSObjectRef constructor,
145 size_t argumentCount,
146 const JSValueRef arguments[],
147 JSValueRef* exception)
149 ArgumentValidator validator(context, argumentCount, arguments);
151 JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
154 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
155 JSObjectSetProperty(context, obj, ctorName, constructor,
156 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
157 JSStringRelease(ctorName);
159 DownloadRequest *priv = new DownloadRequest();
162 priv->setUrl(validator.toString(0, true, ""));
163 } catch (const BasePlatformException& err) {
164 LOGW("url convertion is failed. %s", err.getMessage().c_str());
168 priv->setDestination(validator.toString(1, true, ""));
169 } catch (const BasePlatformException& err) {
170 LOGW("destination convertion is failed. %s", err.getMessage().c_str());
174 priv->setFileName(validator.toString(2, true, ""));
175 } catch (const BasePlatformException& err) {
176 LOGW("fileName convertion is failed. %s", err.getMessage().c_str());
180 std::string networkType = validator.toString(3, true, "ALL");
181 if (networkType.compare("CELLULAR") && networkType.compare("WIFI") && networkType.compare("ALL")) {
183 TypeMismatchException err("networkType is invalid.");
184 JSObjectRef errObj = JSWebAPIErrorFactory::makeErrorObject(context, err);
188 priv->setNetworkType(networkType);
189 } catch (const BasePlatformException& err) {
190 LOGW("networkType convertion is failed. %s", err.getMessage().c_str());
194 priv->setHttpHeader(validator.toStringMap(4, true));
195 } catch (const BasePlatformException& err) {
196 LOGW("httpHeader convertion is failed. %s", err.getMessage().c_str());
199 setPrivateObject(context, obj, priv);
204 DownloadRequest* JSDownloadRequest::getPrivateObject(JSContextRef context, JSObjectRef object)
206 DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
208 throw TypeMismatchException("DownloadRequest's private object is NULL.");
212 JSValueRef url = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL);
213 priv->setUrl(JSUtil::JSValueToString(context, url));
216 JSValueRef destination = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION);
217 priv->setDestination(JSUtil::JSValueToString(context, destination));
220 JSValueRef fileName = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME);
221 priv->setFileName(JSUtil::JSValueToString(context, fileName));
224 JSValueRef networkType = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE);
225 if (JSValueIsNull(context, networkType)) {
226 priv->setNetworkType("ALL");
228 priv->setNetworkType(JSUtil::JSValueToString(context, networkType));
232 JSValueRef httpHeader = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER);
233 if (JSValueIsNull(context, httpHeader)) {
234 priv->setHttpHeader(std::map<std::string, std::string>());
236 priv->setHttpHeader(JSUtil::JSValueToStringMap(context, httpHeader));
242 void JSDownloadRequest::setPrivateObject(JSContextRef context, JSObjectRef object, DownloadRequest* priv)
245 throw TypeMismatchException("DownloadRequest's private object is NULL.");
248 JSObjectSetPrivate(object, static_cast<void*>(priv));
251 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL,
252 JSUtil::toJSValueRef(context, priv->getUrl()), kJSPropertyAttributeNone);
255 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION,
256 JSUtil::toJSValueRef(context, priv->getDestination()), kJSPropertyAttributeNone);
259 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME,
260 JSUtil::toJSValueRef(context, priv->getFileName()), kJSPropertyAttributeNone);
263 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE,
264 JSUtil::toJSValueRef(context, priv->getNetworkType()), kJSPropertyAttributeNone);
267 JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER,
268 JSUtil::toJSValueRef(context, priv->getHttpHeader()), kJSPropertyAttributeNone);