Bug 1146773 - Unify the code paths for adding an item to the reading list (location bar + reader mode). r=margaret, a=sledru
authorJared Wein <jwein@mozilla.com>
Mon, 30 Mar 2015 19:58:00 -0400
changeset 258285 f7d65dc9093b
parent 258284 6238a894c78f
child 258286 38cb0f4d54e8
push id4635
push userryanvm@gmail.com
push date2015-04-06 16:15 +0000
treeherdermozilla-beta@f7d65dc9093b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret, sledru
bugs1146773
milestone38.0
Bug 1146773 - Unify the code paths for adding an item to the reading list (location bar + reader mode). r=margaret, a=sledru
browser/components/readinglist/ReadingList.jsm
browser/modules/ReaderParent.jsm
--- a/browser/components/readinglist/ReadingList.jsm
+++ b/browser/components/readinglist/ReadingList.jsm
@@ -457,32 +457,51 @@ ReadingListImpl.prototype = {
    * Add to the ReadingList the page that is loaded in a given browser.
    *
    * @param {<xul:browser>} browser - Browser element for the document,
    * used to get metadata about the article.
    * @param {nsIURI/string} url - url to add to the reading list.
    * @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,
       resolvedURL: metadata.url,
       excerpt: metadata.description,
     };
 
     if (metadata.previews.length > 0) {
       record.preview = metadata.previews[0];
     }
 
     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:
    *
    *   onItemAdded(item)
    *   onItemUpdated(item)
    *   onItemDeleted(item)
    *
    * @param listener A listener object.
@@ -1085,35 +1104,16 @@ function hash(str) {
     join("");
   return hexStr;
 }
 
 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) {
       let store = new SQLiteStore("reading-list.sqlite");
       this._singleton = new ReadingListImpl(store);
     }
     return this._singleton;
   },
--- a/browser/modules/ReaderParent.jsm
+++ b/browser/modules/ReaderParent.jsm
@@ -40,21 +40,27 @@ let ReaderParent = {
       mm.addMessageListener(msg, this);
     }
   },
 
   receiveMessage: function(message) {
     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;
 
       case "Reader:ArticleGet":
         this._getArticle(message.data.url, message.target).then((article) => {
           // Make sure the target browser is still alive before trying to send data back.
           if (message.target.messageManager) {
             message.target.messageManager.sendAsyncMessage("Reader:ArticleData", { article: article });