| 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" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 !context->contentSecurityPolicy()->allowConnectToSource(url)) { | 63 !context->contentSecurityPolicy()->allowConnectToSource(url)) { |
| 64 // 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 |
| 65 // synchronously before redirection. JavaScript receives no new information. | 65 // synchronously before redirection. JavaScript receives no new information. |
| 66 exceptionState.throwSecurityError( | 66 exceptionState.throwSecurityError( |
| 67 "Refused to send beacon to '" + url.elidedString() + | 67 "Refused to send beacon to '" + url.elidedString() + |
| 68 "' because it violates the document's Content Security Policy."); | 68 "' because it violates the document's Content Security Policy."); |
| 69 return false; | 69 return false; |
| 70 } | 70 } |
| 71 | 71 |
| 72 // If detached from frame, do not allow sending a Beacon. | 72 // If detached from frame, do not allow sending a Beacon. |
| 73 if (!host()->frame()) | 73 if (!supplementable()->frame()) |
| 74 return false; | 74 return false; |
| 75 | 75 |
| 76 return true; | 76 return true; |
| 77 } | 77 } |
| 78 | 78 |
| 79 int NavigatorBeacon::maxAllowance() const { | 79 int NavigatorBeacon::maxAllowance() const { |
| 80 DCHECK(host()->frame()); | 80 DCHECK(supplementable()->frame()); |
| 81 const Settings* settings = host()->frame()->settings(); | 81 const Settings* settings = supplementable()->frame()->settings(); |
| 82 if (settings) { | 82 if (settings) { |
| 83 int maxAllowed = settings->getMaxBeaconTransmission(); | 83 int maxAllowed = settings->getMaxBeaconTransmission(); |
| 84 if (maxAllowed < m_transmittedBytes) | 84 if (maxAllowed < m_transmittedBytes) |
| 85 return 0; | 85 return 0; |
| 86 return maxAllowed - m_transmittedBytes; | 86 return maxAllowed - m_transmittedBytes; |
| 87 } | 87 } |
| 88 return m_transmittedBytes; | 88 return m_transmittedBytes; |
| 89 } | 89 } |
| 90 | 90 |
| 91 void NavigatorBeacon::addTransmittedBytes(int sentBytes) { | 91 void NavigatorBeacon::addTransmittedBytes(int sentBytes) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 111 ExecutionContext* context = scriptState->getExecutionContext(); | 111 ExecutionContext* context = scriptState->getExecutionContext(); |
| 112 KURL url = context->completeURL(urlstring); | 112 KURL url = context->completeURL(urlstring); |
| 113 if (!canSendBeacon(context, url, exceptionState)) | 113 if (!canSendBeacon(context, url, exceptionState)) |
| 114 return false; | 114 return false; |
| 115 | 115 |
| 116 int allowance = maxAllowance(); | 116 int allowance = maxAllowance(); |
| 117 int bytes = 0; | 117 int bytes = 0; |
| 118 bool allowed; | 118 bool allowed; |
| 119 | 119 |
| 120 if (data.isArrayBufferView()) { | 120 if (data.isArrayBufferView()) { |
| 121 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, | 121 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
| 122 data.getAsArrayBufferView(), bytes); | 122 data.getAsArrayBufferView(), bytes); |
| 123 } else if (data.isBlob()) { | 123 } else if (data.isBlob()) { |
| 124 Blob* blob = data.getAsBlob(); | 124 Blob* blob = data.getAsBlob(); |
| 125 if (!FetchUtils::isSimpleContentType(AtomicString(blob->type()))) { | 125 if (!FetchUtils::isSimpleContentType(AtomicString(blob->type()))) { |
| 126 UseCounter::count(context, | 126 UseCounter::count(context, |
| 127 UseCounter::SendBeaconWithNonSimpleContentType); | 127 UseCounter::SendBeaconWithNonSimpleContentType); |
| 128 if (RuntimeEnabledFeatures:: | 128 if (RuntimeEnabledFeatures:: |
| 129 sendBeaconThrowForBlobWithNonSimpleTypeEnabled()) { | 129 sendBeaconThrowForBlobWithNonSimpleTypeEnabled()) { |
| 130 exceptionState.throwSecurityError( | 130 exceptionState.throwSecurityError( |
| 131 "sendBeacon() with a Blob whose type is not CORS-safelisted MIME " | 131 "sendBeacon() with a Blob whose type is not CORS-safelisted MIME " |
| 132 "type is disallowed experimentally. See http://crbug.com/490015 " | 132 "type is disallowed experimentally. See http://crbug.com/490015 " |
| 133 "for details."); | 133 "for details."); |
| 134 return false; | 134 return false; |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 allowed = | 137 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
| 138 PingLoader::sendBeacon(host()->frame(), allowance, url, blob, bytes); | 138 blob, bytes); |
| 139 } else if (data.isString()) { | 139 } else if (data.isString()) { |
| 140 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, | 140 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
| 141 data.getAsString(), bytes); | 141 data.getAsString(), bytes); |
| 142 } else if (data.isFormData()) { | 142 } else if (data.isFormData()) { |
| 143 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, | 143 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
| 144 data.getAsFormData(), bytes); | 144 data.getAsFormData(), bytes); |
| 145 } else { | 145 } else { |
| 146 allowed = PingLoader::sendBeacon(host()->frame(), allowance, url, String(), | 146 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
| 147 bytes); | 147 String(), bytes); |
| 148 } | 148 } |
| 149 | 149 |
| 150 if (allowed) { | 150 if (allowed) { |
| 151 addTransmittedBytes(bytes); | 151 addTransmittedBytes(bytes); |
| 152 return true; | 152 return true; |
| 153 } | 153 } |
| 154 | 154 |
| 155 UseCounter::count(context, UseCounter::SendBeaconQuotaExceeded); | 155 UseCounter::count(context, UseCounter::SendBeaconQuotaExceeded); |
| 156 return false; | 156 return false; |
| 157 } | 157 } |
| 158 | 158 |
| 159 } // namespace blink | 159 } // namespace blink |
| OLD | NEW |