webapprt/content/dbg-webapp-actors.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:ab166dcdec12
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 'use strict';
6
7 const { Promise } = Cu.import("resource://gre/modules/Promise.jsm", {});
8
9 /**
10 * WebappRT-specific actors.
11 */
12
13 /**
14 * Construct a root actor appropriate for use in a server running in the webapp
15 * runtime. The returned root actor:
16 * - respects the factories registered with DebuggerServer.addGlobalActor,
17 * - uses a WebappTabList to supply tab actors,
18 * - sends all webapprt:webapp window documents a Debugger:Shutdown event
19 * when it exits.
20 *
21 * * @param connection DebuggerServerConnection
22 * The conection to the client.
23 */
24 function createRootActor(connection)
25 {
26 let parameters = {
27 tabList: new WebappTabList(connection),
28 globalActorFactories: DebuggerServer.globalActorFactories,
29 onShutdown: sendShutdownEvent
30 };
31 return new RootActor(connection, parameters);
32 }
33
34 /**
35 * A live list of BrowserTabActors representing the current webapp windows,
36 * to be provided to the root actor to answer 'listTabs' requests. In the
37 * webapp runtime, only a single tab per window is ever present.
38 *
39 * @param connection DebuggerServerConnection
40 * The connection in which this list's tab actors may participate.
41 *
42 * @see BrowserTabList for more a extensive description of how tab list objects
43 * work.
44 */
45 function WebappTabList(connection)
46 {
47 BrowserTabList.call(this, connection);
48 }
49
50 WebappTabList.prototype = Object.create(BrowserTabList.prototype);
51
52 WebappTabList.prototype.constructor = WebappTabList;
53
54 WebappTabList.prototype.getList = function() {
55 let topXULWindow = Services.wm.getMostRecentWindow(this._windowType);
56
57 // As a sanity check, make sure all the actors presently in our map get
58 // picked up when we iterate over all windows.
59 let initialMapSize = this._actorByBrowser.size;
60 let foundCount = 0;
61
62 // To avoid mysterious behavior if windows are closed or opened mid-iteration,
63 // we update the map first, and then make a second pass over it to yield
64 // the actors. Thus, the sequence yielded is always a snapshot of the
65 // actors that were live when we began the iteration.
66
67 // Iterate over all webapprt:webapp XUL windows.
68 for (let win of allAppShellDOMWindows(this._windowType)) {
69 let browser = win.document.getElementById("content");
70 if (!browser) {
71 continue;
72 }
73
74 // Do we have an existing actor for this browser? If not, create one.
75 let actor = this._actorByBrowser.get(browser);
76 if (actor) {
77 foundCount++;
78 } else {
79 actor = new WebappTabActor(this._connection, browser);
80 this._actorByBrowser.set(browser, actor);
81 }
82
83 actor.selected = (win == topXULWindow);
84 }
85
86 if (this._testing && initialMapSize !== foundCount) {
87 throw Error("_actorByBrowser map contained actors for dead tabs");
88 }
89
90 this._mustNotify = true;
91 this._checkListening();
92
93 return Promise.resolve([actor for ([_, actor] of this._actorByBrowser)]);
94 };
95
96 /**
97 * Creates a tab actor for handling requests to the single tab, like
98 * attaching and detaching. WebappTabActor respects the actor factories
99 * registered with DebuggerServer.addTabActor.
100 *
101 * We override the title of the XUL window in content/webapp.js so here
102 * we need to override the title property to avoid confusion to the user.
103 * We won't return the title of the contained browser, but the title of
104 * the webapp window.
105 *
106 * @param connection DebuggerServerConnection
107 * The conection to the client.
108 * @param browser browser
109 * The browser instance that contains this tab.
110 */
111 function WebappTabActor(connection, browser)
112 {
113 BrowserTabActor.call(this, connection, browser);
114 }
115
116 WebappTabActor.prototype.constructor = WebappTabActor;
117
118 WebappTabActor.prototype = Object.create(BrowserTabActor.prototype);
119
120 Object.defineProperty(WebappTabActor.prototype, "title", {
121 get: function() {
122 return this.browser.ownerDocument.defaultView.document.title;
123 },
124 enumerable: true,
125 configurable: false
126 });

mercurial