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.
5 #include "content/browser/battery_status/battery_status_service.h"
8 #include "content/browser/battery_status/battery_status_manager.h"
9 #include "content/public/browser/browser_thread.h"
13 BatteryStatusService::BatteryStatusService()
14 : update_callback_(base::Bind(&BatteryStatusService::UpdateBatteryStatus
,
15 base::Unretained(this))),
16 status_updated_(false),
18 callback_list_
.set_removal_callback(
19 base::Bind(&BatteryStatusService::ConsumersChanged
,
20 base::Unretained(this)));
23 BatteryStatusService::~BatteryStatusService() {
26 BatteryStatusService
* BatteryStatusService::GetInstance() {
27 return Singleton
<BatteryStatusService
,
28 LeakySingletonTraits
<BatteryStatusService
> >::get();
31 scoped_ptr
<BatteryStatusService::BatteryUpdateSubscription
>
32 BatteryStatusService::AddCallback(const BatteryUpdateCallback
& callback
) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
34 DCHECK(!is_shutdown_
);
36 if (!battery_fetcher_
)
37 battery_fetcher_
= BatteryStatusManager::Create(update_callback_
);
39 if (callback_list_
.empty()) {
40 bool success
= battery_fetcher_
->StartListeningBatteryChange();
42 // Make sure the promise resolves with the default values in Blink.
43 callback
.Run(blink::WebBatteryStatus());
47 if (status_updated_
) {
48 // Send recent status to the new callback if already available.
49 callback
.Run(status_
);
52 return callback_list_
.Add(callback
);
55 void BatteryStatusService::ConsumersChanged() {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
57 DCHECK(!is_shutdown_
);
59 if (callback_list_
.empty()) {
60 battery_fetcher_
->StopListeningBatteryChange();
61 status_updated_
= false;
65 void BatteryStatusService::UpdateBatteryStatus(
66 const blink::WebBatteryStatus
& status
) {
67 DCHECK(!is_shutdown_
);
68 BrowserThread::PostTask(BrowserThread::IO
,
70 base::Bind(&BatteryStatusService::NotifyConsumers
,
71 base::Unretained(this), status
));
74 void BatteryStatusService::NotifyConsumers(
75 const blink::WebBatteryStatus
& status
) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
78 if (callback_list_
.empty())
82 status_updated_
= true;
83 callback_list_
.Notify(status
);
86 void BatteryStatusService::Shutdown() {
87 if (!callback_list_
.empty())
88 battery_fetcher_
->StopListeningBatteryChange();
89 battery_fetcher_
.reset();
93 const BatteryStatusService::BatteryUpdateCallback
&
94 BatteryStatusService::GetUpdateCallbackForTesting() const {
95 return update_callback_
;
98 void BatteryStatusService::SetBatteryManagerForTesting(
99 BatteryStatusManager
* test_battery_manager
) {
100 battery_fetcher_
.reset(test_battery_manager
);
101 blink::WebBatteryStatus status
;
103 status_updated_
= false;
106 } // namespace content