mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-08 20:47:44 +00:00
b54abd8d84
--HG-- extra : rebase_source : f15018bf2c9427a0d6e8d44da348d8d7d1693219
110 lines
3.3 KiB
JavaScript
110 lines
3.3 KiB
JavaScript
/* 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";
|
|
|
|
let Cu = Components.utils;
|
|
let Ci = Components.interfaces;
|
|
let Cc = Components.classes;
|
|
|
|
function debug(msg) {
|
|
dump("BrowserElementChild - " + msg + "\n");
|
|
}
|
|
|
|
function sendAsyncMsg(msg, data) {
|
|
sendAsyncMessage('browser-element-api:' + msg, data);
|
|
}
|
|
|
|
/**
|
|
* The BrowserElementChild implements one half of <iframe mozbrowser>.
|
|
* (The other half is, unsurprisingly, BrowserElementParent.)
|
|
*
|
|
* This script is injected into an <iframe mozbrowser> via
|
|
* nsIMessageManager::LoadFrameScript().
|
|
*
|
|
* Our job here is to listen for events within this frame and bubble them up to
|
|
* the parent process.
|
|
*/
|
|
|
|
function BrowserElementChild() {
|
|
this._init();
|
|
};
|
|
|
|
BrowserElementChild.prototype = {
|
|
_init: function() {
|
|
debug("Starting up.");
|
|
sendAsyncMsg("hello");
|
|
|
|
docShell.QueryInterface(Ci.nsIWebProgress)
|
|
.addProgressListener(this._progressListener,
|
|
Ci.nsIWebProgress.NOTIFY_LOCATION |
|
|
Ci.nsIWebProgress.NOTIFY_STATE_WINDOW);
|
|
|
|
addEventListener('DOMTitleChanged',
|
|
this._titleChangedHandler.bind(this),
|
|
/* useCapture = */ true,
|
|
/* wantsUntrusted = */ false);
|
|
},
|
|
|
|
_titleChangedHandler: function(e) {
|
|
debug("Got titlechanged: (" + e.target.title + ")");
|
|
var win = e.target.defaultView;
|
|
|
|
// Ignore titlechanges which don't come from the top-level
|
|
// <iframe mozbrowser> window.
|
|
if (win == content) {
|
|
sendAsyncMsg('titlechange', e.target.title);
|
|
}
|
|
else {
|
|
debug("Not top level!");
|
|
}
|
|
},
|
|
|
|
// The docShell keeps a weak reference to the progress listener, so we need
|
|
// to keep a strong ref to it ourselves.
|
|
_progressListener: {
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
|
|
Ci.nsISupportsWeakReference,
|
|
Ci.nsISupports]),
|
|
_seenLoadStart: false,
|
|
|
|
onLocationChange: function(webProgress, request, location, flags) {
|
|
// We get progress events from subshells here, which is kind of weird.
|
|
if (webProgress != docShell) {
|
|
return;
|
|
}
|
|
|
|
// Ignore locationchange events which occur before the first loadstart.
|
|
// These are usually about:blank loads we don't care about.
|
|
if (!this._seenLoadStart) {
|
|
return;
|
|
}
|
|
|
|
sendAsyncMsg('locationchange', location.spec);
|
|
},
|
|
|
|
onStateChange: function(webProgress, request, stateFlags, status) {
|
|
if (webProgress != docShell) {
|
|
return;
|
|
}
|
|
|
|
if (stateFlags & Ci.nsIWebProgressListener.STATE_START) {
|
|
this._seenLoadStart = true;
|
|
sendAsyncMsg('loadstart');
|
|
}
|
|
|
|
if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
|
|
sendAsyncMsg('loadend');
|
|
}
|
|
},
|
|
|
|
onStatusChange: function(webProgress, request, status, message) {},
|
|
onProgressChange: function(webProgress, request, curSelfProgress,
|
|
maxSelfProgress, curTotalProgress, maxTotalProgress) {},
|
|
onSecurityChange: function(webProgress, request, aState) {}
|
|
},
|
|
};
|
|
|
|
var api = new BrowserElementChild();
|