devtools/shared/apps/Devices.jsm
author Julian Descottes <jdescottes@mozilla.com>
Tue, 27 Nov 2018 12:49:11 +0000
changeset 507470 5199632a4ada777750accd0e0e2a0faa96b12d7e
parent 500637 edc4ae8f78e2fb02647d7cd85cb19830a17dcd1d
permissions -rw-r--r--
Bug 1492700 - Stop sending adb-addon updates from Devices.jsm;r=daisuke Depends on D12758 Differential Revision: https://phabricator.services.mozilla.com/D12759

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
const EventEmitter = require("devtools/shared/event-emitter");
const { adbAddon, ADB_ADDON_STATES } = require("devtools/shared/adb/adb-addon");

/* exported EXPORTED_SYMBOLS */

const EXPORTED_SYMBOLS = ["Devices"];

const Devices = {
  _devices: {},

  register: function(name, device) {
    this._devices[name] = device;
    this.emit("register");
  },

  unregister: function(name) {
    delete this._devices[name];
    this.emit("unregister");
  },

  available: function() {
    return Object.keys(this._devices).sort();
  },

  getByName: function(name) {
    return this._devices[name];
  },

  updateAdbAddonStatus: function() {
    const installed = adbAddon.status === ADB_ADDON_STATES.INSTALLED;
    if (!installed) {
      for (const name in this._devices) {
        this.unregister(name);
      }
    }
  },
};

Object.defineProperty(this, "Devices", {
  value: Devices,
  enumerable: true,
  writable: false,
});

EventEmitter.decorate(Devices);

adbAddon.on("update", () => Devices.updateAdbAddonStatus());