OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/beacon/NavigatorBeacon.h" | 5 #include "modules/beacon/NavigatorBeacon.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/modules/v8/ArrayBufferViewOrBlobOrStringOrFormData.h" | 8 #include "bindings/modules/v8/ArrayBufferViewOrBlobOrStringOrFormData.h" |
9 #include "core/dom/DOMArrayBufferView.h" | 9 #include "core/dom/DOMArrayBufferView.h" |
10 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
11 #include "core/dom/ExecutionContext.h" | 11 #include "core/dom/ExecutionContext.h" |
12 #include "core/fetch/FetchUtils.h" | 12 #include "core/fetch/FetchUtils.h" |
13 #include "core/fileapi/Blob.h" | 13 #include "core/fileapi/Blob.h" |
14 #include "core/frame/LocalFrame.h" | 14 #include "core/frame/LocalFrame.h" |
15 #include "core/frame/Settings.h" | 15 #include "core/frame/Settings.h" |
16 #include "core/frame/UseCounter.h" | 16 #include "core/frame/UseCounter.h" |
17 #include "core/frame/csp/ContentSecurityPolicy.h" | 17 #include "core/frame/csp/ContentSecurityPolicy.h" |
18 #include "core/html/FormData.h" | 18 #include "core/html/FormData.h" |
19 #include "core/loader/PingLoader.h" | 19 #include "core/loader/PingLoader.h" |
20 | 20 |
21 namespace blink { | 21 namespace blink { |
22 | 22 |
23 NavigatorBeacon::NavigatorBeacon(Navigator& navigator) | 23 NavigatorBeacon::NavigatorBeacon(Navigator& navigator) |
24 : ContextClient(navigator.frame()), m_transmittedBytes(0) {} | 24 : m_transmittedBytes(0) {} |
sof
2017/01/05 12:05:40
Don't you need to invoke the Supplement<Navigator>
| |
25 | 25 |
26 NavigatorBeacon::~NavigatorBeacon() {} | 26 NavigatorBeacon::~NavigatorBeacon() {} |
27 | 27 |
28 DEFINE_TRACE(NavigatorBeacon) { | 28 DEFINE_TRACE(NavigatorBeacon) { |
29 ContextClient::trace(visitor); | |
30 Supplement<Navigator>::trace(visitor); | 29 Supplement<Navigator>::trace(visitor); |
31 } | 30 } |
32 | 31 |
33 const char* NavigatorBeacon::supplementName() { | 32 const char* NavigatorBeacon::supplementName() { |
34 return "NavigatorBeacon"; | 33 return "NavigatorBeacon"; |
35 } | 34 } |
36 | 35 |
37 NavigatorBeacon& NavigatorBeacon::from(Navigator& navigator) { | 36 NavigatorBeacon& NavigatorBeacon::from(Navigator& navigator) { |
38 NavigatorBeacon* supplement = static_cast<NavigatorBeacon*>( | 37 NavigatorBeacon* supplement = static_cast<NavigatorBeacon*>( |
39 Supplement<Navigator>::from(navigator, supplementName())); | 38 Supplement<Navigator>::from(navigator, supplementName())); |
(...skipping 23 matching lines...) Expand all Loading... | |
63 !context->contentSecurityPolicy()->allowConnectToSource(url)) { | 62 !context->contentSecurityPolicy()->allowConnectToSource(url)) { |
64 // We can safely expose the URL to JavaScript, as these checks happen | 63 // We can safely expose the URL to JavaScript, as these checks happen |
65 // synchronously before redirection. JavaScript receives no new information. | 64 // synchronously before redirection. JavaScript receives no new information. |
66 exceptionState.throwSecurityError( | 65 exceptionState.throwSecurityError( |
67 "Refused to send beacon to '" + url.elidedString() + | 66 "Refused to send beacon to '" + url.elidedString() + |
68 "' because it violates the document's Content Security Policy."); | 67 "' because it violates the document's Content Security Policy."); |
69 return false; | 68 return false; |
70 } | 69 } |
71 | 70 |
72 // If detached from frame, do not allow sending a Beacon. | 71 // If detached from frame, do not allow sending a Beacon. |
73 if (!frame() || !frame()->client()) | 72 if (!host()->frame()) |
haraken
2017/01/05 11:48:37
frame()->client() is not needed because host()->fr
sof
2017/01/05 12:05:40
It might be worth clarifying somewhere that |host(
| |
74 return false; | 73 return false; |
75 | 74 |
76 return true; | 75 return true; |
77 } | 76 } |
78 | 77 |
79 int NavigatorBeacon::maxAllowance() const { | 78 int NavigatorBeacon::maxAllowance() const { |
80 DCHECK(frame()); | 79 DCHECK(host()->frame()); |
81 const Settings* settings = frame()->settings(); | 80 const Settings* settings = host()->frame()->settings(); |
82 if (settings) { | 81 if (settings) { |
83 int maxAllowed = settings->maxBeaconTransmission(); | 82 int maxAllowed = settings->maxBeaconTransmission(); |
84 if (maxAllowed < m_transmittedBytes) | 83 if (maxAllowed < m_transmittedBytes) |
85 return 0; | 84 return 0; |
86 return maxAllowed - m_transmittedBytes; | 85 return maxAllowed - m_transmittedBytes; |
87 } | 86 } |
88 return m_transmittedBytes; | 87 return m_transmittedBytes; |
89 } | 88 } |
90 | 89 |
91 void NavigatorBeacon::addTransmittedBytes(int sentBytes) { | 90 void NavigatorBeacon::addTransmittedBytes(int sentBytes) { |
92 DCHECK_GE(sentBytes, 0); | 91 DCHECK_GE(sentBytes, 0); |
93 m_transmittedBytes += sentBytes; | 92 m_transmittedBytes += sentBytes; |
94 } | 93 } |
95 | 94 |
96 bool NavigatorBeacon::sendBeacon( | 95 bool NavigatorBeacon::sendBeacon( |
97 ExecutionContext* context, | 96 ExecutionContext* context, |
98 Navigator& navigator, | 97 Navigator& navigator, |
99 const String& urlstring, | 98 const String& urlstring, |
100 const ArrayBufferViewOrBlobOrStringOrFormData& data, | 99 const ArrayBufferViewOrBlobOrStringOrFormData& data, |
101 ExceptionState& exceptionState) { | 100 ExceptionState& exceptionState) { |
102 NavigatorBeacon& impl = NavigatorBeacon::from(navigator); | 101 return NavigatorBeacon::from(navigator).sendBeaconImpl(context, urlstring, |
102 data, exceptionState); | |
103 } | |
103 | 104 |
105 bool NavigatorBeacon::sendBeaconImpl( | |
106 ExecutionContext* context, | |
107 const String& urlstring, | |
108 const ArrayBufferViewOrBlobOrStringOrFormData& data, | |
109 ExceptionState& exceptionState) { | |
104 KURL url = context->completeURL(urlstring); | 110 KURL url = context->completeURL(urlstring); |
105 if (!impl.canSendBeacon(context, url, exceptionState)) | 111 if (!canSendBeacon(context, url, exceptionState)) |
106 return false; | 112 return false; |
107 | 113 |
108 int allowance = impl.maxAllowance(); | 114 int allowance = maxAllowance(); |
109 int bytes = 0; | 115 int bytes = 0; |
110 bool allowed; | 116 bool allowed; |
111 | 117 |
112 if (data.isArrayBufferView()) { | 118 if (data.isArrayBufferView()) { |
113 allowed = PingLoader::sendBeacon(impl.frame(), allowance, url, | 119 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, |
114 data.getAsArrayBufferView(), bytes); | 120 data.getAsArrayBufferView(), bytes); |
115 } else if (data.isBlob()) { | 121 } else if (data.isBlob()) { |
116 Blob* blob = data.getAsBlob(); | 122 Blob* blob = data.getAsBlob(); |
117 if (!FetchUtils::isSimpleContentType(AtomicString(blob->type()))) { | 123 if (!FetchUtils::isSimpleContentType(AtomicString(blob->type()))) { |
118 UseCounter::count(context, | 124 UseCounter::count(context, |
119 UseCounter::SendBeaconWithNonSimpleContentType); | 125 UseCounter::SendBeaconWithNonSimpleContentType); |
120 if (RuntimeEnabledFeatures:: | 126 if (RuntimeEnabledFeatures:: |
121 sendBeaconThrowForBlobWithNonSimpleTypeEnabled()) { | 127 sendBeaconThrowForBlobWithNonSimpleTypeEnabled()) { |
122 exceptionState.throwSecurityError( | 128 exceptionState.throwSecurityError( |
123 "sendBeacon() with a Blob whose type is not CORS-safelisted MIME " | 129 "sendBeacon() with a Blob whose type is not CORS-safelisted MIME " |
124 "type is disallowed experimentally. See http://crbug.com/490015 " | 130 "type is disallowed experimentally. See http://crbug.com/490015 " |
125 "for details."); | 131 "for details."); |
126 return false; | 132 return false; |
127 } | 133 } |
128 } | 134 } |
129 allowed = PingLoader::sendBeacon(impl.frame(), allowance, url, blob, bytes); | 135 allowed = |
136 PingLoader::sendBeacon(host()->frame(), allowance, url, blob, bytes); | |
130 } else if (data.isString()) { | 137 } else if (data.isString()) { |
131 allowed = PingLoader::sendBeacon(impl.frame(), allowance, url, | 138 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, |
132 data.getAsString(), bytes); | 139 data.getAsString(), bytes); |
133 } else if (data.isFormData()) { | 140 } else if (data.isFormData()) { |
134 allowed = PingLoader::sendBeacon(impl.frame(), allowance, url, | 141 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, |
135 data.getAsFormData(), bytes); | 142 data.getAsFormData(), bytes); |
136 } else { | 143 } else { |
137 allowed = | 144 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, String(), |
138 PingLoader::sendBeacon(impl.frame(), allowance, url, String(), bytes); | 145 bytes); |
139 } | 146 } |
140 | 147 |
141 if (allowed) { | 148 if (allowed) { |
142 impl.addTransmittedBytes(bytes); | 149 addTransmittedBytes(bytes); |
143 return true; | 150 return true; |
144 } | 151 } |
145 | 152 |
146 UseCounter::count(context, UseCounter::SendBeaconQuotaExceeded); | 153 UseCounter::count(context, UseCounter::SendBeaconQuotaExceeded); |
147 return false; | 154 return false; |
148 } | 155 } |
149 | 156 |
150 } // namespace blink | 157 } // namespace blink |
OLD | NEW |