Commit 8640896a authored by dpapad's avatar dpapad Committed by Commit Bot

Remove CallJavascriptFunctionUnsafe calls from chrome://device-emulator, part1.

Replacing calls to global device_emulator.bluetoothSettings methods with
WebUI events and FireWebUIListener().

This is in preparation of migrating this page to Polymer3.

Bug: 1015241
Change-Id: I3d8bc7f5b008e734670393c0832f741f13195e1d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1868089Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#707396}
parent a9e8c998
......@@ -19,6 +19,9 @@ js_library("audio_settings") {
}
js_library("bluetooth_settings") {
deps = [
"//ui/webui/resources/js:web_ui_listener_behavior",
]
externs_list = [ "$externs_path/chrome_send.js" ]
}
......
......@@ -8,6 +8,7 @@
<link rel="import" href="chrome://resources/cr_elements/cr_radio_button/cr_radio_button.html">
<link rel="import" href="chrome://resources/cr_elements/cr_radio_group/cr_radio_group.html">
<link rel="import" href="chrome://resources/cr_elements/shared_style_css.html">
<link rel="import" href="chrome://resources/html/web_ui_listener_behavior.html">
<link rel="import" href="chrome://resources/polymer/v1_0/iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="icons.html">
<link rel="import" href="shared_styles.html">
......
......@@ -54,6 +54,8 @@ var BluetoothDevice = function() {
Polymer({
is: 'bluetooth-settings',
behaviors: [WebUIListenerBehavior],
properties: {
/**
* A set of bluetooth devices.
......@@ -148,6 +150,16 @@ Polymer({
devicePaths: {},
ready: function() {
this.addWebUIListener(
'bluetooth-device-added', this.addBluetoothDevice_.bind(this));
this.addWebUIListener(
'device-paired-from-tray', this.devicePairedFromTray_.bind(this));
this.addWebUIListener(
'device-removed-from-main-adapter',
this.deviceRemovedFromMainAdapter_.bind(this));
this.addWebUIListener('pair-failed', this.pairFailed_.bind(this));
this.addWebUIListener(
'bluetooth-info-updated', this.updateBluetoothInfo_.bind(this));
chrome.send('requestBluetoothInfo');
},
......@@ -274,19 +286,20 @@ Polymer({
/**
* Called by the WebUI which provides a list of devices which are connected
* to the main adapter.
* @param {!Array<!BluetoothDevice>} predefinedDevices A list of bluetooth
* devices.
* @param {!Array<!BluetoothDevice>} loadedCustomDevices
* @param {!Array<string>} pairingMethodOptions
* @param {!Array<string>} pairingActionOptions
* @param {{
* predefined_devices: !Array<!BluetoothDevice>,
* devices: !Array<!BluetoothDevice>,
* pairing_method_options: !Array<string>,
* pairing_action_options: !Array<string>,
* }} info
* @private
*/
updateBluetoothInfo: function(
predefinedDevices, loadedCustomDevices, pairingMethodOptions,
pairingActionOptions) {
this.predefinedDevices = this.loadDevicesFromList(predefinedDevices, true);
this.devices = this.loadDevicesFromList(loadedCustomDevices, false);
this.deviceAuthenticationMethods = pairingMethodOptions;
this.deviceAuthenticationActions = pairingActionOptions;
updateBluetoothInfo_: function(info) {
this.predefinedDevices =
this.loadDevicesFromList(info.predefined_devices, true);
this.devices = this.loadDevicesFromList(info.devices, false);
this.deviceAuthenticationMethods = info.pairing_method_options;
this.deviceAuthenticationActions = info.pairing_action_options;
},
/**
......@@ -318,8 +331,9 @@ Polymer({
/**
* Called when a device is paired from the Tray. Checks the paired box for
* the device with path |path|.
* @private
*/
devicePairedFromTray: function(path) {
devicePairedFromTray_: function(path) {
var obj = this.devicePaths[path];
if (obj == undefined)
......@@ -368,8 +382,9 @@ Polymer({
/**
* Called from Chrome OS back-end when a pair request fails.
* @param {string} path The path of the device which failed to pair.
* @private
*/
pairFailed: function(path) {
pairFailed_: function(path) {
var obj = this.devicePaths[path];
if (obj == undefined)
......@@ -420,8 +435,9 @@ Polymer({
* The device is only added to the view's list if it is not already in
* the list (i.e. its path has not yet been recorded in |devicePaths|).
* @param {BluetoothDevice} device A bluetooth device.
* @private
*/
addBluetoothDevice: function(device) {
addBluetoothDevice_: function(device) {
if (this.devicePaths[device.path] != undefined) {
var obj = this.devicePaths[device.path];
var devicePath = (obj.predefined ? 'predefinedDevices.' : 'devices.');
......@@ -501,8 +517,9 @@ Polymer({
* adapter's device list. It sets that device's |.discoverable| and |.paired|
* attributes to false.
* @param {string} path A bluetooth device's path.
* @private
*/
deviceRemovedFromMainAdapter: function(path) {
deviceRemovedFromMainAdapter_: function(path) {
if (this.devicePaths[path] == undefined)
return;
......
......@@ -55,16 +55,6 @@ const char kSetHasMouse[] = "setHasMouse";
// and the web UI.
const char kUpdateAudioNodes[] =
"device_emulator.audioSettings.updateAudioNodes";
const char kAddBluetoothDeviceJSCallback[] =
"device_emulator.bluetoothSettings.addBluetoothDevice";
const char kDevicePairedFromTrayJSCallback[] =
"device_emulator.bluetoothSettings.devicePairedFromTray";
const char kDeviceRemovedFromMainAdapterJSCallback[] =
"device_emulator.bluetoothSettings.deviceRemovedFromMainAdapter";
const char kPairFailedJSCallback[] =
"device_emulator.bluetoothSettings.pairFailed";
const char kUpdateBluetoothInfoJSCallback[] =
"device_emulator.bluetoothSettings.updateBluetoothInfo";
const char kUpdatePowerPropertiesJSCallback[] =
"device_emulator.batterySettings.updatePowerProperties";
const char kTouchpadExistsCallback[] =
......@@ -116,24 +106,22 @@ void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded(
owner_->GetDeviceInfo(object_path);
// Request to add the device to the view's list of devices.
owner_->web_ui()->CallJavascriptFunctionUnsafe(kAddBluetoothDeviceJSCallback,
*device);
owner_->FireWebUIListener("bluetooth-device-added", *device);
}
void DeviceEmulatorMessageHandler::BluetoothObserver::DevicePropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) {
if (property_name == kPairedPropertyName) {
owner_->web_ui()->CallJavascriptFunctionUnsafe(
kDevicePairedFromTrayJSCallback, base::Value(object_path.value()));
owner_->FireWebUIListener("device-paired-from-tray",
base::Value(object_path.value()));
}
}
void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved(
const dbus::ObjectPath& object_path) {
owner_->web_ui()->CallJavascriptFunctionUnsafe(
kDeviceRemovedFromMainAdapterJSCallback,
base::Value(object_path.value()));
owner_->FireWebUIListener("device-removed-from-main-adapter",
base::Value(object_path.value()));
}
class DeviceEmulatorMessageHandler::CrasAudioObserver
......@@ -240,44 +228,49 @@ void DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover(
void DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo(
const base::ListValue* args) {
AllowJavascript();
// 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(bluez::FakeBluetoothAdapterClient::kAdapterPath));
base::ListValue devices;
auto devices = std::make_unique<base::ListValue>();
// Get each device's properties.
for (const dbus::ObjectPath& path : paths) {
std::unique_ptr<base::DictionaryValue> device = GetDeviceInfo(path);
devices.Append(std::move(device));
devices->Append(std::move(device));
}
std::unique_ptr<base::ListValue> predefined_devices =
fake_bluetooth_device_client_->GetBluetoothDevicesAsDictionaries();
base::ListValue pairing_method_options;
pairing_method_options.AppendString(
auto pairing_method_options = std::make_unique<base::ListValue>();
pairing_method_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingMethodNone);
pairing_method_options.AppendString(
pairing_method_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingMethodPinCode);
pairing_method_options.AppendString(
pairing_method_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingMethodPassKey);
base::ListValue pairing_action_options;
pairing_action_options.AppendString(
auto pairing_action_options = std::make_unique<base::ListValue>();
pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionDisplay);
pairing_action_options.AppendString(
pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionRequest);
pairing_action_options.AppendString(
pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionConfirmation);
pairing_action_options.AppendString(
pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionFail);
auto info = std::make_unique<base::DictionaryValue>();
info->Set("predefined_devices", std::move(predefined_devices));
info->Set("devices", std::move(devices));
info->Set("pairing_method_options", std::move(pairing_method_options));
info->Set("pairing_action_options", std::move(pairing_action_options));
// Send the list of devices to the view.
web_ui()->CallJavascriptFunctionUnsafe(
kUpdateBluetoothInfoJSCallback, *predefined_devices, devices,
pairing_method_options, pairing_action_options);
FireWebUIListener("bluetooth-info-updated", *info);
}
void DeviceEmulatorMessageHandler::HandleRequestBluetoothPair(
......@@ -291,8 +284,7 @@ void DeviceEmulatorMessageHandler::HandleRequestBluetoothPair(
// by its device ID, which, in this case is the same as its address.
ConnectToBluetoothDevice(props->address.value());
if (!props->paired.value()) {
web_ui()->CallJavascriptFunctionUnsafe(kPairFailedJSCallback,
base::Value(path));
FireWebUIListener("pair-failed", base::Value(path));
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment