| 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 7a6b7c03af6b4579eaf2e11797d1a8bd2b58e846..4bc2fc183c5c0f28ae146e22075e91d519e1b75c 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,23 @@
|
|
|
| #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_bluetooth_adapter_client.h"
|
| +#include "chromeos/dbus/fake_bluetooth_device_client.h"
|
| #include "chromeos/dbus/fake_power_manager_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
|
| @@ -29,22 +33,111 @@ 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 kRemoveBluetoothDeviceJSCallback[] =
|
| + "device_emulator.bluetoothSettings.removeBluetoothDevice";
|
| +const char kUpdateBluetoothInfoJSCallback[] =
|
| + "device_emulator.bluetoothSettings.updateBluetoothInfo";
|
| const char kUpdatePowerPropertiesJSCallback[] =
|
| "device_emulator.batterySettings.updatePowerProperties";
|
|
|
| } // namespace
|
|
|
| +namespace chromeos {
|
| +
|
| +class DeviceEmulatorMessageHandler::BluetoothObserver
|
| + : public BluetoothDeviceClient::Observer {
|
| + public:
|
| + explicit BluetoothObserver(DeviceEmulatorMessageHandler* owner)
|
| + : owner_(owner) {
|
| + owner_->fake_bluetooth_device_client_->AddObserver(this);
|
| + }
|
| +
|
| + ~BluetoothObserver() override {
|
| + owner_->fake_bluetooth_device_client_->RemoveObserver(this);
|
| + }
|
| +
|
| + // chromeos::BluetoothDeviceClient::Observer.
|
| + void DeviceAdded(const dbus::ObjectPath& object_path) override;
|
| +
|
| + // chromeos::BluetoothDeviceClient::Observer.
|
| + void DeviceRemoved(const dbus::ObjectPath& object_path) override;
|
| +
|
| + private:
|
| + DeviceEmulatorMessageHandler* owner_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(BluetoothObserver);
|
| +};
|
| +
|
| +void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded(
|
| + const dbus::ObjectPath& object_path) {
|
| + scoped_ptr<base::DictionaryValue> device = owner_->GetDeviceInfo(
|
| + object_path);
|
| +
|
| + // Request to add the device to the view's list of devices.
|
| + owner_->web_ui()->CallJavascriptFunction(kAddBluetoothDeviceJSCallback,
|
| + *device);
|
| +}
|
| +
|
| +void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved(
|
| + const dbus::ObjectPath& object_path) {
|
| + owner_->web_ui()->CallJavascriptFunction(kRemoveBluetoothDeviceJSCallback,
|
| + base::StringValue(object_path.value()));
|
| +}
|
| +
|
| +class DeviceEmulatorMessageHandler::PowerObserver
|
| + : public PowerManagerClient::Observer {
|
| + public:
|
| + explicit PowerObserver(DeviceEmulatorMessageHandler* owner)
|
| + : owner_(owner) {
|
| + owner_->fake_power_manager_client_->AddObserver(this);
|
| + }
|
| +
|
| + ~PowerObserver() override {
|
| + owner_->fake_power_manager_client_->RemoveObserver(this);
|
| + }
|
| +
|
| + void PowerChanged(
|
| + const power_manager::PowerSupplyProperties& proto) override;
|
| +
|
| + private:
|
| + DeviceEmulatorMessageHandler* owner_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PowerObserver);
|
| +};
|
| +
|
| +void DeviceEmulatorMessageHandler::PowerObserver::PowerChanged(
|
| + const power_manager::PowerSupplyProperties& proto) {
|
| + base::DictionaryValue power_properties;
|
| +
|
| + power_properties.SetInteger("battery_percent", proto.battery_percent());
|
| + power_properties.SetInteger("battery_state", proto.battery_state());
|
| + power_properties.SetInteger("external_power", proto.external_power());
|
| + power_properties.SetInteger("battery_time_to_empty_sec",
|
| + proto.battery_time_to_empty_sec());
|
| + power_properties.SetInteger("battery_time_to_full_sec",
|
| + proto.battery_time_to_full_sec());
|
| +
|
| + owner_->web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback,
|
| + power_properties);
|
| +}
|
| +
|
| 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_power_manager_client_->RemoveObserver(this);
|
| }
|
|
|
| void DeviceEmulatorMessageHandler::Init() {
|
| - fake_power_manager_client_->AddObserver(this);
|
| + bluetooth_observer_.reset(new BluetoothObserver(this));
|
| + power_observer_.reset(new PowerObserver(this));
|
| }
|
|
|
| void DeviceEmulatorMessageHandler::RequestPowerInfo(
|
| @@ -54,17 +147,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));
|
| +
|
| + base::ListValue devices;
|
| +
|
| + // Get each device's properties.
|
| + for (const dbus::ObjectPath& path : paths) {
|
| + scoped_ptr<base::DictionaryValue> device = GetDeviceInfo(path);
|
| + 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(
|
| @@ -126,22 +242,6 @@ void DeviceEmulatorMessageHandler::UpdateTimeToFull(
|
| }
|
| }
|
|
|
| -void DeviceEmulatorMessageHandler::PowerChanged(
|
| - const power_manager::PowerSupplyProperties& proto) {
|
| - base::DictionaryValue power_properties;
|
| -
|
| - power_properties.SetInteger("battery_percent", proto.battery_percent());
|
| - power_properties.SetInteger("battery_state", proto.battery_state());
|
| - power_properties.SetInteger("external_power", proto.external_power());
|
| - power_properties.SetInteger("battery_time_to_empty_sec",
|
| - proto.battery_time_to_empty_sec());
|
| - power_properties.SetInteger("battery_time_to_full_sec",
|
| - proto.battery_time_to_full_sec());
|
| -
|
| - web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback,
|
| - power_properties);
|
| -}
|
| -
|
| void DeviceEmulatorMessageHandler::RegisterMessages() {
|
| web_ui()->RegisterMessageCallback(
|
| kRequestPowerInfo,
|
| @@ -175,4 +275,67 @@ void DeviceEmulatorMessageHandler::RegisterMessages() {
|
| kBluetoothPairFunction,
|
| base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair,
|
| base::Unretained(this)));
|
| + web_ui()->RegisterMessageCallback(
|
| + kRequestBluetoothInfo,
|
| + base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +std::string DeviceEmulatorMessageHandler::CreateBluetoothDeviceFromListValue(
|
| + const base::ListValue* args) {
|
| + const base::DictionaryValue* device_dict = nullptr;
|
| + FakeBluetoothDeviceClient::IncomingDeviceProperties props;
|
| +
|
| + CHECK(args->GetDictionary(0, &device_dict));
|
| + CHECK(device_dict->GetString("path", &props.device_path));
|
| + CHECK(device_dict->GetString("name", &props.device_name));
|
| + CHECK(device_dict->GetString("alias", &props.device_alias));
|
| + CHECK(device_dict->GetString("address", &props.device_address));
|
| + CHECK(device_dict->GetString("pairingMethod", &props.pairing_method));
|
| + CHECK(device_dict->GetString("pairingAuthToken", &props.pairing_auth_token));
|
| + CHECK(device_dict->GetInteger("classValue", &props.device_class));
|
| + CHECK(device_dict->GetBoolean("isTrusted", &props.is_trusted));
|
| +
|
| + // Create the device and store it in the FakeBluetoothDeviceClient's observed
|
| + // list of devices.
|
| + fake_bluetooth_device_client_->CreateDeviceWithProperties(
|
| + dbus::ObjectPath(chromeos::FakeBluetoothAdapterClient::kAdapterPath),
|
| + props);
|
| +
|
| + return props.device_path;
|
| }
|
| +
|
| +scoped_ptr<base::DictionaryValue> DeviceEmulatorMessageHandler::GetDeviceInfo(
|
| + 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);
|
| + } else {
|
| + device->SetString("pairingMethod", "");
|
| + device->SetString("pairingAuthToken", "");
|
| + }
|
| + device->SetInteger("classValue", props->bluetooth_class.value());
|
| + device->SetBoolean("isTrusted", props->trusted.value());
|
| +
|
| + for (const std::string& uuid : props->uuids.value()) {
|
| + uuids->AppendString(uuid);
|
| + }
|
| +
|
| + device->Set("uuids", uuids.Pass());
|
| +
|
| + return device.Pass();
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|