| OLD | NEW |
| (Empty) |
| 1 // Copyright 2011, Google Inc. | |
| 2 // All rights reserved. | |
| 3 // | |
| 4 // Redistribution and use in source and binary forms, with or without | |
| 5 // modification, are permitted provided that the following conditions are | |
| 6 // met: | |
| 7 // | |
| 8 // * Redistributions of source code must retain the above copyright | |
| 9 // notice, this list of conditions and the following disclaimer. | |
| 10 // * Redistributions in binary form must reproduce the above | |
| 11 // copyright notice, this list of conditions and the following disclaimer | |
| 12 // in the documentation and/or other materials provided with the | |
| 13 // distribution. | |
| 14 // * Neither the name of Google Inc. nor the names of its | |
| 15 // contributors may be used to endorse or promote products derived from | |
| 16 // this software without specific prior written permission. | |
| 17 // | |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | |
| 30 #include "config.h" | |
| 31 #include "bindings/core/dart/DartXMLHttpRequest.h" | |
| 32 | |
| 33 #include "bindings/core/dart/DartBlob.h" | |
| 34 #include "bindings/core/dart/DartDOMWrapper.h" | |
| 35 #include "bindings/core/dart/DartDocument.h" | |
| 36 #include "bindings/core/dart/DartFormData.h" | |
| 37 #include "bindings/core/dart/DartStream.h" | |
| 38 #include "bindings/core/dart/DartUtilities.h" | |
| 39 #include "bindings/core/dart/V8Converter.h" | |
| 40 #include "core/dom/ExecutionContext.h" | |
| 41 #include "core/xml/XMLHttpRequest.h" | |
| 42 | |
| 43 namespace blink { | |
| 44 | |
| 45 namespace DartXMLHttpRequestInternal { | |
| 46 | |
| 47 void constructorCallback(Dart_NativeArguments args) | |
| 48 { | |
| 49 Dart_Handle exception = 0; | |
| 50 { | |
| 51 ExecutionContext* context = DartUtilities::scriptExecutionContext(); | |
| 52 if (!context) { | |
| 53 exception = Dart_NewStringFromCString("XMLHttpRequest constructor's
associated context is not available"); | |
| 54 goto fail; | |
| 55 } | |
| 56 | |
| 57 RefPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context); | |
| 58 DartDOMWrapper::returnToDart<DartXMLHttpRequest>(args, xmlHttpRequest); | |
| 59 return; | |
| 60 } | |
| 61 | |
| 62 fail: | |
| 63 Dart_ThrowException(exception); | |
| 64 ASSERT_NOT_REACHED(); | |
| 65 } | |
| 66 | |
| 67 void openCallback(Dart_NativeArguments args) | |
| 68 { | |
| 69 Dart_Handle exception = 0; | |
| 70 { | |
| 71 XMLHttpRequest* receiver = DartDOMWrapper::receiver<XMLHttpRequest>(args
); | |
| 72 DartStringAdapter method = DartUtilities::dartToString(args, 1, exceptio
n); | |
| 73 if (exception) | |
| 74 goto fail; | |
| 75 DartStringAdapter url = DartUtilities::dartToString(args, 2, exception); | |
| 76 if (exception) | |
| 77 goto fail; | |
| 78 | |
| 79 ExecutionContext* context = DartUtilities::scriptExecutionContext(); | |
| 80 if (!context) | |
| 81 return; | |
| 82 KURL fullURL = context->completeURL(url); | |
| 83 | |
| 84 DartExceptionState es; | |
| 85 Dart_Handle arg3 = Dart_GetNativeArgument(args, 3); | |
| 86 if (!Dart_IsNull(arg3)) { | |
| 87 bool async = DartUtilities::dartToBool(arg3, exception); | |
| 88 if (exception) | |
| 89 goto fail; | |
| 90 | |
| 91 Dart_Handle arg4 = Dart_GetNativeArgument(args, 4); | |
| 92 if (!Dart_IsNull(arg4)) { | |
| 93 DartStringAdapter user = DartUtilities::dartToString(arg4, excep
tion); | |
| 94 if (exception) | |
| 95 goto fail; | |
| 96 | |
| 97 Dart_Handle arg5 = Dart_GetNativeArgument(args, 5); | |
| 98 if (!Dart_IsNull(arg5)) { | |
| 99 DartStringAdapter passwd = DartUtilities::dartToString(arg5,
exception); | |
| 100 if (exception) | |
| 101 goto fail; | |
| 102 | |
| 103 receiver->open(method, fullURL, async, user, passwd, es); | |
| 104 } else { | |
| 105 receiver->open(method, fullURL, async, user, es); | |
| 106 } | |
| 107 } else { | |
| 108 receiver->open(method, fullURL, async, es); | |
| 109 } | |
| 110 } else { | |
| 111 receiver->open(method, fullURL, es); | |
| 112 } | |
| 113 | |
| 114 if (es.hadException()) { | |
| 115 exception = es.toDart(args); | |
| 116 goto fail; | |
| 117 } | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 fail: | |
| 122 Dart_ThrowException(exception); | |
| 123 ASSERT_NOT_REACHED(); | |
| 124 } | |
| 125 | |
| 126 // FIXMEDART: we shouldn't have to implement these methods like this. | |
| 127 // Fix the binding script. | |
| 128 void sendCallback_1(Dart_NativeArguments args) { return sendCallback(args); } | |
| 129 void sendCallback_2(Dart_NativeArguments args) { return sendCallback(args); } | |
| 130 void sendCallback_3(Dart_NativeArguments args) { return sendCallback(args); } | |
| 131 void sendCallback_4(Dart_NativeArguments args) { return sendCallback(args); } | |
| 132 void sendCallback_5(Dart_NativeArguments args) { return sendCallback(args); } | |
| 133 void sendCallback_6(Dart_NativeArguments args) { return sendCallback(args); } | |
| 134 void sendCallback_7(Dart_NativeArguments args) { return sendCallback(args); } | |
| 135 | |
| 136 void sendCallback(Dart_NativeArguments args) | |
| 137 { | |
| 138 Dart_Handle exception = 0; | |
| 139 { | |
| 140 XMLHttpRequest* receiver = DartDOMWrapper::receiver<XMLHttpRequest>(args
); | |
| 141 | |
| 142 DartExceptionState es; | |
| 143 Dart_Handle arg1 = Dart_GetNativeArgument(args, 1); | |
| 144 if (Dart_IsNull(arg1)) { | |
| 145 receiver->send(es); | |
| 146 } else if (DartDOMWrapper::subtypeOf(arg1, DartDocument::dartClassId)) { | |
| 147 Document* asDocument = DartDocument::toNative(arg1, exception); | |
| 148 if (exception) | |
| 149 goto fail; | |
| 150 receiver->send(asDocument, es); | |
| 151 } else if (DartDOMWrapper::subtypeOf(arg1, DartBlob::dartClassId)) { | |
| 152 Blob* asBlob = DartBlob::toNative(arg1, exception); | |
| 153 if (exception) | |
| 154 goto fail; | |
| 155 receiver->send(asBlob, es); | |
| 156 } else if (DartDOMWrapper::subtypeOf(arg1, DartFormData::dartClassId)) { | |
| 157 DOMFormData* asDOMFormData = DartFormData::toNative(arg1, exception)
; | |
| 158 if (exception) | |
| 159 goto fail; | |
| 160 receiver->send(asDOMFormData, es); | |
| 161 } else if (Dart_IsByteBuffer(arg1)) { | |
| 162 arg1 = Dart_GetDataFromByteBuffer(arg1); | |
| 163 RefPtr<ArrayBufferView> asArrayBufferView = DartUtilities::dartToArr
ayBufferView(arg1, exception); | |
| 164 if (exception) | |
| 165 goto fail; | |
| 166 receiver->send(asArrayBufferView.get(), es); | |
| 167 } else if (Dart_IsTypedData(arg1)) { | |
| 168 RefPtr<ArrayBufferView> asArrayBufferView = DartUtilities::dartToArr
ayBufferView(arg1, exception); | |
| 169 if (exception) | |
| 170 goto fail; | |
| 171 receiver->send(asArrayBufferView.get(), es); | |
| 172 } else { | |
| 173 DartStringAdapter asString = DartUtilities::dartToString(arg1, excep
tion); | |
| 174 if (exception) | |
| 175 goto fail; | |
| 176 receiver->send(asString, es); | |
| 177 } | |
| 178 | |
| 179 if (es.hadException()) { | |
| 180 exception = es.toDart(args); | |
| 181 goto fail; | |
| 182 } | |
| 183 return; | |
| 184 } | |
| 185 | |
| 186 fail: | |
| 187 Dart_ThrowException(exception); | |
| 188 ASSERT_NOT_REACHED(); | |
| 189 } | |
| 190 | |
| 191 void responseTextGetter(Dart_NativeArguments args) | |
| 192 { | |
| 193 Dart_Handle exception = 0; | |
| 194 { | |
| 195 XMLHttpRequest* receiver = DartDOMWrapper::receiver<XMLHttpRequest>(args
); | |
| 196 | |
| 197 DartExceptionState es; | |
| 198 // FIXME: Can we push this out of the V8 heap? | |
| 199 ScriptString v8String = receiver->responseText(es); | |
| 200 | |
| 201 if (es.hadException()) { | |
| 202 exception = es.toDart(args); | |
| 203 goto fail; | |
| 204 } | |
| 205 | |
| 206 v8::Handle<v8::Value> v8Value = v8String.v8Value(); | |
| 207 | |
| 208 Dart_Handle result; | |
| 209 if (*v8Value) { | |
| 210 // FIXME: Should we cache this? | |
| 211 result = V8Converter::stringToDart(v8Value); | |
| 212 } else { | |
| 213 // If the V8 value is null, there was an error. Return an | |
| 214 // empty string to Dart. | |
| 215 // FIXME: Resolve expected error behavior: | |
| 216 // https://code.google.com/p/dart/issues/detail?id=11998 | |
| 217 result = Dart_NewStringFromCString(""); | |
| 218 } | |
| 219 Dart_SetReturnValue(args, result); | |
| 220 return; | |
| 221 } | |
| 222 | |
| 223 fail: | |
| 224 Dart_ThrowException(exception); | |
| 225 ASSERT_NOT_REACHED(); | |
| 226 } | |
| 227 | |
| 228 void responseGetter(Dart_NativeArguments args) | |
| 229 { | |
| 230 Dart_Handle exception = 0; | |
| 231 { | |
| 232 XMLHttpRequest* receiver = DartDOMWrapper::receiver<XMLHttpRequest>(args
); | |
| 233 | |
| 234 switch (receiver->responseTypeCode()) { | |
| 235 case XMLHttpRequest::ResponseTypeDefault: | |
| 236 case XMLHttpRequest::ResponseTypeText: | |
| 237 { | |
| 238 DartExceptionState es; | |
| 239 ScriptString v8String = receiver->responseText(es); | |
| 240 if (es.hadException()) { | |
| 241 exception = es.toDart(args); | |
| 242 goto fail; | |
| 243 } | |
| 244 | |
| 245 // FIXME: Should we cache this? | |
| 246 Dart_Handle result = V8Converter::stringToDart(v8String.v8Value(
)); | |
| 247 Dart_SetReturnValue(args, result); | |
| 248 return; | |
| 249 } | |
| 250 | |
| 251 case XMLHttpRequest::ResponseTypeJSON: | |
| 252 { | |
| 253 ScriptString v8String = receiver->responseJSONSource(); | |
| 254 if (v8String.isEmpty()) { | |
| 255 Dart_SetReturnValue(args, Dart_Null()); | |
| 256 return; | |
| 257 } | |
| 258 | |
| 259 // FIXME: Should we cache this? | |
| 260 Dart_Handle jsonSource = V8Converter::stringToDart(v8String.v8Va
lue()); | |
| 261 Dart_Handle result = DartUtilities::invokeUtilsMethod("parseJson
", 1, &jsonSource); | |
| 262 if (Dart_IsError(result)) { | |
| 263 exception = result; | |
| 264 goto fail; | |
| 265 } | |
| 266 Dart_SetReturnValue(args, result); | |
| 267 return; | |
| 268 } | |
| 269 | |
| 270 case XMLHttpRequest::ResponseTypeDocument: | |
| 271 { | |
| 272 DartExceptionState es; | |
| 273 Document* document = receiver->responseXML(es); | |
| 274 if (es.hadException()) { | |
| 275 exception = es.toDart(args); | |
| 276 goto fail; | |
| 277 } | |
| 278 DartDOMWrapper::returnToDart<DartDocument>(args, document); | |
| 279 return; | |
| 280 } | |
| 281 | |
| 282 case XMLHttpRequest::ResponseTypeBlob: | |
| 283 { | |
| 284 Blob* blob = receiver->responseBlob(); | |
| 285 DartDOMWrapper::returnToDart<DartBlob>(args, blob); | |
| 286 return; | |
| 287 } | |
| 288 | |
| 289 case XMLHttpRequest::ResponseTypeLegacyStream: | |
| 290 { | |
| 291 Stream* stream = receiver->responseStream(); | |
| 292 DartDOMWrapper::returnToDart<DartStream>(args, stream); | |
| 293 return; | |
| 294 } | |
| 295 | |
| 296 case XMLHttpRequest::ResponseTypeArrayBuffer: | |
| 297 { | |
| 298 ArrayBuffer* arrayBuffer = receiver->responseArrayBuffer(); | |
| 299 Dart_SetReturnValue(args, DartUtilities::arrayBufferToDart(array
Buffer)); | |
| 300 return; | |
| 301 } | |
| 302 } | |
| 303 return; | |
| 304 } | |
| 305 | |
| 306 fail: | |
| 307 Dart_ThrowException(exception); | |
| 308 ASSERT_NOT_REACHED(); | |
| 309 } | |
| 310 | |
| 311 } // DartXMLHttpRequestInternal | |
| 312 | |
| 313 } // WebCore | |
| OLD | NEW |