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") { ...@@ -19,6 +19,9 @@ js_library("audio_settings") {
} }
js_library("bluetooth_settings") { js_library("bluetooth_settings") {
deps = [
"//ui/webui/resources/js:web_ui_listener_behavior",
]
externs_list = [ "$externs_path/chrome_send.js" ] externs_list = [ "$externs_path/chrome_send.js" ]
} }
......
...@@ -8,6 +8,7 @@ ...@@ -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_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/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/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="chrome://resources/polymer/v1_0/iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="icons.html"> <link rel="import" href="icons.html">
<link rel="import" href="shared_styles.html"> <link rel="import" href="shared_styles.html">
......
...@@ -54,6 +54,8 @@ var BluetoothDevice = function() { ...@@ -54,6 +54,8 @@ var BluetoothDevice = function() {
Polymer({ Polymer({
is: 'bluetooth-settings', is: 'bluetooth-settings',
behaviors: [WebUIListenerBehavior],
properties: { properties: {
/** /**
* A set of bluetooth devices. * A set of bluetooth devices.
...@@ -148,6 +150,16 @@ Polymer({ ...@@ -148,6 +150,16 @@ Polymer({
devicePaths: {}, devicePaths: {},
ready: function() { 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'); chrome.send('requestBluetoothInfo');
}, },
...@@ -274,19 +286,20 @@ Polymer({ ...@@ -274,19 +286,20 @@ Polymer({
/** /**
* Called by the WebUI which provides a list of devices which are connected * Called by the WebUI which provides a list of devices which are connected
* to the main adapter. * to the main adapter.
* @param {!Array<!BluetoothDevice>} predefinedDevices A list of bluetooth * @param {{
* devices. * predefined_devices: !Array<!BluetoothDevice>,
* @param {!Array<!BluetoothDevice>} loadedCustomDevices * devices: !Array<!BluetoothDevice>,
* @param {!Array<string>} pairingMethodOptions * pairing_method_options: !Array<string>,
* @param {!Array<string>} pairingActionOptions * pairing_action_options: !Array<string>,
* }} info
* @private
*/ */
updateBluetoothInfo: function( updateBluetoothInfo_: function(info) {
predefinedDevices, loadedCustomDevices, pairingMethodOptions, this.predefinedDevices =
pairingActionOptions) { this.loadDevicesFromList(info.predefined_devices, true);
this.predefinedDevices = this.loadDevicesFromList(predefinedDevices, true); this.devices = this.loadDevicesFromList(info.devices, false);
this.devices = this.loadDevicesFromList(loadedCustomDevices, false); this.deviceAuthenticationMethods = info.pairing_method_options;
this.deviceAuthenticationMethods = pairingMethodOptions; this.deviceAuthenticationActions = info.pairing_action_options;
this.deviceAuthenticationActions = pairingActionOptions;
}, },
/** /**
...@@ -318,8 +331,9 @@ Polymer({ ...@@ -318,8 +331,9 @@ Polymer({
/** /**
* Called when a device is paired from the Tray. Checks the paired box for * Called when a device is paired from the Tray. Checks the paired box for
* the device with path |path|. * the device with path |path|.
* @private
*/ */
devicePairedFromTray: function(path) { devicePairedFromTray_: function(path) {
var obj = this.devicePaths[path]; var obj = this.devicePaths[path];
if (obj == undefined) if (obj == undefined)
...@@ -368,8 +382,9 @@ Polymer({ ...@@ -368,8 +382,9 @@ Polymer({
/** /**
* Called from Chrome OS back-end when a pair request fails. * Called from Chrome OS back-end when a pair request fails.
* @param {string} path The path of the device which failed to pair. * @param {string} path The path of the device which failed to pair.
* @private
*/ */
pairFailed: function(path) { pairFailed_: function(path) {
var obj = this.devicePaths[path]; var obj = this.devicePaths[path];
if (obj == undefined) if (obj == undefined)
...@@ -420,8 +435,9 @@ Polymer({ ...@@ -420,8 +435,9 @@ Polymer({
* The device is only added to the view's list if it is not already in * 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|). * the list (i.e. its path has not yet been recorded in |devicePaths|).
* @param {BluetoothDevice} device A bluetooth device. * @param {BluetoothDevice} device A bluetooth device.
* @private
*/ */
addBluetoothDevice: function(device) { addBluetoothDevice_: function(device) {
if (this.devicePaths[device.path] != undefined) { if (this.devicePaths[device.path] != undefined) {
var obj = this.devicePaths[device.path]; var obj = this.devicePaths[device.path];
var devicePath = (obj.predefined ? 'predefinedDevices.' : 'devices.'); var devicePath = (obj.predefined ? 'predefinedDevices.' : 'devices.');
...@@ -501,8 +517,9 @@ Polymer({ ...@@ -501,8 +517,9 @@ Polymer({
* adapter's device list. It sets that device's |.discoverable| and |.paired| * adapter's device list. It sets that device's |.discoverable| and |.paired|
* attributes to false. * attributes to false.
* @param {string} path A bluetooth device's path. * @param {string} path A bluetooth device's path.
* @private
*/ */
deviceRemovedFromMainAdapter: function(path) { deviceRemovedFromMainAdapter_: function(path) {
if (this.devicePaths[path] == undefined) if (this.devicePaths[path] == undefined)
return; return;
......
...@@ -55,16 +55,6 @@ const char kSetHasMouse[] = "setHasMouse"; ...@@ -55,16 +55,6 @@ const char kSetHasMouse[] = "setHasMouse";
// and the web UI. // and the web UI.
const char kUpdateAudioNodes[] = const char kUpdateAudioNodes[] =
"device_emulator.audioSettings.updateAudioNodes"; "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[] = const char kUpdatePowerPropertiesJSCallback[] =
"device_emulator.batterySettings.updatePowerProperties"; "device_emulator.batterySettings.updatePowerProperties";
const char kTouchpadExistsCallback[] = const char kTouchpadExistsCallback[] =
...@@ -116,23 +106,21 @@ void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded( ...@@ -116,23 +106,21 @@ void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceAdded(
owner_->GetDeviceInfo(object_path); owner_->GetDeviceInfo(object_path);
// Request to add the device to the view's list of devices. // Request to add the device to the view's list of devices.
owner_->web_ui()->CallJavascriptFunctionUnsafe(kAddBluetoothDeviceJSCallback, owner_->FireWebUIListener("bluetooth-device-added", *device);
*device);
} }
void DeviceEmulatorMessageHandler::BluetoothObserver::DevicePropertyChanged( void DeviceEmulatorMessageHandler::BluetoothObserver::DevicePropertyChanged(
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const std::string& property_name) { const std::string& property_name) {
if (property_name == kPairedPropertyName) { if (property_name == kPairedPropertyName) {
owner_->web_ui()->CallJavascriptFunctionUnsafe( owner_->FireWebUIListener("device-paired-from-tray",
kDevicePairedFromTrayJSCallback, base::Value(object_path.value())); base::Value(object_path.value()));
} }
} }
void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved( void DeviceEmulatorMessageHandler::BluetoothObserver::DeviceRemoved(
const dbus::ObjectPath& object_path) { const dbus::ObjectPath& object_path) {
owner_->web_ui()->CallJavascriptFunctionUnsafe( owner_->FireWebUIListener("device-removed-from-main-adapter",
kDeviceRemovedFromMainAdapterJSCallback,
base::Value(object_path.value())); base::Value(object_path.value()));
} }
...@@ -240,44 +228,49 @@ void DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover( ...@@ -240,44 +228,49 @@ void DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover(
void DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo( void DeviceEmulatorMessageHandler::HandleRequestBluetoothInfo(
const base::ListValue* args) { const base::ListValue* args) {
AllowJavascript();
// Get a list containing paths of the devices which are connected to // Get a list containing paths of the devices which are connected to
// the main adapter. // the main adapter.
std::vector<dbus::ObjectPath> paths = std::vector<dbus::ObjectPath> paths =
fake_bluetooth_device_client_->GetDevicesForAdapter( fake_bluetooth_device_client_->GetDevicesForAdapter(
dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath)); dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath));
base::ListValue devices; auto devices = std::make_unique<base::ListValue>();
// Get each device's properties. // Get each device's properties.
for (const dbus::ObjectPath& path : paths) { for (const dbus::ObjectPath& path : paths) {
std::unique_ptr<base::DictionaryValue> device = GetDeviceInfo(path); 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 = std::unique_ptr<base::ListValue> predefined_devices =
fake_bluetooth_device_client_->GetBluetoothDevicesAsDictionaries(); fake_bluetooth_device_client_->GetBluetoothDevicesAsDictionaries();
base::ListValue pairing_method_options; auto pairing_method_options = std::make_unique<base::ListValue>();
pairing_method_options.AppendString( pairing_method_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingMethodNone); bluez::FakeBluetoothDeviceClient::kPairingMethodNone);
pairing_method_options.AppendString( pairing_method_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingMethodPinCode); bluez::FakeBluetoothDeviceClient::kPairingMethodPinCode);
pairing_method_options.AppendString( pairing_method_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingMethodPassKey); bluez::FakeBluetoothDeviceClient::kPairingMethodPassKey);
base::ListValue pairing_action_options; auto pairing_action_options = std::make_unique<base::ListValue>();
pairing_action_options.AppendString( pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionDisplay); bluez::FakeBluetoothDeviceClient::kPairingActionDisplay);
pairing_action_options.AppendString( pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionRequest); bluez::FakeBluetoothDeviceClient::kPairingActionRequest);
pairing_action_options.AppendString( pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionConfirmation); bluez::FakeBluetoothDeviceClient::kPairingActionConfirmation);
pairing_action_options.AppendString( pairing_action_options->AppendString(
bluez::FakeBluetoothDeviceClient::kPairingActionFail); 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. // Send the list of devices to the view.
web_ui()->CallJavascriptFunctionUnsafe( FireWebUIListener("bluetooth-info-updated", *info);
kUpdateBluetoothInfoJSCallback, *predefined_devices, devices,
pairing_method_options, pairing_action_options);
} }
void DeviceEmulatorMessageHandler::HandleRequestBluetoothPair( void DeviceEmulatorMessageHandler::HandleRequestBluetoothPair(
...@@ -291,8 +284,7 @@ void DeviceEmulatorMessageHandler::HandleRequestBluetoothPair( ...@@ -291,8 +284,7 @@ void DeviceEmulatorMessageHandler::HandleRequestBluetoothPair(
// by its device ID, which, in this case is the same as its address. // by its device ID, which, in this case is the same as its address.
ConnectToBluetoothDevice(props->address.value()); ConnectToBluetoothDevice(props->address.value());
if (!props->paired.value()) { if (!props->paired.value()) {
web_ui()->CallJavascriptFunctionUnsafe(kPairFailedJSCallback, FireWebUIListener("pair-failed", base::Value(path));
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