Bug 1059591 - Incorrectly formatted remotely hosted links causes new tab to be empty [r=adw a=sylvestre]
authorEd Lee <edilee@mozilla.com>
Tue, 23 Sep 2014 15:12:20 -0700
changeset 216866 d34488e06177
parent 216865 0217719f20c5
child 216867 8dd30191477e
push id3946
push useredilee@gmail.com
push date2014-09-28 14:45 +0000
treeherdermozilla-beta@2a8947c986ed [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw, sylvestre
bugs1059591
milestone33.0
Bug 1059591 - Incorrectly formatted remotely hosted links causes new tab to be empty [r=adw a=sylvestre] Use catch handler of promises to default to empty array on exceptions.
toolkit/modules/DirectoryLinksProvider.jsm
toolkit/modules/tests/xpcshell/test_DirectoryLinksProvider.js
--- a/toolkit/modules/DirectoryLinksProvider.jsm
+++ b/toolkit/modules/DirectoryLinksProvider.jsm
@@ -376,27 +376,30 @@ let DirectoryLinksProvider = {
    * @param aCallback The function that the array of links is passed to.
    */
   getLinks: function DirectoryLinksProvider_getLinks(aCallback) {
     this._readDirectoryLinksFile().then(rawLinks => {
       // Reset the cache of enhanced images for this new set of links
       this._enhancedLinks.clear();
 
       // all directory links have a frecency of DIRECTORY_FRECENCY
-      aCallback(rawLinks.map((link, position) => {
+      return rawLinks.map((link, position) => {
         // Stash the enhanced image for the site
         if (link.enhancedImageURI) {
           this._enhancedLinks.set(NewTabUtils.extractSite(link.url), link);
         }
 
         link.frecency = DIRECTORY_FRECENCY;
         link.lastVisitDate = rawLinks.length - position;
         return link;
-      }));
-    });
+      });
+    }).catch(ex => {
+      Cu.reportError(ex);
+      return [];
+    }).then(aCallback);
   },
 
   init: function DirectoryLinksProvider_init() {
     this.enabled = this.locale.search(/^(en|de|es|fr|ja|pl|pt|ru)/) == 0;
 
     this._setDefaultEnhanced();
     this._addPrefsObserver();
     // setup directory file path and last download timestamp
--- a/toolkit/modules/tests/xpcshell/test_DirectoryLinksProvider.js
+++ b/toolkit/modules/tests/xpcshell/test_DirectoryLinksProvider.js
@@ -421,16 +421,31 @@ add_task(function test_DirectoryLinksPro
 
 add_task(function test_DirectoryLinksProvider_getLinks_noLocaleData() {
   yield promiseSetupDirectoryLinksProvider({locale: 'zh-CN'});
   let links = yield fetchData();
   do_check_eq(links.length, 0);
   yield promiseCleanDirectoryLinksProvider();
 });
 
+add_task(function test_DirectoryLinksProvider_getLinks_badData() {
+  let data = {
+    "en-US": {
+      "en-US": [{url: "http://example.com", title: "US"}],
+    },
+  };
+  let dataURI = 'data:application/json,' + JSON.stringify(data);
+  yield promiseSetupDirectoryLinksProvider({linksURL: dataURI});
+
+  // Make sure we get nothing for incorrectly formatted data
+  let links = yield fetchData();
+  do_check_eq(links.length, 0);
+  yield promiseCleanDirectoryLinksProvider();
+});
+
 add_task(function test_DirectoryLinksProvider_needsDownload() {
   // test timestamping
   DirectoryLinksProvider._lastDownloadMS = 0;
   do_check_true(DirectoryLinksProvider._needsDownload);
   DirectoryLinksProvider._lastDownloadMS = Date.now();
   do_check_false(DirectoryLinksProvider._needsDownload);
   DirectoryLinksProvider._lastDownloadMS = Date.now() - (60*60*24 + 1)*1000;
   do_check_true(DirectoryLinksProvider._needsDownload);