OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/content/accelerometer/sensor_manager_delegate_chromeos.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "chromeos/accelerometer/accelerometer_reader.h" |
| 9 #include "content/public/browser/sensor_manager_delegate_chromeos.h" |
| 10 #include "ui/accelerometer/accelerometer_types.h" |
| 11 |
| 12 namespace ash { |
| 13 |
| 14 SensorManagerDelegateChromeOS::SensorManagerDelegateChromeOS() { |
| 15 content::SensorManagerDelegateChromeOS::SetDelegate(this); |
| 16 } |
| 17 |
| 18 SensorManagerDelegateChromeOS::~SensorManagerDelegateChromeOS() { |
| 19 // Ensure we have stopped observing when shutdown. |
| 20 StopFetchingDeviceOrientationData(); |
| 21 // When this class is being destructed, content::SensorManagerDelegateChromeOS |
| 22 // will have already been destructed. So calling SetDelegate(nullptr) to |
| 23 // remove this class as the delegate will cause a crash. |
| 24 } |
| 25 |
| 26 void SensorManagerDelegateChromeOS::OnAccelerometerUpdated( |
| 27 const ui::AccelerometerUpdate& update) { |
| 28 ui::AccelerometerSource source; |
| 29 |
| 30 if (update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) { |
| 31 source = ui::ACCELEROMETER_SOURCE_SCREEN; |
| 32 } else if (update.has(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) { |
| 33 source = ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; |
| 34 } else { |
| 35 return; |
| 36 } |
| 37 |
| 38 orientation_callback_.Run(update.get(source).x(), update.get(source).y(), |
| 39 update.get(source).z()); |
| 40 } |
| 41 |
| 42 void SensorManagerDelegateChromeOS::StartFetchingDeviceOrientationData( |
| 43 const content::OrientationDataCallback& callback) { |
| 44 DCHECK(orientation_callback_.is_null()); |
| 45 Shell::GetInstance()->accelerometer_reader()->AddObserver(this); |
| 46 orientation_callback_ = callback; |
| 47 } |
| 48 |
| 49 void SensorManagerDelegateChromeOS::StopFetchingDeviceOrientationData() { |
| 50 if (!orientation_callback_.is_null()) |
| 51 Shell::GetInstance()->accelerometer_reader()->RemoveObserver(this); |
| 52 orientation_callback_.Reset(); |
| 53 } |
| 54 |
| 55 } // namespace ash |
OLD | NEW |