summaryrefslogtreecommitdiffstats
path: root/devtools/client/storage/panel.js
blob: c24059d8e8561bf95577ae989b8a0170576f04bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* 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 EventEmitter = require("devtools/shared/event-emitter");

loader.lazyRequireGetter(this, "StorageFront",
                         "devtools/shared/fronts/storage", true);
loader.lazyRequireGetter(this, "StorageUI",
                         "devtools/client/storage/ui", true);

var StoragePanel = this.StoragePanel =
function StoragePanel(panelWin, toolbox) {
  EventEmitter.decorate(this);

  this._toolbox = toolbox;
  this._target = toolbox.target;
  this._panelWin = panelWin;

  this.destroy = this.destroy.bind(this);
};

exports.StoragePanel = StoragePanel;

StoragePanel.prototype = {
  get target() {
    return this._toolbox.target;
  },

  get panelWindow() {
    return this._panelWin;
  },

  /**
   * open is effectively an asynchronous constructor
   */
  open: function () {
    let targetPromise;
    // We always interact with the target as if it were remote
    if (!this.target.isRemote) {
      targetPromise = this.target.makeRemote();
    } else {
      targetPromise = Promise.resolve(this.target);
    }

    return targetPromise.then(() => {
      this.target.on("close", this.destroy);
      this._front = new StorageFront(this.target.client, this.target.form);

      this.UI = new StorageUI(this._front, this._target,
                              this._panelWin, this._toolbox);
      this.isReady = true;
      this.emit("ready");

      return this;
    }).catch(e => {
      console.log("error while opening storage panel", e);
      this.destroy();
    });
  },

  /**
   * Destroy the storage inspector.
   */
  destroy: function () {
    if (!this._destroyed) {
      this.UI.destroy();
      this.UI = null;

      // Destroy front to ensure packet handler is removed from client
      this._front.destroy();
      this._front = null;
      this._destroyed = true;

      this._target.off("close", this.destroy);
      this._target = null;
      this._toolbox = null;
      this._panelWin = null;
    }

    return Promise.resolve(null);
  },
};