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 "device/battery/battery_status_service.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/single_thread_task_runner.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | |
13 #include "device/battery/battery_monitor_impl.h" | |
14 #include "device/battery/battery_status_manager.h" | |
15 | |
16 namespace device { | |
17 | |
18 BatteryStatusService::BatteryStatusService() | |
19 : main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
20 update_callback_(base::Bind(&BatteryStatusService::NotifyConsumers, | |
21 base::Unretained(this))), | |
22 status_updated_(false), | |
23 is_shutdown_(false) { | |
24 callback_list_.set_removal_callback( | |
25 base::Bind(&BatteryStatusService::ConsumersChanged, | |
26 base::Unretained(this))); | |
27 } | |
28 | |
29 BatteryStatusService::~BatteryStatusService() { | |
30 } | |
31 | |
32 BatteryStatusService* BatteryStatusService::GetInstance() { | |
33 return base::Singleton< | |
34 BatteryStatusService, | |
35 base::LeakySingletonTraits<BatteryStatusService>>::get(); | |
36 } | |
37 | |
38 std::unique_ptr<BatteryStatusService::BatteryUpdateSubscription> | |
39 BatteryStatusService::AddCallback(const BatteryUpdateCallback& callback) { | |
40 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
41 DCHECK(!is_shutdown_); | |
42 | |
43 if (!battery_fetcher_) | |
44 battery_fetcher_ = BatteryStatusManager::Create(update_callback_); | |
45 | |
46 if (callback_list_.empty()) { | |
47 bool success = battery_fetcher_->StartListeningBatteryChange(); | |
48 // On failure pass the default values back. | |
49 if (!success) | |
50 callback.Run(mojom::BatteryStatus()); | |
51 } | |
52 | |
53 if (status_updated_) { | |
54 // Send recent status to the new callback if already available. | |
55 callback.Run(status_); | |
56 } | |
57 | |
58 return callback_list_.Add(callback); | |
59 } | |
60 | |
61 void BatteryStatusService::ConsumersChanged() { | |
62 if (is_shutdown_) | |
63 return; | |
64 | |
65 if (callback_list_.empty()) { | |
66 battery_fetcher_->StopListeningBatteryChange(); | |
67 status_updated_ = false; | |
68 } | |
69 } | |
70 | |
71 void BatteryStatusService::NotifyConsumers(const mojom::BatteryStatus& status) { | |
72 DCHECK(!is_shutdown_); | |
73 | |
74 main_thread_task_runner_->PostTask(FROM_HERE, base::Bind( | |
75 &BatteryStatusService::NotifyConsumersOnMainThread, | |
76 base::Unretained(this), | |
77 status)); | |
78 } | |
79 | |
80 void BatteryStatusService::NotifyConsumersOnMainThread( | |
81 const mojom::BatteryStatus& status) { | |
82 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
83 if (callback_list_.empty()) | |
84 return; | |
85 | |
86 status_ = status; | |
87 status_updated_ = true; | |
88 callback_list_.Notify(status_); | |
89 } | |
90 | |
91 void BatteryStatusService::Shutdown() { | |
92 if (!callback_list_.empty()) | |
93 battery_fetcher_->StopListeningBatteryChange(); | |
94 battery_fetcher_.reset(); | |
95 is_shutdown_ = true; | |
96 } | |
97 | |
98 const BatteryStatusService::BatteryUpdateCallback& | |
99 BatteryStatusService::GetUpdateCallbackForTesting() const { | |
100 return update_callback_; | |
101 } | |
102 | |
103 void BatteryStatusService::SetBatteryManagerForTesting( | |
104 std::unique_ptr<BatteryStatusManager> test_battery_manager) { | |
105 battery_fetcher_ = std::move(test_battery_manager); | |
106 status_ = mojom::BatteryStatus(); | |
107 status_updated_ = false; | |
108 is_shutdown_ = false; | |
109 main_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
110 } | |
111 | |
112 } // namespace device | |
OLD | NEW |