Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Unified Diff: third_party/WebKit/Source/modules/sensor/Sensor.cpp

Issue 2353493002: [Sensors] Allow Sensor API only on secure top-level browsing contexts and add frequency checks (Closed)
Patch Set: Review comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/sensor/Sensor.cpp
diff --git a/third_party/WebKit/Source/modules/sensor/Sensor.cpp b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
index 872afd786fdd7de5f4333a06813c33abd1eb43ad..716713c93032958ce442a110052e43f0a7b8632b 100644
--- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp
+++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
@@ -17,15 +17,40 @@ using namespace device::mojom::blink;
namespace blink {
-Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOptions, SensorType type)
+Sensor::Sensor(ScriptState* scriptState, const SensorOptions& sensorOptions, ExceptionState& exceptionState, SensorType type)
: ActiveScriptWrappable(this)
- , ContextLifecycleObserver(executionContext)
- , PageVisibilityObserver(toDocument(executionContext)->page())
+ , ContextLifecycleObserver(scriptState->getExecutionContext())
+ , PageVisibilityObserver(toDocument(scriptState->getExecutionContext())->page())
, m_sensorOptions(sensorOptions)
, m_type(type)
, m_state(Sensor::SensorState::IDLE)
, m_storedData()
{
+ // Check secure context.
+ String errorMessage;
+ if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) {
+ exceptionState.throwDOMException(SecurityError, errorMessage);
+ return;
+ }
+
+ // Check top-level browsing context.
+ if (!scriptState->domWindow() || !scriptState->domWindow()->frame() || !scriptState->domWindow()->frame()->isMainFrame()) {
+ exceptionState.throwSecurityError("Must be in a top-level browsing context");
+ return;
+ }
+
+ // Check the given frequency value.
+ if (m_sensorOptions.hasFrequency()) {
+ double frequency = m_sensorOptions.frequency();
+ if (frequency <= 0.0) {
+ exceptionState.throwRangeError("Frequency must be positive.");
+ return;
+ }
+ // Cap to 60.0 Hz
+ const double kMaxAllowedFrequency = 60.0;
timvolodine 2016/09/19 16:52:13 think this should be a more 'global' constant some
Mikhail 2016/09/20 13:29:37 Done.
+ if (frequency > kMaxAllowedFrequency)
+ m_sensorOptions.setFrequency(kMaxAllowedFrequency);
+ }
timvolodine 2016/09/19 16:52:13 don't know what the spec says here, maybe output s
Mikhail 2016/09/20 13:29:37 Done.
}
Sensor::~Sensor() = default;

Powered by Google App Engine
This is Rietveld 408576698