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

Unified Diff: content/public/browser/sensor_manager.cc

Issue 680383007: DeviceOrientation API on ChromeOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Isolate Chrome OS changes Created 6 years, 1 month 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: content/public/browser/sensor_manager.cc
diff --git a/content/public/browser/sensor_manager.cc b/content/public/browser/sensor_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6eee7edf21dd336574836fda3ee475c88784d1f3
--- /dev/null
+++ b/content/public/browser/sensor_manager.cc
@@ -0,0 +1,88 @@
+// 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/public/browser/sensor_manager.h"
+
+#include "base/logging.h"
+#include "base/memory/singleton.h"
+
+namespace {
+// Conversion ratio from radians to degrees.
+const double kRad2deg = 180.0 / M_PI;
+}
+
+namespace content {
+
+SensorManager* SensorManager::GetInstance() {
+ return Singleton<SensorManager, LeakySingletonTraits<SensorManager>>::get();
+}
+
+void SensorManager::OnAccelerationIncludingGravity(double x,
timvolodine 2014/11/07 03:27:39 you could probably also implement Device Motion in
jonross 2014/11/07 19:59:37 I was planning to use this patch to settle on how
+ double y,
+ double z) {
+ base::AutoLock autolock(orientation_buffer_lock_);
+ if (!orientation_buffer_)
+ return;
+
+ // Create a unit vector to remove gravity.
+ // TODO(jonross): Stop reversing signs for vector components once
+ // accelerometer values have been reversed.
flackr 2014/11/07 19:30:46 Can you file a bug for this?
jonross 2014/11/07 19:59:37 crbug.com/431391 filed
+ 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());
+
+ // Have the interval boundaries comply with 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;
+
+ 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();
+}
+
+void SensorManager::StartFetchingDeviceOrientationData(
+ DeviceOrientationHardwareBuffer* buffer) {
+ DCHECK(buffer);
+ 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();
+}
+
+void SensorManager::StopFetchingDeviceOrientationData() {
+ base::AutoLock autolock(orientation_buffer_lock_);
+ if (!orientation_buffer_)
+ return;
+ // Null message to signify that sensors are no longer available.
+ orientation_buffer_->seqlock.WriteBegin();
+ orientation_buffer_->data.allAvailableSensorsAreActive = false;
+ orientation_buffer_->seqlock.WriteEnd();
+ orientation_buffer_ = nullptr;
+}
+
+SensorManager::SensorManager() : orientation_buffer_(nullptr) {
+}
+
+SensorManager::~SensorManager() {
+}
+
+} // namespace content
« content/public/browser/sensor_manager.h ('K') | « content/public/browser/sensor_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698