Bug 1330439 - 4. Convert home panel observers to events; r=sebastian

Convert nsIObserverService observers used in home panels to events that
go through EventDispatcher.
This commit is contained in:
Jim Chen 2017-01-25 18:53:58 -05:00
Родитель dfcdeb5ce1
Коммит 296c0b8b43
7 изменённых файлов: 63 добавлений и 83 удалений

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

@ -154,7 +154,7 @@ public class HomeBanner extends LinearLayout
* Sends a message to gecko to request a new banner message. UI is updated in handleMessage.
*/
public void update() {
GeckoAppShell.notifyObservers("HomeBanner:Get", null);
EventDispatcher.getInstance().dispatch("HomeBanner:Get", null);
}
@Override // BundleEventListener

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

@ -18,8 +18,9 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.annotation.RobocopTarget;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.R;
import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.ThreadUtils;
import android.content.Context;
@ -1151,9 +1152,9 @@ public final class HomeConfig {
private final List<String> mConfigOrder;
private final Thread mOriginalThread;
// Each Pair represents parameters to a GeckoAppShell.notifyObservers call;
// the first String is the observer topic and the second string is the notification data.
private List<Pair<String, String>> mNotificationQueue;
// Each Pair represents parameters to a EventDispatcher.dispatch call;
// the String is the event name and the GeckoBundle is the event data.
private List<Pair<String, GeckoBundle>> mNotificationQueue;
private PanelConfig mDefaultPanel;
private int mEnabledCount;
@ -1377,8 +1378,10 @@ public final class HomeConfig {
installed = true;
// Add an event to the queue if a new panel is successfully installed.
mNotificationQueue.add(new Pair<String, String>(
"HomePanels:Installed", panelConfig.getId()));
final GeckoBundle data = new GeckoBundle(1);
data.putString("id", panelConfig.getId());
mNotificationQueue.add(new Pair<String, GeckoBundle>(
"HomePanels:Installed", data));
}
mHasChanged = true;
@ -1414,7 +1417,10 @@ public final class HomeConfig {
}
// Add an event to the queue if a panel is successfully uninstalled.
mNotificationQueue.add(new Pair<String, String>("HomePanels:Uninstalled", panelId));
final GeckoBundle data = new GeckoBundle(1);
data.putString("id", panelId);
mNotificationQueue.add(new Pair<String, GeckoBundle>(
"HomePanels:Uninstalled", data));
mHasChanged = true;
return true;
@ -1488,7 +1494,7 @@ public final class HomeConfig {
// Copy the event queue to a new list, so that we only modify mNotificationQueue on
// the original thread where it was created.
final List<Pair<String, String>> copiedQueue = mNotificationQueue;
final List<Pair<String, GeckoBundle>> copiedQueue = mNotificationQueue;
mNotificationQueue = new ArrayList<>();
ThreadUtils.getBackgroundHandler().post(new Runnable() {
@ -1541,9 +1547,10 @@ public final class HomeConfig {
return mConfigMap.isEmpty();
}
private void sendNotificationsToGecko(List<Pair<String, String>> notifications) {
for (Pair<String, String> p : notifications) {
GeckoAppShell.notifyObservers(p.first, p.second);
private void sendNotificationsToGecko(List<Pair<String, GeckoBundle>> notifications) {
final EventDispatcher dispatcher = EventDispatcher.getInstance();
for (final Pair<String, GeckoBundle> p : notifications) {
dispatcher.dispatch(p.first, p.second);
}
}

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

@ -5,10 +5,11 @@
package org.mozilla.gecko.home;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.R;
import org.mozilla.gecko.home.HomeConfig.AuthConfig;
import org.mozilla.gecko.home.HomeConfig.PanelConfig;
import org.mozilla.gecko.util.GeckoBundle;
import android.content.Context;
import android.text.TextUtils;
@ -44,7 +45,9 @@ class PanelAuthLayout extends LinearLayout {
buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GeckoAppShell.notifyObservers("HomePanels:Authenticate", panelId);
final GeckoBundle data = new GeckoBundle(1);
data.putString("id", panelId);
EventDispatcher.getInstance().dispatch("HomePanels:Authenticate", data);
}
});

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

@ -10,9 +10,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.home.HomeConfig.PanelConfig;
@ -85,24 +83,14 @@ public class PanelInfoManager implements BundleEventListener {
sCallbacks.put(requestId, callback);
}
final JSONObject message = new JSONObject();
try {
message.put("requestId", requestId);
final GeckoBundle message = new GeckoBundle(2);
message.putInt("requestId", requestId);
if (ids != null && ids.size() > 0) {
JSONArray idsArray = new JSONArray();
for (String id : ids) {
idsArray.put(id);
}
message.put("ids", idsArray);
}
} catch (JSONException e) {
Log.e(LOGTAG, "Failed to build event to request panels by id", e);
return;
if (ids != null && ids.size() > 0) {
message.putStringArray("ids", ids.toArray(new String[ids.size()]));
}
GeckoAppShell.notifyObservers("HomePanels:Get", message.toString());
EventDispatcher.getInstance().dispatch("HomePanels:Get", message);
}
/**

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

@ -7,12 +7,10 @@ package org.mozilla.gecko.home;
import org.mozilla.gecko.R;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.home.PanelLayout.DatasetBacked;
import org.mozilla.gecko.home.PanelLayout.FilterManager;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.util.GeckoBundle;
import android.content.Context;
import android.database.Cursor;
@ -30,8 +28,8 @@ import android.view.View;
class PanelRefreshLayout extends SwipeRefreshLayout implements DatasetBacked {
private static final String LOGTAG = "GeckoPanelRefreshLayout";
private static final String JSON_KEY_PANEL_ID = "panelId";
private static final String JSON_KEY_VIEW_INDEX = "viewIndex";
private static final String BUNDLE_KEY_PANEL_ID = "panelId";
private static final String BUNDLE_KEY_VIEW_INDEX = "viewIndex";
private final String panelId;
private final int viewIndex;
@ -75,16 +73,10 @@ class PanelRefreshLayout extends SwipeRefreshLayout implements DatasetBacked {
private class RefreshListener implements OnRefreshListener {
@Override
public void onRefresh() {
final JSONObject response = new JSONObject();
try {
response.put(JSON_KEY_PANEL_ID, panelId);
response.put(JSON_KEY_VIEW_INDEX, viewIndex);
} catch (JSONException e) {
Log.e(LOGTAG, "Could not create refresh message", e);
return;
}
GeckoAppShell.notifyObservers("HomePanels:RefreshView", response.toString());
final GeckoBundle response = new GeckoBundle(2);
response.putString(BUNDLE_KEY_PANEL_ID, panelId);
response.putInt(BUNDLE_KEY_VIEW_INDEX, viewIndex);
EventDispatcher.getInstance().dispatch("HomePanels:RefreshView", response);
}
}
}

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

@ -231,24 +231,7 @@ lazilyLoadedObserverScripts.forEach(function (aScript) {
});
});
// Lazily-loaded JS modules that use observer notifications
[
["Home", ["HomeBanner:Get", "HomePanels:Get", "HomePanels:Authenticate", "HomePanels:RefreshView",
"HomePanels:Installed", "HomePanels:Uninstalled"], "resource://gre/modules/Home.jsm"],
].forEach(module => {
let [name, notifications, resource] = module;
XPCOMUtils.defineLazyModuleGetter(this, name, resource);
let observer = (s, t, d) => {
Services.obs.removeObserver(observer, t);
Services.obs.addObserver(this[name], t, false);
this[name].observe(s, t, d); // Explicitly notify new observer
};
notifications.forEach(notification => {
Services.obs.addObserver(observer, notification, false);
});
});
// Lazily-loaded JS subscripts that use global/window EventDispatcher.
// Lazily-loaded JS subscripts and modules that use global/window EventDispatcher.
[
["ActionBarHandler", WindowEventDispatcher,
["TextSelection:Get", "TextSelection:Action", "TextSelection:End"],
@ -256,11 +239,19 @@ lazilyLoadedObserverScripts.forEach(function (aScript) {
["FindHelper", GlobalEventDispatcher,
["FindInPage:Opened", "FindInPage:Closed"],
"chrome://browser/content/FindHelper.js"],
["Home", GlobalEventDispatcher,
["HomeBanner:Get", "HomePanels:Get", "HomePanels:Authenticate",
"HomePanels:RefreshView", "HomePanels:Installed", "HomePanels:Uninstalled"],
"resource://gre/modules/Home.jsm"],
].forEach(module => {
let [name, dispatcher, events, script] = module;
XPCOMUtils.defineLazyGetter(window, name, function() {
let sandbox = {};
Services.scriptloader.loadSubScript(script, sandbox);
if (script.endsWith(".jsm")) {
Cu.import(script, sandbox);
} else {
Services.scriptloader.loadSubScript(script, sandbox);
}
return sandbox[name];
});
let listener = (event, message, callback) => {

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

@ -124,14 +124,14 @@ var HomeBanner = (function () {
};
return Object.freeze({
onEvent: function(event, message, callback) {
switch(topic) {
onEvent: function(event, data, callback) {
switch(event) {
case "HomeBanner:Click":
_handleClick(message.id);
_handleClick(data.id);
break;
case "HomeBanner:Dismiss":
_handleDismiss(message.id);
_handleDismiss(data.id);
break;
}
},
@ -195,8 +195,6 @@ var HomePanels = (function () {
HomePanelsMessageHandlers = {
"HomePanels:Get": function handlePanelsGet(data) {
data = JSON.parse(data);
let requestId = data.requestId;
let ids = data.ids || null;
@ -219,8 +217,9 @@ var HomePanels = (function () {
});
},
"HomePanels:Authenticate": function handlePanelsAuthenticate(id) {
"HomePanels:Authenticate": function handlePanelsAuthenticate(data) {
// Generate panel options to get auth handler.
let id = data.id;
let options = _registeredPanels[id]();
if (!options.auth) {
throw "Home.panels: Invalid auth for panel.id = " + id;
@ -232,8 +231,6 @@ var HomePanels = (function () {
},
"HomePanels:RefreshView": function handlePanelsRefreshView(data) {
data = JSON.parse(data);
let options = _registeredPanels[data.panelId]();
let view = options.views[data.viewIndex];
@ -250,7 +247,8 @@ var HomePanels = (function () {
view.onrefresh();
},
"HomePanels:Installed": function handlePanelsInstalled(id) {
"HomePanels:Installed": function handlePanelsInstalled(data) {
let id = data.id;
_assertPanelExists(id);
let options = _registeredPanels[id]();
@ -263,7 +261,8 @@ var HomePanels = (function () {
options.oninstall();
},
"HomePanels:Uninstalled": function handlePanelsUninstalled(id) {
"HomePanels:Uninstalled": function handlePanelsUninstalled(data) {
let id = data.id;
_assertPanelExists(id);
let options = _registeredPanels[id]();
@ -473,13 +472,13 @@ this.Home = Object.freeze({
panels: HomePanels,
// Lazy notification observer registered in browser.js
observe: function(subject, topic, data) {
if (topic in HomeBannerMessageHandlers) {
HomeBannerMessageHandlers[topic](data);
} else if (topic in HomePanelsMessageHandlers) {
HomePanelsMessageHandlers[topic](data);
onEvent: function (event, data, callback) {
if (event in HomeBannerMessageHandlers) {
HomeBannerMessageHandlers[event](data);
} else if (event in HomePanelsMessageHandlers) {
HomePanelsMessageHandlers[event](data);
} else {
Cu.reportError("Home.observe: message handler not found for topic: " + topic);
Cu.reportError("Home.observe: message handler not found for event: " + event);
}
}
});