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 kInsertAudioNode[] = "insertAudioNode"; |
| 32 const char kRemoveAudioNode[] = "removeAudioNode"; |
25 | 33 |
26 // Define update functions that will update the power properties to the | 34 // Define update functions that will update the power properties to the |
27 // variables defined in the web UI. | 35 // variables defined in the web UI. |
28 const char kRemoveBluetoothDevice[] = "removeBluetoothDevice"; | 36 const char kRemoveBluetoothDevice[] = "removeBluetoothDevice"; |
29 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; | 37 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; |
30 const char kUpdateBatteryState[] = "updateBatteryState"; | 38 const char kUpdateBatteryState[] = "updateBatteryState"; |
31 const char kUpdateExternalPower[] = "updateExternalPower"; | 39 const char kUpdateExternalPower[] = "updateExternalPower"; |
32 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; | 40 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; |
33 const char kUpdateTimeToFull[] = "updateTimeToFull"; | 41 const char kUpdateTimeToFull[] = "updateTimeToFull"; |
34 | 42 |
35 // Define callback functions that will update the JavaScript variable | 43 // Define callback functions that will update the JavaScript variable |
36 // and the web UI. | 44 // and the web UI. |
| 45 const char kUpdateAudioNodes[] = |
| 46 "device_emulator.audioSettings.updateAudioNodes"; |
37 const char kAddBluetoothDeviceJSCallback[] = | 47 const char kAddBluetoothDeviceJSCallback[] = |
38 "device_emulator.bluetoothSettings.addBluetoothDevice"; | 48 "device_emulator.bluetoothSettings.addBluetoothDevice"; |
39 const char kDevicePairedFromTrayJSCallback[] = | 49 const char kDevicePairedFromTrayJSCallback[] = |
40 "device_emulator.bluetoothSettings.devicePairedFromTray"; | 50 "device_emulator.bluetoothSettings.devicePairedFromTray"; |
41 const char kDeviceRemovedFromMainAdapterJSCallback[] = | 51 const char kDeviceRemovedFromMainAdapterJSCallback[] = |
42 "device_emulator.bluetoothSettings.deviceRemovedFromMainAdapter"; | 52 "device_emulator.bluetoothSettings.deviceRemovedFromMainAdapter"; |
43 const char kPairFailedJSCallback[] = | 53 const char kPairFailedJSCallback[] = |
44 "device_emulator.bluetoothSettings.pairFailed"; | 54 "device_emulator.bluetoothSettings.pairFailed"; |
45 const char kUpdateBluetoothInfoJSCallback[] = | 55 const char kUpdateBluetoothInfoJSCallback[] = |
46 "device_emulator.bluetoothSettings.updateBluetoothInfo"; | 56 "device_emulator.bluetoothSettings.updateBluetoothInfo"; |
(...skipping 29 matching lines...) Expand all Loading... |
76 void DeviceRemoved(const dbus::ObjectPath& object_path) override; | 86 void DeviceRemoved(const dbus::ObjectPath& object_path) override; |
77 | 87 |
78 private: | 88 private: |
79 DeviceEmulatorMessageHandler* owner_; | 89 DeviceEmulatorMessageHandler* owner_; |
80 | 90 |
81 DISALLOW_COPY_AND_ASSIGN(BluetoothObserver); | 91 DISALLOW_COPY_AND_ASSIGN(BluetoothObserver); |
82 }; | 92 }; |
83 | 93 |
84 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded( | 94 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded( |
85 const dbus::ObjectPath& object_path) { | 95 const dbus::ObjectPath& object_path) { |
86 scoped_ptr<base::DictionaryValue> device = owner_->GetDeviceInfo( | 96 scoped_ptr<base::DictionaryValue> device = owner_->GetDeviceInfo(object_path); |
87 object_path); | |
88 | 97 |
89 // Request to add the device to the view's list of devices. | 98 // Request to add the device to the view's list of devices. |
90 owner_->web_ui()->CallJavascriptFunction(kAddBluetoothDeviceJSCallback, | 99 owner_->web_ui()->CallJavascriptFunction(kAddBluetoothDeviceJSCallback, |
91 *device); | 100 *device); |
92 } | 101 } |
93 | 102 |
94 void DeviceEmulatorMessageHandler::BluetoothObserver::DevicePropertyChanged( | 103 void DeviceEmulatorMessageHandler::BluetoothObserver::DevicePropertyChanged( |
95 const dbus::ObjectPath& object_path, | 104 const dbus::ObjectPath& object_path, |
96 const std::string& property_name) { | 105 const std::string& property_name) { |
97 if (property_name == kPairedPropertyName) { | 106 if (property_name == kPairedPropertyName) { |
98 owner_->web_ui()->CallJavascriptFunction(kDevicePairedFromTrayJSCallback, | 107 owner_->web_ui()->CallJavascriptFunction(kDevicePairedFromTrayJSCallback, |
99 base::StringValue(object_path.value())); | 108 base::StringValue(object_path.value())); |
100 } | 109 } |
101 } | 110 } |
102 | 111 |
103 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved( | 112 void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved( |
104 const dbus::ObjectPath& object_path) { | 113 const dbus::ObjectPath& object_path) { |
105 owner_->web_ui()->CallJavascriptFunction( | 114 owner_->web_ui()->CallJavascriptFunction( |
| 115 kRemoveBluetoothDeviceJSCallback, base::StringValue(object_path.value())); |
| 116 } |
| 117 |
| 118 class DeviceEmulatorMessageHandler::CrasAudioObserver |
| 119 : public CrasAudioClient::Observer { |
| 120 public: |
| 121 explicit CrasAudioObserver(DeviceEmulatorMessageHandler* owner) |
| 122 : owner_(owner) { |
| 123 owner_->fake_cras_audio_client_->AddObserver(this); |
| 124 } |
| 125 |
| 126 ~CrasAudioObserver() override { |
| 127 owner_->fake_cras_audio_client_->RemoveObserver(this); |
| 128 } |
| 129 |
| 130 // chromeos::CrasAudioClient::Observer. |
| 131 void NodesChanged() override; |
| 132 |
| 133 private: |
| 134 DeviceEmulatorMessageHandler* owner_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(CrasAudioObserver); |
| 137 }; |
| 138 |
| 139 void DeviceEmulatorMessageHandler::CrasAudioObserver::NodesChanged() { |
| 140 owner_->HandleRequestAudioNodes(nullptr); |
106 kDeviceRemovedFromMainAdapterJSCallback, | 141 kDeviceRemovedFromMainAdapterJSCallback, |
107 base::StringValue(object_path.value())); | 142 base::StringValue(object_path.value())); |
108 } | 143 } |
109 | 144 |
110 class DeviceEmulatorMessageHandler::PowerObserver | 145 class DeviceEmulatorMessageHandler::PowerObserver |
111 : public PowerManagerClient::Observer { | 146 : public PowerManagerClient::Observer { |
112 public: | 147 public: |
113 explicit PowerObserver(DeviceEmulatorMessageHandler* owner) | 148 explicit PowerObserver(DeviceEmulatorMessageHandler* owner) |
114 : owner_(owner) { | 149 : owner_(owner) { |
115 owner_->fake_power_manager_client_->AddObserver(this); | 150 owner_->fake_power_manager_client_->AddObserver(this); |
116 } | 151 } |
117 | 152 |
118 ~PowerObserver() override { | 153 ~PowerObserver() override { |
119 owner_->fake_power_manager_client_->RemoveObserver(this); | 154 owner_->fake_power_manager_client_->RemoveObserver(this); |
120 } | 155 } |
121 | 156 |
122 void PowerChanged( | 157 void PowerChanged(const power_manager::PowerSupplyProperties& proto) override; |
123 const power_manager::PowerSupplyProperties& proto) override; | |
124 | 158 |
125 private: | 159 private: |
126 DeviceEmulatorMessageHandler* owner_; | 160 DeviceEmulatorMessageHandler* owner_; |
127 | 161 |
128 DISALLOW_COPY_AND_ASSIGN(PowerObserver); | 162 DISALLOW_COPY_AND_ASSIGN(PowerObserver); |
129 }; | 163 }; |
130 | 164 |
131 void DeviceEmulatorMessageHandler::PowerObserver::PowerChanged( | 165 void DeviceEmulatorMessageHandler::PowerObserver::PowerChanged( |
132 const power_manager::PowerSupplyProperties& proto) { | 166 const power_manager::PowerSupplyProperties& proto) { |
133 base::DictionaryValue power_properties; | 167 base::DictionaryValue power_properties; |
134 | 168 |
135 power_properties.SetInteger("battery_percent", proto.battery_percent()); | 169 power_properties.SetInteger("battery_percent", proto.battery_percent()); |
136 power_properties.SetInteger("battery_state", proto.battery_state()); | 170 power_properties.SetInteger("battery_state", proto.battery_state()); |
137 power_properties.SetInteger("external_power", proto.external_power()); | 171 power_properties.SetInteger("external_power", proto.external_power()); |
138 power_properties.SetInteger("battery_time_to_empty_sec", | 172 power_properties.SetInteger("battery_time_to_empty_sec", |
139 proto.battery_time_to_empty_sec()); | 173 proto.battery_time_to_empty_sec()); |
140 power_properties.SetInteger("battery_time_to_full_sec", | 174 power_properties.SetInteger("battery_time_to_full_sec", |
141 proto.battery_time_to_full_sec()); | 175 proto.battery_time_to_full_sec()); |
142 | 176 |
143 owner_->web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback, | 177 owner_->web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback, |
144 power_properties); | 178 power_properties); |
145 } | 179 } |
146 | 180 |
147 DeviceEmulatorMessageHandler::DeviceEmulatorMessageHandler() | 181 DeviceEmulatorMessageHandler::DeviceEmulatorMessageHandler() |
148 : fake_bluetooth_device_client_( | 182 : fake_bluetooth_device_client_( |
149 static_cast<chromeos::FakeBluetoothDeviceClient*>( | 183 static_cast<chromeos::FakeBluetoothDeviceClient*>( |
150 chromeos::DBusThreadManager::Get() | 184 chromeos::DBusThreadManager::Get() |
151 ->GetBluetoothDeviceClient())), | 185 ->GetBluetoothDeviceClient())), |
| 186 fake_cras_audio_client_(static_cast<chromeos::FakeCrasAudioClient*>( |
| 187 chromeos::DBusThreadManager::Get() |
| 188 ->GetCrasAudioClient())), |
152 fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( | 189 fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( |
153 chromeos::DBusThreadManager::Get() | 190 chromeos::DBusThreadManager::Get() |
154 ->GetPowerManagerClient())) {} | 191 ->GetPowerManagerClient())) {} |
155 | 192 |
156 DeviceEmulatorMessageHandler::~DeviceEmulatorMessageHandler() { | 193 DeviceEmulatorMessageHandler::~DeviceEmulatorMessageHandler() { |
157 } | 194 } |
158 | 195 |
159 void DeviceEmulatorMessageHandler::Init() { | 196 void DeviceEmulatorMessageHandler::Init() { |
160 bluetooth_observer_.reset(new BluetoothObserver(this)); | 197 bluetooth_observer_.reset(new BluetoothObserver(this)); |
| 198 cras_audio_observer_.reset(new CrasAudioObserver(this)); |
161 power_observer_.reset(new PowerObserver(this)); | 199 power_observer_.reset(new PowerObserver(this)); |
162 } | 200 } |
163 | 201 |
164 void DeviceEmulatorMessageHandler::RequestPowerInfo( | 202 void DeviceEmulatorMessageHandler::RequestPowerInfo( |
165 const base::ListValue* args) { | 203 const base::ListValue* args) { |
166 fake_power_manager_client_->RequestStatusUpdate(); | 204 fake_power_manager_client_->RequestStatusUpdate(); |
167 } | 205 } |
168 | 206 |
169 void DeviceEmulatorMessageHandler::HandleRemoveBluetoothDevice( | 207 void DeviceEmulatorMessageHandler::HandleRemoveBluetoothDevice( |
170 const base::ListValue* args) { | 208 const base::ListValue* args) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 // Try to pair the device with the main adapter. The device is identified | 270 // Try to pair the device with the main adapter. The device is identified |
233 // by its device ID, which, in this case is the same as its address. | 271 // by its device ID, which, in this case is the same as its address. |
234 ash::Shell::GetInstance()->system_tray_delegate()->ConnectToBluetoothDevice( | 272 ash::Shell::GetInstance()->system_tray_delegate()->ConnectToBluetoothDevice( |
235 props->address.value()); | 273 props->address.value()); |
236 if (!props->paired.value()) { | 274 if (!props->paired.value()) { |
237 web_ui()->CallJavascriptFunction(kPairFailedJSCallback, | 275 web_ui()->CallJavascriptFunction(kPairFailedJSCallback, |
238 base::StringValue(path)); | 276 base::StringValue(path)); |
239 } | 277 } |
240 } | 278 } |
241 | 279 |
| 280 void DeviceEmulatorMessageHandler::HandleRequestAudioNodes( |
| 281 const base::ListValue* args) { |
| 282 // Get every active audio node and create a dictionary to |
| 283 // send it to JavaScript. |
| 284 base::ListValue audio_nodes; |
| 285 for (const AudioNode& node : fake_cras_audio_client_->node_list()) { |
| 286 scoped_ptr<base::DictionaryValue> audio_node(new base::DictionaryValue()); |
| 287 |
| 288 audio_node->SetBoolean("isInput", node.is_input); |
| 289 audio_node->SetString("id", base::Uint64ToString(node.id)); |
| 290 audio_node->SetString("deviceName", node.device_name); |
| 291 audio_node->SetString("type", node.type); |
| 292 audio_node->SetString("name", node.name); |
| 293 audio_node->SetBoolean("active", node.active); |
| 294 |
| 295 audio_nodes.Append(audio_node.Pass()); |
| 296 } |
| 297 web_ui()->CallJavascriptFunction(kUpdateAudioNodes, audio_nodes); |
| 298 } |
| 299 |
| 300 void DeviceEmulatorMessageHandler::HandleInsertAudioNode( |
| 301 const base::ListValue* args) { |
| 302 AudioNode audio_node; |
| 303 const base::DictionaryValue* device_dict = nullptr; |
| 304 |
| 305 CHECK(args->GetDictionary(0, &device_dict)); |
| 306 CHECK(args->GetString(1, &audio_node.type)); |
| 307 |
| 308 CHECK(device_dict->GetBoolean("isInput", &audio_node.is_input)); |
| 309 CHECK(device_dict->GetString("deviceName", &audio_node.device_name)); |
| 310 CHECK(device_dict->GetString("name", &audio_node.name)); |
| 311 CHECK(device_dict->GetBoolean("active", &audio_node.active)); |
| 312 |
| 313 std::string tmp_id; |
| 314 CHECK(device_dict->GetString("id", &tmp_id)); |
| 315 CHECK(base::StringToUint64(tmp_id, &audio_node.id)); |
| 316 |
| 317 fake_cras_audio_client_->InsertAudioNodeToList(audio_node); |
| 318 } |
| 319 |
| 320 void DeviceEmulatorMessageHandler::HandleRemoveAudioNode( |
| 321 const base::ListValue* args) { |
| 322 std::string tmp_id; |
| 323 uint64 id; |
| 324 CHECK(args->GetString(0, &tmp_id)); |
| 325 CHECK(base::StringToUint64(tmp_id, &id)); |
| 326 |
| 327 fake_cras_audio_client_->RemoveAudioNodeFromList(id); |
| 328 } |
| 329 |
242 void DeviceEmulatorMessageHandler::UpdateBatteryPercent( | 330 void DeviceEmulatorMessageHandler::UpdateBatteryPercent( |
243 const base::ListValue* args) { | 331 const base::ListValue* args) { |
244 int new_percent; | 332 int new_percent; |
245 if (args->GetInteger(0, &new_percent)) { | 333 if (args->GetInteger(0, &new_percent)) { |
246 power_manager::PowerSupplyProperties props = | 334 power_manager::PowerSupplyProperties props = |
247 fake_power_manager_client_->props(); | 335 fake_power_manager_client_->props(); |
248 props.set_battery_percent(new_percent); | 336 props.set_battery_percent(new_percent); |
249 fake_power_manager_client_->UpdatePowerProperties(props); | 337 fake_power_manager_client_->UpdatePowerProperties(props); |
250 } | 338 } |
251 } | 339 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 base::Unretained(this))); | 417 base::Unretained(this))); |
330 web_ui()->RegisterMessageCallback( | 418 web_ui()->RegisterMessageCallback( |
331 kBluetoothPairFunction, | 419 kBluetoothPairFunction, |
332 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, | 420 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, |
333 base::Unretained(this))); | 421 base::Unretained(this))); |
334 web_ui()->RegisterMessageCallback( | 422 web_ui()->RegisterMessageCallback( |
335 kRequestBluetoothInfo, | 423 kRequestBluetoothInfo, |
336 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo, | 424 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo, |
337 base::Unretained(this))); | 425 base::Unretained(this))); |
338 web_ui()->RegisterMessageCallback( | 426 web_ui()->RegisterMessageCallback( |
| 427 kRequestAudioNodes, |
| 428 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestAudioNodes, |
| 429 base::Unretained(this))); |
| 430 web_ui()->RegisterMessageCallback( |
| 431 kInsertAudioNode, |
| 432 base::Bind(&DeviceEmulatorMessageHandler::HandleInsertAudioNode, |
| 433 base::Unretained(this))); |
| 434 web_ui()->RegisterMessageCallback( |
| 435 kRemoveAudioNode, |
| 436 base::Bind(&DeviceEmulatorMessageHandler::HandleRemoveAudioNode, |
339 kRemoveBluetoothDevice, | 437 kRemoveBluetoothDevice, |
340 base::Bind(&DeviceEmulatorMessageHandler::HandleRemoveBluetoothDevice, | 438 base::Bind(&DeviceEmulatorMessageHandler::HandleRemoveBluetoothDevice, |
341 base::Unretained(this))); | 439 base::Unretained(this))); |
342 } | 440 } |
343 | 441 |
344 std::string DeviceEmulatorMessageHandler::CreateBluetoothDeviceFromListValue( | 442 std::string DeviceEmulatorMessageHandler::CreateBluetoothDeviceFromListValue( |
345 const base::ListValue* args) { | 443 const base::ListValue* args) { |
346 const base::DictionaryValue* device_dict = nullptr; | 444 const base::DictionaryValue* device_dict = nullptr; |
347 FakeBluetoothDeviceClient::IncomingDeviceProperties props; | 445 FakeBluetoothDeviceClient::IncomingDeviceProperties props; |
348 | 446 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 for (const std::string& uuid : props->uuids.value()) { | 495 for (const std::string& uuid : props->uuids.value()) { |
398 uuids->AppendString(uuid); | 496 uuids->AppendString(uuid); |
399 } | 497 } |
400 | 498 |
401 device->Set("uuids", uuids.Pass()); | 499 device->Set("uuids", uuids.Pass()); |
402 | 500 |
403 return device.Pass(); | 501 return device.Pass(); |
404 } | 502 } |
405 | 503 |
406 } // namespace chromeos | 504 } // namespace chromeos |
OLD | NEW |