OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/sensor/SensorProxy.h" | 5 #include "modules/sensor/SensorProxy.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/frame/LocalFrame.h" | 8 #include "core/frame/LocalFrame.h" |
| 9 #include "modules/permissions/PermissionUtils.h" |
9 #include "modules/sensor/SensorProviderProxy.h" | 10 #include "modules/sensor/SensorProviderProxy.h" |
10 #include "modules/sensor/SensorReadingUpdater.h" | 11 #include "modules/sensor/SensorReadingUpdater.h" |
| 12 #include "platform/UserGestureIndicator.h" |
11 #include "platform/mojo/MojoHelper.h" | 13 #include "platform/mojo/MojoHelper.h" |
| 14 #include "platform/weborigin/SecurityOrigin.h" |
12 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
13 | 16 |
14 using namespace device::mojom::blink; | 17 using namespace device::mojom::blink; |
15 | 18 |
16 namespace blink { | 19 namespace blink { |
17 | 20 |
| 21 using mojom::blink::PermissionName; |
| 22 using mojom::blink::PermissionService; |
| 23 using mojom::blink::PermissionStatus; |
| 24 |
| 25 namespace { |
| 26 PermissionName GetPermissionType(SensorType type) { |
| 27 PermissionName permission_name = PermissionName::SENSORS; |
| 28 |
| 29 if (type == SensorType::AMBIENT_LIGHT) |
| 30 permission_name = PermissionName::AMBIENT_LIGHT_SENSOR; |
| 31 else if (type == SensorType::ACCELEROMETER) |
| 32 permission_name = PermissionName::ACCELEROMETER; |
| 33 else if (type == SensorType::GYROSCOPE) |
| 34 permission_name = PermissionName::GYROSCOPE; |
| 35 else if (type == SensorType::MAGNETOMETER) |
| 36 permission_name = PermissionName::MAGNETOMETER; |
| 37 else if (type == SensorType::ABSOLUTE_ORIENTATION) |
| 38 permission_name = PermissionName::ORIENTATION_SENSOR; |
| 39 |
| 40 return permission_name; |
| 41 } |
| 42 } |
| 43 |
18 SensorProxy::SensorProxy(SensorType sensor_type, | 44 SensorProxy::SensorProxy(SensorType sensor_type, |
| 45 RefPtr<SecurityOrigin> origin, |
19 SensorProviderProxy* provider, | 46 SensorProviderProxy* provider, |
20 Page* page) | 47 Page* page) |
21 : PageVisibilityObserver(page), | 48 : PageVisibilityObserver(page), |
22 type_(sensor_type), | 49 type_(sensor_type), |
23 mode_(ReportingMode::CONTINUOUS), | 50 mode_(ReportingMode::CONTINUOUS), |
24 provider_(provider), | 51 provider_(provider), |
25 client_binding_(this), | 52 client_binding_(this), |
26 state_(SensorProxy::kUninitialized), | 53 state_(SensorProxy::kUninitialized), |
27 suspended_(false) {} | 54 suspended_(false), |
| 55 security_origin_(std::move(origin)), |
| 56 binding_(this), |
| 57 permission_subscribed_(false) {} |
| 58 |
| 59 void SensorProxy::OnPermissionStatusChange(PermissionStatus status) { |
| 60 if (state_ == kUninitialized) |
| 61 return; |
| 62 |
| 63 // PermissionName permission_name = GetPermissionType(type_); |
| 64 |
| 65 switch (status) { |
| 66 case mojom::blink::PermissionStatus::DENIED: |
| 67 HandleSensorError(); |
| 68 // HandleSensorError(NotAllowedError, "Permission denied."); |
| 69 return; |
| 70 case mojom::blink::PermissionStatus::ASK: |
| 71 HandleSensorError(); |
| 72 // HandleSensorError(NotAllowedError, "Permission revoked."); |
| 73 return; |
| 74 case mojom::blink::PermissionStatus::GRANTED: |
| 75 if (IsInitializing()) { |
| 76 if (!permission_subscribed_) { |
| 77 DCHECK(!binding_.is_bound()); |
| 78 mojom::blink::PermissionObserverPtr observer; |
| 79 binding_.Bind(mojo::MakeRequest(&observer)); |
| 80 provider_->permission_service_->AddPermissionObserver( |
| 81 CreatePermissionDescriptor(PermissionName::SENSORS), |
| 82 security_origin_, status, std::move(observer)); |
| 83 permission_subscribed_ = true; |
| 84 } |
| 85 state_ = kInitialized; |
| 86 for (Observer* observer : observers_) |
| 87 observer->OnSensorInitialized(); |
| 88 } |
| 89 break; |
| 90 default: |
| 91 NOTREACHED(); |
| 92 } |
| 93 } |
28 | 94 |
29 SensorProxy::~SensorProxy() {} | 95 SensorProxy::~SensorProxy() {} |
30 | 96 |
31 void SensorProxy::Dispose() { | 97 void SensorProxy::Dispose() { |
32 client_binding_.Close(); | 98 client_binding_.Close(); |
33 } | 99 } |
34 | 100 |
35 DEFINE_TRACE(SensorProxy) { | 101 DEFINE_TRACE(SensorProxy) { |
36 visitor->Trace(reading_updater_); | 102 visitor->Trace(reading_updater_); |
37 visitor->Trace(observers_); | 103 visitor->Trace(observers_); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 SensorConfiguration::kMaxAllowedFrequency; | 289 SensorConfiguration::kMaxAllowedFrequency; |
224 DCHECK_GE(kMaxAllowedFrequency, frequency_limits_.second); | 290 DCHECK_GE(kMaxAllowedFrequency, frequency_limits_.second); |
225 | 291 |
226 auto error_callback = | 292 auto error_callback = |
227 WTF::Bind(&SensorProxy::HandleSensorError, WrapWeakPersistent(this)); | 293 WTF::Bind(&SensorProxy::HandleSensorError, WrapWeakPersistent(this)); |
228 sensor_.set_connection_error_handler( | 294 sensor_.set_connection_error_handler( |
229 ConvertToBaseCallback(std::move(error_callback))); | 295 ConvertToBaseCallback(std::move(error_callback))); |
230 | 296 |
231 reading_updater_ = SensorReadingUpdater::Create(this, mode_); | 297 reading_updater_ = SensorReadingUpdater::Create(this, mode_); |
232 | 298 |
233 state_ = kInitialized; | |
234 | |
235 for (Observer* observer : observers_) | 299 for (Observer* observer : observers_) |
236 observer->OnSensorInitialized(); | 300 observer->OnSensorInitialized(); |
| 301 |
| 302 // PermissionName permission_name = GetPermissionType(type_); |
| 303 |
| 304 provider_->permission_service_->RequestPermission( |
| 305 CreatePermissionDescriptor(PermissionName::SENSORS), security_origin_, |
| 306 UserGestureIndicator::ProcessingUserGesture(), |
| 307 ConvertToBaseCallback(WTF::Bind(&SensorProxy::OnPermissionStatusChange, |
| 308 WrapWeakPersistent(this)))); |
237 } | 309 } |
238 | 310 |
239 void SensorProxy::OnAddConfigurationCompleted( | 311 void SensorProxy::OnAddConfigurationCompleted( |
240 double frequency, | 312 double frequency, |
241 std::unique_ptr<Function<void(bool)>> callback, | 313 std::unique_ptr<Function<void(bool)>> callback, |
242 bool result) { | 314 bool result) { |
243 if (result) { | 315 if (result) { |
244 frequencies_used_.push_back(frequency); | 316 frequencies_used_.push_back(frequency); |
245 std::sort(frequencies_used_.begin(), frequencies_used_.end()); | 317 std::sort(frequencies_used_.begin(), frequencies_used_.end()); |
246 if (IsActive()) | 318 if (IsActive()) |
(...skipping 24 matching lines...) Expand all Loading... |
271 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | 343 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
272 auto version = seqlock.ReadBegin(); | 344 auto version = seqlock.ReadBegin(); |
273 auto reading_data = buffer->reading; | 345 auto reading_data = buffer->reading; |
274 if (seqlock.ReadRetry(version)) | 346 if (seqlock.ReadRetry(version)) |
275 return false; | 347 return false; |
276 result = reading_data; | 348 result = reading_data; |
277 return true; | 349 return true; |
278 } | 350 } |
279 | 351 |
280 } // namespace blink | 352 } // namespace blink |
OLD | NEW |