Update change log and spec for wrt-plugins-tizen_0.4.43
[platform/framework/web/wrt-plugins-tizen.git] / src / Download / JSDownloadRequest.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 <string>
19
20 #include <JavaScriptCore/JavaScript.h>
21
22 #include <Logger.h>
23 #include <JSUtil.h>
24 #include <JSWebAPIErrorFactory.h>
25 #include <ArgumentValidator.h>
26
27 #include "JSDownloadRequest.h"
28 #include "DownloadRequest.h"
29
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"
35
36 using namespace DeviceAPI::Common;
37
38 namespace DeviceAPI {
39 namespace Download {
40
41 JSClassDefinition JSDownloadRequest::m_classInfo = {
42     0,
43     kJSClassAttributeNone,
44     "DownloadRequest",
45     NULL, //parentClass
46     NULL, //staticValues,
47     NULL, //staticFunctions,
48     initialize,
49     finalize,
50     NULL, //hasProperty,
51     NULL, //getProperty,
52     setProperty, //setProperty,
53     NULL, //deleteProperty,
54     NULL, //getPropertyNames,
55     NULL, //callAsFunction,
56     NULL, //constructor
57     NULL, //hasInstance,
58     NULL, //convertToType,
59 };
60
61
62 JSClassRef JSDownloadRequest::m_jsClassRef = JSClassCreate(JSDownloadRequest::getClassInfo());
63
64 const JSClassDefinition* JSDownloadRequest::getClassInfo()
65 {
66     return &(m_classInfo);
67 }
68
69 JSClassRef JSDownloadRequest::getClassRef()
70 {
71     if (!m_jsClassRef) {
72         m_jsClassRef = JSClassCreate(&m_classInfo);
73     }
74     return m_jsClassRef;
75 }
76
77 void JSDownloadRequest::initialize(JSContextRef context, JSObjectRef object)
78 {
79     if (!JSObjectGetPrivate(object)) {
80         DownloadRequest *priv = new DownloadRequest();
81         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
82             delete priv;
83         }
84     }
85 }
86
87 void JSDownloadRequest::finalize(JSObjectRef object)
88 {
89     DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
90     if (priv) {
91         JSObjectSetPrivate(object, NULL);
92         delete priv;
93     }
94 }
95
96 bool JSDownloadRequest::setProperty(JSContextRef context, JSObjectRef object,
97         JSStringRef propertyName, JSValueRef value,  JSValueRef* exception)
98 {
99     try {
100         // check url
101         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_URL)) {
102             JSUtil::JSValueToString(context, value);
103         }
104
105         // check destination
106         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_DESTINATION)) {
107             JSUtil::JSValueToString(context, value);
108         }
109
110         // check fileName
111         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_DOWNLOAD_REQUEST_FILE_NAME)) {
112             JSUtil::JSValueToString(context, value);
113         }
114
115         // check networkType
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")
120                 {
121                     throw TypeMismatchException("Invalid networkType");
122                 }
123             }
124         }
125
126         // check httpHeader
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");
131                 }
132                 JSUtil::JSValueToObject(context, value);
133             }
134         }
135     } catch (const BasePlatformException &err) {
136        LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
137        return true;
138     }
139
140     return false;
141 }
142
143 JSObjectRef JSDownloadRequest::constructor(JSContextRef context,
144     JSObjectRef constructor,
145     size_t argumentCount,
146     const JSValueRef arguments[],
147     JSValueRef* exception)
148 {
149     ArgumentValidator validator(context, argumentCount, arguments);
150
151     JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
152
153     // constructor
154     JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
155     JSObjectSetProperty(context, obj, ctorName, constructor,
156         kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
157     JSStringRelease(ctorName);
158
159     DownloadRequest *priv = new DownloadRequest();
160
161     try {
162         priv->setUrl(validator.toString(0, true, ""));
163     } catch (const BasePlatformException& err) {
164         LOGW("url convertion is failed. %s", err.getMessage().c_str());
165     }
166
167     try {
168         priv->setDestination(validator.toString(1, true, ""));
169     } catch (const BasePlatformException& err) {
170         LOGW("destination convertion is failed. %s", err.getMessage().c_str());
171     }
172
173     try {
174         priv->setFileName(validator.toString(2, true, ""));
175     } catch (const BasePlatformException& err) {
176         LOGW("fileName convertion is failed. %s", err.getMessage().c_str());
177     }
178
179     try {
180         std::string networkType = validator.toString(3, true, "ALL");
181         if (networkType.compare("CELLULAR") && networkType.compare("WIFI") && networkType.compare("ALL")) {
182             delete priv;
183             TypeMismatchException err("networkType is invalid.");
184             JSObjectRef errObj = JSWebAPIErrorFactory::makeErrorObject(context, err);
185             *exception = errObj;
186             return errObj;
187         }
188         priv->setNetworkType(networkType);
189     } catch (const BasePlatformException& err) {
190         LOGW("networkType convertion is failed. %s", err.getMessage().c_str());
191     }
192
193     try {
194         priv->setHttpHeader(validator.toStringMap(4, true));
195     } catch (const BasePlatformException& err) {
196         LOGW("httpHeader convertion is failed. %s", err.getMessage().c_str());
197     }
198
199     setPrivateObject(context, obj, priv);
200
201     return obj;
202 }
203
204 DownloadRequest* JSDownloadRequest::getPrivateObject(JSContextRef context, JSObjectRef object)
205 {
206     DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
207     if (!priv) {
208         throw TypeMismatchException("DownloadRequest's private object is NULL.");
209     }
210
211     // url
212     JSValueRef url = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL);
213     priv->setUrl(JSUtil::JSValueToString(context, url));
214
215     // destination
216     JSValueRef destination = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION);
217     priv->setDestination(JSUtil::JSValueToString(context, destination));
218
219     // fileName
220     JSValueRef fileName = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME);
221     priv->setFileName(JSUtil::JSValueToString(context, fileName));
222
223     // networkType
224     JSValueRef networkType = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE);
225     if (JSValueIsNull(context, networkType)) {
226         priv->setNetworkType("ALL");
227     } else {
228         priv->setNetworkType(JSUtil::JSValueToString(context, networkType));
229     }
230
231     // httpHeader
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>());
235     } else {
236         priv->setHttpHeader(JSUtil::JSValueToStringMap(context, httpHeader));
237     }
238
239     return priv;
240 }
241
242 void JSDownloadRequest::setPrivateObject(JSContextRef context, JSObjectRef object, DownloadRequest* priv)
243 {
244     if (!priv) {
245         throw TypeMismatchException("DownloadRequest's private object is NULL.");
246     }
247
248     JSObjectSetPrivate(object, static_cast<void*>(priv));
249
250     // url
251     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL,
252             JSUtil::toJSValueRef(context, priv->getUrl()), kJSPropertyAttributeNone);
253
254     // destination
255     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION,
256             JSUtil::toJSValueRef(context, priv->getDestination()), kJSPropertyAttributeNone);
257
258     // fileName
259     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME,
260             JSUtil::toJSValueRef(context, priv->getFileName()), kJSPropertyAttributeNone);
261
262     // networkType
263     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE,
264             JSUtil::toJSValueRef(context, priv->getNetworkType()), kJSPropertyAttributeNone);
265
266     // httpHeader
267     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER,
268             JSUtil::toJSValueRef(context, priv->getHttpHeader()), kJSPropertyAttributeNone);
269 }
270
271 } // Download
272 } // DeviceAPI