Index: ash/content/accelerometer/sensor_manager_delegate_chromeos.cc |
diff --git a/ash/content/accelerometer/sensor_manager_delegate_chromeos.cc b/ash/content/accelerometer/sensor_manager_delegate_chromeos.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2b02297187d24078b448e0ca36b2cac3de2a9368 |
--- /dev/null |
+++ b/ash/content/accelerometer/sensor_manager_delegate_chromeos.cc |
@@ -0,0 +1,55 @@ |
+// 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 "ash/content/accelerometer/sensor_manager_delegate_chromeos.h" |
+ |
+#include "ash/shell.h" |
+#include "chromeos/accelerometer/accelerometer_reader.h" |
+#include "content/public/browser/sensor_manager_delegate_chromeos.h" |
+#include "ui/accelerometer/accelerometer_types.h" |
+ |
+namespace ash { |
+ |
+SensorManagerDelegateChromeOS::SensorManagerDelegateChromeOS() { |
+ content::SensorManagerDelegateChromeOS::SetDelegate(this); |
+} |
+ |
+SensorManagerDelegateChromeOS::~SensorManagerDelegateChromeOS() { |
+ // Ensure we have stopped observing when shutdown. |
+ StopFetchingDeviceOrientationData(); |
+ // When this class is being destructed, content::SensorManagerDelegateChromeOS |
+ // will have already been destructed. So calling SetDelegate(nullptr) to |
+ // remove this class as the delegate will cause a crash. |
+} |
+ |
+void SensorManagerDelegateChromeOS::OnAccelerometerUpdated( |
+ const ui::AccelerometerUpdate& update) { |
+ ui::AccelerometerSource source; |
+ |
+ if (update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) { |
+ source = ui::ACCELEROMETER_SOURCE_SCREEN; |
+ } else if (update.has(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) { |
+ source = ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; |
+ } else { |
+ return; |
+ } |
+ |
+ orientation_callback_.Run(update.get(source).x(), update.get(source).y(), |
+ update.get(source).z()); |
+} |
+ |
+void SensorManagerDelegateChromeOS::StartFetchingDeviceOrientationData( |
+ const content::OrientationDataCallback& callback) { |
+ DCHECK(orientation_callback_.is_null()); |
+ Shell::GetInstance()->accelerometer_reader()->AddObserver(this); |
+ orientation_callback_ = callback; |
+} |
+ |
+void SensorManagerDelegateChromeOS::StopFetchingDeviceOrientationData() { |
+ if (!orientation_callback_.is_null()) |
+ Shell::GetInstance()->accelerometer_reader()->RemoveObserver(this); |
+ orientation_callback_.Reset(); |
+} |
+ |
+} // namespace ash |