Bug 1117226 - Use sendRequestForResult for reader mode favicon and add-to-list requests. r=rnewman

--HG--
extra : rebase_source : b7415f523fb9cdcc50f7111d839885d9422e734a
This commit is contained in:
Margaret Leibovic 2015-01-27 15:47:26 +01:00
Родитель 86a215aeba
Коммит 5dac230289
3 изменённых файлов: 51 добавлений и 70 удалений

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

@ -10,7 +10,6 @@ import org.mozilla.gecko.db.BrowserContract.ReadingListItems;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.favicons.Favicons;
import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.GeckoEventListener;
import org.mozilla.gecko.util.NativeEventListener;
import org.mozilla.gecko.util.NativeJSObject;
import org.mozilla.gecko.util.ThreadUtils;
@ -22,7 +21,7 @@ import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public final class ReadingListHelper implements GeckoEventListener, NativeEventListener {
public final class ReadingListHelper implements NativeEventListener {
private static final String LOGTAG = "ReadingListHelper";
protected final Context context;
@ -30,43 +29,31 @@ public final class ReadingListHelper implements GeckoEventListener, NativeEventL
public ReadingListHelper(Context context) {
this.context = context;
EventDispatcher.getInstance().registerGeckoThreadListener((GeckoEventListener) this,
"Reader:AddToList", "Reader:FaviconRequest");
EventDispatcher.getInstance().registerGeckoThreadListener((NativeEventListener) this,
"Reader:ListStatusRequest", "Reader:RemoveFromList");
"Reader:AddToList", "Reader:FaviconRequest", "Reader:ListStatusRequest", "Reader:RemoveFromList");
}
public void uninit() {
EventDispatcher.getInstance().unregisterGeckoThreadListener((GeckoEventListener) this,
"Reader:AddToList", "Reader:FaviconRequest");
EventDispatcher.getInstance().unregisterGeckoThreadListener((NativeEventListener) this,
"Reader:ListStatusRequest", "Reader:RemoveFromList");
}
@Override
public void handleMessage(String event, JSONObject message) {
switch(event) {
case "Reader:AddToList": {
handleAddToList(message);
break;
}
case "Reader:FaviconRequest": {
handleReaderModeFaviconRequest(message.optString("url"));
break;
}
}
"Reader:AddToList", "Reader:FaviconRequest", "Reader:ListStatusRequest", "Reader:RemoveFromList");
}
@Override
public void handleMessage(final String event, final NativeJSObject message,
final EventCallback callback) {
switch(event) {
case "Reader:AddToList": {
handleAddToList(callback, message);
break;
}
case "Reader:FaviconRequest": {
handleReaderModeFaviconRequest(callback, message.getString("url"));
break;
}
case "Reader:RemoveFromList": {
handleRemoveFromList(message.getString("url"));
break;
}
case "Reader:ListStatusRequest": {
handleReadingListStatusRequest(callback, message.getString("url"));
break;
@ -77,10 +64,21 @@ public final class ReadingListHelper implements GeckoEventListener, NativeEventL
/**
* A page can be added to the ReadingList by long-tap of the page-action
* icon, or by tapping the readinglist-add icon in the ReaderMode banner.
*
* This method will only add new items, not update existing items.
*/
private void handleAddToList(final JSONObject message) {
private void handleAddToList(final EventCallback callback, final NativeJSObject message) {
final ContentResolver cr = context.getContentResolver();
final String url = message.optString("url");
final String url = message.getString("url");
// We can't access a NativeJSObject from the background thread, so we need to get the
// values here, even if we may not use them to insert an item into the DB.
final ContentValues values = new ContentValues();
values.put(ReadingListItems.URL, url);
values.put(ReadingListItems.TITLE, message.getString("title"));
values.put(ReadingListItems.LENGTH, message.getInt("length"));
values.put(ReadingListItems.EXCERPT, message.getString("excerpt"));
values.put(ReadingListItems.CONTENT_STATUS, message.getInt("status"));
final BrowserDB db = GeckoProfile.get(context).getDB();
ThreadUtils.postToBackgroundThread(new Runnable() {
@ -88,29 +86,21 @@ public final class ReadingListHelper implements GeckoEventListener, NativeEventL
public void run() {
if (db.isReadingListItem(cr, url)) {
showToast(R.string.reading_list_duplicate, Toast.LENGTH_SHORT);
callback.sendError("URL already in reading list: " + url);
} else {
final ContentValues values = new ContentValues();
values.put(ReadingListItems.URL, url);
values.put(ReadingListItems.TITLE, message.optString("title"));
values.put(ReadingListItems.LENGTH, message.optInt("length"));
values.put(ReadingListItems.EXCERPT, message.optString("excerpt"));
values.put(ReadingListItems.CONTENT_STATUS, message.optInt("status"));
db.addReadingListItem(cr, values);
showToast(R.string.reading_list_added, Toast.LENGTH_SHORT);
callback.sendSuccess(url);
}
}
});
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Reader:Added", url));
}
/**
* Gecko (ReaderMode) requests the page favicon to append to the
* document head for display.
*/
private void handleReaderModeFaviconRequest(final String url) {
private void handleReaderModeFaviconRequest(final EventCallback callback, final String url) {
final BrowserDB db = GeckoProfile.get(context).getDB();
(new UIAsyncTask.WithoutParams<String>(ThreadUtils.getBackgroundHandler()) {
@Override
@ -121,7 +111,6 @@ public final class ReadingListHelper implements GeckoEventListener, NativeEventL
@Override
public void onPostExecute(String faviconUrl) {
JSONObject args = new JSONObject();
if (faviconUrl != null) {
try {
args.put("url", url);
@ -130,9 +119,7 @@ public final class ReadingListHelper implements GeckoEventListener, NativeEventL
Log.w(LOGTAG, "Error building JSON favicon arguments.", e);
}
}
GeckoAppShell.sendEventToGecko(
GeckoEvent.createBroadcastEvent("Reader:FaviconReturn", args.toString()));
callback.sendSuccess(args.toString());
}
}).execute();
}

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

@ -17,11 +17,6 @@ let Reader = {
observe: function Reader_observe(aMessage, aTopic, aData) {
switch (aTopic) {
case "Reader:Added": {
let mm = window.getGroupMessageManager("browsers");
mm.broadcastAsyncMessage("Reader:Added", { url: aData });
break;
}
case "Reader:Removed": {
let uri = Services.io.newURI(aData, null, null);
ReaderMode.removeArticleFromCache(uri).catch(e => Cu.reportError("Error removing article from cache: " + e));
@ -56,10 +51,13 @@ let Reader = {
receiveMessage: function(message) {
switch (message.name) {
case "Reader:AddToList":
this.addArticleToReadingList(message.data.article);
case "Reader:AddToList": {
// If the article is coming from reader mode, we must have fetched it already.
let article = message.data.article;
article.status = this.STATUS_FETCHED_ARTICLE;
this._addArticleToReadingList(article);
break;
}
case "Reader:ArticleGet":
this._getArticle(message.data.url, message.target).then((article) => {
message.target.messageManager.sendAsyncMessage("Reader:ArticleData", { article: article });
@ -67,14 +65,11 @@ let Reader = {
break;
case "Reader:FaviconRequest": {
let observer = (s, t, d) => {
Services.obs.removeObserver(observer, "Reader:FaviconReturn", false);
message.target.messageManager.sendAsyncMessage("Reader:FaviconReturn", JSON.parse(d));
};
Services.obs.addObserver(observer, "Reader:FaviconReturn", false);
Messaging.sendRequest({
Messaging.sendRequestForResult({
type: "Reader:FaviconRequest",
url: message.data.url
}).then(data => {
message.target.messageManager.sendAsyncMessage("Reader:FaviconReturn", JSON.parse(data));
});
break;
}
@ -200,31 +195,30 @@ let Reader = {
article = {
url: urlWithoutRef,
title: tab.browser.contentDocument.title,
length: 0,
excerpt: "",
status: this.STATUS_FETCH_FAILED_UNSUPPORTED_FORMAT,
};
} else {
article.status = this.STATUS_FETCHED_ARTICLE;
}
this.addArticleToReadingList(article);
this._addArticleToReadingList(article);
}),
addArticleToReadingList: function(article) {
if (!article || !article.url) {
Cu.reportError("addArticleToReadingList requires article with valid URL");
return;
}
Messaging.sendRequest({
_addArticleToReadingList: function(article) {
Messaging.sendRequestForResult({
type: "Reader:AddToList",
url: truncate(article.url, MAX_URI_LENGTH),
title: truncate(article.title || "", MAX_TITLE_LENGTH),
length: article.length || 0,
excerpt: article.excerpt || "",
title: truncate(article.title, MAX_TITLE_LENGTH),
length: article.length,
excerpt: article.excerpt,
status: article.status,
});
ReaderMode.storeArticleInCache(article).catch(e => Cu.reportError("Error storing article in cache: " + e));
}).then((url) => {
let mm = window.getGroupMessageManager("browsers");
mm.broadcastAsyncMessage("Reader:Added", { url: url });
ReaderMode.storeArticleInCache(article).catch(e => Cu.reportError("Error storing article in cache: " + e));
}).catch(Cu.reportError);
},
/**

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

@ -146,7 +146,7 @@ let lazilyLoadedObserverScripts = [
["Feedback", ["Feedback:Show"], "chrome://browser/content/Feedback.js"],
["SelectionHandler", ["TextSelection:Get"], "chrome://browser/content/SelectionHandler.js"],
["EmbedRT", ["GeckoView:ImportScript"], "chrome://browser/content/EmbedRT.js"],
["Reader", ["Reader:Added", "Reader:Removed", "Gesture:DoubleTap"], "chrome://browser/content/Reader.js"],
["Reader", ["Reader:Removed", "Gesture:DoubleTap"], "chrome://browser/content/Reader.js"],
];
if (AppConstants.MOZ_WEBRTC) {
lazilyLoadedObserverScripts.push(