Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2008, 2009, 2010 Google Inc. 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 
     31 #include "config.h"
     32 #include "bindings/core/v8/V8XMLHttpRequest.h"
     33 
     34 #include "bindings/core/v8/V8Blob.h"
     35 #include "bindings/core/v8/V8Document.h"
     36 #include "bindings/core/v8/V8FormData.h"
     37 #include "bindings/core/v8/V8HTMLDocument.h"
     38 #include "bindings/core/v8/V8Stream.h"
     39 #include "bindings/v8/ExceptionMessages.h"
     40 #include "bindings/v8/ExceptionState.h"
     41 #include "bindings/v8/V8Binding.h"
     42 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
     43 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h"
     44 #include "core/dom/Document.h"
     45 #include "core/fileapi/Stream.h"
     46 #include "core/inspector/InspectorInstrumentation.h"
     47 #include "core/workers/WorkerGlobalScope.h"
     48 #include "core/xml/XMLHttpRequest.h"
     49 #include "wtf/ArrayBuffer.h"
     50 #include <v8.h>
     51 
     52 namespace WebCore {
     53 
     54 void V8XMLHttpRequest::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
     55 {
     56     ExecutionContext* context = currentExecutionContext(info.GetIsolate());
     57 
     58     RefPtr<SecurityOrigin> securityOrigin;
     59     if (context->isDocument()) {
     60         DOMWrapperWorld& world = DOMWrapperWorld::current(info.GetIsolate());
     61         if (world.isIsolatedWorld())
     62             securityOrigin = world.isolatedWorldSecurityOrigin();
     63     }
     64 
     65     RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context, securityOrigin);
     66 
     67     v8::Handle<v8::Object> wrapper = info.Holder();
     68     V8DOMWrapper::associateObjectWithWrapper<V8XMLHttpRequest>(xmlHttpRequest.release(), &wrapperTypeInfo, wrapper, info.GetIsolate(), WrapperConfiguration::Dependent);
     69     info.GetReturnValue().Set(wrapper);
     70 }
     71 
     72 void V8XMLHttpRequest::responseTextAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
     73 {
     74     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
     75     ExceptionState exceptionState(ExceptionState::GetterContext, "responseText", "XMLHttpRequest", info.Holder(), info.GetIsolate());
     76     ScriptString text = xmlHttpRequest->responseText(exceptionState);
     77     if (exceptionState.throwIfNeeded())
     78         return;
     79     if (text.isEmpty()) {
     80         v8SetReturnValueString(info, emptyString(), info.GetIsolate());
     81         return;
     82     }
     83     v8SetReturnValue(info, text.v8Value());
     84 }
     85 
     86 void V8XMLHttpRequest::responseAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
     87 {
     88     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
     89 
     90     switch (xmlHttpRequest->responseTypeCode()) {
     91     case XMLHttpRequest::ResponseTypeDefault:
     92     case XMLHttpRequest::ResponseTypeText:
     93         responseTextAttributeGetterCustom(info);
     94         return;
     95 
     96     case XMLHttpRequest::ResponseTypeJSON:
     97         {
     98             v8::Isolate* isolate = info.GetIsolate();
     99 
    100             ScriptString jsonSource = xmlHttpRequest->responseJSONSource();
    101             if (jsonSource.isEmpty()) {
    102                 v8SetReturnValue(info, v8::Null(isolate));
    103                 return;
    104             }
    105 
    106             // Catch syntax error.
    107             v8::TryCatch exceptionCatcher;
    108             v8::Handle<v8::Value> json = v8::JSON::Parse(jsonSource.v8Value());
    109             if (exceptionCatcher.HasCaught() || json.IsEmpty())
    110                 v8SetReturnValue(info, v8::Null(isolate));
    111             else
    112                 v8SetReturnValue(info, json);
    113             return;
    114         }
    115 
    116     case XMLHttpRequest::ResponseTypeDocument:
    117         {
    118             ExceptionState exceptionState(ExceptionState::GetterContext, "response", "XMLHttpRequest", info.Holder(), info.GetIsolate());
    119             Document* document = xmlHttpRequest->responseXML(exceptionState);
    120             if (exceptionState.throwIfNeeded())
    121                 return;
    122             v8SetReturnValueFast(info, document, xmlHttpRequest);
    123             return;
    124         }
    125 
    126     case XMLHttpRequest::ResponseTypeBlob:
    127         {
    128             Blob* blob = xmlHttpRequest->responseBlob();
    129             v8SetReturnValueFast(info, blob, xmlHttpRequest);
    130             return;
    131         }
    132 
    133     case XMLHttpRequest::ResponseTypeStream:
    134         {
    135             Stream* stream = xmlHttpRequest->responseStream();
    136             v8SetReturnValueFast(info, stream, xmlHttpRequest);
    137             return;
    138         }
    139 
    140     case XMLHttpRequest::ResponseTypeArrayBuffer:
    141         {
    142             ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer();
    143             if (arrayBuffer) {
    144                 arrayBuffer->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate());
    145             }
    146             v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest);
    147             return;
    148         }
    149     }
    150 }
    151 
    152 void V8XMLHttpRequest::openMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
    153 {
    154     // Four cases:
    155     // open(method, url)
    156     // open(method, url, async)
    157     // open(method, url, async, user)
    158     // open(method, url, async, user, passwd)
    159 
    160     ExceptionState exceptionState(ExceptionState::ExecutionContext, "open", "XMLHttpRequest", info.Holder(), info.GetIsolate());
    161 
    162     if (info.Length() < 2) {
    163         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
    164         exceptionState.throwIfNeeded();
    165         return;
    166     }
    167 
    168     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
    169 
    170     TOSTRING_VOID(V8StringResource<>, method, info[0]);
    171     TOSTRING_VOID(V8StringResource<>, urlstring, info[1]);
    172 
    173     ExecutionContext* context = currentExecutionContext(info.GetIsolate());
    174     KURL url = context->completeURL(urlstring);
    175 
    176     if (info.Length() >= 3) {
    177         bool async = info[2]->BooleanValue();
    178 
    179         if (info.Length() >= 4 && !info[3]->IsUndefined()) {
    180             TOSTRING_VOID(V8StringResource<WithNullCheck>, user, info[3]);
    181 
    182             if (info.Length() >= 5 && !info[4]->IsUndefined()) {
    183                 TOSTRING_VOID(V8StringResource<WithNullCheck>, password, info[4]);
    184                 xmlHttpRequest->open(method, url, async, user, password, exceptionState);
    185             } else {
    186                 xmlHttpRequest->open(method, url, async, user, exceptionState);
    187             }
    188         } else {
    189             xmlHttpRequest->open(method, url, async, exceptionState);
    190         }
    191     } else {
    192         xmlHttpRequest->open(method, url, exceptionState);
    193     }
    194 
    195     exceptionState.throwIfNeeded();
    196 }
    197 
    198 static bool isDocumentType(v8::Handle<v8::Value> value, v8::Isolate* isolate)
    199 {
    200     // FIXME: add other document types.
    201     return V8Document::hasInstance(value, isolate) || V8HTMLDocument::hasInstance(value, isolate);
    202 }
    203 
    204 void V8XMLHttpRequest::sendMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
    205 {
    206     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
    207 
    208     InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->executionContext(), xmlHttpRequest->url());
    209 
    210     ExceptionState exceptionState(ExceptionState::ExecutionContext, "send", "XMLHttpRequest", info.Holder(), info.GetIsolate());
    211     if (info.Length() < 1)
    212         xmlHttpRequest->send(exceptionState);
    213     else {
    214         v8::Handle<v8::Value> arg = info[0];
    215         if (isUndefinedOrNull(arg)) {
    216             xmlHttpRequest->send(exceptionState);
    217         } else if (isDocumentType(arg, info.GetIsolate())) {
    218             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
    219             Document* document = V8Document::toNative(object);
    220             ASSERT(document);
    221             xmlHttpRequest->send(document, exceptionState);
    222         } else if (V8Blob::hasInstance(arg, info.GetIsolate())) {
    223             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
    224             Blob* blob = V8Blob::toNative(object);
    225             ASSERT(blob);
    226             xmlHttpRequest->send(blob, exceptionState);
    227         } else if (V8FormData::hasInstance(arg, info.GetIsolate())) {
    228             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
    229             DOMFormData* domFormData = V8FormData::toNative(object);
    230             ASSERT(domFormData);
    231             xmlHttpRequest->send(domFormData, exceptionState);
    232         } else if (V8ArrayBuffer::hasInstance(arg, info.GetIsolate())) {
    233             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
    234             ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(object);
    235             ASSERT(arrayBuffer);
    236             xmlHttpRequest->send(arrayBuffer, exceptionState);
    237         } else if (V8ArrayBufferView::hasInstance(arg, info.GetIsolate())) {
    238             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
    239             ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(object);
    240             ASSERT(arrayBufferView);
    241             xmlHttpRequest->send(arrayBufferView, exceptionState);
    242         } else {
    243             TOSTRING_VOID(V8StringResource<WithNullCheck>, argString, arg);
    244             xmlHttpRequest->send(argString, exceptionState);
    245         }
    246     }
    247 
    248     exceptionState.throwIfNeeded();
    249 }
    250 
    251 } // namespace WebCore
    252