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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 return false; | 58 return false; |
59 } | 59 } |
60 | 60 |
61 // If detached from frame, do not allow sending a Beacon. | 61 // If detached from frame, do not allow sending a Beacon. |
62 if (!supplementable()->frame()) | 62 if (!supplementable()->frame()) |
63 return false; | 63 return false; |
64 | 64 |
65 return true; | 65 return true; |
66 } | 66 } |
67 | 67 |
| 68 // Determine the remaining size allowance for Beacon transmissions. |
| 69 // If (-1) is returned, a no limit policy is in place, otherwise |
| 70 // it is the max size (in bytes) of a beacon request. |
| 71 // |
| 72 // The loader takes the allowance into account once the Beacon |
| 73 // payload size has been determined, deciding if the transmission |
| 74 // will be allowed to go ahead or not. |
68 int NavigatorBeacon::maxAllowance() const { | 75 int NavigatorBeacon::maxAllowance() const { |
69 DCHECK(supplementable()->frame()); | 76 DCHECK(supplementable()->frame()); |
70 const Settings* settings = supplementable()->frame()->settings(); | 77 const Settings* settings = supplementable()->frame()->settings(); |
71 if (settings) { | 78 if (settings) { |
72 int maxAllowed = settings->getMaxBeaconTransmission(); | 79 int maxAllowed = settings->getMaxBeaconTransmission(); |
73 if (maxAllowed < m_transmittedBytes) | 80 // Any negative value represent no max limit. |
| 81 if (maxAllowed < 0) |
| 82 return -1; |
| 83 if (static_cast<size_t>(maxAllowed) <= m_transmittedBytes) |
74 return 0; | 84 return 0; |
75 return maxAllowed - m_transmittedBytes; | 85 return maxAllowed - static_cast<int>(m_transmittedBytes); |
76 } | 86 } |
77 return m_transmittedBytes; | 87 return -1; |
78 } | 88 } |
79 | 89 |
80 void NavigatorBeacon::addTransmittedBytes(int sentBytes) { | 90 void NavigatorBeacon::addTransmittedBytes(size_t sentBytes) { |
81 DCHECK_GE(sentBytes, 0); | |
82 m_transmittedBytes += sentBytes; | 91 m_transmittedBytes += sentBytes; |
83 } | 92 } |
84 | 93 |
85 bool NavigatorBeacon::sendBeacon( | 94 bool NavigatorBeacon::sendBeacon( |
86 ScriptState* scriptState, | 95 ScriptState* scriptState, |
87 Navigator& navigator, | 96 Navigator& navigator, |
88 const String& urlstring, | 97 const String& urlstring, |
89 const ArrayBufferViewOrBlobOrStringOrFormData& data, | 98 const ArrayBufferViewOrBlobOrStringOrFormData& data, |
90 ExceptionState& exceptionState) { | 99 ExceptionState& exceptionState) { |
91 return NavigatorBeacon::from(navigator).sendBeaconImpl(scriptState, urlstring, | 100 return NavigatorBeacon::from(navigator).sendBeaconImpl(scriptState, urlstring, |
92 data, exceptionState); | 101 data, exceptionState); |
93 } | 102 } |
94 | 103 |
95 bool NavigatorBeacon::sendBeaconImpl( | 104 bool NavigatorBeacon::sendBeaconImpl( |
96 ScriptState* scriptState, | 105 ScriptState* scriptState, |
97 const String& urlstring, | 106 const String& urlstring, |
98 const ArrayBufferViewOrBlobOrStringOrFormData& data, | 107 const ArrayBufferViewOrBlobOrStringOrFormData& data, |
99 ExceptionState& exceptionState) { | 108 ExceptionState& exceptionState) { |
100 ExecutionContext* context = scriptState->getExecutionContext(); | 109 ExecutionContext* context = scriptState->getExecutionContext(); |
101 KURL url = context->completeURL(urlstring); | 110 KURL url = context->completeURL(urlstring); |
102 if (!canSendBeacon(context, url, exceptionState)) | 111 if (!canSendBeacon(context, url, exceptionState)) |
103 return false; | 112 return false; |
104 | 113 |
105 int allowance = maxAllowance(); | 114 int allowance = maxAllowance(); |
106 int bytes = 0; | 115 size_t beaconSize = 0; |
107 bool allowed; | 116 bool allowed; |
108 | 117 |
109 if (data.isArrayBufferView()) { | 118 if (data.isArrayBufferView()) { |
110 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, | 119 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
111 data.getAsArrayBufferView(), bytes); | 120 data.getAsArrayBufferView(), beaconSize); |
112 } else if (data.isBlob()) { | 121 } else if (data.isBlob()) { |
113 Blob* blob = data.getAsBlob(); | 122 Blob* blob = data.getAsBlob(); |
114 if (!FetchUtils::isSimpleContentType(AtomicString(blob->type()))) { | 123 if (!FetchUtils::isSimpleContentType(AtomicString(blob->type()))) { |
115 UseCounter::count(context, | 124 UseCounter::count(context, |
116 UseCounter::SendBeaconWithNonSimpleContentType); | 125 UseCounter::SendBeaconWithNonSimpleContentType); |
117 if (RuntimeEnabledFeatures:: | 126 if (RuntimeEnabledFeatures:: |
118 sendBeaconThrowForBlobWithNonSimpleTypeEnabled()) { | 127 sendBeaconThrowForBlobWithNonSimpleTypeEnabled()) { |
119 exceptionState.throwSecurityError( | 128 exceptionState.throwSecurityError( |
120 "sendBeacon() with a Blob whose type is not CORS-safelisted MIME " | 129 "sendBeacon() with a Blob whose type is not CORS-safelisted MIME " |
121 "type is disallowed experimentally. See http://crbug.com/490015 " | 130 "type is disallowed experimentally. See http://crbug.com/490015 " |
122 "for details."); | 131 "for details."); |
123 return false; | 132 return false; |
124 } | 133 } |
125 } | 134 } |
126 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, | 135 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
127 blob, bytes); | 136 blob, beaconSize); |
128 } else if (data.isString()) { | 137 } else if (data.isString()) { |
129 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, | 138 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
130 data.getAsString(), bytes); | 139 data.getAsString(), beaconSize); |
131 } else if (data.isFormData()) { | 140 } else if (data.isFormData()) { |
132 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, | 141 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
133 data.getAsFormData(), bytes); | 142 data.getAsFormData(), beaconSize); |
134 } else { | 143 } else { |
135 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, | 144 allowed = PingLoader::sendBeacon(supplementable()->frame(), allowance, url, |
136 String(), bytes); | 145 String(), beaconSize); |
137 } | 146 } |
138 | 147 |
139 if (allowed) { | 148 if (!allowed) { |
140 addTransmittedBytes(bytes); | 149 UseCounter::count(context, UseCounter::SendBeaconQuotaExceeded); |
141 return true; | 150 return false; |
142 } | 151 } |
143 | 152 |
144 UseCounter::count(context, UseCounter::SendBeaconQuotaExceeded); | 153 // Only accumulate transmission size if a limit is imposed. |
145 return false; | 154 if (allowance >= 0) |
| 155 addTransmittedBytes(beaconSize); |
| 156 return true; |
146 } | 157 } |
147 | 158 |
148 } // namespace blink | 159 } // namespace blink |
OLD | NEW |