Update change log and spec for wrt-plugins-tizen_0.4.44
[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             networkType = "ALL";
183             LOGW("networkType is invalid. 'ALL' type is set.");
184         }
185         priv->setNetworkType(networkType);
186     } catch (const BasePlatformException& err) {
187         LOGW("networkType convertion is failed. %s", err.getMessage().c_str());
188     }
189
190     try {
191         priv->setHttpHeader(validator.toStringMap(4, true));
192     } catch (const BasePlatformException& err) {
193         LOGW("httpHeader convertion is failed. %s", err.getMessage().c_str());
194     }
195
196     setPrivateObject(context, obj, priv);
197
198     return obj;
199 }
200
201 DownloadRequest* JSDownloadRequest::getPrivateObject(JSContextRef context, JSObjectRef object)
202 {
203     DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
204     if (!priv) {
205         throw TypeMismatchException("DownloadRequest's private object is NULL.");
206     }
207
208     // url
209     JSValueRef url = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL);
210     priv->setUrl(JSUtil::JSValueToString(context, url));
211
212     // destination
213     JSValueRef destination = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION);
214     priv->setDestination(JSUtil::JSValueToString(context, destination));
215
216     // fileName
217     JSValueRef fileName = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME);
218     priv->setFileName(JSUtil::JSValueToString(context, fileName));
219
220     // networkType
221     JSValueRef networkType = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE);
222     if (JSValueIsNull(context, networkType)) {
223         priv->setNetworkType("ALL");
224     } else {
225         priv->setNetworkType(JSUtil::JSValueToString(context, networkType));
226     }
227
228     // httpHeader
229     JSValueRef httpHeader = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER);
230     if (JSValueIsNull(context, httpHeader)) {
231         priv->setHttpHeader(std::map<std::string, std::string>());
232     } else {
233         priv->setHttpHeader(JSUtil::JSValueToStringMap(context, httpHeader));
234     }
235
236     return priv;
237 }
238
239 void JSDownloadRequest::setPrivateObject(JSContextRef context, JSObjectRef object, DownloadRequest* priv)
240 {
241     if (!priv) {
242         throw TypeMismatchException("DownloadRequest's private object is NULL.");
243     }
244
245     JSObjectSetPrivate(object, static_cast<void*>(priv));
246
247     // url
248     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL,
249             JSUtil::toJSValueRef(context, priv->getUrl()), kJSPropertyAttributeNone);
250
251     // destination
252     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION,
253             JSUtil::toJSValueRef(context, priv->getDestination()), kJSPropertyAttributeNone);
254
255     // fileName
256     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME,
257             JSUtil::toJSValueRef(context, priv->getFileName()), kJSPropertyAttributeNone);
258
259     // networkType
260     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE,
261             JSUtil::toJSValueRef(context, priv->getNetworkType()), kJSPropertyAttributeNone);
262
263     // httpHeader
264     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER,
265             JSUtil::toJSValueRef(context, priv->getHttpHeader()), kJSPropertyAttributeNone);
266 }
267
268 } // Download
269 } // DeviceAPI