Bug 1102416: make Yahoo the default search plugin for en-US in American time zones, r=dolske, a=gavin
authorGavin Sharp <gavin@gavinsharp.com>
Sun, 23 Nov 2014 20:38:22 -0800
changeset 227441 2af28d4d94dbd8229b80ebcd77d77099632b1853
parent 227440 e08ba260399ae9ad2ac9758d2339b97d0fdb05a6
child 227443 21c5b265756ead78c44dab5428430abb555c9232
child 227450 8274648ad79f62e5e0858122fcd9188ea1efa2eb
push id591
push usergsharp@mozilla.com
push dateWed, 26 Nov 2014 00:47:09 +0000
treeherdermozilla-release@2af28d4d94db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdolske, gavin
bugs1102416
milestone34.0
Bug 1102416: make Yahoo the default search plugin for en-US in American time zones, r=dolske, a=gavin
browser/app/profile/firefox.js
browser/locales/en-US/chrome/browser-region/region.properties
toolkit/components/search/nsSearchService.js
--- a/browser/app/profile/firefox.js
+++ b/browser/app/profile/firefox.js
@@ -396,16 +396,22 @@ pref("browser.search.defaultenginename",
 // disable logging for the search service by default
 pref("browser.search.log", false);
 
 // Ordering of Search Engines in the Engine list. 
 pref("browser.search.order.1",                "chrome://browser-region/locale/region.properties");
 pref("browser.search.order.2",                "chrome://browser-region/locale/region.properties");
 pref("browser.search.order.3",                "chrome://browser-region/locale/region.properties");
 
+// Hack for Firefox 34
+pref("browser.search.defaultenginename.US",      "chrome://browser-region/locale/region.properties");
+pref("browser.search.order.US.1",                "chrome://browser-region/locale/region.properties");
+pref("browser.search.order.US.2",                "chrome://browser-region/locale/region.properties");
+pref("browser.search.order.US.3",                "chrome://browser-region/locale/region.properties");
+
 // search bar results always open in a new tab
 pref("browser.search.openintab", false);
 
 // context menu searches open in the foreground
 pref("browser.search.context.loadInBackground", false);
 
 // send ping to the server to update
 pref("browser.search.update", true);
--- a/browser/locales/en-US/chrome/browser-region/region.properties
+++ b/browser/locales/en-US/chrome/browser-region/region.properties
@@ -5,16 +5,25 @@
 # Default search engine
 browser.search.defaultenginename=Google
 
 # Search engine order (order displayed in the search bar dropdown)s
 browser.search.order.1=Google
 browser.search.order.2=Yahoo
 browser.search.order.3=Bing
 
+# Hack for "NA market" only default changes
+# These override the equivalents above when the client detects that it is in
+# North America
+browser.search.defaultenginename.US=Yahoo
+browser.search.order.US.1=Yahoo
+browser.search.order.US.2=Google
+browser.search.order.US.3=Bing
+
+
 # This is the default set of web based feed handlers shown in the reader
 # selection UI
 browser.contentHandlers.types.0.title=My Yahoo!
 browser.contentHandlers.types.0.uri=https://add.my.yahoo.com/rss?url=%s
 
 # increment this number when anything gets changed in the list below.  This will
 # cause Firefox to re-read these prefs and inject any new handlers into the 
 # profile database.  Note that "new" is defined as "has a different URL"; this
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -396,16 +396,49 @@ loadListener.prototype = {
   // FIXME: bug 253127
   // nsIHttpEventSink
   onRedirect: function (aChannel, aNewChannel) {},
   // nsIProgressEventSink
   onProgress: function (aRequest, aContext, aProgress, aProgressMax) {},
   onStatus: function (aRequest, aContext, aStatus, aStatusArg) {}
 }
 
+// Hacky method that tries to determine if this user is in a US geography, and
+// using an en-US build.
+function getIsUS() {
+  // If we've set the pref before, just return that result.
+  let cachePref = "browser.search.isUS";
+  try {
+    return Services.prefs.getBoolPref(cachePref);
+  } catch(e) {}
+
+  if (getLocale() != "en-US") {
+    Services.prefs.setBoolPref(cachePref, false);
+    return false;
+  }
+
+  // Timezone assumptions! We assume that if the system clock's timezone is
+  // between Newfoundland and Hawaii, that the user is in North America.
+
+  // This includes all of South America as well, but we have relatively few
+  // en-US users there, so that's OK.
+
+  // 150 minutes = 2.5 hours (UTC-2.5), which is
+  // Newfoundland Daylight Time (http://www.timeanddate.com/time/zones/ndt)
+
+  // 600 minutes = 10 hours (UTC-10), which is
+  // Hawaii-Aleutian Standard Time (http://www.timeanddate.com/time/zones/hast)
+
+  let UTCOffset = (new Date()).getTimezoneOffset();
+  let isNA = UTCOffset >= 150 && UTCOffset <= 600;
+
+  Services.prefs.setBoolPref(cachePref, isNA);
+
+  return isNA;
+}
 
 /**
  * Used to verify a given DOM node's localName and namespaceURI.
  * @param aElement
  *        The element to verify.
  * @param aLocalNameArray
  *        An array of strings to compare against aElement's localName.
  * @param aNameSpaceArray
@@ -2987,18 +3020,26 @@ SearchService.prototype = {
   },
 
   // Get the original Engine object that belongs to the defaultenginename pref
   // of the default branch.
   get _originalDefaultEngine() {
     let defaultPrefB = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF);
     let nsIPLS = Ci.nsIPrefLocalizedString;
     let defaultEngine;
+
+    let defPref;
+    if (getIsUS()) {
+      defPref = "defaultenginename.US";
+    } else {
+      defPref = "defaultenginename";
+    }
+
     try {
-      defaultEngine = defaultPrefB.getComplexValue("defaultenginename", nsIPLS).data;
+      defaultEngine = defaultPrefB.getComplexValue(defPref, nsIPLS).data;
     } catch (ex) {
       // If the default pref is invalid (e.g. an add-on set it to a bogus value)
       // getEngineByName will just return null, which is the best we can do.
     }
     return this.getEngineByName(defaultEngine);
   },
 
   _buildCache: function SRCH_SVC__buildCache() {
@@ -3767,17 +3808,21 @@ SearchService.prototype = {
 
           this.__sortedEngines.push(engine);
           addedEngines[engine.name] = engine;
         }
       }
       catch (e) { }
 
       while (true) {
-        engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + (++i));
+        prefName = BROWSER_SEARCH_PREF + "order.";
+        if (getIsUS()) {
+          prefName += "US.";
+        }
+        engineName = getLocalizedPref(prefName + (++i));
         if (!engineName)
           break;
 
         engine = this._engines[engineName];
         if (!engine || engine.name in addedEngines)
           continue;
 
         this.__sortedEngines.push(engine);
@@ -3918,17 +3963,22 @@ SearchService.prototype = {
           engineOrder[engineName] = i++;
       }
     } catch (e) {
       LOG("Getting extra order prefs failed: " + e);
     }
 
     // Now look through the "browser.search.order" branch.
     for (var j = 1; ; j++) {
-      engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + j);
+      var prefName = BROWSER_SEARCH_PREF + "order.";
+      if (getIsUS()) {
+        prefName += "US.";
+      }
+      prefName += j;
+      engineName = getLocalizedPref(prefName);
       if (!engineName)
         break;
 
       if (!(engineName in engineOrder))
         engineOrder[engineName] = i++;
     }
 
     LOG("getDefaultEngines: engineOrder: " + engineOrder.toSource());