Bug 961523 - Never show a snippet again after a user dismisses it. r=bnicholson
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Thu, 20 Feb 2014 18:04:08 -0800
changeset 170207 14d9c2321e0e0f1f5a85a890364864103ed92e03
parent 170206 a9af498c077df6d881ebc44fc05bba4075c74a07
child 170208 c281177594d634ede8d9161f7e245853b35dfd43
child 170367 365329ab2aa7695f02d717e8851f03ed8caa57a1
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersbnicholson
bugs961523
milestone30.0a1
Bug 961523 - Never show a snippet again after a user dismisses it. r=bnicholson
mobile/android/components/Snippets.js
--- a/mobile/android/components/Snippets.js
+++ b/mobile/android/components/Snippets.js
@@ -25,16 +25,19 @@ const SNIPPETS_STATS_URL_PREF = "browser
 const SNIPPETS_GEO_URL_PREF = "browser.snippets.geoUrl";
 
 // Timestamp when we last updated the user's country code.
 const SNIPPETS_GEO_LAST_UPDATE_PREF = "browser.snippets.geoLastUpdate";
 
 // Pref where we'll cache the user's country.
 const SNIPPETS_COUNTRY_CODE_PREF = "browser.snippets.countryCode";
 
+// Pref where we store an array IDs of snippets that should not be shown again
+const SNIPPETS_REMOVED_IDS_PREF = "browser.snippets.removedIds";
+
 // How frequently we update the user's country code from the server (30 days).
 const SNIPPETS_GEO_UPDATE_INTERVAL_MS = 86400000*30;
 
 // Should be bumped up if the snippets content format changes.
 const SNIPPETS_VERSION = 1;
 
 XPCOMUtils.defineLazyGetter(this, "gSnippetsURL", function() {
   let updateURL = Services.prefs.getCharPref(SNIPPETS_UPDATE_URL_PREF).replace("%SNIPPETS_VERSION%", SNIPPETS_VERSION);
@@ -164,41 +167,74 @@ var gMessageIds = [];
 function updateBanner(response) {
   // Remove the current messages, if there are any.
   gMessageIds.forEach(function(id) {
     Home.banner.remove(id);
   })
   gMessageIds = [];
 
   let messages = JSON.parse(response);
+
+  try {
+    let removedSnippetIds = JSON.parse(Services.prefs.getCharPref(SNIPPETS_REMOVED_IDS_PREF));
+    messages = messages.filter(function(message) {
+      // Only include the snippet if it has not been previously removed.
+      return removedSnippetIds.indexOf(message.id) === -1;
+    });
+  } catch (e) {
+    // If the pref doesn't exist, there aren't any snippets to filter out.
+  }
+
   messages.forEach(function(message) {
     // Don't add this message to the banner if it's not supposed to be shown in this country.
     if ("target_geo" in message && message.target_geo != gCountryCode) {
       return;
     }
     let id = Home.banner.add({
       text: message.text,
       icon: message.icon,
       onclick: function() {
         let parentId = gChromeWin.BrowserApp.selectedTab.id;
         gChromeWin.BrowserApp.addTab(message.url, { parentId: parentId });
       },
+      ondismiss: function() {
+        // Remove this snippet from the banner, and store its id so we'll never show it again.
+        Home.banner.remove(id);
+        removeSnippet(message.id);
+      },
       onshown: function() {
         // 10% of the time, record the snippet id and a timestamp
         if (Math.random() < .1) {
           writeStat(message.id, new Date().toISOString());
         }
       }
     });
     // Keep track of the message we added so that we can remove it later.
     gMessageIds.push(id);
   });
 }
 
 /**
+ * Appends snippet id to the end of `snippets-removed.txt`
+ *
+ * @param snippetId unique id for snippet, sent from snippets server
+ */
+function removeSnippet(snippetId) {
+  let removedSnippetIds;
+  try {
+    removedSnippetIds = JSON.parse(Services.prefs.getCharPref(SNIPPETS_REMOVED_IDS_PREF));
+  } catch (e) {
+    removedSnippetIds = [];
+  }
+
+  removedSnippetIds.push(snippetId);
+  Services.prefs.setCharPref(SNIPPETS_REMOVED_IDS_PREF, JSON.stringify(removedSnippetIds));
+}
+
+/**
  * Appends snippet id and timestamp to the end of `snippets-stats.txt`.
  *
  * @param snippetId unique id for snippet, sent from snippets server
  * @param timestamp in ISO8601
  */
 function writeStat(snippetId, timestamp) {
   let data = gEncoder.encode(snippetId + "," + timestamp + ";");