Bug 694136 - [birch] Refactor the browser JS and XUL files [r=fabrice r=kats]

This commit is contained in:
Mark Finkle 2011-10-12 17:31:04 -04:00
parent 4f31612e48
commit 6bd6047208
3 changed files with 210 additions and 259 deletions

View File

@ -95,6 +95,12 @@ public class GeckoAppShell
static private final int NOTIFY_IME_CANCELCOMPOSITION = 2;
static private final int NOTIFY_IME_FOCUSCHANGE = 3;
/* Keep in sync with constants found here:
http://mxr.mozilla.org/mozilla-central/source/uriloader/base/nsIWebProgressListener.idl
*/
static private final int WPL_STATE_START = 0x00000001;
static private final int WPL_STATE_IS_DOCUMENT = 0x00020000;
static private File sCacheFile = null;
static private int sFreeSpace = -1;
@ -1635,64 +1641,64 @@ public class GeckoAppShell
if (type.equals("DOMContentLoaded")) {
final String uri = geckoObject.getString("uri");
final String title = geckoObject.getString("title");
final String stat = geckoObject.getString("stat");
final CharSequence titleText = title;
getMainHandler().post(new Runnable() {
public void run() {
GeckoApp.mAwesomeBar.setText(titleText);
GeckoApp.addHistoryEntry(new GeckoApp.HistoryEntry(uri, title));
GeckoApp.mProgressBar.setVisibility(View.GONE);
}
});
Log.i("GeckoShell", "URI - " + uri + ", title - " + title + ", status - " + stat);
}
else if (type.equals("log")) {
public void run() {
GeckoApp.mAwesomeBar.setText(titleText);
GeckoApp.addHistoryEntry(new GeckoApp.HistoryEntry(uri, title));
GeckoApp.mProgressBar.setVisibility(View.GONE);
}
});
Log.i("GeckoShell", "URI - " + uri + ", title - " + title);
} else if (type.equals("log")) {
// generic log listener
final String msg = geckoObject.getString("msg");
Log.i("GeckoShell", "Log: " + msg);
}
else if (type.equals("onLocationChange")) {
} else if (type.equals("onLocationChange")) {
final String uri = geckoObject.getString("uri");
final CharSequence uriText = uri;
Log.i("GeckoShell", "URI - " + uri);
getMainHandler().post(new Runnable() {
public void run() {
GeckoApp.mAwesomeBar.setText(uriText);
}
});
}
else if (type.equals("onStateChange")) {
String state = geckoObject.getString("state");
String stateIs = geckoObject.getString("stateIs");
if (state == "start") {
GeckoApp.mProgressBar.setVisibility(View.VISIBLE);
GeckoApp.mProgressBar.setIndeterminate(true);
public void run() {
GeckoApp.mAwesomeBar.setText(uriText);
}
});
} else if (type.equals("onStateChange")) {
int state = geckoObject.getInt("state");
Log.i("GeckoShell", "State - " + state);
if ((state & WPL_STATE_START) != 0) {
if ((state & WPL_STATE_IS_DOCUMENT) != 0) {
Log.i("GeckoShell", "Got a document start");
getMainHandler().post(new Runnable() {
public void run() {
GeckoApp.mProgressBar.setVisibility(View.VISIBLE);
GeckoApp.mProgressBar.setIndeterminate(true);
}
});
}
}
}
else if (type.equals("onProgressChange")) {
} else if (type.equals("onProgressChange")) {
final int current = geckoObject.getInt("current");
final int total = geckoObject.getInt("total");
getMainHandler().post(new Runnable() {
public void run() {
if (total == -1) {
GeckoApp.mProgressBar.setIndeterminate(true);
} else if (current < total) {
GeckoApp.mProgressBar.setIndeterminate(false);
GeckoApp.mProgressBar.setMax(total);
GeckoApp.mProgressBar.setProgress(current);
}
else {
GeckoApp.mProgressBar.setIndeterminate(false);
}
public void run() {
if (total == -1) {
GeckoApp.mProgressBar.setIndeterminate(true);
} else if (current < total) {
GeckoApp.mProgressBar.setIndeterminate(false);
GeckoApp.mProgressBar.setMax(total);
GeckoApp.mProgressBar.setProgress(current);
} else {
GeckoApp.mProgressBar.setIndeterminate(false);
}
});
}
});
Log.i("GeckoShell", "progress - " + current + "/" + total);
}
} catch (Exception e) {
Log.i("GeckoShell", "handleGeckoMessage throws "+e);
Log.i("GeckoShell", "handleGeckoMessage throws " + e);
}
}
}

