summaryrefslogtreecommitdiffstats
path: root/remote/sessions/ContentProcessSession.jsm
blob: 1aaa822001bd414d02efff242e13b6c5451c7391 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* 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";

var EXPORTED_SYMBOLS = ["ContentProcessSession"];

const { ContentProcessDomains } = ChromeUtils.import(
  "chrome://remote/content/domains/ContentProcessDomains.jsm"
);
const { DomainCache } = ChromeUtils.import(
  "chrome://remote/content/domains/DomainCache.jsm"
);

ChromeUtils.defineModuleGetter(
  this,
  "ContextObserver",
  "chrome://remote/content/observers/ContextObserver.jsm"
);

class ContentProcessSession {
  constructor(messageManager, browsingContext, content, docShell) {
    this.messageManager = messageManager;
    this.browsingContext = browsingContext;
    this.content = content;
    this.docShell = docShell;
    // Most children or sibling classes are going to assume that docShell
    // implements the following interface. So do the QI only once from here.
    this.docShell.QueryInterface(Ci.nsIWebNavigation);

    this.domains = new DomainCache(this, ContentProcessDomains);
    this.messageManager.addMessageListener("remote:request", this);
    this.messageManager.addMessageListener("remote:destroy", this);
  }

  destructor() {
    this._contextObserver?.destructor();

    this.messageManager.removeMessageListener("remote:request", this);
    this.messageManager.removeMessageListener("remote:destroy", this);
    this.domains.clear();
  }

  get contextObserver() {
    if (!this._contextObserver) {
      this._contextObserver = new ContextObserver(
        this.docShell.chromeEventHandler
      );
    }
    return this._contextObserver;
  }

  // Domain event listener

  onEvent(eventName, params) {
    this.messageManager.sendAsyncMessage("remote:event", {
      browsingContextId: this.browsingContext.id,
      event: {
        eventName,
        params,
      },
    });
  }

  // nsIMessageListener

  async receiveMessage({ name, data }) {
    const { browsingContextId } = data;

    // We may have more than one tab loaded in the same process,
    // and debug the two at the same time. We want to ensure not
    // mixing up the requests made against two such tabs.
    // Each tab is going to have its own frame script instance
    // and two communication channels are going to be set up via
    // the two message managers.
    if (browsingContextId != this.browsingContext.id) {
      return;
    }

    switch (name) {
      case "remote:request":
        try {
          const { id, domain, command, params } = data.request;

          const result = await this.domains.execute(domain, command, params);

          this.messageManager.sendAsyncMessage("remote:result", {
            browsingContextId,
            id,
            result,
          });
        } catch (e) {
          this.messageManager.sendAsyncMessage("remote:error", {
            browsingContextId,
            id: data.request.id,
            error: {
              name: e.name || "exception",
              message: e.message || String(e),
              stack: e.stack,
            },
          });
        }
        break;

      case "remote:destroy":
        this.destructor();
        break;
    }
  }
}