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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const ObservableObject = require("devtools/shared/observable-object");
michael@0 6 const {getDeviceFront} = require("devtools/server/actors/device");
michael@0 7 const {Connection} = require("devtools/client/connection-manager");
michael@0 8
michael@0 9 const {Cu} = require("chrome");
michael@0 10
michael@0 11 const _knownDeviceStores = new WeakMap();
michael@0 12
michael@0 13 let DeviceStore;
michael@0 14
michael@0 15 module.exports = DeviceStore = function(connection) {
michael@0 16 // If we already know about this connection,
michael@0 17 // let's re-use the existing store.
michael@0 18 if (_knownDeviceStores.has(connection)) {
michael@0 19 return _knownDeviceStores.get(connection);
michael@0 20 }
michael@0 21
michael@0 22 _knownDeviceStores.set(connection, this);
michael@0 23
michael@0 24 ObservableObject.call(this, {});
michael@0 25
michael@0 26 this._resetStore();
michael@0 27
michael@0 28 this.destroy = this.destroy.bind(this);
michael@0 29 this._onStatusChanged = this._onStatusChanged.bind(this);
michael@0 30
michael@0 31 this._connection = connection;
michael@0 32 this._connection.once(Connection.Events.DESTROYED, this.destroy);
michael@0 33 this._connection.on(Connection.Events.STATUS_CHANGED, this._onStatusChanged);
michael@0 34 this._onTabListChanged = this._onTabListChanged.bind(this);
michael@0 35 this._onStatusChanged();
michael@0 36 return this;
michael@0 37 };
michael@0 38
michael@0 39 DeviceStore.prototype = {
michael@0 40 destroy: function() {
michael@0 41 if (this._connection) {
michael@0 42 // While this.destroy is bound using .once() above, that event may not
michael@0 43 // have occurred when the DeviceStore client calls destroy, so we
michael@0 44 // manually remove it here.
michael@0 45 this._connection.off(Connection.Events.DESTROYED, this.destroy);
michael@0 46 this._connection.off(Connection.Events.STATUS_CHANGED, this._onStatusChanged);
michael@0 47 _knownDeviceStores.delete(this._connection);
michael@0 48 this._connection = null;
michael@0 49 }
michael@0 50 },
michael@0 51
michael@0 52 _resetStore: function() {
michael@0 53 this.object.description = {};
michael@0 54 this.object.permissions = [];
michael@0 55 this.object.tabs = [];
michael@0 56 },
michael@0 57
michael@0 58 _onStatusChanged: function() {
michael@0 59 if (this._connection.status == Connection.Status.CONNECTED) {
michael@0 60 // Watch for changes to remote browser tabs
michael@0 61 this._connection.client.addListener("tabListChanged",
michael@0 62 this._onTabListChanged);
michael@0 63 this._listTabs();
michael@0 64 } else {
michael@0 65 if (this._connection.client) {
michael@0 66 this._connection.client.removeListener("tabListChanged",
michael@0 67 this._onTabListChanged);
michael@0 68 }
michael@0 69 this._resetStore();
michael@0 70 }
michael@0 71 },
michael@0 72
michael@0 73 _onTabListChanged: function() {
michael@0 74 this._listTabs();
michael@0 75 },
michael@0 76
michael@0 77 _listTabs: function() {
michael@0 78 if (!this._connection) {
michael@0 79 return;
michael@0 80 }
michael@0 81 this._connection.client.listTabs((resp) => {
michael@0 82 if (resp.error) {
michael@0 83 this._connection.disconnect();
michael@0 84 return;
michael@0 85 }
michael@0 86 this._deviceFront = getDeviceFront(this._connection.client, resp);
michael@0 87 // Save remote browser's tabs
michael@0 88 this.object.tabs = resp.tabs;
michael@0 89 this._feedStore();
michael@0 90 });
michael@0 91 },
michael@0 92
michael@0 93 _feedStore: function() {
michael@0 94 this._getDeviceDescription();
michael@0 95 this._getDevicePermissionsTable();
michael@0 96 },
michael@0 97
michael@0 98 _getDeviceDescription: function() {
michael@0 99 return this._deviceFront.getDescription()
michael@0 100 .then(json => {
michael@0 101 json.dpi = Math.ceil(json.dpi);
michael@0 102 this.object.description = json;
michael@0 103 });
michael@0 104 },
michael@0 105
michael@0 106 _getDevicePermissionsTable: function() {
michael@0 107 return this._deviceFront.getRawPermissionsTable()
michael@0 108 .then(json => {
michael@0 109 let permissionsTable = json.rawPermissionsTable;
michael@0 110 let permissionsArray = [];
michael@0 111 for (let name in permissionsTable) {
michael@0 112 permissionsArray.push({
michael@0 113 name: name,
michael@0 114 app: permissionsTable[name].app,
michael@0 115 privileged: permissionsTable[name].privileged,
michael@0 116 certified: permissionsTable[name].certified,
michael@0 117 });
michael@0 118 }
michael@0 119 this.object.permissions = permissionsArray;
michael@0 120 });
michael@0 121 }
michael@0 122 };

mercurial