Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5395)

Unified Diff: chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_handler.cc

Issue 1258783009: Add functionality to Bluetooth settings UI. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..fb77f2b48f0194b0f458a696acc64cb5d5acd3ab 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
@@ -28,22 +32,107 @@ 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) {
+ owner_->web_ui()->CallJavascriptFunction(
+ kUpdatePowerPropertiesJSCallback,
+ base::FundamentalValue(proto.battery_percent()),
+ base::FundamentalValue(proto.external_power()),
+ base::FundamentalValue(
+ static_cast<int>(proto.battery_time_to_empty_sec())),
+ base::FundamentalValue(
+ static_cast<int>(proto.battery_time_to_full_sec())));
+}
+
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(
@@ -53,17 +142,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(
@@ -108,23 +220,12 @@ 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);
}
-void DeviceEmulatorMessageHandler::PowerChanged(
- const power_manager::PowerSupplyProperties& proto) {
- web_ui()->CallJavascriptFunction(
- kUpdatePowerPropertiesJSCallback,
- base::FundamentalValue(proto.battery_percent()),
- base::FundamentalValue(proto.external_power()),
- base::FundamentalValue(
- static_cast<int>(proto.battery_time_to_empty_sec())),
- base::FundamentalValue(
- static_cast<int>(proto.battery_time_to_full_sec())));
-}
-
void DeviceEmulatorMessageHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
kRequestPowerInfo,
@@ -154,4 +255,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

Powered by Google App Engine
This is Rietveld 408576698