Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
PlacesProvider messages in AboutRemoteNewTab.jsm
Browse files Browse the repository at this point in the history
  • Loading branch information
oyiptong committed Nov 9, 2015
1 parent 2620537 commit 52de8f5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
47 changes: 44 additions & 3 deletions browser/components/newtab/RemoteAboutNewTab.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* 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, SearchProvider, RemoteNewTabLocation, RemoteNewTabUtils, Task */
/* globals BackgroundPageThumbs, PageThumbs, DirectoryLinksProvider */
/* globals BackgroundPageThumbs, PageThumbs, DirectoryLinksProvider, PlacesProvider */

/* exported RemoteAboutNewTab */

Expand Down Expand Up @@ -33,6 +33,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "RemoteNewTabLocation",
"resource:///modules/RemoteNewTabLocation.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "SearchProvider",
"resource:///modules/SearchProvider.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesProvider",
"resource:///modules/PlacesProvider.jsm");

let RemoteAboutNewTab = {

Expand Down Expand Up @@ -109,6 +111,34 @@ let RemoteAboutNewTab = {
Services.search.currentEngine = Services.search.getEngineByName(message.data.engineName);
},

/**
* Notifies when history is cleared
*/
placesClearHistory: function() {
this.pageListener.sendAsyncMessage("NewTab:PlacesClearHistory");
},

/**
* Notifies when a link has changed
*/
placesLinkChanged: function(link) {
this.pageListener.sendAsyncMessage("NewTab:PlacesLinkChanged", link);
},

/**
* Notifies when many links have changed
*/
placesManyLinksChanged: function() {
this.pageListener.sendAsyncMessage("NewTab:PlacesManyLinksChanged");
},

/**
* Notifies when one URL has been deleted
*/
placesDeleteURI: function(data) {
this.pageListener.sendAsyncMessage("NewTab:PlacesDeleteURI", data);
},

/**
* Initializes the grid for the first time when the page loads.
* Fetch all the links and send them down to the child to populate
Expand All @@ -117,14 +147,17 @@ let RemoteAboutNewTab = {
* @param {Object} message
* A RemotePageManager message.
*/
initializeGrid: function(message) {
initializeGrid: Task.async(function*(message) {
let placesLinks = yield PlacesProvider.links.getLinks();

RemoteNewTabUtils.links.populateCache(() => {
message.target.sendAsyncMessage("NewTab:InitializeLinks", {
links: RemoteNewTabUtils.links.getLinks(),
enhancedLinks: this.getEnhancedLinks(),
placesLinks
});
});
},
}),

/**
* Inits the content iframe with the newtab location
Expand Down Expand Up @@ -297,6 +330,10 @@ let RemoteAboutNewTab = {
Services.obs.addObserver(this, "browser:purge-session-history", true);
Services.prefs.addObserver("browser.search.hiddenOneOffs", this, false);
Services.obs.addObserver(this, "browser-search-engine-modified", 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));
},

/**
Expand All @@ -307,6 +344,10 @@ let RemoteAboutNewTab = {
Services.obs.removeObserver(this, "browser:purge-session-history");
Services.prefs.removeObserver("browser.search.hiddenOneOffs", this);
Services.obs.removeObserver(this, "browser-search-engine-modified");
PlacesProvider.links.off("deleteURI", this.placesDeleteURI);
PlacesProvider.links.off("clearHistory", this.placesClearHistory);
PlacesProvider.links.off("linkChanged", this.placesLinkChanged);
PlacesProvider.links.off("manyLinksChanged", this.placesManyLinksChanged);
},

QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
Expand Down
43 changes: 43 additions & 0 deletions browser/components/newtab/tests/xpcshell/test_RemoteAboutNewTab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

/* global XPCOMUtils, RemoteAboutNewTab, PlacesProvider */
/* global do_get_profile, run_next_test, add_task */
/* global equal, ok */
/* exported run_test */

const {
utils: Cu,
interfaces: Ci,
} = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "PlacesProvider",
"resource:///modules/PlacesProvider.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "RemoteAboutNewTab",
"resource:///modules/RemoteAboutNewTab.jsm");

// ensure a profile exists
do_get_profile();

function run_test() {
run_next_test();
}

add_task(function* test_PlacesEventListener() {
RemoteAboutNewTab.init();
let oldPageListener = RemoteAboutNewTab.pageListener;
RemoteAboutNewTab.pageListener = {};
let promise = new Promise(resolve => {
RemoteAboutNewTab.pageListener.sendAsyncMessage = function(name, data) {
if (name == "NewTab:PlacesClearHistory") {
resolve();
}
};
PlacesProvider.links.emit("clearHistory");
});
yield promise;
RemoteAboutNewTab.pageListener = oldPageListener;
RemoteAboutNewTab.uninit();
});
1 change: 1 addition & 0 deletions browser/components/newtab/tests/xpcshell/xpcshell.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ skip-if = toolkit == 'android' || toolkit == 'gonk'
[test_NewTabPrefsProvider.js]
[test_NewTabURL.js]
[test_PlacesProvider.js]
[test_RemoteAboutNewTab.js]
[test_RemoteNewTabLocation.js]
[test_RemoteNewTabUtils.js]

0 comments on commit 52de8f5

Please sign in to comment.