From 2deefd019c12428af21b723879df0081074a9211 Mon Sep 17 00:00:00 2001 From: Ryan VanderMeulen Date: Thu, 5 Feb 2015 10:39:12 -0500 Subject: [PATCH] Backed out changeset 434a86b48ee0 (bug 1129567) because it wasn't meant to land on trunk. --- addon-sdk/source/lib/sdk/page-mod.js | 36 +++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/addon-sdk/source/lib/sdk/page-mod.js b/addon-sdk/source/lib/sdk/page-mod.js index ae7aff8990ac..94dd671e6151 100644 --- a/addon-sdk/source/lib/sdk/page-mod.js +++ b/addon-sdk/source/lib/sdk/page-mod.js @@ -14,11 +14,10 @@ const { getAttachEventType, WorkerHost } = require('./content/utils'); const { Class } = require('./core/heritage'); const { Disposable } = require('./core/disposable'); const { WeakReference } = require('./core/reference'); -const { Worker } = require('./deprecated/sync-worker'); +const { Worker } = require('./content/worker'); const { EventTarget } = require('./event/target'); const { on, emit, once, setListeners } = require('./event/core'); const { on: domOn, removeListener: domOff } = require('./dom/events'); -const { pipe } = require('./event/utils'); const { isRegExp, isUndefined } = require('./lang/type'); const { merge } = require('./util/object'); const { windowIterator } = require('./deprecated/window-utils'); @@ -114,7 +113,6 @@ const PageMod = Class({ modContract.properties(modelFor), EventTarget, Disposable, - WeakReference ], extends: WorkerHost(workerFor), setup: function PageMod(options) { @@ -191,6 +189,10 @@ function applyOnExistingDocuments (mod) { getTabs().forEach(tab => { // Fake a newly created document let window = getTabContentWindow(tab); + // on startup with e10s, contentWindow might not exist yet, + // in which case we will get notified by "document-element-inserted". + if (!window || !window.frames) + return; let uri = getTabURI(tab); if (has(mod.attachTo, "top") && modMatchesURI(mod, uri)) onContent(mod, window); @@ -213,11 +215,15 @@ function createWorker (mod, window) { onError: (e) => emit(mod, 'error', e) }); workers.set(mod, worker); - pipe(worker, mod); - emit(mod, 'attach', worker); - once(worker, 'detach', function detach() { - worker.destroy(); - }); + worker.on('*', (event, ...args) => { + // worker's "attach" event passes a window as the argument + // page-mod's "attach" event needs a worker + if (event === 'attach') + emit(mod, event, worker) + else + emit(mod, event, ...args); + }) + once(worker, 'detach', () => worker.destroy()); } function onContent (mod, window) { @@ -256,6 +262,20 @@ function onContent (mod, window) { return; domOff(window, eventName, onReady, true); createWorker(mod, window); + + // Attaching is asynchronous so if the document is already loaded we will + // miss the pageshow event so send a synthetic one. + if (window.document.readyState == "complete") { + mod.on('attach', worker => { + try { + worker.send('pageshow'); + emit(worker, 'pageshow'); + } + catch (e) { + // This can fail if an earlier attach listener destroyed the worker + } + }); + } }, true); }