Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "content/browser/battery_status/battery_status_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "content/browser/battery_status/battery_status_manager.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 static void AddCallbackIfNecessary( | |
| 14 std::vector<BatteryStatusUpdateCallback>& callbacks, | |
| 15 const BatteryStatusUpdateCallback& callback) { | |
| 16 for (size_t i = 0; i < callbacks.size(); ++i) { | |
| 17 if (callbacks[i].Equals(callback)) | |
| 18 return; | |
| 19 } | |
| 20 callbacks.push_back(callback); | |
| 21 } | |
| 22 | |
| 23 static void RemoveCallbackIfNecessary( | |
| 24 std::vector<BatteryStatusUpdateCallback>& callbacks, | |
| 25 const BatteryStatusUpdateCallback& callback) { | |
| 26 for (size_t i = 0; i < callbacks.size(); ++i) { | |
| 27 if (callbacks[i].Equals(callback)) { | |
| 28 callbacks[i] = callbacks.back(); | |
| 29 callbacks.pop_back(); | |
| 30 return; | |
| 31 } | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 BatteryStatusService::BatteryStatusService() | |
| 36 : status_updated_(false), | |
| 37 is_shutdown_(false) { | |
| 38 update_callback_ = base::Bind(&BatteryStatusService::UpdateBatteryStatus, | |
| 39 base::Unretained(this)); | |
| 40 } | |
| 41 | |
| 42 BatteryStatusService::~BatteryStatusService() { | |
| 43 } | |
| 44 | |
| 45 BatteryStatusService* BatteryStatusService::GetInstance() { | |
| 46 return Singleton<BatteryStatusService, | |
| 47 LeakySingletonTraits<BatteryStatusService> >::get(); | |
| 48 } | |
| 49 | |
| 50 void BatteryStatusService::AddCallback( | |
| 51 const BatteryStatusUpdateCallback& callback) { | |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 53 DCHECK(!is_shutdown_); | |
| 54 | |
| 55 if (!battery_fetcher_) | |
| 56 battery_fetcher_.reset(new BatteryStatusManager(update_callback_)); | |
| 57 | |
| 58 if (callbacks_.empty()) | |
| 59 battery_fetcher_->StartListeningBatteryChange(); | |
| 60 | |
| 61 if (status_updated_) { | |
| 62 // Send recent status to the new callback if already available. | |
| 63 callback.Run(status_); | |
| 64 } | |
| 65 | |
| 66 AddCallbackIfNecessary(callbacks_, callback); | |
|
jochen (gone - plz use gerrit)
2014/05/19 13:14:22
can this actually happen that the same callback is
timvolodine
2014/05/22 10:02:33
actually no, the message filter keeps track of whe
| |
| 67 } | |
| 68 | |
| 69 void BatteryStatusService::RemoveCallback( | |
| 70 const BatteryStatusUpdateCallback& callback) { | |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 72 DCHECK(!is_shutdown_); | |
| 73 | |
| 74 RemoveCallbackIfNecessary(callbacks_, callback); | |
| 75 | |
| 76 if (callbacks_.empty()) { | |
| 77 battery_fetcher_->StopListeningBatteryChange(); | |
| 78 status_updated_ = false; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 const BatteryStatusUpdateCallback& | |
| 83 BatteryStatusService::update_callback() const { | |
| 84 return update_callback_; | |
| 85 } | |
| 86 | |
| 87 void BatteryStatusService::UpdateBatteryStatus( | |
| 88 const blink::WebBatteryStatus& status) { | |
| 89 DCHECK(!is_shutdown_); | |
| 90 BrowserThread::PostTask(BrowserThread::IO, | |
|
Michael van Ouwerkerk
2014/05/19 11:19:22
I think you can turn BatteryStatusMessageFilter in
timvolodine
2014/05/22 10:02:33
The idea is to keep non-UI things away from the UI
| |
| 91 FROM_HERE, | |
| 92 base::Bind(&BatteryStatusService::NotifyConsumers, | |
| 93 base::Unretained(this), status)); | |
| 94 } | |
| 95 | |
| 96 void BatteryStatusService::NotifyConsumers( | |
| 97 const blink::WebBatteryStatus& status) { | |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 99 | |
| 100 if (callbacks_.empty()) | |
| 101 return; | |
| 102 | |
| 103 status_ = status; | |
| 104 status_updated_ = true; | |
| 105 std::vector<BatteryStatusUpdateCallback>::const_iterator it = | |
| 106 callbacks_.begin(); | |
| 107 for (; it != callbacks_.end(); ++it) | |
| 108 (*it).Run(status_); | |
| 109 } | |
| 110 | |
| 111 void BatteryStatusService::Shutdown() { | |
| 112 battery_fetcher_.reset(); | |
| 113 is_shutdown_ = true; | |
| 114 } | |
| 115 | |
| 116 void BatteryStatusService::SetBatteryManagerForTests( | |
| 117 BatteryStatusManagerInterface* test_battery_manager) { | |
| 118 battery_fetcher_.reset(test_battery_manager); | |
| 119 blink::WebBatteryStatus status; | |
| 120 status_ = status; | |
| 121 status_updated_ = false; | |
| 122 callbacks_.clear(); | |
| 123 } | |
| 124 | |
| 125 } // namespace content | |
| OLD | NEW |