OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/battery_status/battery_status_service.h" | 5 #include "device/battery/battery_status_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/browser/battery_status/battery_status_manager.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "device/battery/battery_monitor_impl.h" | |
11 #include "device/battery/battery_status_manager.h" | |
10 | 12 |
11 namespace content { | 13 namespace device { |
12 | 14 |
13 BatteryStatusService::BatteryStatusService() | 15 BatteryStatusService::BatteryStatusService() |
14 : update_callback_(base::Bind(&BatteryStatusService::UpdateBatteryStatus, | 16 : main_thread_task_runner_(base::MessageLoop::current()->task_runner()), |
17 update_callback_(base::Bind(&BatteryStatusService::NotifyConsumers, | |
15 base::Unretained(this))), | 18 base::Unretained(this))), |
16 status_updated_(false), | 19 status_updated_(false), |
17 is_shutdown_(false) { | 20 is_shutdown_(false) { |
18 callback_list_.set_removal_callback( | 21 callback_list_.set_removal_callback( |
19 base::Bind(&BatteryStatusService::ConsumersChanged, | 22 base::Bind(&BatteryStatusService::ConsumersChanged, |
20 base::Unretained(this))); | 23 base::Unretained(this))); |
21 } | 24 } |
22 | 25 |
23 BatteryStatusService::~BatteryStatusService() { | 26 BatteryStatusService::~BatteryStatusService() { |
24 } | 27 } |
25 | 28 |
26 BatteryStatusService* BatteryStatusService::GetInstance() { | 29 BatteryStatusService* BatteryStatusService::GetInstance() { |
27 return Singleton<BatteryStatusService, | 30 return Singleton<BatteryStatusService, |
28 LeakySingletonTraits<BatteryStatusService> >::get(); | 31 LeakySingletonTraits<BatteryStatusService> >::get(); |
29 } | 32 } |
30 | 33 |
31 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> | 34 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> |
32 BatteryStatusService::AddCallback(const BatteryUpdateCallback& callback) { | 35 BatteryStatusService::AddCallback(const BatteryUpdateCallback& callback) { |
timvolodine
2014/09/30 16:26:01
how about adding a check that this only runs on th
ppi
2014/10/06 13:50:02
Done.
| |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
34 DCHECK(!is_shutdown_); | 36 DCHECK(!is_shutdown_); |
35 | 37 |
36 if (!battery_fetcher_) | 38 if (!battery_fetcher_) |
37 battery_fetcher_ = BatteryStatusManager::Create(update_callback_); | 39 battery_fetcher_ = BatteryStatusManager::Create(update_callback_); |
38 | 40 |
39 if (callback_list_.empty()) { | 41 if (callback_list_.empty()) { |
40 bool success = battery_fetcher_->StartListeningBatteryChange(); | 42 bool success = battery_fetcher_->StartListeningBatteryChange(); |
41 if (!success) { | 43 // On failure pass the default values back. |
42 // Make sure the promise resolves with the default values in Blink. | 44 if (!success) |
43 callback.Run(blink::WebBatteryStatus()); | 45 callback.Run(BatteryStatus()); |
44 } | |
45 } | 46 } |
46 | 47 |
47 if (status_updated_) { | 48 if (status_updated_) { |
48 // Send recent status to the new callback if already available. | 49 // Send recent status to the new callback if already available. |
49 callback.Run(status_); | 50 callback.Run(status_); |
50 } | 51 } |
51 | 52 |
52 return callback_list_.Add(callback); | 53 return callback_list_.Add(callback); |
53 } | 54 } |
54 | 55 |
55 void BatteryStatusService::ConsumersChanged() { | 56 void BatteryStatusService::ConsumersChanged() { |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 57 if (is_shutdown_) |
57 DCHECK(!is_shutdown_); | 58 return; |
58 | 59 |
59 if (callback_list_.empty()) { | 60 if (callback_list_.empty()) { |
60 battery_fetcher_->StopListeningBatteryChange(); | 61 battery_fetcher_->StopListeningBatteryChange(); |
61 status_updated_ = false; | 62 status_updated_ = false; |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 void BatteryStatusService::UpdateBatteryStatus( | 66 void BatteryStatusService::NotifyConsumers(const BatteryStatus& status) { |
66 const blink::WebBatteryStatus& status) { | |
67 DCHECK(!is_shutdown_); | 67 DCHECK(!is_shutdown_); |
68 BrowserThread::PostTask(BrowserThread::IO, | 68 |
69 FROM_HERE, | 69 main_thread_task_runner_->PostTask(FROM_HERE, base::Bind( |
70 base::Bind(&BatteryStatusService::NotifyConsumers, | 70 &BatteryStatusService::NotifyConsumersOnMainThread, |
71 base::Unretained(this), status)); | 71 base::Unretained(this), |
72 status)); | |
72 } | 73 } |
73 | 74 |
74 void BatteryStatusService::NotifyConsumers( | 75 void BatteryStatusService::NotifyConsumersOnMainThread( |
75 const blink::WebBatteryStatus& status) { | 76 const BatteryStatus& status) { |
timvolodine
2014/09/30 16:26:02
also I think we need DCHECK here that this always
ppi
2014/10/06 13:50:02
Done.
| |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 77 status_ = status; |
timvolodine
2014/09/30 16:26:01
I think these two lines should come after the if(c
ppi
2014/10/06 13:50:02
Done.
| |
78 status_updated_ = true; | |
77 | 79 |
78 if (callback_list_.empty()) | 80 if (callback_list_.empty()) |
79 return; | 81 return; |
80 | 82 |
81 status_ = status; | 83 callback_list_.Notify(status_); |
82 status_updated_ = true; | |
83 callback_list_.Notify(status); | |
84 } | 84 } |
85 | 85 |
86 void BatteryStatusService::Shutdown() { | 86 void BatteryStatusService::Shutdown() { |
87 if (!callback_list_.empty()) | 87 if (!callback_list_.empty()) |
88 battery_fetcher_->StopListeningBatteryChange(); | 88 battery_fetcher_->StopListeningBatteryChange(); |
89 battery_fetcher_.reset(); | 89 battery_fetcher_.reset(); |
90 is_shutdown_ = true; | 90 is_shutdown_ = true; |
91 } | 91 } |
92 | 92 |
93 const BatteryStatusService::BatteryUpdateCallback& | 93 const BatteryStatusService::BatteryUpdateCallback& |
94 BatteryStatusService::GetUpdateCallbackForTesting() const { | 94 BatteryStatusService::GetUpdateCallbackForTesting() const { |
95 return update_callback_; | 95 return update_callback_; |
96 } | 96 } |
97 | 97 |
98 void BatteryStatusService::SetBatteryManagerForTesting( | 98 void BatteryStatusService::SetBatteryManagerForTesting( |
99 BatteryStatusManager* test_battery_manager) { | 99 scoped_ptr<BatteryStatusManager> test_battery_manager) { |
100 battery_fetcher_.reset(test_battery_manager); | 100 battery_fetcher_ = test_battery_manager.Pass(); |
101 blink::WebBatteryStatus status; | 101 status_ = BatteryStatus(); |
102 status_ = status; | |
103 status_updated_ = false; | 102 status_updated_ = false; |
104 } | 103 } |
105 | 104 |
106 } // namespace content | 105 } // namespace device |
OLD | NEW |