OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/sensor/sensor_service.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/single_thread_task_runner.h" |
| 12 #include "device/sensor/sensor_impl.h" |
| 13 #include "device/sensor/sensor_manager.h" |
| 14 |
| 15 namespace device { |
| 16 |
| 17 SensorService::SensorService() |
| 18 : main_thread_task_runner_(base::MessageLoop::current()->task_runner()), |
| 19 update_callback_( |
| 20 base::Bind(&SensorService::NotifyConsumers, base::Unretained(this))), |
| 21 reading_updated_(false), |
| 22 is_shutdown_(false) { |
| 23 callback_list_.set_removal_callback( |
| 24 base::Bind(&SensorService::ConsumersChanged, base::Unretained(this))); |
| 25 } |
| 26 |
| 27 SensorService::~SensorService() {} |
| 28 |
| 29 SensorService* SensorService::GetInstance() { |
| 30 return base::Singleton<SensorService, |
| 31 base::LeakySingletonTraits<SensorService>>::get(); |
| 32 } |
| 33 |
| 34 scoped_ptr<SensorService::SensorUpdateSubscription> SensorService::AddCallback( |
| 35 const SensorUpdateCallback& callback) { |
| 36 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 37 DCHECK(!is_shutdown_); |
| 38 |
| 39 if (!sensor_fetcher_) |
| 40 sensor_fetcher_ = SensorManager::Create(update_callback_); |
| 41 |
| 42 if (callback_list_.empty()) { |
| 43 bool success = sensor_fetcher_->StartListeningSensorChange(); |
| 44 // On failure pass the default values back. |
| 45 if (!success) |
| 46 callback.Run(AmbientLightSensorReading()); |
| 47 } |
| 48 |
| 49 if (reading_updated_) { |
| 50 // Send recent reading to the new callback if already available. |
| 51 callback.Run(reading_); |
| 52 } |
| 53 |
| 54 return callback_list_.Add(callback); |
| 55 } |
| 56 |
| 57 void SensorService::ConsumersChanged() { |
| 58 if (is_shutdown_) |
| 59 return; |
| 60 |
| 61 if (callback_list_.empty()) { |
| 62 sensor_fetcher_->StopListeningSensorChange(); |
| 63 reading_updated_ = false; |
| 64 } |
| 65 } |
| 66 |
| 67 void SensorService::NotifyConsumers(const AmbientLightSensorReading& reading) { |
| 68 DCHECK(!is_shutdown_); |
| 69 |
| 70 main_thread_task_runner_->PostTask( |
| 71 FROM_HERE, base::Bind(&SensorService::NotifyConsumersOnMainThread, |
| 72 base::Unretained(this), reading)); |
| 73 } |
| 74 |
| 75 void SensorService::NotifyConsumersOnMainThread( |
| 76 const AmbientLightSensorReading& reading) { |
| 77 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 78 if (callback_list_.empty()) |
| 79 return; |
| 80 |
| 81 reading_ = reading; |
| 82 reading_updated_ = true; |
| 83 callback_list_.Notify(reading_); |
| 84 } |
| 85 |
| 86 void SensorService::Shutdown() { |
| 87 if (!callback_list_.empty()) |
| 88 sensor_fetcher_->StopListeningSensorChange(); |
| 89 sensor_fetcher_.reset(); |
| 90 is_shutdown_ = true; |
| 91 } |
| 92 |
| 93 } // namespace device |
OLD | NEW |