summaryrefslogtreecommitdiffstats
path: root/toolkit/content/process-content.js
blob: 2ff8f908a956d7a397143517819ba5c12c53c5b6 (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
/* 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 { classes: Cc, interfaces: Ci, utils: Cu } = Components;

// Creates a new PageListener for this process. This will listen for page loads
// and for those that match URLs provided by the parent process will set up
// a dedicated message port and notify the parent process.
Cu.import("resource://gre/modules/RemotePageManager.jsm");
Cu.import("resource://gre/modules/Services.jsm");

const gInContentProcess = Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_CONTENT;

Services.cpmm.addMessageListener("gmp-plugin-crash", msg => {
  let gmpservice = Cc["@mozilla.org/gecko-media-plugin-service;1"]
                     .getService(Ci.mozIGeckoMediaPluginService);

  gmpservice.RunPluginCrashCallbacks(msg.data.pluginID, msg.data.pluginName);
});

if (gInContentProcess) {
  let ProcessObserver = {
    TOPICS: [
      "inner-window-destroyed",
      "xpcom-shutdown",
    ],

    init() {
      for (let topic of this.TOPICS) {
        Services.obs.addObserver(this, topic, false);
        Services.cpmm.addMessageListener("Memory:GetSummary", this);
      }
    },

    uninit() {
      for (let topic of this.TOPICS) {
        Services.obs.removeObserver(this, topic);
        Services.cpmm.removeMessageListener("Memory:GetSummary", this);
      }
    },

    receiveMessage(msg) {
      if (msg.name != "Memory:GetSummary") {
        return;
      }
      let pid = Services.appinfo.processID;
      let memMgr = Cc["@mozilla.org/memory-reporter-manager;1"]
                     .getService(Ci.nsIMemoryReporterManager);
      let rss = memMgr.resident;
      let uss = memMgr.residentUnique;
      Services.cpmm.sendAsyncMessage("Memory:Summary", {
        pid,
        summary: {
          uss,
          rss,
        }
      });
    },

    observe(subject, topic, data) {
      switch (topic) {
        case "inner-window-destroyed": {
          // Forward inner-window-destroyed notifications with the
          // inner window ID, so that code in the parent that should
          // do something when content windows go away can do it
          let innerWindowID =
            subject.QueryInterface(Ci.nsISupportsPRUint64).data;
          Services.cpmm.sendAsyncMessage("Toolkit:inner-window-destroyed",
                                         innerWindowID);
          break;
        }
        case "xpcom-shutdown": {
          this.uninit();
          break;
        }
      }
    },
  };

  ProcessObserver.init();
}