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

Side by Side Diff: content/browser/battery_status/battery_status_service.cc

Issue 334223003: Battery Status API: end-to-end browsertests and default values. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 6 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
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 "content/browser/battery_status/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 "content/browser/battery_status/battery_status_manager.h"
9 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
10 10
11 namespace content { 11 namespace content {
12 12
13 BatteryStatusService::BatteryStatusService() 13 BatteryStatusService::BatteryStatusService()
14 : status_updated_(false), 14 : update_callback_(base::Bind(&BatteryStatusService::UpdateBatteryStatus,
15 base::Unretained(this))),
16 status_updated_(false),
15 is_shutdown_(false) { 17 is_shutdown_(false) {
16 update_callback_ = base::Bind(&BatteryStatusService::UpdateBatteryStatus,
17 base::Unretained(this));
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)); 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
34 DCHECK(!is_shutdown_); 34 DCHECK(!is_shutdown_);
35 35
36 if (!battery_fetcher_) 36 if (!battery_fetcher_)
37 battery_fetcher_.reset(new BatteryStatusManager(update_callback_)); 37 battery_fetcher_.reset(new BatteryStatusManager(update_callback_));
38 38
39 if (callback_list_.empty()) 39 if (callback_list_.empty()) {
40 battery_fetcher_->StartListeningBatteryChange(); 40 bool success = battery_fetcher_->StartListeningBatteryChange();
41 if (!success) {
42 // Make sure the promise resolves with the default values in Blink.
43 callback.Run(blink::WebBatteryStatus());
44 }
45 }
41 46
42 if (status_updated_) { 47 if (status_updated_) {
43 // Send recent status to the new callback if already available. 48 // Send recent status to the new callback if already available.
44 callback.Run(status_); 49 callback.Run(status_);
45 } 50 }
46 51
47 return callback_list_.Add(callback); 52 return callback_list_.Add(callback);
48 } 53 }
49 54
50 void BatteryStatusService::ConsumersChanged() { 55 void BatteryStatusService::ConsumersChanged() {
(...skipping 22 matching lines...) Expand all
73 if (callback_list_.empty()) 78 if (callback_list_.empty())
74 return; 79 return;
75 80
76 status_ = status; 81 status_ = status;
77 status_updated_ = true; 82 status_updated_ = true;
78 callback_list_.Notify(status); 83 callback_list_.Notify(status);
79 } 84 }
80 85
81 void BatteryStatusService::Shutdown() { 86 void BatteryStatusService::Shutdown() {
82 if (!callback_list_.empty()) 87 if (!callback_list_.empty())
83 battery_fetcher_->StopListeningBatteryChange(); 88 battery_fetcher_->StopListeningBatteryChange();
84 battery_fetcher_.reset(); 89 battery_fetcher_.reset();
85 is_shutdown_ = true; 90 is_shutdown_ = true;
86 } 91 }
87 92
88 const BatteryStatusService::BatteryUpdateCallback& 93 const BatteryStatusService::BatteryUpdateCallback&
89 BatteryStatusService::GetUpdateCallbackForTesting() const { 94 BatteryStatusService::GetUpdateCallbackForTesting() const {
90 return update_callback_; 95 return update_callback_;
91 } 96 }
92 97
93 void BatteryStatusService::SetBatteryManagerForTesting( 98 void BatteryStatusService::SetBatteryManagerForTesting(
94 BatteryStatusManager* test_battery_manager) { 99 BatteryStatusManager* test_battery_manager) {
95 battery_fetcher_.reset(test_battery_manager); 100 battery_fetcher_.reset(test_battery_manager);
96 blink::WebBatteryStatus status; 101 blink::WebBatteryStatus status;
97 status_ = status; 102 status_ = status;
98 status_updated_ = false; 103 status_updated_ = false;
99 } 104 }
100 105
101 } // namespace content 106 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/battery_status/battery_status_browsertest.cc ('k') | content/content_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698