Skip to content

Commit 7b8bafa

Browse files
committed
Move Fetch from legacy to new DOM exceptions
https://bugs.webkit.org/show_bug.cgi?id=163195 Reviewed by Chris Dumez. * Modules/fetch/FetchHeaders.cpp: (WebCore::canWriteHeader): Use ExceptionOr. (WebCore::FetchHeaders::append): Ditto. (WebCore::FetchHeaders::remove): Ditto. (WebCore::FetchHeaders::get): Ditto. (WebCore::FetchHeaders::has): Ditto. (WebCore::FetchHeaders::set): Ditto. (WebCore::FetchHeaders::filterAndFill): Ditto. (WebCore::FetchHeaders::Iterator::next): Got rid of unneeded code to clear out m_keys after the last call to next, since it will be deleted as soon as the iterator is deleted. * Modules/fetch/FetchHeaders.h: Use pragma once. Use ExceptionOr. * Modules/fetch/FetchHeaders.idl: Move to non-legacy exceptions. * Modules/fetch/FetchRequest.cpp: (WebCore::FetchRequest::setBody): Use ExceptionOr. (WebCore::FetchRequest::clone): Ditto. * Modules/fetch/FetchRequest.h: Use pragma once. Use ExceptionOr. * Modules/fetch/FetchRequest.idl: Move to non-legacy exceptions. * Modules/fetch/FetchResponse.cpp: (WebCore::FetchResponse::redirect): Use ExceptionOr. (WebCore::FetchResponse::setStatus): Ditto. * Modules/fetch/FetchResponse.h: Use ExceptionOr. * Modules/fetch/FetchResponse.idl: Move to non-legacy exceptions. * bindings/js/JSDOMBinding.h: Added implementation of toJSNullableString. * bindings/scripts/CodeGeneratorJS.pm: (NativeToJSValue): Call toJSNullableString and toJSString for functions that return ExceptionOr<String>. Canonical link: https://commits.webkit.org/181085@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@207037 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 6f84faf commit 7b8bafa

12 files changed

Lines changed: 167 additions & 129 deletions

File tree

Source/WebCore/ChangeLog

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
2016-10-10 Darin Adler <[email protected]>
2+
3+
Move Fetch from legacy to new DOM exceptions
4+
https://bugs.webkit.org/show_bug.cgi?id=163195
5+
6+
Reviewed by Chris Dumez.
7+
8+
* Modules/fetch/FetchHeaders.cpp:
9+
(WebCore::canWriteHeader): Use ExceptionOr.
10+
(WebCore::FetchHeaders::append): Ditto.
11+
(WebCore::FetchHeaders::remove): Ditto.
12+
(WebCore::FetchHeaders::get): Ditto.
13+
(WebCore::FetchHeaders::has): Ditto.
14+
(WebCore::FetchHeaders::set): Ditto.
15+
(WebCore::FetchHeaders::filterAndFill): Ditto.
16+
(WebCore::FetchHeaders::Iterator::next): Got rid of unneeded code to
17+
clear out m_keys after the last call to next, since it will be deleted
18+
as soon as the iterator is deleted.
19+
20+
* Modules/fetch/FetchHeaders.h: Use pragma once. Use ExceptionOr.
21+
22+
* Modules/fetch/FetchHeaders.idl: Move to non-legacy exceptions.
23+
24+
* Modules/fetch/FetchRequest.cpp:
25+
(WebCore::FetchRequest::setBody): Use ExceptionOr.
26+
(WebCore::FetchRequest::clone): Ditto.
27+
28+
* Modules/fetch/FetchRequest.h: Use pragma once. Use ExceptionOr.
29+
30+
* Modules/fetch/FetchRequest.idl: Move to non-legacy exceptions.
31+
32+
* Modules/fetch/FetchResponse.cpp:
33+
(WebCore::FetchResponse::redirect): Use ExceptionOr.
34+
(WebCore::FetchResponse::setStatus): Ditto.
35+
36+
* Modules/fetch/FetchResponse.h: Use ExceptionOr.
37+
38+
* Modules/fetch/FetchResponse.idl: Move to non-legacy exceptions.
39+
40+
* bindings/js/JSDOMBinding.h: Added implementation of toJSNullableString.
41+
42+
* bindings/scripts/CodeGeneratorJS.pm:
43+
(NativeToJSValue): Call toJSNullableString and toJSString for
44+
functions that return ExceptionOr<String>.
45+
146
2016-10-10 Nan Wang <[email protected]>
247

348
AX: Expose invalid status for input types with that information

Source/WebCore/Modules/fetch/FetchHeaders.cpp

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@
3636

