7cbfd60eb27ad96ef726b4fd6d0e6aaa3867b4f5
[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 <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     NULL, //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 JSObjectRef JSDownloadRequest::constructor(JSContextRef context,
97     JSObjectRef constructor,
98     size_t argumentCount,
99     const JSValueRef arguments[],
100     JSValueRef* exception)
101 {
102         ArgumentValidator validator(context, argumentCount, arguments);
103
104         JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
105
106         // constructor
107     JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
108     JSObjectSetProperty(context, obj, ctorName, constructor,
109         kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
110     JSStringRelease(ctorName);
111
112         DownloadRequest *priv = new DownloadRequest();
113
114         try {
115                 priv->setUrl(validator.toString(0, true, ""));
116         } catch (const BasePlatformException& err) {
117                 LogWarning("url convertion is failed. %s", err.getMessage().c_str());
118         }
119
120         try {
121                 priv->setDestination(validator.toString(1, true, ""));
122         } catch (const BasePlatformException& err) {
123                 LogWarning("url destination is failed. %s", err.getMessage().c_str());
124         }
125
126         try {
127                 priv->setFileName(validator.toString(2, true, ""));
128         } catch (const BasePlatformException& err) {
129                 LogWarning("url fileName is failed. %s", err.getMessage().c_str());
130         }
131
132         try {
133                 priv->setNetworkType(validator.toString(3, true, ""));
134         } catch (const BasePlatformException& err) {
135                 LogWarning("url networkType is failed. %s", err.getMessage().c_str());
136         }
137
138         try {
139                 priv->setHttpHeader(validator.toStringMap(4, true));
140         } catch (const BasePlatformException& err) {
141                 LogWarning("url networkType is failed. %s", err.getMessage().c_str());
142         }
143
144         setPrivateObject(context, obj, priv);
145
146     return obj;
147 }
148
149 DownloadRequest* JSDownloadRequest::getPrivateObject(JSContextRef context, JSObjectRef object)
150 {
151         DownloadRequest *priv = static_cast<DownloadRequest*>(JSObjectGetPrivate(object));
152     if (!priv) {
153         throw TypeMismatchException("DownloadRequest's private object is NULL.");
154     }
155
156     // url
157     JSValueRef url = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL);
158     priv->setUrl(JSUtil::JSValueToString(context, url));
159
160     // destination
161     JSValueRef destination = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION);
162     priv->setDestination(JSUtil::JSValueToString(context, destination));
163
164     // fileName
165     JSValueRef fileName = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME);
166     priv->setFileName(JSUtil::JSValueToString(context, fileName));
167
168     // networkType
169     JSValueRef networkType = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE);
170     priv->setNetworkType(JSUtil::JSValueToString(context, networkType));
171
172     // httpHeader
173     JSValueRef httpHeader = JSUtil::getProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER);
174     priv->setHttpHeader(JSUtil::JSValueToStringMap(context, httpHeader));
175
176     return priv;
177 }
178
179 void JSDownloadRequest::setPrivateObject(JSContextRef context, JSObjectRef object, DownloadRequest* priv)
180 {
181         if (priv) {
182                 JSObjectSetPrivate(object, static_cast<void*>(priv));
183         }
184
185         // url
186         JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_URL,
187                         JSUtil::toJSValueRef(context, priv->getUrl()), kJSPropertyAttributeNone);
188
189         // destination
190         JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_DESTINATION,
191                         JSUtil::toJSValueRef(context, priv->getDestination()), kJSPropertyAttributeNone);
192
193         // fileName
194         JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_FILE_NAME,
195                         JSUtil::toJSValueRef(context, priv->getFileName()), kJSPropertyAttributeNone);
196
197         // networkType
198         JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_NETWORK_TYPE,
199                         JSUtil::toJSValueRef(context, priv->getNetworkType()), kJSPropertyAttributeNone);
200
201         // httpHeader
202         JSUtil::setProperty(context, object, TIZEN_DOWNLOAD_REQUEST_HTTP_HEADER,
203                         JSUtil::toJSValueRef(context, priv->getHttpHeader()), kJSPropertyAttributeNone);
204 }
205
206 } // Download
207 } // DeviceAPI