Revision control

Line Code
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* vim: set ts=2 sw=2 sts=2 et 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";

/**
 * This module implements logic for managing JavaScript actor instances bound to
 * message managers. It handles lazily instantiating those actors based on
 * DOM events, IPC messages, or observer notifications, and is meant to entirely
 * replace the existing concept of frame scripts.
 *
 * All actors must be registered in the parent process, before the first child
 * process starts. Once all actors have been registered, the actor data is
 * mangled into a form which can be handled efficiently during content process
 * startup, and shared with all content processes. Frame scripts in those
 * content processes attach that data to frame message managers via
 * ActorManagerChild, which handles instantiating and dispatching to those
 * actors as necessary.
 *
 *
 * Each actor is a class which lives in a JSM, and has a constructor which takes
 * a single message manager argument. Each actor may conceptually have both
 * Child and Parent variants, but only Child variants are currently implemented.
 * The parent and child variants live in separate JSMs, and have separate class
 * names, each of which have Child or Parent appended to their names, as
 * appropriate. For instance, the Browser actor has a child instance named
 * BrowserChild which lives in BrowserChild.jsm.
 *
 *
 * Actors are defined by calling ActorManagerParent.addActors, with an object
 * containing a property for each actor being defined, whose value is an object
 * describing how the actor should be loaded. That object may have the following
 * properties:
 *
 * - "child": The actor definition for the child side of the actor.
 *
 * Each "child" (or "parent", when it is implemented) actor definition may
 * contain the following properties:
 *
 * - "module": The URI from which the modules is loaded. This should be a
 *   resource: URI, ideally beginning with "resource://gre/actors/" or
 *   "resource:///actors/", with a filename matching the name of the actor for
 *   the given side. So, the child side of the Browser actor should live at
 *   "resource://gre/actors/BrowserChild.jsm".
 *
 * - "group": A group name which restricts the message managers to which this
 *   actor may be attached. This should match the "messagemanagergroup"
 *   attribute of a <browser> element. Frame scripts are responsible for
 *   attaching the appropriate actors to the appropriate browsers using
 *   ActorManagerChild.attach().
 *
 * - "events": An object containing a property for each event the actor will
 *   listen for, with an options object, as accepted by addEventListener, as its
 *   value. For each such property, an event listener will be added to the
 *   message manager[1] for the given event name, which delegates to the actor's
 *   handleEvent method.
 *
 * - "messages": An array of message manager message names. For each message
 *   name in the list, a message listener will be added to the frame message
 *   manager, and the messages it receives will be delegated to the actor's
 *   receiveMessage method.
 *
 * - "observers": An array of observer topics. A global observer will be added
 *   for each topic in the list, and observer notifications for it will be
 *   delegated to the actor's observe method. Note that observers are global in
 *   nature, and these notifications may therefore have nothing to do with the
 *   message manager the actor is bound to. The actor itself is responsible for
 *   filtering the notifications that apply to it.
 *
 *   These observers are automatically unregistered when the message manager is
 *   destroyed.
 *
 * - "matches": An array of URL match patterns (as accepted by the MatchPattern
 *   class in MatchPattern.webidl) which restrict which pages the actor may be
 *   instantiated for. If this is defined, the actor will only receive DOM
 *   events sent to windows which match this pattern, and will only receive
 *   message manager messages for frame message managers which are currently
 *   hosting a matching DOM window.
 *
 * - "allFrames": If "matches" is specified, this modifies its behavior to
 *   allow it to match sub-frames as well as top-level frames. If "allFrames" is
 *   not specified, it will match only top-level frames. See
 *   MozDocumentMather.webidl for more information.
 *
 * - "matchAboutBlank": If "matches" is specified, this modifies its behavior to
 *   allow it to match about:blank pages. See MozDocumentMather.webidl for more
 *   information.
 *
 * [1]: For actors which specify "matches" to restrict them to a certain set of
 *      windows, the listener will be added to the DOM window rather than the
 *      frame message manager.
 */

