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