Chromium Code Reviews| Index: chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_handler.cc |
| diff --git a/chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_handler.cc b/chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_handler.cc |
| index 83b0af469c120e9e1f88e6de44394d81965bded6..b0a343d8ad2939fd4b7ae76f7bea214ccfb9383f 100644 |
| --- a/chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_handler.cc |
| +++ b/chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_handler.cc |
| @@ -4,19 +4,21 @@ |
| #include "chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_handler.h" |
| -#include <stdlib.h> |
| - |
| +#include "ash/shell.h" |
| +#include "ash/system/tray/system_tray_delegate.h" |
| #include "base/bind.h" |
| #include "base/values.h" |
| #include "chromeos/dbus/dbus_thread_manager.h" |
| -#include "chromeos/dbus/fake_power_manager_client.h" |
| +#include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
| #include "content/public/browser/web_ui.h" |
| +#include "device/bluetooth/bluetooth_device_chromeos.h" |
| namespace { |
| // Define the name of the callback functions that will be used by JavaScript. |
| const char kBluetoothDiscoverFunction[] = "requestBluetoothDiscover"; |
| const char kBluetoothPairFunction[] = "requestBluetoothPair"; |
| +const char kRequestBluetoothInfo[] = "requestBluetoothInfo"; |
| const char kRequestPowerInfo[] = "requestPowerInfo"; |
| // Define update functions that will update the power properties to the |
| @@ -28,21 +30,96 @@ const char kUpdateTimeToFull[] = "updateTimeToFull"; |
| // Define callback functions that will update the JavaScript variable |
| // and the web UI. |
| +const char kAddBluetoothDeviceJSCallback[] = |
| + "device_emulator.bluetoothSettings.addBluetoothDevice"; |
| +const char kUpdateBluetoothInfoJSCallback[] = |
| + "device_emulator.bluetoothSettings.updateBluetoothInfo"; |
| const char kUpdatePowerPropertiesJSCallback[] = |
| "device_emulator.batterySettings.updatePowerProperties"; |
| } // namespace |
| DeviceEmulatorMessageHandler::DeviceEmulatorMessageHandler() |
| - : fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( |
| + : fake_bluetooth_device_client_( |
| + static_cast<chromeos::FakeBluetoothDeviceClient*>( |
| + chromeos::DBusThreadManager::Get() |
| + ->GetBluetoothDeviceClient())), |
| + fake_power_manager_client_(static_cast<chromeos::FakePowerManagerClient*>( |
| chromeos::DBusThreadManager::Get() |
| ->GetPowerManagerClient())) {} |
| DeviceEmulatorMessageHandler::~DeviceEmulatorMessageHandler() { |
| + fake_bluetooth_device_client_->RemoveObserver(this); |
| fake_power_manager_client_->RemoveObserver(this); |
| } |
| +void DeviceEmulatorMessageHandler::DeviceAdded( |
| + const dbus::ObjectPath& object_path) { |
| + scoped_ptr<base::DictionaryValue> device = GetDeviceInfo(object_path); |
| + |
| + // Request to add the device to the view's list of devices. |
| + web_ui()->CallJavascriptFunction(kAddBluetoothDeviceJSCallback, *device); |
| +} |
| + |
| +std::string DeviceEmulatorMessageHandler::CreateBluetoothDeviceFromListValue( |
|
xiyuan
2015/07/30 17:21:22
This does not match the declaration order in heade
rfrappier
2015/07/30 22:21:39
Done.
|
| + const base::ListValue* args) { |
| + const base::DictionaryValue* device_dict = NULL; |
|
stevenjb
2015/07/30 18:28:51
s/NULL/nullptr/
rfrappier
2015/07/30 22:21:39
Done.
|
| + std::string path, name, alias, address, pairing_method, pairing_auth_token; |
| + int device_class; |
| + bool is_trusted; |
| + |
| + args->GetDictionary(0, &device_dict); |
| + device_dict->GetString("path", &path); |
| + device_dict->GetString("name", &name); |
| + device_dict->GetString("alias", &alias); |
| + device_dict->GetString("address", &address); |
| + device_dict->GetString("pairingMethod", &pairing_method); |
| + device_dict->GetString("pairingAuthToken", &pairing_auth_token); |
| + device_dict->GetInteger("classValue", &device_class); |
| + device_dict->GetBoolean("isTrusted", &is_trusted); |
|
xiyuan
2015/07/30 17:21:22
nit: CHECK() all the Getxxx.
rfrappier
2015/07/30 22:21:39
Done.
|
| + |
| + // Create the device and store it in the FakeBluetoothDeviceClient's observed |
| + // list of devices. |
| + fake_bluetooth_device_client_->CreateDeviceWithProperties( |
| + dbus::ObjectPath(chromeos::FakeBluetoothAdapterClient::kAdapterPath), |
| + dbus::ObjectPath(path), name, alias, address, pairing_method, |
| + pairing_auth_token, device_class, is_trusted); |
| + |
| + return path; |
| +} |
| + |
| +scoped_ptr<base::DictionaryValue> DeviceEmulatorMessageHandler::GetDeviceInfo( |
|
xiyuan
2015/07/30 17:21:22
move this down to match header file order.
rfrappier
2015/07/30 22:21:39
Done.
|
| + const dbus::ObjectPath& object_path) { |
| + // Get the device's properties. |
| + chromeos::FakeBluetoothDeviceClient::Properties* props = |
| + fake_bluetooth_device_client_->GetProperties(object_path); |
| + scoped_ptr<base::DictionaryValue> device(new base::DictionaryValue()); |
| + scoped_ptr<base::ListValue> uuids(new base::ListValue); |
| + chromeos::FakeBluetoothDeviceClient::SimulatedPairingOptions* options = |
| + fake_bluetooth_device_client_->GetPairingOptions(object_path); |
| + |
| + device->SetString("path", object_path.value()); |
| + device->SetString("name", props->name.value()); |
| + device->SetString("alias", props->alias.value()); |
| + device->SetString("address", props->address.value()); |
| + if (options) { |
| + device->SetString("pairingMethod", options->pairing_method); |
| + device->SetString("pairingAuthToken", options->pairing_auth_token); |
| + } |
| + device->SetInteger("classValue", props->bluetooth_class.value()); |
| + device->SetBoolean("isTrusted", props->trusted.value()); |
| + |
| + for (size_t i = 0; i < props->uuids.value().size(); ++i) { |
| + uuids->AppendString(props->uuids.value()[i]); |
| + } |
| + |
| + device->Set("uuids", uuids.Pass()); |
| + |
| + return device; |
| +} |
| + |
| void DeviceEmulatorMessageHandler::Init() { |
| + fake_bluetooth_device_client_->AddObserver(this); |
| fake_power_manager_client_->AddObserver(this); |
| } |
| @@ -53,17 +130,40 @@ void DeviceEmulatorMessageHandler::RequestPowerInfo( |
| void DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover( |
| const base::ListValue* args) { |
| - const base::DictionaryValue* device = NULL; |
| - args->GetDictionary(0, &device); |
| - // TODO(rfrapp): Create device if it doesn't exist and discover it. |
| + CreateBluetoothDeviceFromListValue(args); |
| +} |
| + |
| +void DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo( |
| + const base::ListValue* args) { |
| + // Get a list containing paths of the devices which are connected to |
| + // the main adapter. |
| + std::vector<dbus::ObjectPath> paths = |
| + fake_bluetooth_device_client_->GetDevicesForAdapter( |
| + dbus::ObjectPath(chromeos::FakeBluetoothAdapterClient::kAdapterPath)); |
| + |
| + scoped_ptr<base::ListValue> devices(new base::ListValue); |
|
xiyuan
2015/07/30 17:21:22
nit: "base::ListValue devices;" works so not neces
rfrappier
2015/07/30 22:21:39
Done.
|
| + |
| + // Get each device's properties. |
| + for (size_t i = 0; i < paths.size(); ++i) { |
| + scoped_ptr<base::DictionaryValue> device = GetDeviceInfo(paths[i]); |
| + devices->Append(device.Pass()); |
| + } |
| + |
| + // Send the list of devices to the view. |
| + web_ui()->CallJavascriptFunction(kUpdateBluetoothInfoJSCallback, *devices); |
| } |
| void DeviceEmulatorMessageHandler::HandleRequestBluetoothPair( |
| const base::ListValue* args) { |
| - const base::DictionaryValue* device = NULL; |
| - args->GetDictionary(0, &device); |
| - // TODO(rfrapp): Create device if it doesn't exist and pair it to the main |
| - // adapter. |
| + // Create the device if it does not already exist. |
| + std::string path = CreateBluetoothDeviceFromListValue(args); |
| + chromeos::FakeBluetoothDeviceClient::Properties* props = |
| + fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(path)); |
| + |
| + // Try to pair the device with the main adapter. The device is identified |
| + // by its device ID, which, in this case is the same as its address. |
| + ash::Shell::GetInstance()->system_tray_delegate()->ConnectToBluetoothDevice( |
| + props->address.value()); |
| } |
| void DeviceEmulatorMessageHandler::UpdateBatteryPercent( |
| @@ -108,6 +208,7 @@ void DeviceEmulatorMessageHandler::UpdateTimeToFull( |
| power_manager::PowerSupplyProperties props = |
| fake_power_manager_client_->props(); |
| int new_time; |
| + |
| if (args->GetInteger(0, &new_time)) |
| props.set_battery_time_to_full_sec(new_time); |
| fake_power_manager_client_->UpdatePowerProperties(props); |
| @@ -154,4 +255,8 @@ void DeviceEmulatorMessageHandler::RegisterMessages() { |
| kBluetoothPairFunction, |
| base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, |
| base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + kRequestBluetoothInfo, |
| + base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo, |
| + base::Unretained(this))); |
| } |