Bug 1196762 - Part 2: PrefsProvider and PlacesProvider messaging integration in RemoteAboutNewTab r=Mardak draft
authorUrsula Sarracini <ursulasarracini@gmail.com>
Tue, 10 Nov 2015 15:15:53 -0500
changeset 308622 cbb1af56e7a6edc1d775a79e01462dd1db9b4aed
parent 308621 df54d572ab0f8e2061563cfac8c22564bb913f49
child 308623 9c65e91ae2cacdbbe2fcf1374bbb5fb9d589f756
push id7503
push userolivier@olivieryiptong.com
push dateFri, 13 Nov 2015 10:40:54 +0000
reviewersMardak
bugs1196762
milestone45.0a1
Bug 1196762 - Part 2: PrefsProvider and PlacesProvider messaging integration in RemoteAboutNewTab r=Mardak
browser/components/newtab/RemoteAboutNewTab.jsm
--- a/browser/components/newtab/RemoteAboutNewTab.jsm
+++ b/browser/components/newtab/RemoteAboutNewTab.jsm
@@ -1,14 +1,13 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
 /* globals Services, XPCOMUtils, RemotePages, RemoteNewTabLocation, RemoteNewTabUtils, Task  */
-/* globals BackgroundPageThumbs, PageThumbs, DirectoryLinksProvider */
+/* globals BackgroundPageThumbs, PageThumbs, DirectoryLinksProvider, PlacesProvider, NewTabPrefsProvider */
 /* exported RemoteAboutNewTab */
 
 "use strict";
 
 let Ci = Components.interfaces;
 let Cu = Components.utils;
 const XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
 
@@ -26,36 +25,78 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "BackgroundPageThumbs",
   "resource://gre/modules/BackgroundPageThumbs.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "PageThumbs",
   "resource://gre/modules/PageThumbs.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "DirectoryLinksProvider",
   "resource:///modules/DirectoryLinksProvider.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "RemoteNewTabLocation",
   "resource:///modules/RemoteNewTabLocation.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesProvider",