3737
namespace WebCore {
3838

39-
static bool canWriteHeader(const String& name, const String& value, FetchHeaders::Guard guard, ExceptionCode& ec)
39+
static ExceptionOr<bool> canWriteHeader(const String& name, const String& value, FetchHeaders::Guard guard)
4040
{
41-
if (!isValidHTTPToken(name) || !isValidHTTPHeaderValue(value)) {
42-
ec = TypeError;
43-
return false;
44-
}
45-
if (guard == FetchHeaders::Guard::Immutable) {
46-
ec = TypeError;
47-
return false;
48-
}
41+
if (!isValidHTTPToken(name) || !isValidHTTPHeaderValue(value))
42+
return Exception { TypeError };
43+
if (guard == FetchHeaders::Guard::Immutable)
44+
return Exception { TypeError };
4945
if (guard == FetchHeaders::Guard::Request && isForbiddenHeaderName(name))
5046
return false;
5147
if (guard == FetchHeaders::Guard::RequestNoCors && !isSimpleHeader(name, value))
@@ -55,79 +51,86 @@ static bool canWriteHeader(const String& name, const String& value, FetchHeaders
5551
return true;
5652
}
5753

58-
void FetchHeaders::append(const String& name, const String& value, ExceptionCode& ec)
54+
ExceptionOr<void> FetchHeaders::append(const String& name, const String& value)
5955
{
6056
String normalizedValue = stripLeadingAndTrailingHTTPSpaces(value);
61-
if (!canWriteHeader(name, normalizedValue, m_guard, ec))
62-
return;
57+
auto canWriteResult = canWriteHeader(name, normalizedValue, m_guard);
58+
if (canWriteResult.hasException())
59+
return canWriteResult.releaseException();
60+
if (!canWriteResult.releaseReturnValue())
61+
return { };
6362
m_headers.add(name, normalizedValue);
63+
return { };
6464
}
6565

66-
void FetchHeaders::remove(const String& name, ExceptionCode& ec)
66+
ExceptionOr<void> FetchHeaders::remove(const String& name)
6767
{
68-
if (!canWriteHeader(name, String(), m_guard, ec))
69-
return;
68+
auto canWriteResult = canWriteHeader(name, { }, m_guard);
69+
if (canWriteResult.hasException())
70+
return canWriteResult.releaseException();
71+
if (!canWriteResult.releaseReturnValue())
72+
return { };
7073
m_headers.remove(name);
74+
return { };
7175
}
7276

73-
String FetchHeaders::get(const String& name, ExceptionCode& ec) const
77+
ExceptionOr<String> FetchHeaders::get(const String& name) const
7478
{
75-
if (!isValidHTTPToken(name)) {
76-
ec = TypeError;
77-
return String();
78-
}
79+
if (!isValidHTTPToken(name))
80+
return Exception { TypeError };
7981
return m_headers.get(name);
8082
}
8183

82-
bool FetchHeaders::has(const String& name, ExceptionCode& ec) const
84+
ExceptionOr<bool> FetchHeaders::has(const String& name) const
8385
{
84-
if (!isValidHTTPToken(name)) {
85-
ec = TypeError;
86-
return false;
87-
}
86+
if (!isValidHTTPToken(name))
87+
return Exception { TypeError };
8888
return m_headers.contains(name);
8989
}
9090

91-
void FetchHeaders::set(const String& name, const String& value, ExceptionCode& ec)
91+
ExceptionOr<void> FetchHeaders::set(const String& name, const String& value)
9292
{
9393
String normalizedValue = stripLeadingAndTrailingHTTPSpaces(value);
94-
if (!canWriteHeader(name, normalizedValue, m_guard, ec))
95-
return;
94+
auto canWriteResult = canWriteHeader(name, normalizedValue, m_guard);
95+
if (canWriteResult.hasException())
96+
return canWriteResult.releaseException();
97+
if (!canWriteResult.releaseReturnValue())
98+
return { };
9699
m_headers.set(name, normalizedValue);
100+
return { };
97101
}
98102

99103
void FetchHeaders::fill(const FetchHeaders* headers)
100104
{
101105
ASSERT(m_guard != Guard::Immutable);
102-
103106
if (!headers)
104107
return;
105-
106108
filterAndFill(headers->m_headers, m_guard);
107109
}
108110

109111
void FetchHeaders::filterAndFill(const HTTPHeaderMap& headers, Guard guard)
110112
{
111-
ExceptionCode ec;
112113
for (auto& header : headers) {
113-
if (canWriteHeader(header.key, header.value, guard, ec)) {
114-
if (header.keyAsHTTPHeaderName)
115-
m_headers.add(header.keyAsHTTPHeaderName.value(), header.value);
116-
else
117-
m_headers.add(header.key, header.value);
118-
}
114+
auto canWriteResult = canWriteHeader(header.key, header.value, guard);
115+
if (canWriteResult.hasException())
116+
continue;
117+
if (!canWriteResult.releaseReturnValue())
118+
continue;
119+
if (header.keyAsHTTPHeaderName)
120+
m_headers.add(header.keyAsHTTPHeaderName.value(), header.value);
121+
else
122+
m_headers.add(header.key, header.value);
119123
}
120124
}
121125

122126
Optional<WTF::KeyValuePair<String, String>> FetchHeaders::Iterator::next()
123127
{
124128
while (m_currentIndex < m_keys.size()) {
125-
String key = m_keys[m_currentIndex++];
126-
String value = m_headers->m_headers.get(key);
129+
auto key = m_keys[m_currentIndex++];
130+
auto value = m_headers->m_headers.get(key);
127131
if (!value.isNull())
128-
return WTF::KeyValuePair<String, String>(WTFMove(key), WTFMove(value));
132+
return WTF::KeyValuePair<String, String> { WTFMove(key), WTFMove(value) };
129133
}
130-
m_keys.clear();
131134
return Nullopt;
132135
}
133136

@@ -137,7 +140,6 @@ FetchHeaders::Iterator::Iterator(FetchHeaders& headers)
137140
m_keys.reserveInitialCapacity(headers.m_headers.size());
138141
for (auto& header : headers.m_headers)
139142
m_keys.uncheckedAppend(header.key.convertToASCIILowercase());
140-
141143
std::sort(m_keys.begin(), m_keys.end(), WTF::codePointCompareLessThan);
142144
}
143145

Source/WebCore/Modules/fetch/FetchHeaders.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030

3131
#if ENABLE(FETCH_API)
3232

33+
#include "ExceptionOr.h"
3334
#include "HTTPHeaderMap.h"
3435
#include <wtf/HashTraits.h>
35-
#include <wtf/Optional.h>
3636

3737
namespace WebCore {
3838

39-
typedef int ExceptionCode;
40-
4139
class FetchHeaders : public RefCounted<FetchHeaders> {
4240
public:
4341
enum class Guard {
@@ -48,17 +46,16 @@ class FetchHeaders : public RefCounted<FetchHeaders> {
4846
Response
4947
};
5048

51-
static Ref<FetchHeaders> create(Guard guard = Guard::None) { return adoptRef(*new FetchHeaders(guard)); }
52-
static Ref<FetchHeaders> create(const FetchHeaders& headers) { return adoptRef(*new FetchHeaders(headers.m_guard, headers.m_headers)); }
49+
static Ref<FetchHeaders> create(Guard guard = Guard::None) { return adoptRef(*new FetchHeaders { guard }); }
50+
static Ref<FetchHeaders> create(const FetchHeaders& headers) { return adoptRef(*new FetchHeaders { headers }); }
5351

54-
void append(const String& name, const String& value, ExceptionCode&);
55-
void remove(const String&, ExceptionCode&);
56-
String get(const String&, ExceptionCode&) const;
57-
bool has(const String&, ExceptionCode&) const;
58-
void set(const String& name, const String& value, ExceptionCode&);
52+
ExceptionOr<void> append(const String& name, const String& value);
53+
ExceptionOr<void> remove(const String&);
54+
ExceptionOr<String> get(const String&) const;
55+
ExceptionOr<bool> has(const String&) const;
56+
ExceptionOr<void> set(const String& name, const String& value);
5957

6058
void fill(const FetchHeaders*);
61-
6259
void filterAndFill(const HTTPHeaderMap&, Guard);
6360

6461
String fastGet(HTTPHeaderName name) const { return m_headers.get(name); }
@@ -71,23 +68,30 @@ class FetchHeaders : public RefCounted<FetchHeaders> {
7168

7269
private:
7370
Ref<FetchHeaders> m_headers;
74-
size_t m_currentIndex = 0;
71+
size_t m_currentIndex { 0 };
7572
Vector<String> m_keys;
7673
};
77-
Iterator createIterator() { return Iterator(*this); }
74+
Iterator createIterator() { return Iterator { *this }; }
7875

7976
const HTTPHeaderMap& internalHeaders() const { return m_headers; }
8077

8178
void setGuard(Guard);
8279

8380
private:
8481
FetchHeaders(Guard guard) : m_guard(guard) { }
85-
FetchHeaders(Guard guard, const HTTPHeaderMap& headers) : m_guard(guard), m_headers(headers) { }
82+
FetchHeaders(const FetchHeaders&);
8683

8784
Guard m_guard;
8885
HTTPHeaderMap m_headers;
8986
};
9087

88+
inline FetchHeaders::FetchHeaders(const FetchHeaders& other)
89+
: RefCounted()
90+
, m_guard(other.m_guard)
91+
, m_headers(other.m_headers)
92+
{
93+
}
94+
9195
inline void FetchHeaders::setGuard(Guard guard)
9296
{
9397
ASSERT(!m_headers.size());

Source/WebCore/Modules/fetch/FetchHeaders.idl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
InterfaceName=Headers,
3535
JSBuiltinConstructor,
3636
PrivateIdentifier,
37-
PublicIdentifier]
38-
interface FetchHeaders {
39-
[MayThrowLegacyException] void append(DOMString name, DOMString value);
40-
[MayThrowLegacyException, ImplementedAs=remove] void delete(DOMString name);
41-
[MayThrowLegacyException] DOMString? get(DOMString name);
42-
[MayThrowLegacyException] boolean has(DOMString name);
43-
[MayThrowLegacyException] void set(DOMString name, DOMString value);
37+
PublicIdentifier,
38+
] interface FetchHeaders {
39+
[MayThrowException] void append(DOMString name, DOMString value);
40+
[MayThrowException, ImplementedAs=remove] void delete(DOMString name);
41+
[MayThrowException] DOMString? get(DOMString name);
42+
[MayThrowException] boolean has(DOMString name);
43+
[MayThrowException] void set(DOMString name, DOMString value);
4444

4545
[EnabledAtRuntime=DOMIterator] iterable<DOMString, DOMString>;
4646

47-
[PrivateIdentifier, MayThrowLegacyException, ImplementedAs=append] void appendFromJS(DOMString name, DOMString value);
48-
[PrivateIdentifier, ImplementedAs=fill] void fillFromJS(FetchHeaders? headers);
47+
[ImplementedAs=append, MayThrowException, PrivateIdentifier] void appendFromJS(DOMString name, DOMString value);
48+
[ImplementedAs=fill, PrivateIdentifier] void fillFromJS(FetchHeaders? headers);
4949
};

Source/WebCore/Modules/fetch/FetchRequest.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -261,30 +261,23 @@ ExceptionOr<Ref<FetchHeaders>> FetchRequest::initializeWith(FetchRequest& input,
261261
return initializeOptions(init);
262262
}
263263

264-
void FetchRequest::setBody(JSC::ExecState& execState, JSC::JSValue body, FetchRequest* request, ExceptionCode& ec)
264+
ExceptionOr<void> FetchRequest::setBody(JSC::ExecState& execState, JSC::JSValue body, FetchRequest* request)
265265
{
266266
if (!body.isNull()) {
267-
if (!methodCanHaveBody(m_internalRequest)) {
268-
ec = TypeError;
269-
return;
270-
}
271-
267+
if (!methodCanHaveBody(m_internalRequest))
268+
return Exception { TypeError };
272269
ASSERT(scriptExecutionContext());
273270
extractBody(*scriptExecutionContext(), execState, body);
274-
if (isBodyNull()) {
275-
ec = TypeError;
276-
return;
277-
}
271+
if (isBodyNull())
272+
return Exception { TypeError };
278273
} else if (request && !request->isBodyNull()) {
279-
if (!methodCanHaveBody(m_internalRequest)) {
280-
ec = TypeError;
281-
return;
282-
}
283-
274+
if (!methodCanHaveBody(m_internalRequest))
275+
return Exception { TypeError };
284276
m_body = WTFMove(request->m_body);
285277
request->setDisturbed();
286278
}
287279
updateContentType();
280+
return { };
288281
}
289282

290283
String FetchRequest::referrer() const
@@ -316,12 +309,10 @@ ResourceRequest FetchRequest::internalRequest() const
316309
return request;
317310
}
318311

319-
RefPtr<FetchRequest> FetchRequest::clone(ScriptExecutionContext& context, ExceptionCode& ec)
312+
ExceptionOr<Ref<FetchRequest>> FetchRequest::clone(ScriptExecutionContext& context)
320313
{
321-
if (isDisturbedOrLocked()) {
322-
ec = TypeError;
323-
return nullptr;
324-
}
314+
if (isDisturbedOrLocked())
315+
return Exception { TypeError };
325316

326317
auto clone = adoptRef(*new FetchRequest(context, Nullopt, FetchHeaders::create(m_headers.get()), FetchRequest::InternalRequest(m_internalRequest)));
327318
clone->cloneBody(*this);

0 commit comments

Comments
 (0)