OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_hand ler.h" | 5 #include "chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_hand ler.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/system/audio/audio_observer.h" | |
8 #include "ash/system/tray/system_tray_delegate.h" | 9 #include "ash/system/tray/system_tray_delegate.h" |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/strings/string_number_conversions.h" | |
10 #include "base/values.h" | 12 #include "base/values.h" |
11 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
12 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | 14 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
13 #include "chromeos/dbus/fake_bluetooth_device_client.h" | 15 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
16 #include "chromeos/dbus/fake_cras_audio_client.h" | |
14 #include "chromeos/dbus/fake_power_manager_client.h" | 17 #include "chromeos/dbus/fake_power_manager_client.h" |
15 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
16 #include "device/bluetooth/bluetooth_device_chromeos.h" | 19 #include "device/bluetooth/bluetooth_device_chromeos.h" |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 // Define the name of the callback functions that will be used by JavaScript. | 23 // Define the name of the callback functions that will be used by JavaScript. |
21 const char kBluetoothDiscoverFunction[] = "requestBluetoothDiscover"; | 24 const char kBluetoothDiscoverFunction[] = "requestBluetoothDiscover"; |
22 const char kBluetoothPairFunction[] = "requestBluetoothPair"; | 25 const char kBluetoothPairFunction[] = "requestBluetoothPair"; |
23 const char kRequestBluetoothInfo[] = "requestBluetoothInfo"; | 26 const char kRequestBluetoothInfo[] = "requestBluetoothInfo"; |
24 const char kRequestPowerInfo[] = "requestPowerInfo"; | 27 const char kRequestPowerInfo[] = "requestPowerInfo"; |
28 const char kRequestAudioNodes[] = "requestAudioNodes"; | |
29 | |
30 // Define update function that will update the state of the audio ui. | |
31 const char kUpdateAudioNodes[] = | |
32 "device_emulator.audioSettings.updateAudioNodes"; | |
xiyuan
2015/08/07 20:17:12
nit: Move this with other JS callback names around
mozartalouis
2015/08/07 22:44:07
Done.
| |
33 const char kInsertAudioNode[] = "insertAudioNode"; | |
34 const char kRemoveAudioNode[] = "removeAudioNode"; | |
25 | 35 |
26 // Define update functions that will update the power properties to the | 36 // Define update functions that will update the power properties to the |
27 // variables defined in the web UI. | 37 // variables defined in the web UI. |
28 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; | 38 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; |
29 const char kUpdateBatteryState[] = "updateBatteryState"; | 39 const char kUpdateBatteryState[] = "updateBatteryState"; |
30 const char kUpdateExternalPower[] = "updateExternalPower"; | 40 const char kUpdateExternalPower[] = "updateExternalPower"; |
31 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; | 41 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; |
32 const char kUpdateTimeToFull[] = "updateTimeToFull"; | 42 const char kUpdateTimeToFull[] = "updateTimeToFull"; |
33 | 43 |
34 // Define callback functions that will update the JavaScript variable | 44 // Define callback functions that will update the JavaScript variable |
(...skipping 30 matching lines...) Expand all Loading... | |
65 void DeviceRemoved(const dbus::ObjectPath& object_path) override; | 75 void DeviceRemoved(const dbus::ObjectPath& object_path) override; |
66 | 76 |
67 private: | 77 private: |
68 DeviceEmulatorMessageHandler* owner_; | 78 DeviceEmulatorMessageHandler* owner_; |
69 | 79 |
70 DISALLOW_COPY_AND_ASSIGN(BluetoothObserver); | 80 DISALLOW_COPY_AND_ASSIGN(BluetoothObserver); |
71 }; | 81 }; |
72 | 82 |
73 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded( | 83 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded( |
74 const dbus::ObjectPath& object_path) { | 84 const dbus::ObjectPath& object_path) { |
75 scoped_ptr<base::DictionaryValue> device = owner_->GetDeviceInfo( | 85 scoped_ptr<base::DictionaryValue> device = owner_->GetDeviceInfo(object_path); |
76 object_path); | |
77 | 86 |
78 // Request to add the device to the view's list of devices. | 87 // Request to add the device to the view's list of devices. |
79 owner_->web_ui()->CallJavascriptFunction(kAddBluetoothDeviceJSCallback, | 88 owner_->web_ui()->CallJavascriptFunction(kAddBluetoothDeviceJSCallback, |
80 *device); | 89 *device); |
81 } | 90 } |
82 | 91 |
83 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved( | 92 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved( |
84 const dbus::ObjectPath& object_path) { | 93 const dbus::ObjectPath& object_path) { |
85 owner_->web_ui()->CallJavascriptFunction(kRemoveBluetoothDeviceJSCallback, | 94 owner_->web_ui()->CallJavascriptFunction( |
86 base::StringValue(object_path.value())); | 95 kRemoveBluetoothDeviceJSCallback, base::StringValue(object_path.value())); |
96 } | |
97 | |
98 class DeviceEmulatorMessageHandler::CrasAudioObserver | |
99 : public CrasAudioClient::Observer { | |
100 public: | |
101 explicit CrasAudioObserver(DeviceEmulatorMessageHandler* owner) | |
102 : owner_(owner) { | |
103 owner_->fake_cras_audio_client_->AddObserver(this); | |
104 } | |
105 | |
106 ~CrasAudioObserver() override { | |
107 owner_->fake_cras_audio_client_->RemoveObserver(this); | |
108 } | |
109 | |
110 // chromeos::CrasAudioClient::Observer. | |
111 void NodesChanged() override; | |
112 | |
113 private: | |
114 DeviceEmulatorMessageHandler* owner_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(CrasAudioObserver); | |
117 }; | |
118 | |
119 void DeviceEmulatorMessageHandler::CrasAudioObserver::NodesChanged() { | |
120 owner_->HandleRequestAudioNodes(nullptr); | |
87 } | 121 } |
88 | 122 |
89 class DeviceEmulatorMessageHandler::PowerObserver | 123 class DeviceEmulatorMessageHandler::PowerObserver |
90 : public PowerManagerClient::Observer { | 124 : public PowerManagerClient::Observer { |
91 public: | 125 public: |
92 explicit PowerObserver(DeviceEmulatorMessageHandler* owner) | 126 explicit PowerObserver(DeviceEmulatorMessageHandler* owner) |
93 : owner_(owner) { | 127 : owner_(owner) { |
94 owner_->fake_power_manager_client_->AddObserver(this); | 128 owner_->fake_power_manager_client_->AddObserver(this); |
95 } | 129 } |
96 | 130 |
97 ~PowerObserver() override { | 131 ~PowerObserver() override { |
98 owner_->fake_power_manager_client_->RemoveObserver(this); | 132 owner_->fake_power_manager_client_->RemoveObserver(this); |
99 } | 133 } |
100 | 134 |
101 void PowerChanged( | 135 void PowerChanged(const power_manager::PowerSupplyProperties& proto) override; |
102 const power_manager::PowerSupplyProperties& proto) override; | |
103 | 136 |
104 private: | 137 private: |
105 DeviceEmulatorMessageHandler* owner_; | 138 DeviceEmulatorMessageHandler* owner_; |
106 | 139 |
107 DISALLOW_COPY_AND_ASSIGN(PowerObserver); | 140 DISALLOW_COPY_AND_ASSIGN(PowerObserver); |
108 }; | 141 }; |
109 | 142 |
110 void DeviceEmulatorMessageHandler::PowerObserver::PowerChanged( | 143 void DeviceEmulatorMessageHandler::PowerObserver::PowerChanged( |
111 const power_manager::PowerSupplyProperties& proto) { | 144 const power_manager::PowerSupplyProperties& proto) { |
112 base::DictionaryValue power_properties; | 145 base::DictionaryValue power_properties; |
113 | 146 |
114 power_properties.SetInteger("battery_percent", proto.battery_percent()); | 147 power_properties.SetInteger("battery_percent", proto.battery_percent()); |
115 power_properties.SetInteger("battery_state", proto.battery_state()); | 148 power_properties.SetInteger("battery_state", proto.battery_state()); |
116 power_properties.SetInteger("external_power", proto.external_power()); | 149 power_properties.SetInteger("external_power", proto.external_power()); |
117 power_properties.SetInteger("battery_time_to_empty_sec", | 150 power_properties.SetInteger("battery_time_to_empty_sec", |
118 proto.battery_time_to_empty_sec()); | 151 proto.battery_time_to_empty_sec()); |
119 power_properties.SetInteger("battery_time_to_full_sec", | 152 power_properties.SetInteger("battery_time_to_full_sec", |
120 proto.battery_time_to_full_sec()); | 153 proto.battery_time_to_full_sec()); |
121 | 154 |
122 owner_->web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback, | 155 owner_->web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback, |
123 power_properties); | 156 power_properties); |
124 } | 157 } |
125 | 158 |
126 DeviceEmulatorMessageHandler::DeviceEmulatorMessageHandler() | 159 DeviceEmulatorMessageHandler::DeviceEmulatorMessageHandler() |
127 : fake_bluetooth_device_client_( | 160 : fake_bluetooth_device_client_( |
128 static_cast<chromeos::FakeBluetoothDeviceClient*>( | 161 static_cast<chromeos::FakeBluetoothDeviceClient*>( |
129 chromeos::DBusThreadManager::Get() | 162 chromeos::DBusThreadManager::Get() |
130 ->GetBluetoothDeviceClient())), | 163 ->GetBluetoothDeviceClient())), |
164 fake_cras_audio_client_(static_cast<chromeos::FakeCrasAudioClient*>( | |
165 chromeos::DBusThreadManager::Get() | |
166 ->GetCrasAudioClient())), | |
131 fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( | 167 fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( |
132 chromeos::DBusThreadManager::Get() | 168 chromeos::DBusThreadManager::Get() |
133 ->GetPowerManagerClient())) {} | 169 ->GetPowerManagerClient())) {} |
134 | 170 |
135 DeviceEmulatorMessageHandler::~DeviceEmulatorMessageHandler() { | 171 DeviceEmulatorMessageHandler::~DeviceEmulatorMessageHandler() { |
136 } | 172 } |
137 | 173 |
138 void DeviceEmulatorMessageHandler::Init() { | 174 void DeviceEmulatorMessageHandler::Init() { |
139 bluetooth_observer_.reset(new BluetoothObserver(this)); | 175 bluetooth_observer_.reset(new BluetoothObserver(this)); |
176 cras_audio_observer_.reset(new CrasAudioObserver(this)); | |
140 power_observer_.reset(new PowerObserver(this)); | 177 power_observer_.reset(new PowerObserver(this)); |
141 } | 178 } |
142 | 179 |
143 void DeviceEmulatorMessageHandler::RequestPowerInfo( | 180 void DeviceEmulatorMessageHandler::RequestPowerInfo( |
144 const base::ListValue* args) { | 181 const base::ListValue* args) { |
145 fake_power_manager_client_->RequestStatusUpdate(); | 182 fake_power_manager_client_->RequestStatusUpdate(); |
146 } | 183 } |
147 | 184 |
148 void DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover( | 185 void DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover( |
149 const base::ListValue* args) { | 186 const base::ListValue* args) { |
(...skipping 26 matching lines...) Expand all Loading... | |
176 std::string path = CreateBluetoothDeviceFromListValue(args); | 213 std::string path = CreateBluetoothDeviceFromListValue(args); |
177 chromeos::FakeBluetoothDeviceClient::Properties* props = | 214 chromeos::FakeBluetoothDeviceClient::Properties* props = |
178 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(path)); | 215 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(path)); |
179 | 216 |
180 // Try to pair the device with the main adapter. The device is identified | 217 // Try to pair the device with the main adapter. The device is identified |
181 // by its device ID, which, in this case is the same as its address. | 218 // by its device ID, which, in this case is the same as its address. |
182 ash::Shell::GetInstance()->system_tray_delegate()->ConnectToBluetoothDevice( | 219 ash::Shell::GetInstance()->system_tray_delegate()->ConnectToBluetoothDevice( |
183 props->address.value()); | 220 props->address.value()); |
184 } | 221 } |
185 | 222 |
223 void DeviceEmulatorMessageHandler::HandleRequestAudioNodes( | |
224 const base::ListValue* args) { | |
225 // Get every active audio node and create a dictionary to | |
226 // send it to JavaScript. | |
227 base::ListValue audio_nodes; | |
228 for (const AudioNode& node : fake_cras_audio_client_->node_list()) { | |
229 scoped_ptr<base::DictionaryValue> audio_node(new base::DictionaryValue()); | |
230 | |
231 audio_node->SetBoolean("is_input", node.is_input); | |
232 audio_node->SetString("id", base::Uint64ToString(node.id)); | |
233 audio_node->SetString("device_name", node.device_name); | |
234 audio_node->SetString("type", node.type); | |
235 audio_node->SetString("name", node.name); | |
236 audio_node->SetBoolean("active", node.active); | |
237 | |
238 audio_nodes.Append(audio_node.Pass()); | |
239 } | |
240 web_ui()->CallJavascriptFunction(kUpdateAudioNodes, audio_nodes); | |
241 } | |
242 | |
243 void DeviceEmulatorMessageHandler::HandleInsertAudioNode( | |
244 const base::ListValue* args) { | |
245 AudioNode audio_node; | |
246 const base::DictionaryValue* device_dict = nullptr; | |
247 | |
248 CHECK(args->GetDictionary(0, &device_dict)); | |
249 CHECK(device_dict->GetBoolean("isInput", &audio_node.is_input)); | |
250 CHECK(device_dict->GetString("deviceName", &audio_node.device_name)); | |
251 CHECK(device_dict->GetString("type", &audio_node.type)); | |
252 CHECK(device_dict->GetString("name", &audio_node.name)); | |
253 CHECK(device_dict->GetBoolean("active", &audio_node.active)); | |
254 | |
255 std::string tmp_id; | |
256 CHECK(device_dict->GetString("id", &tmp_id)); | |
257 CHECK(base::StringToUint64(tmp_id, &audio_node.id)); | |
258 | |
259 fake_cras_audio_client_->InsertAudioNodeToList(audio_node); | |
260 } | |
261 | |
262 void DeviceEmulatorMessageHandler::HandleRemoveAudioNode( | |
263 const base::ListValue* args) { | |
264 std::string tmp_id; | |
265 uint64 id; | |
266 CHECK(args->GetString(0, &tmp_id)); | |
267 CHECK(base::StringToUint64(tmp_id, &id)); | |
268 | |
269 fake_cras_audio_client_->RemoveAudioNodeFromList(id); | |
270 } | |
271 | |
186 void DeviceEmulatorMessageHandler::UpdateBatteryPercent( | 272 void DeviceEmulatorMessageHandler::UpdateBatteryPercent( |
187 const base::ListValue* args) { | 273 const base::ListValue* args) { |
188 int new_percent; | 274 int new_percent; |
189 if (args->GetInteger(0, &new_percent)) { | 275 if (args->GetInteger(0, &new_percent)) { |
190 power_manager::PowerSupplyProperties props = | 276 power_manager::PowerSupplyProperties props = |
191 fake_power_manager_client_->props(); | 277 fake_power_manager_client_->props(); |
192 props.set_battery_percent(new_percent); | 278 props.set_battery_percent(new_percent); |
193 fake_power_manager_client_->UpdatePowerProperties(props); | 279 fake_power_manager_client_->UpdatePowerProperties(props); |
194 } | 280 } |
195 } | 281 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover, | 358 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover, |
273 base::Unretained(this))); | 359 base::Unretained(this))); |
274 web_ui()->RegisterMessageCallback( | 360 web_ui()->RegisterMessageCallback( |
275 kBluetoothPairFunction, | 361 kBluetoothPairFunction, |
276 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, | 362 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, |
277 base::Unretained(this))); | 363 base::Unretained(this))); |
278 web_ui()->RegisterMessageCallback( | 364 web_ui()->RegisterMessageCallback( |
279 kRequestBluetoothInfo, | 365 kRequestBluetoothInfo, |
280 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo, | 366 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo, |
281 base::Unretained(this))); | 367 base::Unretained(this))); |
368 web_ui()->RegisterMessageCallback( | |
369 kRequestAudioNodes, | |
370 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestAudioNodes, | |
371 base::Unretained(this))); | |
372 web_ui()->RegisterMessageCallback( | |
373 kInsertAudioNode, | |
374 base::Bind(&DeviceEmulatorMessageHandler::HandleInsertAudioNode, | |
375 base::Unretained(this))); | |
376 web_ui()->RegisterMessageCallback( | |
377 kRemoveAudioNode, | |
378 base::Bind(&DeviceEmulatorMessageHandler::HandleRemoveAudioNode, | |
379 base::Unretained(this))); | |
282 } | 380 } |
283 | 381 |
284 std::string DeviceEmulatorMessageHandler::CreateBluetoothDeviceFromListValue( | 382 std::string DeviceEmulatorMessageHandler::CreateBluetoothDeviceFromListValue( |
285 const base::ListValue* args) { | 383 const base::ListValue* args) { |
286 const base::DictionaryValue* device_dict = nullptr; | 384 const base::DictionaryValue* device_dict = nullptr; |
287 FakeBluetoothDeviceClient::IncomingDeviceProperties props; | 385 FakeBluetoothDeviceClient::IncomingDeviceProperties props; |
288 | 386 |
289 CHECK(args->GetDictionary(0, &device_dict)); | 387 CHECK(args->GetDictionary(0, &device_dict)); |
290 CHECK(device_dict->GetString("path", &props.device_path)); | 388 CHECK(device_dict->GetString("path", &props.device_path)); |
291 CHECK(device_dict->GetString("name", &props.device_name)); | 389 CHECK(device_dict->GetString("name", &props.device_name)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 for (const std::string& uuid : props->uuids.value()) { | 430 for (const std::string& uuid : props->uuids.value()) { |
333 uuids->AppendString(uuid); | 431 uuids->AppendString(uuid); |
334 } | 432 } |
335 | 433 |
336 device->Set("uuids", uuids.Pass()); | 434 device->Set("uuids", uuids.Pass()); |
337 | 435 |
338 return device.Pass(); | 436 return device.Pass(); |
339 } | 437 } |
340 | 438 |
341 } // namespace chromeos | 439 } // namespace chromeos |
OLD | NEW |