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

Side by Side Diff: device/sensor/sensor_service.cc

Issue 1892083002: Generic Sensor API : Ambient Light Sensor API. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re entrancy fix Created 4 years, 7 months 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 unified diff | Download patch
OLDNEW
(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 std::unique_ptr<SensorService::SensorUpdateSubscription>
35 SensorService::AddCallback(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698