wrt-plugins-tizen_0.4.23
[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 <JSWebAPIError.h>
25 #include <JSWebAPIErrorFactory.h>
26 #include <ArgumentValidator.h>
27
28 #include "JSDownloadRequest.h"
29 #include "DownloadRequest.h"
30
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"
36
37 using namespace DeviceAPI::Common;
38
39 namespace DeviceAPI {
40 namespace Download {
41
42 JSClassDefinition JSDownloadRequest::m_classInfo = {
43     0,
44     kJSClassAttributeNone,
45     "DownloadRequest",
46     NULL, //parentClass
47     NULL, //staticValues,
48     NULL, //staticFunctions,
49     initialize,
50     finalize,
51     NULL, //hasProperty,
52     NULL, //getProperty,
53     setProperty, //setProperty,
54     NULL, //deleteProperty,
55     NULL, //getPropertyNames,
56     NULL, //callAsFunction,
57     NULL, //constructor
58     NULL, //hasInstance,
59     NULL, //convertToType,
60 };
61
62
63 JSClassRef JSDownloadRequest::m_jsClassRef = JSClassCreate(JSDownloadRequest::getClassInfo());
64
65 const JSClassDefinition* JSDownloadRequest::getClassInfo()
66 {
67     return &(m_classInfo);
68 }
69
70 JSClassRef JSDownloadRequest::getClassRef()
71 {
72     if (!m_jsClassRef) {
73         m_jsClassRef = JSClassCreate(&m_classInfo);
74     }
75     return m_jsClassRef;
76 }
77
78 void JSDownloadRequest::initialize(JSContextRef context, JSObjectRef object)
79 {
80     if (!JSObjectGetPrivate(object)) {
81         DownloadRequest *priv = new DownloadRequest();
82         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
83             delete priv;
84         }
85     }
86 }
87
88 void JSDownloadRequest::finalize(JSObjectRef object)
89 {
90     DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
91     if (priv) {
92         JSObjectSetPrivate(object, NULL);
93         delete priv;
94     }
95 }
96
97 bool JSDownloadRequest::setProperty(JSContextRef context, JSObjectRef object,
98         JSStringRef propertyName, JSValueRef value,  JSValueRef* exception)
99 {
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"))
104         {
105             return true;
106         }
107     }
108     return false;
109 }
110
111 JSObjectRef JSDownloadRequest::constructor(JSContextRef context,
112     JSObjectRef constructor,
113     size_t argumentCount,
114     const JSValueRef arguments[],
115     JSValueRef* exception)
116 {
117     ArgumentValidator validator(context, argumentCount, arguments);
118
119     JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
120
121     // constructor
122     JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
123     JSObjectSetProperty(context, obj, ctorName, constructor,
124         kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
125     JSStringRelease(ctorName);
126
127     DownloadRequest *priv = new DownloadRequest();
128
129     try {
130         priv->setUrl(validator.toString(0, true, ""));
131     } catch (const BasePlatformException& err) {
132         LoggerW("url convertion is failed. %s", err.getMessage().c_str());
133     }
134
135     try {
136         priv->setDestination(validator.toString(1, true, ""));
137     } catch (const BasePlatformException& err) {
138         LoggerW("destination convertion is failed. %s", err.getMessage().c_str());
139     }
140
141     try {
142         priv->setFileName(validator.toString(2, true, ""));
143     } catch (const BasePlatformException& err) {
144         LoggerW("fileName convertion is failed. %s", err.getMessage().c_str());
145     }
146
147     try {
148         std::string networkType = validator.toString(3, true, "ALL");
149         if (networkType.compare("CELLULAR") && networkType.compare("WIFI") && networkType.compare("ALL")) {
150             delete priv;
151             TypeMismatchException err("networkType is invalid.");
152             JSObjectRef errObj = JSWebAPIErrorFactory::makeErrorObject(context, err);
153             *exception = errObj;
154             return errObj;
155         }
156         priv->setNetworkType(networkType);
157     } catch (const BasePlatformException& err) {
158         LoggerW("networkType convertion is failed. %s", err.getMessage().c_str());
159     }
160
161     try {
162         priv->setHttpHeader(validator.toStringMap(4, true));
163     } catch (const BasePlatformException& err) {
164         LoggerW("httpHeader convertion is failed. %s", err.getMessage().c_str());
165     }
166
167     setPrivateObject(context, obj, priv);
168
169     return obj;
170 }
171
172 DownloadRequest* JSDownloadRequest::getPrivateObject(JSContextRef context, JSObjectRef object)
173 {
174     DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
175     if (!priv) {
176         throw TypeMismatchException("DownloadRequest's private object is NULL.");
177     }
178
179     // url
180     JSValueRef url = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL);
181     priv->setUrl(JSUtil::JSValueToString(context, url));
182
183     // destination
184     JSValueRef destination = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION);
185     priv->setDestination(JSUtil::JSValueToString(context, destination));
186
187     // fileName
188     JSValueRef fileName = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME);
189     priv->setFileName(JSUtil::JSValueToString(context, fileName));
190
191     // networkType
192     JSValueRef networkType = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE);
193     priv->setNetworkType(JSUtil::JSValueToString(context, networkType));
194
195     // httpHeader
196     JSValueRef httpHeader = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER);
197     priv->setHttpHeader(JSUtil::JSValueToStringMap(context, httpHeader));
198
199     return priv;
200 }
201
202 void JSDownloadRequest::setPrivateObject(JSContextRef context, JSObjectRef object, DownloadRequest* priv)
203 {
204     if (!priv) {
205         throw TypeMismatchException("DownloadRequest's private object is NULL.");
206     }
207
208     JSObjectSetPrivate(object, static_cast<void*>(priv));
209
210     // url
211     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL,
212             JSUtil::toJSValueRef(context, priv->getUrl()), kJSPropertyAttributeNone);
213
214     // destination
215     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION,
216             JSUtil::toJSValueRef(context, priv->getDestination()), kJSPropertyAttributeNone);
217
218     // fileName
219     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME,
220             JSUtil::toJSValueRef(context, priv->getFileName()), kJSPropertyAttributeNone);
221
222     // networkType
223     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE,
224             JSUtil::toJSValueRef(context, priv->getNetworkType()), kJSPropertyAttributeNone);
225
226     // httpHeader
227     JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER,
228             JSUtil::toJSValueRef(context, priv->getHttpHeader()), kJSPropertyAttributeNone);
229 }
230
231 } // Download
232 } // DeviceAPI