browser/devtools/app-manager/device-store.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/app-manager/device-store.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const ObservableObject = require("devtools/shared/observable-object");
     1.9 +const {getDeviceFront} = require("devtools/server/actors/device");
    1.10 +const {Connection} = require("devtools/client/connection-manager");
    1.11 +
    1.12 +const {Cu} = require("chrome");
    1.13 +
    1.14 +const _knownDeviceStores = new WeakMap();
    1.15 +
    1.16 +let DeviceStore;
    1.17 +
    1.18 +module.exports = DeviceStore = function(connection) {
    1.19 +  // If we already know about this connection,
    1.20 +  // let's re-use the existing store.
    1.21 +  if (_knownDeviceStores.has(connection)) {
    1.22 +    return _knownDeviceStores.get(connection);
    1.23 +  }
    1.24 +
    1.25 +  _knownDeviceStores.set(connection, this);
    1.26 +
    1.27 +  ObservableObject.call(this, {});
    1.28 +
    1.29 +  this._resetStore();
    1.30 +
    1.31 +  this.destroy = this.destroy.bind(this);
    1.32 +  this._onStatusChanged = this._onStatusChanged.bind(this);
    1.33 +
    1.34 +  this._connection = connection;
    1.35 +  this._connection.once(Connection.Events.DESTROYED, this.destroy);
    1.36 +  this._connection.on(Connection.Events.STATUS_CHANGED, this._onStatusChanged);
    1.37 +  this._onTabListChanged = this._onTabListChanged.bind(this);
    1.38 +  this._onStatusChanged();
    1.39 +  return this;
    1.40 +};
    1.41 +
    1.42 +DeviceStore.prototype = {
    1.43 +  destroy: function() {
    1.44 +    if (this._connection) {
    1.45 +      // While this.destroy is bound using .once() above, that event may not
    1.46 +      // have occurred when the DeviceStore client calls destroy, so we
    1.47 +      // manually remove it here.
    1.48 +      this._connection.off(Connection.Events.DESTROYED, this.destroy);
    1.49 +      this._connection.off(Connection.Events.STATUS_CHANGED, this._onStatusChanged);
    1.50 +      _knownDeviceStores.delete(this._connection);
    1.51 +      this._connection = null;
    1.52 +    }
    1.53 +  },
    1.54 +
    1.55 +  _resetStore: function() {
    1.56 +    this.object.description = {};
    1.57 +    this.object.permissions = [];
    1.58 +    this.object.tabs = [];
    1.59 +  },
    1.60 +
    1.61 +  _onStatusChanged: function() {
    1.62 +    if (this._connection.status == Connection.Status.CONNECTED) {
    1.63 +      // Watch for changes to remote browser tabs
    1.64 +      this._connection.client.addListener("tabListChanged",
    1.65 +                                          this._onTabListChanged);
    1.66 +      this._listTabs();
    1.67 +    } else {
    1.68 +      if (this._connection.client) {
    1.69 +        this._connection.client.removeListener("tabListChanged",
    1.70 +                                               this._onTabListChanged);
    1.71 +      }
    1.72 +      this._resetStore();
    1.73 +    }
    1.74 +  },
    1.75 +
    1.76 +  _onTabListChanged: function() {
    1.77 +    this._listTabs();
    1.78 +  },
    1.79 +
    1.80 +  _listTabs: function() {
    1.81 +    if (!this._connection) {
    1.82 +      return;
    1.83 +    }
    1.84 +    this._connection.client.listTabs((resp) => {
    1.85 +      if (resp.error) {
    1.86 +        this._connection.disconnect();
    1.87 +        return;
    1.88 +      }
    1.89 +      this._deviceFront = getDeviceFront(this._connection.client, resp);
    1.90 +      // Save remote browser's tabs
    1.91 +      this.object.tabs = resp.tabs;
    1.92 +      this._feedStore();
    1.93 +    });
    1.94 +  },
    1.95 +
    1.96 +  _feedStore: function() {
    1.97 +    this._getDeviceDescription();
    1.98 +    this._getDevicePermissionsTable();
    1.99 +  },
   1.100 +
   1.101 +  _getDeviceDescription: function() {
   1.102 +    return this._deviceFront.getDescription()
   1.103 +    .then(json => {
   1.104 +      json.dpi = Math.ceil(json.dpi);
   1.105 +      this.object.description = json;
   1.106 +    });
   1.107 +  },
   1.108 +
   1.109 +  _getDevicePermissionsTable: function() {
   1.110 +    return this._deviceFront.getRawPermissionsTable()
   1.111 +    .then(json => {
   1.112 +      let permissionsTable = json.rawPermissionsTable;
   1.113 +      let permissionsArray = [];
   1.114 +      for (let name in permissionsTable) {
   1.115 +        permissionsArray.push({
   1.116 +          name: name,
   1.117 +          app: permissionsTable[name].app,
   1.118 +          privileged: permissionsTable[name].privileged,
   1.119 +          certified: permissionsTable[name].certified,
   1.120 +        });
   1.121 +      }
   1.122 +      this.object.permissions = permissionsArray;
   1.123 +    });
   1.124 +  }
   1.125 +};

mercurial