| 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/tray/system_tray_delegate.h" | 8 #include "ash/system/tray/system_tray_delegate.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | 13 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
| 13 #include "chromeos/dbus/fake_bluetooth_device_client.h" | 14 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
| 15 #include "chromeos/dbus/fake_cras_audio_client.h" |
| 14 #include "chromeos/dbus/fake_power_manager_client.h" | 16 #include "chromeos/dbus/fake_power_manager_client.h" |
| 15 #include "content/public/browser/web_ui.h" | 17 #include "content/public/browser/web_ui.h" |
| 16 #include "device/bluetooth/bluetooth_device_chromeos.h" | 18 #include "device/bluetooth/bluetooth_device_chromeos.h" |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // Define the name of the callback functions that will be used by JavaScript. | 22 // Define the name of the callback functions that will be used by JavaScript. |
| 21 const char kBluetoothDiscoverFunction[] = "requestBluetoothDiscover"; | 23 const char kBluetoothDiscoverFunction[] = "requestBluetoothDiscover"; |
| 22 const char kBluetoothPairFunction[] = "requestBluetoothPair"; | 24 const char kBluetoothPairFunction[] = "requestBluetoothPair"; |
| 23 const char kRequestBluetoothInfo[] = "requestBluetoothInfo"; | 25 const char kRequestBluetoothInfo[] = "requestBluetoothInfo"; |
| 24 const char kRequestPowerInfo[] = "requestPowerInfo"; | 26 const char kRequestPowerInfo[] = "requestPowerInfo"; |
| 27 const char kRequestAudioNodes[] = "requestAudioNodes"; |
| 28 |
| 29 // Define update function that will update the state of the audio ui. |
| 30 const char kInsertAudioNode[] = "insertAudioNode"; |
| 31 const char kRemoveAudioNode[] = "removeAudioNode"; |
| 25 | 32 |
| 26 // Define update functions that will update the power properties to the | 33 // Define update functions that will update the power properties to the |
| 27 // variables defined in the web UI. | 34 // variables defined in the web UI. |
| 28 const char kRemoveBluetoothDevice[] = "removeBluetoothDevice"; | 35 const char kRemoveBluetoothDevice[] = "removeBluetoothDevice"; |
| 29 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; | 36 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; |
| 30 const char kUpdateBatteryState[] = "updateBatteryState"; | 37 const char kUpdateBatteryState[] = "updateBatteryState"; |
| 31 const char kUpdateExternalPower[] = "updateExternalPower"; | 38 const char kUpdateExternalPower[] = "updateExternalPower"; |
| 32 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; | 39 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; |
| 33 const char kUpdateTimeToFull[] = "updateTimeToFull"; | 40 const char kUpdateTimeToFull[] = "updateTimeToFull"; |
| 34 | 41 |
| 35 // Define callback functions that will update the JavaScript variable | 42 // Define callback functions that will update the JavaScript variable |
| 36 // and the web UI. | 43 // and the web UI. |
| 44 const char kUpdateAudioNodes[] = |
| 45 "device_emulator.audioSettings.updateAudioNodes"; |
| 37 const char kAddBluetoothDeviceJSCallback[] = | 46 const char kAddBluetoothDeviceJSCallback[] = |
| 38 "device_emulator.bluetoothSettings.addBluetoothDevice"; | 47 "device_emulator.bluetoothSettings.addBluetoothDevice"; |
| 39 const char kDevicePairedFromTrayJSCallback[] = | 48 const char kDevicePairedFromTrayJSCallback[] = |
| 40 "device_emulator.bluetoothSettings.devicePairedFromTray"; | 49 "device_emulator.bluetoothSettings.devicePairedFromTray"; |
| 41 const char kDeviceRemovedFromMainAdapterJSCallback[] = | 50 const char kDeviceRemovedFromMainAdapterJSCallback[] = |
| 42 "device_emulator.bluetoothSettings.deviceRemovedFromMainAdapter"; | 51 "device_emulator.bluetoothSettings.deviceRemovedFromMainAdapter"; |
| 43 const char kPairFailedJSCallback[] = | 52 const char kPairFailedJSCallback[] = |
| 44 "device_emulator.bluetoothSettings.pairFailed"; | 53 "device_emulator.bluetoothSettings.pairFailed"; |
| 45 const char kUpdateBluetoothInfoJSCallback[] = | 54 const char kUpdateBluetoothInfoJSCallback[] = |
| 46 "device_emulator.bluetoothSettings.updateBluetoothInfo"; | 55 "device_emulator.bluetoothSettings.updateBluetoothInfo"; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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( |
| 106 kDeviceRemovedFromMainAdapterJSCallback, | 115 kDeviceRemovedFromMainAdapterJSCallback, |
| 107 base::StringValue(object_path.value())); | 116 base::StringValue(object_path.value())); |
| 108 } | 117 } |
| 109 | 118 |
| 119 class DeviceEmulatorMessageHandler::CrasAudioObserver |
| 120 : public CrasAudioClient::Observer { |
| 121 public: |
| 122 explicit CrasAudioObserver(DeviceEmulatorMessageHandler* owner) |
| 123 : owner_(owner) { |
| 124 owner_->fake_cras_audio_client_->AddObserver(this); |
| 125 } |
| 126 |
| 127 ~CrasAudioObserver() override { |
| 128 owner_->fake_cras_audio_client_->RemoveObserver(this); |
| 129 } |
| 130 |
| 131 // chromeos::CrasAudioClient::Observer. |
| 132 void NodesChanged() override; |
| 133 |
| 134 private: |
| 135 DeviceEmulatorMessageHandler* owner_; |
| 136 DISALLOW_COPY_AND_ASSIGN(CrasAudioObserver); |
| 137 }; |
| 138 |
| 139 void DeviceEmulatorMessageHandler::CrasAudioObserver::NodesChanged() { |
| 140 owner_->HandleRequestAudioNodes(nullptr); |
| 141 } |
| 142 |
| 110 class DeviceEmulatorMessageHandler::PowerObserver | 143 class DeviceEmulatorMessageHandler::PowerObserver |
| 111 : public PowerManagerClient::Observer { | 144 : public PowerManagerClient::Observer { |
| 112 public: | 145 public: |
| 113 explicit PowerObserver(DeviceEmulatorMessageHandler* owner) | 146 explicit PowerObserver(DeviceEmulatorMessageHandler* owner) |
| 114 : owner_(owner) { | 147 : owner_(owner) { |
| 115 owner_->fake_power_manager_client_->AddObserver(this); | 148 owner_->fake_power_manager_client_->AddObserver(this); |
| 116 } | 149 } |
| 117 | 150 |
| 118 ~PowerObserver() override { | 151 ~PowerObserver() override { |
| 119 owner_->fake_power_manager_client_->RemoveObserver(this); | 152 owner_->fake_power_manager_client_->RemoveObserver(this); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 142 | 175 |
| 143 owner_->web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback, | 176 owner_->web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback, |
| 144 power_properties); | 177 power_properties); |
| 145 } | 178 } |
| 146 | 179 |
| 147 DeviceEmulatorMessageHandler::DeviceEmulatorMessageHandler() | 180 DeviceEmulatorMessageHandler::DeviceEmulatorMessageHandler() |
| 148 : fake_bluetooth_device_client_( | 181 : fake_bluetooth_device_client_( |
| 149 static_cast<chromeos::FakeBluetoothDeviceClient*>( | 182 static_cast<chromeos::FakeBluetoothDeviceClient*>( |
| 150 chromeos::DBusThreadManager::Get() | 183 chromeos::DBusThreadManager::Get() |
| 151 ->GetBluetoothDeviceClient())), | 184 ->GetBluetoothDeviceClient())), |
| 185 fake_cras_audio_client_(static_cast<chromeos::FakeCrasAudioClient*>( |
| 186 chromeos::DBusThreadManager::Get() |
| 187 ->GetCrasAudioClient())), |
| 152 fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( | 188 fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( |
| 153 chromeos::DBusThreadManager::Get() | 189 chromeos::DBusThreadManager::Get() |
| 154 ->GetPowerManagerClient())) {} | 190 ->GetPowerManagerClient())) {} |
| 155 | 191 |
| 156 DeviceEmulatorMessageHandler::~DeviceEmulatorMessageHandler() { | 192 DeviceEmulatorMessageHandler::~DeviceEmulatorMessageHandler() { |
| 157 } | 193 } |
| 158 | 194 |
| 159 void DeviceEmulatorMessageHandler::Init() { | 195 void DeviceEmulatorMessageHandler::Init() { |
| 160 bluetooth_observer_.reset(new BluetoothObserver(this)); | 196 bluetooth_observer_.reset(new BluetoothObserver(this)); |
| 197 cras_audio_observer_.reset(new CrasAudioObserver(this)); |
| 161 power_observer_.reset(new PowerObserver(this)); | 198 power_observer_.reset(new PowerObserver(this)); |
| 162 } | 199 } |
| 163 | 200 |
| 164 void DeviceEmulatorMessageHandler::RequestPowerInfo( | 201 void DeviceEmulatorMessageHandler::RequestPowerInfo( |
| 165 const base::ListValue* args) { | 202 const base::ListValue* args) { |
| 166 fake_power_manager_client_->RequestStatusUpdate(); | 203 fake_power_manager_client_->RequestStatusUpdate(); |
| 167 } | 204 } |
| 168 | 205 |
| 169 void DeviceEmulatorMessageHandler::HandleRemoveBluetoothDevice( | 206 void DeviceEmulatorMessageHandler::HandleRemoveBluetoothDevice( |
| 170 const base::ListValue* args) { | 207 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 | 269 // 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. | 270 // by its device ID, which, in this case is the same as its address. |
| 234 ash::Shell::GetInstance()->system_tray_delegate()->ConnectToBluetoothDevice( | 271 ash::Shell::GetInstance()->system_tray_delegate()->ConnectToBluetoothDevice( |
| 235 props->address.value()); | 272 props->address.value()); |
| 236 if (!props->paired.value()) { | 273 if (!props->paired.value()) { |
| 237 web_ui()->CallJavascriptFunction(kPairFailedJSCallback, | 274 web_ui()->CallJavascriptFunction(kPairFailedJSCallback, |
| 238 base::StringValue(path)); | 275 base::StringValue(path)); |
| 239 } | 276 } |
| 240 } | 277 } |
| 241 | 278 |
| 279 void DeviceEmulatorMessageHandler::HandleRequestAudioNodes( |
| 280 const base::ListValue* args) { |
| 281 // Get every active audio node and create a dictionary to |
| 282 // send it to JavaScript. |
| 283 base::ListValue audio_nodes; |
| 284 for (const AudioNode& node : fake_cras_audio_client_->node_list()) { |
| 285 scoped_ptr<base::DictionaryValue> audio_node(new base::DictionaryValue()); |
| 286 |
| 287 audio_node->SetBoolean("isInput", node.is_input); |
| 288 audio_node->SetString("id", base::Uint64ToString(node.id)); |
| 289 audio_node->SetString("deviceName", node.device_name); |
| 290 audio_node->SetString("type", node.type); |
| 291 audio_node->SetString("name", node.name); |
| 292 audio_node->SetBoolean("active", node.active); |
| 293 |
| 294 audio_nodes.Append(audio_node.Pass()); |
| 295 } |
| 296 web_ui()->CallJavascriptFunction(kUpdateAudioNodes, audio_nodes); |
| 297 } |
| 298 |
| 299 void DeviceEmulatorMessageHandler::HandleInsertAudioNode( |
| 300 const base::ListValue* args) { |
| 301 AudioNode audio_node; |
| 302 const base::DictionaryValue* device_dict = nullptr; |
| 303 |
| 304 CHECK(args->GetDictionary(0, &device_dict)); |
| 305 CHECK(device_dict->GetBoolean("isInput", &audio_node.is_input)); |
| 306 CHECK(device_dict->GetString("deviceName", &audio_node.device_name)); |
| 307 CHECK(device_dict->GetString("type", &audio_node.type)); |
| 308 CHECK(device_dict->GetString("name", &audio_node.name)); |
| 309 CHECK(device_dict->GetBoolean("active", &audio_node.active)); |
| 310 |
| 311 std::string tmp_id; |
| 312 CHECK(device_dict->GetString("id", &tmp_id)); |
| 313 CHECK(base::StringToUint64(tmp_id, &audio_node.id)); |
| 314 |
| 315 fake_cras_audio_client_->InsertAudioNodeToList(audio_node); |
| 316 } |
| 317 |
| 318 void DeviceEmulatorMessageHandler::HandleRemoveAudioNode( |
| 319 const base::ListValue* args) { |
| 320 std::string tmp_id; |
| 321 uint64 id; |
| 322 CHECK(args->GetString(0, &tmp_id)); |
| 323 CHECK(base::StringToUint64(tmp_id, &id)); |
| 324 |
| 325 fake_cras_audio_client_->RemoveAudioNodeFromList(id); |
| 326 } |
| 327 |
| 242 void DeviceEmulatorMessageHandler::UpdateBatteryPercent( | 328 void DeviceEmulatorMessageHandler::UpdateBatteryPercent( |
| 243 const base::ListValue* args) { | 329 const base::ListValue* args) { |
| 244 int new_percent; | 330 int new_percent; |
| 245 if (args->GetInteger(0, &new_percent)) { | 331 if (args->GetInteger(0, &new_percent)) { |
| 246 power_manager::PowerSupplyProperties props = | 332 power_manager::PowerSupplyProperties props = |
| 247 fake_power_manager_client_->props(); | 333 fake_power_manager_client_->props(); |
| 248 props.set_battery_percent(new_percent); | 334 props.set_battery_percent(new_percent); |
| 249 fake_power_manager_client_->UpdatePowerProperties(props); | 335 fake_power_manager_client_->UpdatePowerProperties(props); |
| 250 } | 336 } |
| 251 } | 337 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 base::Unretained(this))); | 403 base::Unretained(this))); |
| 318 web_ui()->RegisterMessageCallback( | 404 web_ui()->RegisterMessageCallback( |
| 319 kUpdateTimeToEmpty, | 405 kUpdateTimeToEmpty, |
| 320 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToEmpty, | 406 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToEmpty, |
| 321 base::Unretained(this))); | 407 base::Unretained(this))); |
| 322 web_ui()->RegisterMessageCallback( | 408 web_ui()->RegisterMessageCallback( |
| 323 kUpdateTimeToFull, | 409 kUpdateTimeToFull, |
| 324 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToFull, | 410 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToFull, |
| 325 base::Unretained(this))); | 411 base::Unretained(this))); |
| 326 web_ui()->RegisterMessageCallback( | 412 web_ui()->RegisterMessageCallback( |
| 413 kRequestAudioNodes, |
| 414 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestAudioNodes, |
| 415 base::Unretained(this))); |
| 416 web_ui()->RegisterMessageCallback( |
| 417 kInsertAudioNode, |
| 418 base::Bind(&DeviceEmulatorMessageHandler::HandleInsertAudioNode, |
| 419 base::Unretained(this))); |
| 420 web_ui()->RegisterMessageCallback( |
| 421 kRemoveAudioNode, |
| 422 base::Bind(&DeviceEmulatorMessageHandler::HandleRemoveAudioNode, |
| 423 base::Unretained(this))); |
| 424 web_ui()->RegisterMessageCallback( |
| 327 kBluetoothDiscoverFunction, | 425 kBluetoothDiscoverFunction, |
| 328 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover, | 426 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover, |
| 329 base::Unretained(this))); | 427 base::Unretained(this))); |
| 330 web_ui()->RegisterMessageCallback( | 428 web_ui()->RegisterMessageCallback( |
| 331 kBluetoothPairFunction, | 429 kBluetoothPairFunction, |
| 332 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, | 430 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, |
| 333 base::Unretained(this))); | 431 base::Unretained(this))); |
| 334 web_ui()->RegisterMessageCallback( | 432 web_ui()->RegisterMessageCallback( |
| 335 kRequestBluetoothInfo, | 433 kRequestBluetoothInfo, |
| 336 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo, | 434 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo, |
| (...skipping 60 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 |