var EXPORTED_SYMBOLS = ["ActorManagerParent"];

ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");

const {DefaultMap} = ExtensionUtils;

let ACTORS = {
  AudibleAutoplay: {
    child: {
      module: "resource://gre/actors/AudibleAutoplayChild.jsm",
      events: {
        "AudibleAutoplayMediaOccurred": {},
      },
    },
  },

  AudioPlayback: {
    child: {
      module: "resource://gre/actors/AudioPlaybackChild.jsm",
      messages: [
        "AudioPlayback",
      ],
      observers: [
        "audio-playback",
      ],
    },
  },

  Browser: {
    child: {
      module: "resource://gre/actors/BrowserChild.jsm",
      events: {
        "DOMWindowClose": {},
      },

      messages: [
        "Browser:CreateAboutBlank",
        "InPermitUnload",
        "PermitUnload",
      ],
    },
  },

  Controllers: {
    child: {
      module: "resource://gre/actors/ControllersChild.jsm",
      messages: [
        "ControllerCommands:Do",
        "ControllerCommands:DoWithParams",
      ],
    },
  },

  DateTimePicker: {
    child: {
      module: "resource://gre/actors/DateTimePickerChild.jsm",
      events: {
        "MozOpenDateTimePicker": {},
      },
    },
  },

  ExtFind: {
    child: {
      module: "resource://gre/actors/ExtFindChild.jsm",
      messages: [
        "ext-Finder:CollectResults",
        "ext-Finder:HighlightResults",
        "ext-Finder:clearHighlighting",
      ],
    },
  },

  FindBar: {
    child: {
      module: "resource://gre/actors/FindBarChild.jsm",
      events: {
        "keypress": {mozSystemGroup: true},
      },
    },
  },

  Finder: {
    child: {
      module: "resource://gre/actors/FinderChild.jsm",
      messages: [
        "Finder:Initialize",
      ],
    },
  },

  ManifestMessages: {
    child: {
      module: "resource://gre/modules/ManifestMessagesChild.jsm",
      messages: [
        "DOM:Manifest:FireAppInstalledEvent",
        "DOM:ManifestObtainer:Obtain",
        "DOM:WebManifest:fetchIcon",
        "DOM:WebManifest:hasManifestLink",
      ],
    },
  },

  PopupBlocking: {
    child: {
      module: "resource://gre/actors/PopupBlockingChild.jsm",
      events: {
        "DOMPopupBlocked": {capture: true},
      },
    },
  },

  Printing: {
    child: {
      module: "resource://gre/actors/PrintingChild.jsm",
      events: {
        "PrintingError": {capture: true},
        "printPreviewUpdate": {capture: true},
      },
      messages: [
        "Printing:Preview:Enter",
        "Printing:Preview:Exit",
        "Printing:Preview:Navigate",
        "Printing:Preview:ParseDocument",
        "Printing:Print",
      ],
    },
  },

  PurgeSessionHistory: {
    child: {
      module: "resource://gre/actors/PurgeSessionHistoryChild.jsm",
      messages: [
        "Browser:PurgeSessionHistory",
      ],
    },
  },

  Select: {
    child: {
      module: "resource://gre/actors/SelectChild.jsm",
      events: {
        "mozshowdropdown": {},
        "mozshowdropdown-sourcetouch": {},
      },
    },
  },

  SelectionSource: {
    child: {
      module: "resource://gre/actors/SelectionSourceChild.jsm",
      messages: [
        "ViewSource:GetSelection",
      ],
    },
  },

  Thumbnails: {
    child: {
      module: "resource://gre/actors/ThumbnailsChild.jsm",
      messages: [
        "Browser:Thumbnail:Request",
        "Browser:Thumbnail:CheckState",
        "Browser:Thumbnail:GetOriginalURL",
      ],
    },
  },

  UnselectedTabHover: {
    child: {
      module: "resource://gre/actors/UnselectedTabHoverChild.jsm",
      events: {
        "UnselectedTabHover:Enable": {},
        "UnselectedTabHover:Disable": {},
      },
      messages: [
        "Browser:UnselectedTabHover",
      ],
    },
  },

  WebChannel: {
    child: {
      module: "resource://gre/actors/WebChannelChild.jsm",
      events: {
        "WebChannelMessageToChrome": {capture: true, wantUntrusted: true},
      },
      messages: [
        "WebChannelMessageToContent",
      ],
    },
  },

  WebNavigation: {
    child: {
      module: "resource://gre/actors/WebNavigationChild.jsm",
      messages: [
        "WebNavigation:GoBack",
        "WebNavigation:GoForward",
        "WebNavigation:GotoIndex",
        "WebNavigation:LoadURI",
        "WebNavigation:Reload",
        "WebNavigation:SetOriginAttributes",
        "WebNavigation:Stop",
      ],
    },
  },

  Zoom: {
    child: {
      module: "resource://gre/actors/ZoomChild.jsm",
      events: {
        "FullZoomChange": {},
        "TextZoomChange": {},
        "ZoomChangeUsingMouseWheel": {},
      },
      messages: [
        "FullZoom",
        "TextZoom",
      ],
    },
  },
};

