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

Side by Side Diff: chrome/browser/chromeos/power/peripheral_battery_observer.cc

Issue 13638018: Add PeripheralBatteryObserver (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: removed unused const Created 7 years, 8 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
(Empty)
1 // Copyright (c) 2013 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 "chrome/browser/chromeos/power/peripheral_battery_observer.h"
6
7 #include "ash/shell.h"
8 #include "base/bind.h"
9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/notifications/notification.h"
13 #include "chrome/browser/notifications/notification_ui_manager.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "device/bluetooth/bluetooth_adapter_factory.h"
17 #include "device/bluetooth/bluetooth_device.h"
18 #include "grit/ash_strings.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/image/image.h"
23
24 namespace chromeos {
25
26 namespace {
27
28 const int kLowBatteryLevel = 15;
Daniel Erat 2013/04/06 01:52:34 add comments describing what these mean. for exam
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
29 const int kNotificationInterval = 60;
Daniel Erat 2013/04/06 01:52:34 include units in this constant's name, e.g. kNotif
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
30
31 class PeripheralBatteryNotificationDelegate : public NotificationDelegate {
Daniel Erat 2013/04/06 01:52:34 is this class really necessary? it looks like it
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 offline talk to dewittj@ about making Notification
32 public:
33
Daniel Erat 2013/04/06 01:52:34 delete blank line
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
34 explicit PeripheralBatteryNotificationDelegate(const std::string& id)
35 : id_(id) {}
36
37 // Overridden from NotificationDelegate:
38 virtual void Display() OVERRIDE {}
39 virtual void Error() OVERRIDE {}
40 virtual void Close(bool by_user) OVERRIDE {}
41 virtual void Click() OVERRIDE {}
42 virtual std::string id() const OVERRIDE { return id_; }
43 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
44 return NULL;
45 }
46
47 private:
48 const std::string id_;
49
50 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryNotificationDelegate);
51 };
52
53 base::TimeDelta TimeNow() {
54 return base::TimeDelta::FromInternalValue(
Daniel Erat 2013/04/06 01:52:34 why are you converting from one class's internal v
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Removed.
55 base::TimeTicks::Now().ToInternalValue());
56 }
57
58 } // namespace
59
60 PeripheralBatteryObserver::PeripheralBatteryObserver()
61 : weakptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(
62 new base::WeakPtrFactory<PeripheralBatteryObserver>(this))) {
63 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
64 device::BluetoothAdapterFactory::GetAdapter(
65 base::Bind(&PeripheralBatteryObserver::InitializeOnBluetoothReady,
66 weakptr_factory_->GetWeakPtr()));
67 }
68
69 PeripheralBatteryObserver::~PeripheralBatteryObserver() {
70 if (bluetooth_adapter_.get())
71 bluetooth_adapter_->RemoveObserver(this);
72 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
73 }
74
75 void PeripheralBatteryObserver::InitializeOnBluetoothReady(
76 scoped_refptr<device::BluetoothAdapter> adapter) {
77 bluetooth_adapter_ = adapter;
78 CHECK(bluetooth_adapter_);
79 bluetooth_adapter_->AddObserver(this);
80 }
81
82 void PeripheralBatteryObserver::PeripheralBatteryStatusReceived(
83 const std::string& path, const std::string& name, int level) {
Daniel Erat 2013/04/06 01:52:34 one parameter per line
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
84 if (level < -1 || level > 100) {
85 LOG(ERROR) << "Invalid battery level " << level
86 << " for device " << name << " at path " << path;
87 return;
88 }
89 // If unknown battery level received, cancel any existing notification.
90 if (level == -1)
91 g_browser_process->notification_ui_manager()->CancelById(path);
Daniel Erat 2013/04/06 01:52:34 can you return immediately here? is there any rea
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
92
93 // Post the notification in 2 cases:
94 // 1. It's the first time the battery level is received, and it is
95 // below kLowBatteryLevel.
96 // 2. The battery level is in record and it drops below kLowBatteryLevel.
97 if (batteries_.find(path) == batteries_.end()) {
98 BatteryInfo battery(path, name, level, base::TimeDelta(), TimeNow());
99 if (level >= 0 && level < kLowBatteryLevel)
100 PostNotification(&battery);
101 batteries_[path] = battery;
102 } else {
103 BatteryInfo* battery = &batteries_[path];
104 battery->set_name(name);
105 int old_level = battery->level();
106 battery->set_level(level);
107 battery->set_update_timestamp(TimeNow());
108 if (old_level >= kLowBatteryLevel && level >= 0 && level < kLowBatteryLevel)
109 PostNotification(battery);
110 }
111 }
112
113 void PeripheralBatteryObserver::DeviceChanged(device::BluetoothAdapter* adapter,
114 device::BluetoothDevice* device) {
115 if (!device->IsPaired())
116 RemoveBattery(UTF16ToUTF8(device->GetName()));
Daniel Erat 2013/04/06 01:52:34 is it guaranteed that the names that you get here
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 So the name can actually be set by the user so it
117 }
118
119 void PeripheralBatteryObserver::DeviceRemoved(device::BluetoothAdapter* adapter,
120 device::BluetoothDevice* device) {
121 RemoveBattery(UTF16ToUTF8(device->GetName()));
122 }
123
124 void PeripheralBatteryObserver::RemoveBattery(const std::string& name) {
125 std::map<std::string, BatteryInfo>::iterator it = batteries_.begin();
126 for (; it != batteries_.end(); it++) {
127 if (name == it->second.name()) {
Daniel Erat 2013/04/06 01:52:34 could you just key the map by device name instead
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Changed to use the device's bluetooth address as t
128 g_browser_process->notification_ui_manager()->CancelById(it->first);
129 batteries_.erase(it);
130 break;
131 }
132 }
133 }
134
135 void PeripheralBatteryObserver::PostNotification(BatteryInfo* battery) {
Daniel Erat 2013/04/06 01:52:34 this should take a const BatteryInfo& instead of a
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
136 // Only post notification if kNotificationInterval seconds have passed
137 // since last notification showed, avoiding the case the battery level
Daniel Erat 2013/04/06 01:52:34 nit: s/case the/case where the/
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
138 // oscillates around the threshold level.
139 if (TimeNow() - battery->last_notification_timestamp() <
140 base::TimeDelta::FromSeconds(kNotificationInterval))
141 return;
142
143 NotificationUIManager* notification_manager =
144 g_browser_process->notification_ui_manager();
145
146 std::string string_id = battery->path();
147 std::string string_text = base::StringPrintf("Battery Low (%d%%)",
Daniel Erat 2013/04/06 01:52:34 this needs to be a translatable string resource
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
148 battery->level());
149
150 if (notification_manager->DoesIdExist(string_id)) {
151 if (!notification_manager->CancelById(string_id)) {
Jun Mukai 2013/04/06 01:11:56 You do not need to cancel it by yourself. Notifica
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 great.
152 LOG(ERROR) << "Can't cancel notification for ID " << string_id;
153 return;
154 }
155 }
156
157 Notification notification(
Jun Mukai 2013/04/06 01:11:56 Please add my TODO here: TODO(mukai): add SYSTEM p
Yufeng Shen (Slow to review) 2013/04/09 00:45:08 Done.
158 GURL(),
159 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
160 IDR_NOTIFICATION_PERIPHERAL_BATTERY_LOW),
161 UTF8ToUTF16(battery->name()),
162 UTF8ToUTF16(string_text),
163 WebKit::WebTextDirectionDefault,
164 string16(),
165 UTF8ToUTF16(string_id),
166 new PeripheralBatteryNotificationDelegate(string_id));
167
168 notification_manager->Add(notification,
169 ProfileManager::GetDefaultProfileOrOffTheRecord());
170
171 battery->set_notification_timestamp(TimeNow());
172 }
173
174 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698