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 "third_party/blink/renderer/bindings/core/v8/v8_xml_http_request.h"
32
33 #include "third_party/blink/renderer/bindings/core/v8/v8_array_buffer.h"
34 #include "third_party/blink/renderer/bindings/core/v8/v8_array_buffer_view.h"
35 #include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
36 #include "third_party/blink/renderer/bindings/core/v8/v8_blob.h"
37 #include "third_party/blink/renderer/bindings/core/v8/v8_document.h"
38 #include "third_party/blink/renderer/bindings/core/v8/v8_form_data.h"
39 #include "third_party/blink/renderer/bindings/core/v8/v8_html_document.h"
40 #include "third_party/blink/renderer/core/dom/document.h"
41 #include "third_party/blink/renderer/core/probe/core_probes.h"
42 #include "third_party/blink/renderer/core/workers/worker_global_scope.h"
43 #include "third_party/blink/renderer/core/xmlhttprequest/xml_http_request.h"
44 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
45 #include "v8/include/v8.h"
46
47 namespace blink {
48
ResponseTextAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value> & info)49 void V8XMLHttpRequest::ResponseTextAttributeGetterCustom(
50 const v8::FunctionCallbackInfo<v8::Value>& info) {
51 XMLHttpRequest* xml_http_request = V8XMLHttpRequest::ToImpl(info.Holder());
52 ExceptionState exception_state(info.GetIsolate(),
53 ExceptionState::kGetterContext,
54 "XMLHttpRequest", "responseText");
55 v8::Local<v8::String> text = xml_http_request->responseText(exception_state);
56 if (text.IsEmpty()) {
57 V8SetReturnValueString(info, g_empty_string, info.GetIsolate());
58 return;
59 }
60 V8SetReturnValue(info, text);
61 }
62
ResponseAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value> & info)63 void V8XMLHttpRequest::ResponseAttributeGetterCustom(
64 const v8::FunctionCallbackInfo<v8::Value>& info) {
65 XMLHttpRequest* xml_http_request = V8XMLHttpRequest::ToImpl(info.Holder());
66 ExceptionState exception_state(info.GetIsolate(),
67 ExceptionState::kGetterContext,
68 "XMLHttpRequest", "response");
69
70 switch (xml_http_request->GetResponseTypeCode()) {
71 case XMLHttpRequest::kResponseTypeDefault:
72 case XMLHttpRequest::kResponseTypeText:
73 ResponseTextAttributeGetterCustom(info);
74 return;
75
76 case XMLHttpRequest::kResponseTypeJSON: {
77 v8::Isolate* isolate = info.GetIsolate();
78
79 v8::Local<v8::String> json_source =
80 xml_http_request->ResponseJSONSource();
81 if (json_source.IsEmpty()) {
82 V8SetReturnValueNull(info);
83 return;
84 }
85
86 // Catch syntax error. Swallows an exception (when thrown) as the
87 // spec says. https://xhr.spec.whatwg.org/#response-body
88 v8::Local<v8::Value> json =
89 FromJSONString(isolate, isolate->GetCurrentContext(),
90 ToCoreString(json_source), exception_state);
91 if (exception_state.HadException()) {
92 exception_state.ClearException();
93 V8SetReturnValueNull(info);
94 } else {
95 V8SetReturnValue(info, json);
96 }
97 return;
98 }
99
100 case XMLHttpRequest::kResponseTypeDocument: {
101 Document* document = xml_http_request->responseXML(exception_state);
102 V8SetReturnValueFast(info, document, xml_http_request);
103 return;
104 }
105
106 case XMLHttpRequest::kResponseTypeBlob: {
107 Blob* blob = xml_http_request->ResponseBlob();
108 V8SetReturnValueFast(info, blob, xml_http_request);
109 return;
110 }
111
112 case XMLHttpRequest::kResponseTypeArrayBuffer: {
113 DOMArrayBuffer* array_buffer = xml_http_request->ResponseArrayBuffer();
114 V8SetReturnValueFast(info, array_buffer, xml_http_request);
115 return;
116 }
117 }
118 }
119
120 } // namespace blink
121