+  "resource:///modules/PlacesProvider.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "NewTabPrefsProvider",
+  "resource:///modules/NewTabPrefsProvider.jsm");
 
 let RemoteAboutNewTab = {
 
   pageListener: null,
 
   /**
    * Initialize the RemotePageManager and add all message listeners for this page
    */
   init: function() {
     this.pageListener = new RemotePages("about:remote-newtab");
     this.pageListener.addMessageListener("NewTab:InitializeGrid", this.initializeGrid.bind(this));
     this.pageListener.addMessageListener("NewTab:UpdateGrid", this.updateGrid.bind(this));
+    this.pageListener.addMessageListener("NewTab:Customize", this.customize.bind(this));
     this.pageListener.addMessageListener("NewTab:CaptureBackgroundPageThumbs",
         this.captureBackgroundPageThumb.bind(this));
     this.pageListener.addMessageListener("NewTab:PageThumbs", this.createPageThumb.bind(this));
     this.pageListener.addMessageListener("NewTabFrame:GetInit", this.initContentFrame.bind(this));
 
     this._addObservers();
   },
 
+  customize: function(message) {
+    if (message.data.enabled !== undefined) {
+      Services.prefs.setBoolPref("browser.newtabpage.enabled", message.data.enabled);
+    }
+    if (message.data.enhanced !== undefined) {
+      Services.prefs.setBoolPref("browser.newtabpage.enhanced", message.data.enhanced);
+    }
+  },
+
+  /**
+   * Notifies when history is cleared
+   */
+  placesClearHistory: function() {
+    this.pageListener.sendAsyncMessage("NewTab:PlacesClearHistory");
+  },
+
+  /**
+   * Notifies when a link has changed
+   */
+  placesLinkChanged: function(name, data) { // jshint ignore:line
+    this.pageListener.sendAsyncMessage("NewTab:PlacesLinkChanged", data);
+  },
+
+  /**
+   * Notifies when many links have changed
+   */
+  placesManyLinksChanged: function() {
+    this.pageListener.sendAsyncMessage("NewTab:PlacesManyLinksChanged");
+  },
+
+  /**
+   * Notifies when one URL has been deleted
+   */
+  placesDeleteURI: function(name, data) { // jshint ignore:line
+    this.pageListener.sendAsyncMessage("NewTab:PlacesDeleteURI", data.url);
+  },
+
   /**
    * Initializes the grid for the first time when the page loads.
    * Fetch all the links and send them down to the child to populate
    * the grid with.
    *
    * @param {Object} message
    *        A RemotePageManager message.
    */
@@ -181,17 +222,16 @@ let RemoteAboutNewTab = {
   /**
    * Listens for a preference change or session purge for all pages and sends
    * a message to update the pages that are open. If a session purge occured,
    * also clear the links cache and update the set of links to display, as they
    * may have changed, then proceed with the page update.
    */
   observe: function(aSubject, aTopic, aData) { // jshint ignore:line
     let extraData;
-    let refreshPage = false;
     if (aTopic === "browser:purge-session-history") {
       RemoteNewTabUtils.links.resetCache();
       RemoteNewTabUtils.links.populateCache(() => {
         this.pageListener.sendAsyncMessage("NewTab:UpdateLinks", {
           links: RemoteNewTabUtils.links.getLinks(),
           enhancedLinks: this.getEnhancedLinks(),
         });
       });
@@ -201,30 +241,56 @@ let RemoteAboutNewTab = {
       if (aTopic !== "page-thumbnail:create") {
         // Change the topic for enhanced and enabled observers.
         aTopic = aData;
       }
       this.pageListener.sendAsyncMessage("NewTab:Observe", {topic: aTopic, data: extraData});
     }
   },
 
+  setEnabled: function(name, data) { // jshint ignore:line
+    this.pageListener.sendAsyncMessage("NewTab:setEnabled", data);
+  },
+
+  setEnhanced: function(name, data) { // jshint ignore:line
+    this.pageListener.sendAsyncMessage("NewTab:setEnhanced", data);
+  },
+
+  setPinned: function(name, data) { // jshint ignore:line
+    this.pageListener.sendAsyncMessage("NewTab:setPinnedLinks", data);
+  },
+
   /**
    * Add all observers that about:newtab page must listen for.
    */
   _addObservers: function() {
     Services.obs.addObserver(this, "page-thumbnail:create", true);
     Services.obs.addObserver(this, "browser:purge-session-history", true);
+    PlacesProvider.links.on("deleteURI", this.placesDeleteURI.bind(this));
+    PlacesProvider.links.on("clearHistory", this.placesClearHistory.bind(this));
+    PlacesProvider.links.on("linkChanged", this.placesLinkChanged.bind(this));
+    PlacesProvider.links.on("manyLinksChanged", this.placesManyLinksChanged.bind(this));
+    NewTabPrefsProvider.prefs.on("browser.newtabpage.enabled", this.setEnabled.bind(this));
+    NewTabPrefsProvider.prefs.on("browser.newtabpage.enhanced", this.setEnhanced.bind(this));
+    NewTabPrefsProvider.prefs.on("browser.newtabpage.pinned", this.setPinned.bind(this));
   },
 
   /**
    * Remove all observers on the page.
    */
   _removeObservers: function() {
     Services.obs.removeObserver(this, "page-thumbnail:create");
     Services.obs.removeObserver(this, "browser:purge-session-history");
+    PlacesProvider.links.off("deleteURI", this.placesDeleteURI);
+    PlacesProvider.links.off("clearHistory", this.placesClearHistory);
+    PlacesProvider.links.off("linkChanged", this.placesLinkChanged);
+    PlacesProvider.links.off("manyLinksChanged", this.placesManyLinksChanged);
+    NewTabPrefsProvider.prefs.off("browser.newtabpage.enabled", this.setEnabled.bind(this));
+    NewTabPrefsProvider.prefs.off("browser.newtabpage.enhanced", this.setEnhanced.bind(this));
+    NewTabPrefsProvider.prefs.off("browser.newtabpage.pinned", this.setPinned.bind(this));
   },
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference]),
 
   uninit: function() {
     this._removeObservers();
     this.pageListener.destroy();