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

Side by Side Diff: device/battery/battery_status_service.cc

Issue 457933002: Replace Chrome IPC with Mojo IPC for querying BatteryStatus service Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 3 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 | Annotate | Revision Log
OLDNEW
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 "device/battery/battery_monitor_impl.h"
9 #include "content/public/browser/browser_thread.h" 9 #include "device/battery/battery_status_manager.h"
10 10
11 namespace content { 11 namespace device {
12 12
13 BatteryStatusService::BatteryStatusService() 13 BatteryStatusService::BatteryStatusService()
14 : update_callback_(base::Bind(&BatteryStatusService::UpdateBatteryStatus, 14 : update_callback_(base::Bind(&BatteryStatusService::NotifyConsumers,
15 base::Unretained(this))), 15 base::Unretained(this))),
16 status_updated_(false), 16 status_updated_(false),
17 is_shutdown_(false) { 17 is_shutdown_(false) {
18 callback_list_.set_removal_callback( 18 callback_list_.set_removal_callback(
19 base::Bind(&BatteryStatusService::ConsumersChanged, 19 base::Bind(&BatteryStatusService::ConsumersChanged,
20 base::Unretained(this))); 20 base::Unretained(this)));
21 } 21 }
22 22
23 BatteryStatusService::~BatteryStatusService() { 23 BatteryStatusService::~BatteryStatusService() {
24 } 24 }
25 25
26 BatteryStatusService* BatteryStatusService::GetInstance() { 26 BatteryStatusService* BatteryStatusService::GetInstance() {
27 return Singleton<BatteryStatusService, 27 return Singleton<BatteryStatusService,
28 LeakySingletonTraits<BatteryStatusService> >::get(); 28 LeakySingletonTraits<BatteryStatusService> >::get();
29 } 29 }
30 30
31 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> 31 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription>
32 BatteryStatusService::AddCallback(const BatteryUpdateCallback& callback) { 32 BatteryStatusService::AddCallback(const BatteryUpdateCallback& callback) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
34 DCHECK(!is_shutdown_); 33 DCHECK(!is_shutdown_);
35 34
36 if (!battery_fetcher_) 35 if (!battery_fetcher_)
37 battery_fetcher_ = BatteryStatusManager::Create(update_callback_); 36 battery_fetcher_ = BatteryStatusManager::Create(update_callback_);
38 37
39 if (callback_list_.empty()) { 38 if (callback_list_.empty()) {
40 bool success = battery_fetcher_->StartListeningBatteryChange(); 39 bool success = battery_fetcher_->StartListeningBatteryChange();
41 if (!success) { 40 if (!success)
42 // Make sure the promise resolves with the default values in Blink. 41 callback.Run(BatteryStatus());
43 callback.Run(blink::WebBatteryStatus());
44 }
45 } 42 }
46 43
47 if (status_updated_) { 44 if (status_updated_) {
48 // Send recent status to the new callback if already available. 45 // Send recent status to the new callback if already available.
49 callback.Run(status_); 46 callback.Run(status_);
50 } 47 }
51 48
52 return callback_list_.Add(callback); 49 return callback_list_.Add(callback);
53 } 50 }
54 51
55 void BatteryStatusService::ConsumersChanged() { 52 void BatteryStatusService::ConsumersChanged() {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 53 if (is_shutdown_)
57 DCHECK(!is_shutdown_); 54 return;
58 55
59 if (callback_list_.empty()) { 56 if (callback_list_.empty()) {
60 battery_fetcher_->StopListeningBatteryChange(); 57 battery_fetcher_->StopListeningBatteryChange();
61 status_updated_ = false; 58 status_updated_ = false;
62 } 59 }
63 } 60 }
64 61
65 void BatteryStatusService::UpdateBatteryStatus( 62 void BatteryStatusService::NotifyConsumers(const BatteryStatus& status) {
66 const blink::WebBatteryStatus& status) {
67 DCHECK(!is_shutdown_); 63 DCHECK(!is_shutdown_);
68 BrowserThread::PostTask(BrowserThread::IO,
69 FROM_HERE,
70 base::Bind(&BatteryStatusService::NotifyConsumers,
71 base::Unretained(this), status));
72 }
73 64
74 void BatteryStatusService::NotifyConsumers( 65 status_ = status;
75 const blink::WebBatteryStatus& status) { 66 status_updated_ = true;
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
77 67
78 if (callback_list_.empty()) 68 if (callback_list_.empty())
79 return; 69 return;
80 70
81 status_ = status; 71 callback_list_.Notify(status_);
82 status_updated_ = true;
83 callback_list_.Notify(status);
84 } 72 }
85 73
86 void BatteryStatusService::Shutdown() { 74 void BatteryStatusService::Shutdown() {
87 if (!callback_list_.empty()) 75 if (!callback_list_.empty())
88 battery_fetcher_->StopListeningBatteryChange(); 76 battery_fetcher_->StopListeningBatteryChange();
89 battery_fetcher_.reset(); 77 battery_fetcher_.reset();
90 is_shutdown_ = true; 78 is_shutdown_ = true;
91 } 79 }
92 80
93 const BatteryStatusService::BatteryUpdateCallback& 81 const BatteryStatusService::BatteryUpdateCallback&
94 BatteryStatusService::GetUpdateCallbackForTesting() const { 82 BatteryStatusService::GetUpdateCallbackForTesting() const {
95 return update_callback_; 83 return update_callback_;
96 } 84 }
97 85
98 void BatteryStatusService::SetBatteryManagerForTesting( 86 void BatteryStatusService::SetBatteryManagerForTesting(
99 BatteryStatusManager* test_battery_manager) { 87 scoped_ptr<BatteryStatusManager> test_battery_manager) {
100 battery_fetcher_.reset(test_battery_manager); 88 battery_fetcher_ = test_battery_manager.Pass();
101 blink::WebBatteryStatus status; 89 status_ = BatteryStatus();
102 status_ = status;
103 status_updated_ = false; 90 status_updated_ = false;
104 } 91 }
105 92
106 } // namespace content 93 } // namespace device
OLDNEW
« no previous file with comments | « device/battery/battery_status_service.h ('k') | device/battery/battery_status_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698