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