Index: content/browser/device_sensors/sensor_manager_chromeos.cc |
diff --git a/content/browser/device_sensors/sensor_manager_chromeos.cc b/content/browser/device_sensors/sensor_manager_chromeos.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..63f73800c3d2dbc15ffe2ca9c4d3ffcdda6d5f5a |
--- /dev/null |
+++ b/content/browser/device_sensors/sensor_manager_chromeos.cc |
@@ -0,0 +1,118 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/device_sensors/sensor_manager_chromeos.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/memory/singleton.h" |
+#include "content/public/browser/sensor_manager_delegate_chromeos.h" |
+#include "ui/gfx/geometry/vector3d_f.h" |
+ |
+namespace { |
+// Conversion ratio from radians to degrees. |
+const double kRad2deg = 180.0 / M_PI; |
+} |
+ |
+namespace content { |
+ |
+SensorManagerChromeOS* SensorManagerChromeOS::GetInstance() { |
+ return Singleton<SensorManagerChromeOS, |
+ LeakySingletonTraits<SensorManagerChromeOS>>::get(); |
+} |
+ |
+void SensorManagerChromeOS::SetDelegate( |
+ SensorManagerDelegateChromeOS* delegate) { |
+ delegate_ = delegate; |
+} |
+ |
+bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( |
+ DeviceOrientationHardwareBuffer* buffer) { |
+ DCHECK(buffer); |
+ if (!delegate_) |
+ return false; |
+ |
+ { |
+ // Lock must be released before providing the callback to prevent potential |
+ // deadlock. |
+ base::AutoLock autolock(orientation_buffer_lock_); |
+ orientation_buffer_ = buffer; |
+ |
+ // No compass information, so we cannot provide absolute orientation. |
+ orientation_buffer_->seqlock.WriteBegin(); |
+ orientation_buffer_->data.absolute = false; |
+ orientation_buffer_->data.hasAbsolute = true; |
+ orientation_buffer_->seqlock.WriteEnd(); |
+ } |
+ |
+ delegate_->StartFetchingDeviceOrientationData(callback_); |
+ |
+ return true; |
+} |
+ |
+void SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
+ if (!delegate_) |
+ return; |
+ delegate_->StopFetchingDeviceOrientationData(); |
+ base::AutoLock autolock(orientation_buffer_lock_); |
+ if (!orientation_buffer_) |
+ return; |
+ // Make sure to indicate that the sensor data is no longer available. |
+ orientation_buffer_->seqlock.WriteBegin(); |
+ orientation_buffer_->data.allAvailableSensorsAreActive = false; |
+ orientation_buffer_->seqlock.WriteEnd(); |
+ orientation_buffer_ = nullptr; |
+} |
+ |
+SensorManagerChromeOS::~SensorManagerChromeOS() { |
+} |
+ |
+SensorManagerChromeOS::SensorManagerChromeOS() |
+ : callback_( |
+ base::Bind(&SensorManagerChromeOS::OnAccelerationIncludingGravity, |
+ base::Unretained(this))), |
+ orientation_buffer_(nullptr), |
+ delegate_(nullptr) { |
+} |
+ |
+void SensorManagerChromeOS::OnAccelerationIncludingGravity(double x, |
+ double y, |
+ double z) { |
+ base::AutoLock autolock(orientation_buffer_lock_); |
+ if (!orientation_buffer_) |
+ return; |
+ // Create a unit vector for trigonometry |
+ // TODO(jonross): Stop reversing signs for vector components once |
+ // accelerometer values have been fixed. crbug.com/431391 |
+ // Ternaries are to remove -0.0f which gives incorrect trigonometrical |
+ // results. |
+ gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
+ data.Scale(1.0f / data.Length()); |
+ |
+ // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
+ // x = sin(gamma) |
+ // y = -cos(gamma) * sin(beta) |
+ // z = cos(beta) * cos(gamma) |
+ // With only accelerometer alpha cannot be provided. |
+ double beta = kRad2deg * atan2(data.y(), data.z()); |
+ double gamma = kRad2deg * asin(data.x()); |
+ |
+ // Convert beta and gamma to fit the intervals in the specification. Beta is |
+ // [-180, 180) and gamma is [-90, 90). |
+ if (beta >= 180.0f) |
+ beta = -180.0f; |
+ if (gamma >= 90.0f) |
+ gamma = -90.0f; |
+ // TODO: move chromeos/accelerometer to /content/browser/device_sensors/ and |
+ // have it generate the beta and gamma values. crbug.com/431865 |
+ orientation_buffer_->seqlock.WriteBegin(); |
+ orientation_buffer_->data.beta = beta; |
+ orientation_buffer_->data.hasBeta = true; |
+ orientation_buffer_->data.gamma = gamma; |
+ orientation_buffer_->data.hasGamma = true; |
+ orientation_buffer_->data.allAvailableSensorsAreActive = true; |
+ orientation_buffer_->seqlock.WriteEnd(); |
+} |
+ |
+} // namespace content |