class ActorSet {
  constructor(group, actorSide) {
    this.group = group;
    this.actorSide = actorSide;

    this.actors = new Map();
    this.events = [];
    this.messages = new DefaultMap(() => []);
    this.observers = new DefaultMap(() => []);
  }

  addActor(actorName, actor) {
    actorName += this.actorSide;
    this.actors.set(actorName, {module: actor.module});

    if (actor.events) {
      for (let [event, options] of Object.entries(actor.events)) {
        this.events.push({actor: actorName, event, options});
      }
    }
    for (let msg of actor.messages || []) {
      this.messages.get(msg).push(actorName);
    }
    for (let topic of actor.observers || []) {
      this.observers.get(topic).push(actorName);
    }
  }
}

const {sharedData} = Services.ppmm;

var ActorManagerParent = {
  // Actor sets which should be loaded in the child side, keyed by
  // "messagemanagergroup".
  childGroups: new DefaultMap(group => new ActorSet(group, "Child")),
  // Actor sets which should be loaded in the parent side, keyed by
  // "messagemanagergroup".
  parentGroups: new DefaultMap(group => new ActorSet(group, "Parent")),

  // Singleton actor sets, which should be loaded only for documents which match
  // a specific pattern. The keys in this map are plain objects specifying
  // filter keys as understood by MozDocumentMatcher.
  singletons: new DefaultMap(() => new ActorSet(null, "Child")),

  addActors(actors) {
    for (let [actorName, actor] of Object.entries(actors)) {
      let {child} = actor;
      {
        let actorSet;
        if (child.matches) {
          actorSet = this.singletons.get({matches: child.matches,
                                          allFrames: child.allFrames,
                                          matchAboutBlank: child.matchAboutBlank});
        } else {
          actorSet = this.childGroups.get(child.group || null);
        }

        actorSet.addActor(actorName, child);
      }

      if (actor.parent) {
        let {parent} = actor;
        this.parentGroups.get(parent.group || null).addActor(
          actorName, parent);
      }
    }
  },

  /**
   * Serializes the current set of registered actors into ppmm.sharedData, for
   * use by ActorManagerChild. This must be called before any frame message
   * managers have been created. It will have no effect on existing message
   * managers.
   */
  flush() {
    for (let [name, data] of this.childGroups) {
      sharedData.set(`ChildActors:${name || ""}`, data);
    }
    sharedData.set("ChildSingletonActors", this.singletons);
  },
};

ActorManagerParent.addActors(ACTORS);