Bug 840177 - Part 6: Record urlbar searches in Firefox Health Report; r=gavin
authorGregory Szorc <gps@mozilla.com>
Thu, 14 Feb 2013 14:47:49 -0800
changeset 131803 953b1db7a246b5b07612088f8784c4aee331f139
parent 131802 1725b56ec1a3ec1b357d7fd68da49b69aa530a08
child 131809 31e89328fe12042f5affd2905d280ef412086c8f
child 131895 bcf070a2b4231797ce1b78eb6eaa862623708150
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs840177
milestone21.0a1
first release with
nightly linux32
953b1db7a246 / 21.0a1 / 20130215031040 / files
nightly linux64
953b1db7a246 / 21.0a1 / 20130215031040 / files
nightly mac
953b1db7a246 / 21.0a1 / 20130215031040 / files
nightly win32
953b1db7a246 / 21.0a1 / 20130215031040 / files
nightly win64
953b1db7a246 / 21.0a1 / 20130215031040 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 840177 - Part 6: Record urlbar searches in Firefox Health Report; r=gavin
browser/base/content/test/Makefile.in
browser/base/content/test/browser_urlbar_search_healthreport.js
browser/components/nsBrowserGlue.js
--- a/browser/base/content/test/Makefile.in
+++ b/browser/base/content/test/Makefile.in
@@ -217,16 +217,17 @@ endif
                  browser_tabs_owner.js \
                  browser_unloaddialogs.js \
                  browser_urlbarAutoFillTrimURLs.js \
                  browser_urlbarCopying.js \
                  browser_urlbarEnter.js \
                  browser_urlbarRevert.js \
                  browser_urlbarStop.js \
                  browser_urlbarTrimURLs.js \
+                 browser_urlbar_search_healthreport.js \
                  browser_urlHighlight.js \
                  browser_visibleFindSelection.js \
                  browser_visibleTabs.js \
                  browser_visibleTabs_contextMenu.js \
                  browser_visibleTabs_bookmarkAllPages.js \
                  browser_visibleTabs_bookmarkAllTabs.js \
                  browser_visibleTabs_tabPreview.js \
                  bug592338.html \
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/browser_urlbar_search_healthreport.js
@@ -0,0 +1,62 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+function test() {
+  waitForExplicitFinish();
+
+  if (!("@mozilla.org/datareporting/service;1" in Cc)) {
+    ok(true, "Firefox Health Report is not enabled.");
+    finish();
+    return;
+  }
+
+  let reporter = Cc["@mozilla.org/datareporting/service;1"]
+                   .getService()
+                   .wrappedJSObject
+                   .healthReporter;
+  ok(reporter, "Health Reporter available.");
+  reporter.onInit().then(function onInit() {
+    let provider = reporter.getProvider("org.mozilla.searches");
+    ok(provider, "Searches provider is available.");
+    let m = provider.getMeasurement("counts", 1);
+
+    m.getValues().then(function onData(data) {
+      let now = new Date();
+      let oldCount = 0;
+
+      // This will need changed if default search engine is not Google.
+      let field = "google.urlbar";
+
+      if (data.days.hasDay(now)) {
+        let day = data.days.getDay(now);
+        if (day.has(field)) {
+          oldCount = day.get(field);
+        }
+      }
+
+      let tab = gBrowser.addTab();
+      gBrowser.selectedTab = tab;
+
+      gURLBar.value = "firefox health report";
+      gURLBar.handleCommand();
+
+      executeSoon(function afterSearch() {
+        gBrowser.removeTab(tab);
+
+        m.getValues().then(function onData(data) {
+          ok(data.days.hasDay(now), "FHR has data for today.");
+          let day = data.days.getDay(now);
+          ok(day.has(field), "FHR has url bar count for today.");
+
+          let newCount = day.get(field);
+
+          is(newCount, oldCount + 1, "Exactly one search has been recorded.");
+          finish();
+        });
+      });
+    });
+  });
+}
+
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -285,18 +285,43 @@ BrowserGlue.prototype = {
             win.openUILinkIn(data, "tab");
             linkHandled.data = true;
           }
         }
         break;
       case "profile-before-change":
         this._onProfileShutdown();
         break;
+#ifdef MOZ_SERVICES_HEALTHREPORT
+      case "keyword-search":
+        // This is very similar to code in
+        // browser.js:BrowserSearch.recordSearchInHealthReport(). The code could
+        // be consolidated if there is will. We need the observer in
+        // nsBrowserGlue to prevent double counting.
+        let reporter = Cc["@mozilla.org/datareporting/service;1"]
+                         .getService()
+                         .wrappedJSObject
+                         .healthReporter;
+
+        if (!reporter) {
+          return;
+        }
+
+        reporter.onInit().then(function record() {
+          try {
+            reporter.getProvider("org.mozilla.searches").recordSearch(data,
+                                                                      "urlbar");
+          } catch (ex) {
+            Cu.reportError(ex);
+          }
+        });
+        break;
+#endif
     }
-  }, 
+  },
 
   // initialization (called on application startup) 
   _init: function BG__init() {
     let os = Services.obs;
     os.addObserver(this, "prefservice:after-app-defaults", false);
     os.addObserver(this, "final-ui-startup", false);
     os.addObserver(this, "browser-delayed-startup-finished", false);
     os.addObserver(this, "sessionstore-windows-restored", false);
@@ -317,16 +342,19 @@ BrowserGlue.prototype = {
     os.addObserver(this, "places-database-locked", false);
     this._isPlacesLockedObserver = true;
     os.addObserver(this, "distribution-customization-complete", false);
     os.addObserver(this, "places-shutdown", false);
     this._isPlacesShutdownObserver = true;
     os.addObserver(this, "defaultURIFixup-using-keyword-pref", false);
     os.addObserver(this, "handle-xul-text-link", false);
     os.addObserver(this, "profile-before-change", false);
+#ifdef MOZ_SERVICES_HEALTHREPORT
+    os.addObserver(this, "keyword-search", false);
+#endif
   },
 
   // cleanup (called on application shutdown)
   _dispose: function BG__dispose() {
     let os = Services.obs;
     os.removeObserver(this, "prefservice:after-app-defaults");
     os.removeObserver(this, "final-ui-startup");
     os.removeObserver(this, "sessionstore-windows-restored");
@@ -348,16 +376,19 @@ BrowserGlue.prototype = {
       os.removeObserver(this, "places-init-complete");
     if (this._isPlacesLockedObserver)
       os.removeObserver(this, "places-database-locked");
     if (this._isPlacesShutdownObserver)
       os.removeObserver(this, "places-shutdown");
     os.removeObserver(this, "defaultURIFixup-using-keyword-pref");
     os.removeObserver(this, "handle-xul-text-link");
     os.removeObserver(this, "profile-before-change");
+#ifdef MOZ_SERVICES_HEALTHREPORT
+    os.removeObserver(this, "keyword-search");
+#endif
   },
 
   _onAppDefaults: function BG__onAppDefaults() {
     // apply distribution customizations (prefs)
     // other customizations are applied in _onProfileStartup()
     this._distributionCustomizer.applyPrefDefaults();
   },