Bug 1372681 - [3.3] Add GeckoView handlers. r=snorp,jchen

This commit is contained in:
Eugen Sawin 2017-06-13 00:18:44 +02:00 коммит произвёл Eugen Sawin
Родитель 7f564e065b
Коммит b2ba9bbe43
5 изменённых файлов: 151 добавлений и 110 удалений

Просмотреть файл

@ -92,10 +92,102 @@ public class GeckoView extends LayerView {
private final EventDispatcher mEventDispatcher =
new EventDispatcher(mNativeQueue);
/* package */ ContentListener mContentListener;
/* package */ NavigationListener mNavigationListener;
/* package */ ProgressListener mProgressListener;
/* package */ ScrollListener mScrollListener;
private final GeckoViewHandler<ContentListener> mContentHandler =
new GeckoViewHandler<ContentListener>(
"GeckoViewContent", this,
new String[]{
"GeckoView:DOMTitleChanged",
"GeckoView:FullScreenEnter",
"GeckoView:FullScreenExit"
}
) {
@Override
public void handleMessage(final ContentListener listener,
final String event,
final GeckoBundle message,
final EventCallback callback) {
if ("GeckoView:DOMTitleChanged".equals(event)) {
listener.onTitleChange(GeckoView.this,
message.getString("title"));
} else if ("GeckoView:FullScreenEnter".equals(event)) {
listener.onFullScreen(GeckoView.this, true);
} else if ("GeckoView:FullScreenExit".equals(event)) {
listener.onFullScreen(GeckoView.this, false);
}
}
};
private final GeckoViewHandler<NavigationListener> mNavigationHandler =
new GeckoViewHandler<NavigationListener>(
"GeckoViewNavigation", this,
new String[]{ "GeckoView:LocationChange" }
) {
@Override
public void handleMessage(final NavigationListener listener,
final String event,
final GeckoBundle message,
final EventCallback callback) {
if ("GeckoView:LocationChange".equals(event)) {
listener.onLocationChange(GeckoView.this,
message.getString("uri"));
listener.onCanGoBack(GeckoView.this,
message.getBoolean("canGoBack"));
listener.onCanGoForward(GeckoView.this,
message.getBoolean("canGoForward"));
}
}
};
private final GeckoViewHandler<ProgressListener> mProgressHandler =
new GeckoViewHandler<ProgressListener>(
"GeckoViewProgress", this,
new String[]{
"GeckoView:PageStart",
"GeckoView:PageStop",
"GeckoView:SecurityChanged"
}
) {
@Override
public void handleMessage(final ProgressListener listener,
final String event,
final GeckoBundle message,
final EventCallback callback) {
if ("GeckoView:PageStart".equals(event)) {
listener.onPageStart(GeckoView.this,
message.getString("uri"));
} else if ("GeckoView:PageStop".equals(event)) {
listener.onPageStop(GeckoView.this,
message.getBoolean("success"));
} else if ("GeckoView:SecurityChanged".equals(event)) {
int state = message.getInt("status") &
GeckoView.ProgressListener.STATE_ALL;
listener.onSecurityChange(GeckoView.this, state);
}
}
};
private final GeckoViewHandler<ScrollListener> mScrollHandler =
new GeckoViewHandler<ScrollListener>(
"GeckoViewScroll", this,
new String[]{ "GeckoView:ScrollChanged" }
) {
@Override
public void handleMessage(final ScrollListener listener,
final String event,
final GeckoBundle message,
final EventCallback callback) {
if ("GeckoView:ScrollChanged".equals(event)) {
listener.onScrollChanged(GeckoView.this,
message.getInt("scrollX"),
message.getInt("scrollY"));
}
}
};
private PromptDelegate mPromptDelegate;
private InputConnectionListener mInputConnectionListener;
@ -200,15 +292,7 @@ public class GeckoView extends LayerView {
private class Listener implements BundleEventListener {
/* package */ void registerListeners() {
getEventDispatcher().registerUiThreadListener(this,
"GeckoView:DOMTitleChanged",
"GeckoView:FullScreenEnter",
"GeckoView:FullScreenExit",
"GeckoView:LocationChange",
"GeckoView:PageStart",
"GeckoView:PageStop",
"GeckoView:Prompt",
"GeckoView:SecurityChanged",
"GeckoView:ScrollChanged",
null);
}
@ -219,48 +303,8 @@ public class GeckoView extends LayerView {
Log.d(LOGTAG, "handleMessage: event = " + event);
}
if ("GeckoView:DOMTitleChanged".equals(event)) {
if (mContentListener != null) {
mContentListener.onTitleChange(GeckoView.this, message.getString("title"));
}
} else if ("GeckoView:FullScreenEnter".equals(event)) {
if (mContentListener != null) {
mContentListener.onFullScreen(GeckoView.this, true);
}
} else if ("GeckoView:FullScreenExit".equals(event)) {
if (mContentListener != null) {
mContentListener.onFullScreen(GeckoView.this, false);
}
} else if ("GeckoView:LocationChange".equals(event)) {
if (mNavigationListener == null) {
// We shouldn't be getting this event.
mEventDispatcher.dispatch("GeckoViewNavigation:Inactive", null);
} else {
mNavigationListener.onLocationChange(GeckoView.this, message.getString("uri"));
mNavigationListener.onCanGoBack(GeckoView.this, message.getBoolean("canGoBack"));
mNavigationListener.onCanGoForward(GeckoView.this, message.getBoolean("canGoForward"));
}
} else if ("GeckoView:PageStart".equals(event)) {
if (mProgressListener != null) {
mProgressListener.onPageStart(GeckoView.this, message.getString("uri"));
}
} else if ("GeckoView:PageStop".equals(event)) {
if (mProgressListener != null) {
mProgressListener.onPageStop(GeckoView.this, message.getBoolean("success"));
}
} else if ("GeckoView:Prompt".equals(event)) {
if ("GeckoView:Prompt".equals(event)) {
handlePromptEvent(GeckoView.this, message, callback);
} else if ("GeckoView:SecurityChanged".equals(event)) {
if (mProgressListener != null) {
int state = message.getInt("status") & ProgressListener.STATE_ALL;
mProgressListener.onSecurityChange(GeckoView.this, state);
}
} else if ("GeckoView:ScrollChanged".equals(event)) {
if (mScrollListener != null) {
mScrollListener.onScrollChanged(GeckoView.this,
message.getInt("scrollX"),
message.getInt("scrollY"));
}
}
}
}
@ -624,10 +668,10 @@ public class GeckoView extends LayerView {
/**
* Set the content callback handler.
* This will replace the current handler.
* @param content An implementation of ContentListener.
* @param listener An implementation of ContentListener.
*/
public void setContentListener(ContentListener content) {
mContentListener = content;
public void setContentListener(ContentListener listener) {
mContentHandler.setListener(listener, this);
}
/**
@ -635,16 +679,16 @@ public class GeckoView extends LayerView {
* @return The current content callback handler.
*/
public ContentListener getContentListener() {
return mContentListener;
return mContentHandler.getListener();
}
/**
* Set the progress callback handler.
* This will replace the current handler.
* @param progress An implementation of ProgressListener.
* @param listener An implementation of ProgressListener.
*/
public void setProgressListener(ProgressListener progress) {
mProgressListener = progress;
public void setProgressListener(ProgressListener listener) {
mProgressHandler.setListener(listener, this);
}
/**
@ -652,25 +696,16 @@ public class GeckoView extends LayerView {
* @return The current progress callback handler.
*/
public ProgressListener getProgressListener() {
return mProgressListener;
return mProgressHandler.getListener();
}
/**
* Set the navigation callback handler.
* This will replace the current handler.
* @param navigation An implementation of NavigationListener.
* @param listener An implementation of NavigationListener.
*/
public void setNavigationListener(NavigationListener listener) {
if (mNavigationListener == listener) {
return;
}
if (listener == null) {
mEventDispatcher.dispatch("GeckoViewNavigation:Inactive", null);
} else if (mNavigationListener == null) {
mEventDispatcher.dispatch("GeckoViewNavigation:Active", null);
}
mNavigationListener = listener;
mNavigationHandler.setListener(listener, this);
}
/**
@ -678,7 +713,7 @@ public class GeckoView extends LayerView {
* @return The current navigation callback handler.
*/
public NavigationListener getNavigationListener() {
return mNavigationListener;
return mNavigationHandler.getListener();
}
/**
@ -698,10 +733,7 @@ public class GeckoView extends LayerView {
* @param listener An implementation of ScrollListener.
*/
public void setScrollListener(ScrollListener listener) {
if (mScrollListener == listener) {
return;
}
mScrollListener = listener;
mScrollHandler.setListener(listener, this);
}
/**

Просмотреть файл

@ -19,17 +19,20 @@ function debug(aMsg) {
class GeckoViewContent extends GeckoViewModule {
init() {
this.messageManager.loadFrameScript("chrome://geckoview/content/GeckoViewContent.js", true);
this.messageManager.loadFrameScript(
"chrome://geckoview/content/GeckoViewContent.js", true);
}
register() {
this.window.addEventListener("MozDOMFullScreen:Entered", this,
/* capture */ true, /* untrusted */ false);
this.window.addEventListener("MozDOMFullScreen:Exited", this,
/* capture */ true, /* untrusted */ false);
this.eventDispatcher.registerListener(this, "GeckoViewContent:ExitFullScreen");
this.messageManager.addMessageListener("GeckoView:DOMFullscreenExit", this);
this.messageManager.addMessageListener("GeckoView:DOMFullscreenRequest", this);
this.messageManager.addMessageListener("GeckoView:DOMTitleChanged", this);
this.window.addEventListener("MozDOMFullscreen:Entered", this,
/* capture */ true, /* untrusted */ false);
this.window.addEventListener("MozDOMFullscreen:Exited", this,
/* capture */ true, /* untrusted */ false);
this.eventDispatcher.registerListener(this, ["GeckoViewContent:ExitFullScreen"]);
}
// Bundle event handler.
@ -42,6 +45,17 @@ class GeckoViewContent extends GeckoViewModule {
}
}
unregister() {
this.window.removeEventListener("MozDOMFullScreen:Entered", this,
/* capture */ true);
this.window.removeEventListener("MozDOMFullScreen:Exited", this,
/* capture */ true);
this.eventDispatcher.unregisterListener(this, "GeckoViewContent:ExitFullScreen");
this.messageManager.removeMessageListener("GeckoView:DOMFullscreenExit", this);
this.messageManager.removeMessageListener("GeckoView:DOMFullscreenRequest", this);
this.messageManager.removeMessageListener("GeckoView:DOMTitleChanged", this);
}
// DOM event handler
handleEvent(aEvent) {
debug("handleEvent: aEvent.type=" + aEvent.type);

Просмотреть файл

@ -33,13 +33,7 @@ class GeckoViewNavigation extends GeckoViewModule {
init() {
this.window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow = this;
// We need to listen initially because the "GeckoViewNavigation:Active"
// event may be dispatched before this object is constructed.
this.registerProgressListener();
this.eventDispatcher.registerListener(this, [
"GeckoViewNavigation:Active",
"GeckoViewNavigation:Inactive",
"GeckoView:GoBack",
"GeckoView:GoForward",
"GeckoView:LoadUri",
@ -53,12 +47,6 @@ class GeckoViewNavigation extends GeckoViewModule {
debug("onEvent: aEvent=" + aEvent + ", aData=" + JSON.stringify(aData));
switch (aEvent) {
case "GeckoViewNavigation:Active":
this.registerProgressListener();
break;
case "GeckoViewNavigation:Inactive":
this.unregisterProgressListener();
break;
case "GeckoView:GoBack":
this.browser.goBack();
break;
@ -95,8 +83,9 @@ class GeckoViewNavigation extends GeckoViewModule {
return false;
}
registerProgressListener() {
debug("registerProgressListener");
register() {
debug("register");
let flags = Ci.nsIWebProgress.NOTIFY_LOCATION;
this.progressFilter =
Cc["@mozilla.org/appshell/component/browser-status-filter;1"]
@ -105,8 +94,9 @@ class GeckoViewNavigation extends GeckoViewModule {
this.browser.addProgressListener(this.progressFilter, flags);
}
unregisterProgressListener() {
debug("unregisterProgressListener");
unregister() {
debug("unregister");
if (!this.progressFilter) {
return;
}

Просмотреть файл

@ -15,21 +15,18 @@ XPCOMUtils.defineLazyModuleGetter(this, "EventDispatcher",
"resource://gre/modules/Messaging.jsm");
var dump = Cu.import("resource://gre/modules/AndroidLog.jsm", {})
.AndroidLog.d.bind(null, "ViewNavigation");
.AndroidLog.d.bind(null, "ViewProgress");
function debug(aMsg) {
// dump(aMsg);
}
class GeckoViewProgress extends GeckoViewModule {
init() {
this.registerProgressListener();
}
register() {
debug("register");
registerProgressListener() {
debug("registerProgressListeners()");
let flags = Ci.nsIWebProgress.NOTIFY_STATE_NETWORK | Ci.nsIWebProgress.NOTIFY_SECURITY;
let flags = Ci.nsIWebProgress.NOTIFY_STATE_NETWORK |
Ci.nsIWebProgress.NOTIFY_SECURITY;
this.progressFilter =
Cc["@mozilla.org/appshell/component/browser-status-filter;1"]
.createInstance(Ci.nsIWebProgress);
@ -37,6 +34,15 @@ class GeckoViewProgress extends GeckoViewModule {
this.browser.addProgressListener(this.progressFilter, flags);
}
unregister() {
debug("unregister");
if (this.progressFilter) {
this.progressFilter.removeProgressListener(this);
this.browser.removeProgressListener(this.progressFilter);
}
}
onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) {
debug("onStateChange()");

Просмотреть файл

@ -21,7 +21,6 @@ class GeckoViewScroll extends GeckoViewModule {
init() {
this.messageManager.loadFrameScript(
"chrome://geckoview/content/GeckoViewScrollContent.js", true);
this.register();
}
register() {