Bug 1330411 - 1. Convert ActionBarHandler observers to events; r=sebastian

Convert observers in ActionBarHandler.js to events that go through
WindowEventDispatcher. "TextSelection:Get" now replies through the
callback interface rather than a "TextSelection:Data" event. The patch
also adds new lazy-loading events in browser.js to replace the
lazy-loading observers that ActionBarHandler relied on.
This commit is contained in:
Jim Chen 2017-01-25 18:53:58 -05:00
parent 9a9702cab0
commit 59b3ffa09c
6 changed files with 52 additions and 55 deletions

View File

@ -18,9 +18,6 @@ import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MenuItem;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
@ -213,7 +210,9 @@ class ActionBarTextSelection implements TextSelection, BundleEventListener {
@Override
public boolean onActionItemClicked(ActionModeCompat mode, MenuItem item) {
final GeckoBundle obj = mItems[item.getItemId()];
GeckoAppShell.notifyObservers("TextSelection:Action", obj.getString("id"));
final GeckoBundle data = new GeckoBundle(1);
data.putString("id", obj.getString("id"));
GeckoApp.getEventDispatcher().dispatch("TextSelection:Action", data);
return true;
}
@ -222,15 +221,10 @@ class ActionBarTextSelection implements TextSelection, BundleEventListener {
public void onDestroyActionMode(ActionModeCompat mode) {
mActionMode = null;
mCallback = null;
final JSONObject args = new JSONObject();
try {
args.put("selectionID", selectionID);
} catch (JSONException e) {
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:End", e);
return;
}
GeckoAppShell.notifyObservers("TextSelection:End", args.toString());
final GeckoBundle data = new GeckoBundle(1);
data.putInt("selectionID", selectionID);
GeckoApp.getEventDispatcher().dispatch("TextSelection:End", data);
}
}
}

View File

@ -12,7 +12,8 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoApp;
import org.mozilla.gecko.util.GeckoBundle;
import java.util.List;
@ -51,7 +52,9 @@ public class FloatingActionModeCallback extends ActionMode.Callback2 {
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
final TextAction action = actions.get(item.getItemId());
GeckoAppShell.notifyObservers("TextSelection:Action", action.getId());
final GeckoBundle data = new GeckoBundle(1);
data.putString("id", action.getId());
GeckoApp.getEventDispatcher().dispatch("TextSelection:Action", data);
return true;
}

View File

@ -12,9 +12,6 @@ import android.util.Log;
import android.util.TypedValue;
import android.view.ActionMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoApp;
import org.mozilla.gecko.GeckoAppShell;
@ -73,15 +70,9 @@ public class FloatingToolbarTextSelection implements TextSelection, BundleEventL
return;
}
final JSONObject args = new JSONObject();
try {
args.put("selectionID", selectionID);
} catch (JSONException e) {
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:End", e);
return;
}
GeckoAppShell.notifyObservers("TextSelection:End", args.toString());
final GeckoBundle data = new GeckoBundle(1);
data.putInt("selectionID", selectionID);
GeckoApp.getEventDispatcher().dispatch("TextSelection:End", data);
}
@Override

View File

