| Index: chrome/browser/resources/bluetooth_internals/bluetooth_internals.js | 
| diff --git a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js | 
| index 93a73935c52d3cabcc24309e489e4b00cc7de649..4057293273bc1ddaa7726553896bef63b21fd86d 100644 | 
| --- a/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js | 
| +++ b/chrome/browser/resources/bluetooth_internals/bluetooth_internals.js | 
| @@ -7,128 +7,42 @@ | 
| *     chrome://bluetooth-internals/. | 
| */ | 
|  | 
| -(function() { | 
| -  var adapter, adapterClient, bluetoothAdapter, bluetoothDevice, connection; | 
| - | 
| -  var REMOVED_CSS = 'removed'; | 
| - | 
| -  /* | 
| -   * Data model for a cached device. | 
| -   * @constructor | 
| -   * @param {!bluetoothDevice.DeviceInfo} info | 
| -   */ | 
| -  var Device = function(info) { this.info = info; }; | 
| - | 
| -  /** | 
| -   * The implementation of AdapterClient in | 
| -   *     device/bluetooth/public/interfaces/adapter.mojom. This also manages the | 
| -   *     client-side collection of devices. | 
| -   * @constructor | 
| -   */ | 
| -  var AdapterClient = function() { this.devices_ = new Map(); }; | 
| -  AdapterClient.prototype = { | 
| -    /** | 
| -     * Caches the device info and updates the device list. | 
| -     * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 
| -     */ | 
| -    deviceAdded: function(deviceInfo) { | 
| -      if (this.devices_.has(deviceInfo.address)) { | 
| -        var deviceElement = $(deviceInfo.address); | 
| -        deviceElement.classList.remove(REMOVED_CSS); | 
| -      } else { | 
| -        this.devices_.set(deviceInfo.address, new Device(deviceInfo)); | 
| - | 
| -        var deviceRowTemplate = $('device-row-template'); | 
| -        var deviceRow = document.importNode( | 
| -            deviceRowTemplate.content.children[0], true /* deep */); | 
| -        deviceRow.id = deviceInfo.address; | 
| - | 
| -        var deviceList = $('device-list'); | 
| -        deviceList.appendChild(deviceRow); | 
| -      } | 
| - | 
| -      this.deviceChanged(deviceInfo); | 
| -    }, | 
| - | 
| -    /** | 
| -     * Marks device as removed. | 
| -     * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 
| -     */ | 
| -    deviceRemoved: function(deviceInfo) { | 
| -      $(deviceInfo.address).classList.add(REMOVED_CSS); | 
| -    }, | 
| - | 
| -    /** | 
| -     * Updates cached device and updates the device list. | 
| -     * @param {!bluetoothDevice.DeviceInfo} deviceInfo | 
| -     */ | 
| -    deviceChanged: function(deviceInfo) { | 
| -      console.log(new Date(), deviceInfo); | 
| - | 
| -      assert(this.devices_.has(deviceInfo.address), 'Device does not exist.'); | 
| - | 
| -      this.devices_.get(deviceInfo.address).info = deviceInfo; | 
| - | 
| -      var deviceRow = $(deviceInfo.address); | 
| -      deviceRow.querySelector('.device-name').textContent = | 
| -          deviceInfo.name_for_display; | 
| -      deviceRow.querySelector('.device-address').textContent = | 
| -          deviceInfo.address; | 
| - | 
| -      var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || | 
| -          deviceRow.querySelector('.device-rssi').textContent; | 
| -      deviceRow.querySelector('.device-rssi').textContent = rssi; | 
| -    } | 
| -  }; | 
| - | 
| -  /** | 
| -   * Initializes Mojo proxies for page and Bluetooth services. | 
| -   * @return {!Promise} resolves if adapter is acquired, rejects if Bluetooth | 
| -   *     is not supported. | 
| -   */ | 
| -  function initializeProxies() { | 
| -    return importModules([ | 
| -      'content/public/renderer/frame_interfaces', | 
| -      'device/bluetooth/public/interfaces/adapter.mojom', | 
| -      'device/bluetooth/public/interfaces/device.mojom', | 
| -      'mojo/public/js/connection', | 
| -    ]).then(function([frameInterfaces, ...modules]) { | 
| -      // Destructure here to assign global variables. | 
| -      [bluetoothAdapter, bluetoothDevice, connection] = modules; | 
| - | 
| -      // Hook up the instance properties. | 
| -      AdapterClient.prototype.__proto__ = | 
| -          bluetoothAdapter.AdapterClient.stubClass.prototype; | 
| - | 
| -      var adapterFactory = connection.bindHandleToProxy( | 
| -          frameInterfaces.getInterface(bluetoothAdapter.AdapterFactory.name), | 
| -          bluetoothAdapter.AdapterFactory); | 
| - | 
| -      // Get an Adapter service. | 
| -      return adapterFactory.getAdapter(); | 
| -    }).then(function(response) { | 
| -      if (!response.adapter) { | 
| -        throw new Error('Bluetooth Not Supported on this platform.'); | 
| -      } | 
| - | 
| -      adapter = connection.bindHandleToProxy(response.adapter, | 
| -                                             bluetoothAdapter.Adapter); | 
| - | 
| -      // Create a message pipe and bind one end to client | 
| -      // implementation and the other to the Adapter service. | 
| -      adapterClient = new AdapterClient(); | 
| -      adapter.setClient(connection.bindStubDerivedImpl(adapterClient)); | 
| -    }); | 
| -  } | 
| - | 
| -  document.addEventListener('DOMContentLoaded', function() { | 
| -    initializeProxies() | 
| -      .then(function() { return adapter.getInfo(); }) | 
| +cr.define('bluetooth_internals', function() { | 
| +  function initializeViews() { | 
| +    var adapterBroker = null; | 
| +    adapter_broker.getAdapterBroker() | 
| +      .then(function(broker) { adapterBroker = broker; }) | 
| +      .then(function() { return adapterBroker.getInfo(); }) | 
| .then(function(response) { console.log('adapter', response.info); }) | 
| -      .then(function() { return adapter.getDevices(); }) | 
| +      .then(function() { return adapterBroker.getDevices(); }) | 
| .then(function(response) { | 
| -        response.devices.forEach(adapterClient.deviceAdded, adapterClient); | 
| +        // Hook up device collection events. | 
| +        var devices = new device_collection.DeviceCollection([]); | 
| +        adapterBroker.addEventListener('deviceadded', function(event) { | 
| +          devices.addOrUpdate(event.detail.deviceInfo); | 
| +        }); | 
| +        adapterBroker.addEventListener('devicechanged', function(event) { | 
| +          devices.addOrUpdate(event.detail.deviceInfo); | 
| +        }); | 
| +        adapterBroker.addEventListener('deviceremoved', function(event) { | 
| +          devices.remove(event.detail.deviceInfo); | 
| +        }); | 
| + | 
| +        response.devices.forEach(devices.addOrUpdate, | 
| +                                 devices /* this */); | 
| + | 
| +        var deviceTable = new device_table.DeviceTable(); | 
| +        deviceTable.setDevices(devices); | 
| +        document.body.appendChild(deviceTable); | 
| }) | 
| .catch(function(error) { console.error(error); }); | 
| -  }); | 
| -})(); | 
| +  } | 
| + | 
| +  return { | 
| +    initializeViews: initializeViews | 
| +  }; | 
| + | 
| +}); | 
| + | 
| +document.addEventListener( | 
| +    'DOMContentLoaded', bluetooth_internals.initializeViews); | 
|  |