blob: b60e75e9bedcacd3cff641909827e2e9d1ccc2a1 [file] [log] [blame]
weinig@apple.com81ed4492008-04-18 00:56:23 +00001/*
mark.lam@apple.com17ae4902021-02-19 15:51:15 +00002 * Copyright (C) 2008-2021 Apple Inc. All rights reserved.
weinig@apple.com81ed4492008-04-18 00:56:23 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
mjs@apple.com92047332014-03-15 04:08:27 +000013 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
weinig@apple.com81ed4492008-04-18 00:56:23 +000014 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "JSXMLHttpRequest.h"
31
jianli@chromium.orgec0183f2010-01-20 23:53:27 +000032#include "JSBlob.h"
commit-queue@webkit.orge1967142017-06-15 17:49:50 +000033#include "JSDOMConvertBufferSource.h"
34#include "JSDOMConvertInterface.h"
35#include "JSDOMConvertJSON.h"
commit-queue@webkit.org425d3aa2017-07-18 03:52:23 +000036#include "JSDOMConvertNullable.h"
37#include "JSDOMConvertStrings.h"
weinig@apple.com81ed4492008-04-18 00:56:23 +000038#include "JSDocument.h"
weinig@apple.com81ed4492008-04-18 00:56:23 +000039
weinig@apple.com81ed4492008-04-18 00:56:23 +000040
41namespace WebCore {
keith_miller@apple.comce64b732017-10-17 07:10:58 +000042using namespace JSC;
weinig@apple.com81ed4492008-04-18 00:56:23 +000043
mark.lam@apple.com17ae4902021-02-19 15:51:15 +000044template<typename Visitor>
45void JSXMLHttpRequest::visitAdditionalChildren(Visitor& visitor)
mhahnenberg@apple.com982c9ea2011-09-23 19:40:09 +000046{
commit-queue@webkit.orgc02d7032018-05-05 23:45:15 +000047 if (auto* upload = wrapped().optionalUpload())
ggaren@apple.com4e21a1e2011-04-25 00:47:17 +000048 visitor.addOpaqueRoot(upload);
weinig@apple.coma244b912008-07-30 02:39:48 +000049
commit-queue@webkit.orgc02d7032018-05-05 23:45:15 +000050 if (auto* responseDocument = wrapped().optionalResponseXML())
ggaren@apple.com4e21a1e2011-04-25 00:47:17 +000051 visitor.addOpaqueRoot(responseDocument);
weinig@apple.com81ed4492008-04-18 00:56:23 +000052}
53
mark.lam@apple.com17ae4902021-02-19 15:51:15 +000054DEFINE_VISIT_ADDITIONAL_CHILDREN(JSXMLHttpRequest);
55
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +000056JSValue JSXMLHttpRequest::response(JSGlobalObject& lexicalGlobalObject) const
crogers@google.com01f283a2010-11-23 21:51:00 +000057{
commit-queue@webkit.orgc50546c2017-07-22 03:54:16 +000058 auto cacheResult = [&] (JSValue value) -> JSValue {
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +000059 m_response.set(lexicalGlobalObject.vm(), this, value);
commit-queue@webkit.orgc50546c2017-07-22 03:54:16 +000060 return value;
61 };
62
63
64 if (wrapped().responseCacheIsValid())
65 return m_response.get();
66
darin@apple.com0739f812016-05-01 04:50:10 +000067 auto type = wrapped().responseType();
68
69 switch (type) {
darin@apple.com53438e92016-05-03 05:47:34 +000070 case XMLHttpRequest::ResponseType::EmptyString:
commit-queue@webkit.org425d3aa2017-07-18 03:52:23 +000071 case XMLHttpRequest::ResponseType::Text: {
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +000072 auto scope = DECLARE_THROW_SCOPE(lexicalGlobalObject.vm());
73 return cacheResult(toJS<IDLNullable<IDLUSVString>>(lexicalGlobalObject, scope, wrapped().responseText()));
commit-queue@webkit.org425d3aa2017-07-18 03:52:23 +000074 }
darin@apple.com0739f812016-05-01 04:50:10 +000075 default:
76 break;
77 }
78
79 if (!wrapped().doneWithoutErrors())
commit-queue@webkit.orgc50546c2017-07-22 03:54:16 +000080 return cacheResult(jsNull());
commit-queue@webkit.orgcae42a42014-02-06 09:42:26 +000081
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +000082 JSValue value;
darin@apple.com0739f812016-05-01 04:50:10 +000083 switch (type) {
darin@apple.com53438e92016-05-03 05:47:34 +000084 case XMLHttpRequest::ResponseType::EmptyString:
85 case XMLHttpRequest::ResponseType::Text:
darin@apple.com0739f812016-05-01 04:50:10 +000086 ASSERT_NOT_REACHED();
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +000087 return jsUndefined();
crogers@google.com01f283a2010-11-23 21:51:00 +000088
darin@apple.com53438e92016-05-03 05:47:34 +000089 case XMLHttpRequest::ResponseType::Json:
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +000090 value = toJS<IDLJSON>(*globalObject(), wrapped().responseTextIgnoringResponseType());
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +000091 if (!value)
92 value = jsNull();
93 break;
rniwa@webkit.org89ac8962013-09-03 18:45:51 +000094
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +000095 case XMLHttpRequest::ResponseType::Document: {
darin@apple.com85704942016-10-08 23:01:27 +000096 auto document = wrapped().responseXML();
97 ASSERT(!document.hasException());
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +000098 value = toJS<IDLInterface<Document>>(lexicalGlobalObject, *globalObject(), document.releaseReturnValue());
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +000099 break;
100 }
darin@apple.com85704942016-10-08 23:01:27 +0000101
darin@apple.com53438e92016-05-03 05:47:34 +0000102 case XMLHttpRequest::ResponseType::Blob:
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +0000103 value = toJSNewlyCreated<IDLInterface<Blob>>(lexicalGlobalObject, *globalObject(), wrapped().createResponseBlob());
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +0000104 break;
crogers@google.com01f283a2010-11-23 21:51:00 +0000105
darin@apple.com53438e92016-05-03 05:47:34 +0000106 case XMLHttpRequest::ResponseType::Arraybuffer:
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +0000107 value = toJS<IDLInterface<ArrayBuffer>>(lexicalGlobalObject, *globalObject(), wrapped().createResponseArrayBuffer());
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +0000108 break;
crogers@google.com01f283a2010-11-23 21:51:00 +0000109 }
darin@apple.com85704942016-10-08 23:01:27 +0000110
commit-queue@webkit.orgd2c5b6e2016-07-21 06:51:05 +0000111 wrapped().didCacheResponse();
commit-queue@webkit.orgc50546c2017-07-22 03:54:16 +0000112 return cacheResult(value);
crogers@google.com01f283a2010-11-23 21:51:00 +0000113}
114
weinig@apple.com81ed4492008-04-18 00:56:23 +0000115} // namespace WebCore