@ -88,8 +88,8 @@ var ActionBarHandler = {
/**
* ActionBarHandler notification observers.
*/
observe: function(subject, topic, data) {
switch (topic) {
onEvent: function(event, data, callback) {
switch (event) {
// User click an ActionBar button.
case "TextSelection:Action": {
if (!this._selectionID) {
@ -97,7 +97,7 @@ var ActionBarHandler = {
}
for (let type in this.actions) {
let action = this.actions[type];
if (action.id == data) {
if (action.id == data.id) {
action.action(this._targetElement, this._contentWindow);
break;
}
@ -107,12 +107,11 @@ var ActionBarHandler = {
// Provide selected text to FindInPageBar on request.
case "TextSelection:Get": {
Messaging.sendRequest({
type: "TextSelection:Data",
requestId: data,
text: this._getSelectedText(),
});
try {
callback.onSuccess(this._getSelectedText());
} catch (e) {
callback.onError(e.toString());
}
this._uninit();
break;
}
@ -120,7 +119,7 @@ var ActionBarHandler = {
// User closed ActionBar by clicking "checkmark" button.
case "TextSelection:End": {
// End the requested selection only.
if (this._selectionID == JSON.parse(data).selectionID) {
if (this._selectionID == data.selectionID) {
this._uninit();
}
break;

View File

@ -9,12 +9,13 @@ var Ci = Components.interfaces;
var Cu = Components.utils;
var Cr = Components.results;
Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/AsyncPrefs.jsm");
Cu.import("resource://gre/modules/DelayedInit.jsm");
Cu.import("resource://gre/modules/Messaging.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
if (AppConstants.ACCESSIBILITY) {
XPCOMUtils.defineLazyModuleGetter(this, "AccessFu",
@ -45,12 +46,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
"resource://gre/modules/Downloads.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "EventDispatcher",
"resource://gre/modules/Messaging.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Messaging",
"resource://gre/modules/Messaging.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "UserAgentOverrides",
"resource://gre/modules/UserAgentOverrides.jsm");
@ -130,6 +125,9 @@ XPCOMUtils.defineLazyServiceGetter(this, "FontEnumerator",
"@mozilla.org/gfx/fontenumerator;1",
"nsIFontEnumerator");
var GlobalEventDispatcher = EventDispatcher.instance;
var WindowEventDispatcher = EventDispatcher.for(window);
var lazilyLoadedBrowserScripts = [
["SelectHelper", "chrome://browser/content/SelectHelper.js"],
["InputWidgetHelper", "chrome://browser/content/InputWidgetHelper.js"],
@ -166,11 +164,6 @@ var lazilyLoadedObserverScripts = [
["PrintHelper", ["Print:PDF"], "chrome://browser/content/PrintHelper.js"],
];
lazilyLoadedObserverScripts.push(
["ActionBarHandler", ["TextSelection:Get", "TextSelection:Action", "TextSelection:End"],
"chrome://browser/content/ActionBarHandler.js"]
);
if (AppConstants.MOZ_WEBRTC) {
lazilyLoadedObserverScripts.push(
["WebrtcUI", ["getUserMedia:request",
@ -256,6 +249,26 @@ lazilyLoadedObserverScripts.forEach(function (aScript) {
});
});
// Lazily-loaded JS subscripts that use global/window EventDispatcher.
[
["ActionBarHandler", WindowEventDispatcher,
["TextSelection:Get", "TextSelection:Action", "TextSelection:End"],
"chrome://browser/content/ActionBarHandler.js"],
].forEach(module => {
let [name, dispatcher, events, script] = module;
XPCOMUtils.defineLazyGetter(window, name, function() {
let sandbox = {};
Services.scriptloader.loadSubScript(script, sandbox);
return sandbox[name];
});
let listener = (event, message, callback) => {
dispatcher.unregisterListener(listener, event);
dispatcher.registerListener(window[name], event);
window[name].onEvent(event, message, callback); // Explicitly notify new listener
};
dispatcher.registerListener(listener, events);
});
XPCOMUtils.defineLazyServiceGetter(this, "Haptic",
"@mozilla.org/widget/hapticfeedback;1", "nsIHapticFeedback");
@ -340,9 +353,6 @@ function InitLater(fn, object, name) {
return DelayedInit.schedule(fn, object, name, 15000 /* 15s max wait */);
}
XPCOMUtils.defineLazyGetter(this, "GlobalEventDispatcher", () => EventDispatcher.instance);
XPCOMUtils.defineLazyGetter(this, "WindowEventDispatcher", () => EventDispatcher.for(window));
var BrowserApp = {
_tabs: [],
_selectedTab: null,

View File

@ -144,8 +144,8 @@ function UIhasActionByID(expectedActionID) {
* Messages the ActionBarHandler to close the Selection UI.
*/
function closeSelectionUI() {
Services.obs.notifyObservers(null, "TextSelection:End",
JSON.stringify({selectionID: gChromeWin.ActionBarHandler._selectionID}));
gChromeWin.WindowEventDispatcher.dispatch("TextSelection:End",
{selectionID: gChromeWin.ActionBarHandler._selectionID});
}
/**