Bug 1146773 - Unify the code paths for adding an item to the reading list (location bar + reader mode). r=margaret

This commit is contained in:
Jared Wein 2015-03-30 19:58:00 -04:00
parent 4eb339f2ea
commit d2a191a121
2 changed files with 31 additions and 25 deletions

View File

@ -421,7 +421,7 @@ ReadingListImpl.prototype = {
* @return {Promise} Promise that is fullfilled with the added item.
*/
addItemFromBrowser: Task.async(function* (browser, url) {
let metadata = yield getMetadataFromBrowser(browser);
let metadata = yield this.getMetadataFromBrowser(browser);
let record = {
url: url,
title: metadata.title,
@ -436,6 +436,25 @@ ReadingListImpl.prototype = {
return (yield this.addItem(record));
}),
/**
* Get page metadata from the content document in a given <xul:browser>.
* @see PageMetadata.jsm
*
* @param {<xul:browser>} browser - Browser element for the document.
* @returns {Promise} Promise that is fulfilled with an object describing the metadata.
*/
getMetadataFromBrowser(browser) {
let mm = browser.messageManager;
return new Promise(resolve => {
function handleResult(msg) {
mm.removeMessageListener("PageMetadata:PageDataResult", handleResult);
resolve(msg.json);
}
mm.addMessageListener("PageMetadata:PageDataResult", handleResult);
mm.sendAsyncMessage("PageMetadata:GetPageData");
});
},
/**
* Adds a listener that will be notified when the list changes. Listeners
* are objects with the following optional methods:
@ -1043,25 +1062,6 @@ function clone(obj) {
return Cu.cloneInto(obj, {}, { cloneFunctions: false });
}
/**
* Get page metadata from the content document in a given <xul:browser>.
* @see PageMetadata.jsm
*
* @param {<xul:browser>} browser - Browser element for the document.
* @returns {Promise} Promise that is fulfilled with an object describing the metadata.
*/
function getMetadataFromBrowser(browser) {
let mm = browser.messageManager;
return new Promise(resolve => {
function handleResult(msg) {
mm.removeMessageListener("PageMetadata:PageDataResult", handleResult);
resolve(msg.json);
}
mm.addMessageListener("PageMetadata:PageDataResult", handleResult);
mm.sendAsyncMessage("PageMetadata:GetPageData");
});
}
Object.defineProperty(this, "ReadingList", {
get() {
if (!this._singleton) {

View File

@ -45,11 +45,17 @@ let ReaderParent = {
switch (message.name) {
case "Reader:AddToList":
let article = message.data.article;
ReadingList.addItem({
url: article.url,
title: article.title,
excerpt: article.excerpt,
status: article.status
ReadingList.getMetadataFromBrowser(message.target).then(function(metadata) {
if (metadata.previews.length > 0) {
article.preview = metadata.previews[0];
}
ReadingList.addItem({
url: article.url,
title: article.title,
excerpt: article.excerpt,
preview: article.preview
});
});
break;