View File

@ -8,275 +8,221 @@ Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/Services.jsm")
function dump(a) {
Cc["@mozilla.org/consoleservice;1"]
.getService(Ci.nsIConsoleService)
.logStringMessage(a);
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(a);
}
function SendMessageToJava(aMessage) {
let bridge = Cc["@mozilla.org/android/bridge;1"].getService(Ci.nsIAndroidBridge);
bridge.handleGeckoMessage(JSON.stringify(aMessage));
}
var fennecProgressListener = null;
var fennecEventHandler = null;
var responses = {};
function startup() {
window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow = new nsBrowserAccess(this);
dump("zerdatime " + Date.now() + " - browser chrome startup finished.");
var httpRequestObserver = {
fennecProgressListener = new FennecProgressListener(this);
observe: function(subject, topic, data) {
let browser = document.getElementById("browser");
browser.addProgressListener(fennecProgressListener, Ci.nsIWebProgress.NOTIFY_ALL);
if (topic == "http-on-examine-response") {
var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
responses[subject.URI.spec] = httpChannel.responseStatus;
}
},
get observerService() {
return Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
},
register: function() {
this.observerService.addObserver(this, "http-on-examine-response", false);
},
unregister: function() {
this.observerService.removeObserver(this, "http-on-examine-response");
}
fennecEventHandler = new FennecEventHandler(browser);
window.addEventListener("click", fennecEventHandler, true);
window.addEventListener("mousedown", fennecEventHandler, true);
window.addEventListener("mouseup", fennecEventHandler, true);
window.addEventListener("mousemove", fennecEventHandler, true);
};
httpRequestObserver.register();
browser.addEventListener("MozMagnifyGestureStart", fennecEventHandler, true);
browser.addEventListener("MozMagnifyGestureUpdate", fennecEventHandler, true);
browser.addEventListener("DOMContentLoaded", fennecEventHandler, true);
window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow = new nsBrowserAccess(this);
dump("zerdatime " + Date.now() + " - browser chrome startup finished.");
let uri = "about:support";
try {
uri = Services.prefs.getCharPref("browser.last.uri");
} catch (e) {};
fennecProgressListener = new FennecProgressListener(this);
let frame = document.getElementById('home');
frame.addProgressListener(fennecProgressListener, Ci.nsIWebProgress.NOTIFY_ALL);
fennecEventHandler = new FennecEventHandler(frame);
window.addEventListener("click", fennecEventHandler, true);
window.addEventListener("mousedown", fennecEventHandler, true);
window.addEventListener("mouseup", fennecEventHandler, true);
window.addEventListener("mousemove", fennecEventHandler, true);
frame.addEventListener("MozMagnifyGestureStart", fennecEventHandler, true);
frame.addEventListener("MozMagnifyGestureUpdate", fennecEventHandler, true);
let uri = "about:support";
try {
uri = Services.prefs.getCharPref("browser.last.uri");
} catch (e){};
// XXX maybe we don't do this if the launch was kicked off from external
Services.io.offline = false;
frame.loadURI(uri, null, null);
// XXX maybe we don't do this if the launch was kicked off from external
Services.io.offline = false;
browser.loadURI(uri, null, null);
}
function nsBrowserAccess(chromeWindow) {
this.chrome = chromeWindow;
function nsBrowserAccess() {
}
nsBrowserAccess.prototype = {
openURI: function browser_openURI(aURI, aOpener, aWhere, aContext) {
let frame = this.chrome.document.getElementById("home");
Services.io.offline = false;
frame.loadURI(aURI.spec, null, null);
return null;
dump("nsBrowserAccess::openURI");
let browser = document.getElementById("browser");
// Why does returning the browser.contentWindow not work here?
Services.io.offline = false;
browser.loadURI(aURI.spec, null, null);
return null;
},
openURIInFrame: function browser_openURIInFrame(aURI, aOpener, aWhere, aContext) {
dump("nsBrowserAccess::openURIInFrame");
return null;
dump("nsBrowserAccess::openURIInFrame");
return null;
},
isTabContentWindow: function(aWindow) {
return !(this.chrome == aWindow);
// We have no tabs yet
return false;
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIBrowserDOMWindow]),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIBrowserDOMWindow])
};
function SendMessageToJava(obj) {
Cc["@mozilla.org/android/bridge;1"]
.getService(Ci.nsIAndroidBridge)
.handleGeckoMessage(JSON.stringify(obj));
}
function FennecProgressListener(chromeWindow){
this.chrome = chromeWindow;
function FennecProgressListener() {
}
FennecProgressListener.prototype = {
onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
let windowID = 0; //aWebProgress.DOMWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
let message = {
gecko: {
type: "onStateChange",
windowID: windowID,
state: aStateFlags
}
};
onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
SendMessageToJava(message);
},
let windowID = 0;//aWebProgress.DOMWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
onLocationChange: function(aWebProgress, aRequest, aLocationURI) {
try {
let browser = document.getElementById("browser");
let uri = browser.currentURI.spec;
let windowID = 0;
let state = "start";
if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP)
state = "stop";
let stateIs = "network"
if (aStateFlags & Ci.nsIWebProgressListener.STATE_IS_DOCUMENT)
stateIs = "document";
dump("Setting Last uri to: " + uri);
Services.prefs.setCharPref("browser.last.uri", uri);
let message = {
"gecko": {
"type" : "onStateChange",
"windowID" : windowID,
"state" : state,
"stateIs" : stateIs
}};
gecko: {
type: "onLocationChange",
windowID: windowID,
uri: uri
}
};
SendMessageToJava(message);
if (state == "start" && stateIs == "document") {
let browser = this.chrome.document.getElementById("home");
let uri = browser.currentURI.spec;
//TODO: add the event listener when creating a new tab
browser.contentDocument.addEventListener("DOMTitleChanged", function () {
if (uri == Services.prefs.getCharPref("browser.last.uri")) {
let showMessage = {
gecko: {
type: "hideLoadingScreen",
windowID: windowID
}
};
let stat = 0;
if (responses[uri]) {
stat = responses[uri];
responses = {};
}
SendMessageToJava({
gecko: {
type: "DOMContentLoaded",
windowID: windowID,
uri: uri,
title: browser.contentTitle,
stat: stat
}
});
}, false);
SendMessageToJava(showMessage);
}
} catch (e) {
dump("onLocationChange throws " + e);
}
},
},
onSecurityChange: function(aBrowser, aWebProgress, aRequest, aState) {
dump("progressListener.onSecurityChange");
},
onLocationChange: function (aWebProgress, aRequest, aLocationURI) {
try {
onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
let windowID = 0; //aWebProgress.DOMWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
let message = {
gecko: {
type: "onProgressChange",
windowID: windowID,
current: aCurTotalProgress,
total: aMaxTotalProgress
}
};
let browser = this.chrome.document.getElementById("home");
let uri = browser.currentURI.spec;
let windowID = 0;
SendMessageToJava(message);
},
onStatusChange: function(aBrowser, aWebProgress, aRequest, aStatus, aMessage) {
dump("progressListener.onStatusChange");
},
dump("Setting Last uri to: " + uri);
Services.prefs.setCharPref("browser.last.uri", uri);
let message = {
"gecko": {
"type" : "onLocationChange",
"windowID" : windowID,
"uri" : uri
}
};
SendMessageToJava(message);
if (uri == Services.prefs.getCharPref("browser.last.uri")) {
let showMessage = {
"gecko": {
"type" : "hideLoadingScreen",
"windowID" : windowID
}};
SendMessageToJava(showMessage);
}
} catch (e) {dump("onLocationChange throws " + e); }
},
onSecurityChange: function (aBrowser, aWebProgress, aRequest, aState) { dump("progressListener.onSecurityChange"); },
onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
let windowID = 0;//aWebProgress.DOMWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
let message = {
"gecko": {
"type" : "onProgressChange",
"windowID" : windowID,
"current" : aCurTotalProgress,
"total" : aMaxTotalProgress
}};
SendMessageToJava(message);
},
onStatusChange: function (aBrowser, aWebProgress, aRequest, aStatus, aMessage) { dump("progressListener.onStatusChange");},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
Ci.nsISupportsWeakReference]),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference])
};
function FennecEventHandler(browser) {
this.browser = browser;
this._updateLastPosition(0, 0);
this.browser = browser;
this._updateLastPosition(0, 0);
}
FennecEventHandler.prototype = {
handleEvent: function(aEvent) {
switch (aEvent.type) {
case "click":
if (this.blockClick) {
aEvent.stopPropagation();
aEvent.preventDefault();
}
break;
case "mousedown":
this.startX = aEvent.clientX;
this.startY = aEvent.clientY;
this.blockClick = false;
this._updateLastPosition(aEvent.clientX, aEvent.clientY);
aEvent.stopPropagation();
aEvent.preventDefault();
break;
case "mousemove":
let dx = aEvent.clientX - this.lastX;
let dy = aEvent.clientY - this.lastY;
this.browser.contentWindow.wrappedJSObject.scrollBy(-dx, -dy);
this._updateLastPosition(aEvent.clientX, aEvent.clientY);
aEvent.stopPropagation();
aEvent.preventDefault();
break;
case "mouseup":
if (Math.abs(aEvent.clientX - this.startX) > 10 ||
Math.abs(aEvent.clientY - this.startY) > 10) {
this.blockClick = true;
}
aEvent.stopPropagation();
aEvent.preventDefault();
break;
case "MozMagnifyGestureStart":
this._pinchDelta = 0;
break;
case "MozMagnifyGestureUpdate":
if (!aEvent.delta)
break;
this._pinchDelta += (aEvent.delta / 100);
if (Math.abs(this._pinchDelta) >= 1) {
let delta = Math.round(this._pinchDelta);
this.browser.markupDocumentViewer.fullZoom += delta;
this._pinchDelta = 0;
}
break;
handleEvent: function(aEvent) {
switch (aEvent.type) {
case "DOMContentLoaded":
SendMessageToJava({
gecko: {
type: "DOMContentLoaded",
windowID: 0,
uri: this.browser.currentURI.spec,
title: this.browser.contentTitle
}
});
break;
case "click":
if (this.blockClick) {
aEvent.stopPropagation();
aEvent.preventDefault();
}
},
_updateLastPosition: function(x, y) {
this.lastX = x;
this.lastY = y;
break;
case "mousedown":
this.startX = aEvent.clientX;
this.startY = aEvent.clientY;
this.blockClick = false;
this._updateLastPosition(aEvent.clientX, aEvent.clientY);
aEvent.stopPropagation();
aEvent.preventDefault();
break;
case "mousemove":
let dx = aEvent.clientX - this.lastX;
let dy = aEvent.clientY - this.lastY;
this.browser.contentWindow.wrappedJSObject.scrollBy(-dx, -dy);
this._updateLastPosition(aEvent.clientX, aEvent.clientY);
aEvent.stopPropagation();
aEvent.preventDefault();
break;
case "mouseup":
if (Math.abs(aEvent.clientX - this.startX) > 10 || Math.abs(aEvent.clientY - this.startY) > 10) {
this.blockClick = true;
}
aEvent.stopPropagation();
aEvent.preventDefault();
break;
case "MozMagnifyGestureStart":
this._pinchDelta = 0;
break;
case "MozMagnifyGestureUpdate":
if (!aEvent.delta)
break;
this._pinchDelta += (aEvent.delta / 100);
if (Math.abs(this._pinchDelta) >= 1) {
let delta = Math.round(this._pinchDelta);
this.browser.markupDocumentViewer.fullZoom += delta;
this._pinchDelta = 0;
}
break;
}
},
_updateLastPosition: function(x, y) {
this.lastX = x;
this.lastY = y;
}
};

View File

@ -7,8 +7,7 @@
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:html="http://www.w3.org/1999/xhtml">
<script type="application/javascript" src="chrome://browser/content/browser.js"/>
<browser id="home" type="content-primary" flex="1"/>
<script type="application/javascript" src="chrome://browser/content/browser.js"/>
<browser id="browser" type="content-primary" flex="1"/>
</window>