Bug 1310814 - Use public NSLocale API for getting the system country code. r=spohl
authorMarkus Stange <mstange@themasta.com>
Wed, 26 Oct 2016 17:56:59 -0400
changeset 319644 55ccd7f2591a5083819fc46105d0cd95796689c7
parent 319643 c9a8a051dd30220ae7a575547de2888e794d67aa
child 319645 d9413eb3db64787916c60efcb7b766cc24420d94
push id83207
push userphilringnalda@gmail.com
push dateThu, 27 Oct 2016 01:59:11 +0000
treeherdermozilla-inbound@58220cbd6604 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersspohl
bugs1310814
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1310814 - Use public NSLocale API for getting the system country code. r=spohl This potentially changes behavior. On my machine, the old code returned NS_ERROR_FAILURE because the dictionary did not have a "countryCode" entry, but this new implementation correctly returns the string "CA". MozReview-Commit-ID: 17Vme0fcAIw
xpcom/base/MacHelpers.mm
--- a/xpcom/base/MacHelpers.mm
+++ b/xpcom/base/MacHelpers.mm
@@ -9,22 +9,25 @@
 
 #import <Foundation/Foundation.h>
 
 namespace mozilla {
 
 nsresult
 GetSelectedCityInfo(nsAString& aCountryCode)
 {
-  NSDictionary* selected_city =
-    [[NSUserDefaults standardUserDefaults]
-      objectForKey:@"com.apple.preferences.timezone.selected_city"];
-  NSString *countryCode = (NSString *)
-    [selected_city objectForKey:@"CountryCode"];
-  const char *countryCodeUTF8 = [countryCode UTF8String];
+  // Can be replaced with [[NSLocale currentLocale] countryCode] once we build
+  // with the 10.12 SDK.
+  id countryCode = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
+
+  if (![countryCode isKindOfClass:[NSString class]]) {
+    return NS_ERROR_FAILURE;
+  }
+
+  const char* countryCodeUTF8 = [(NSString*)countryCode UTF8String];
 
   if (!countryCodeUTF8) {
     return NS_ERROR_FAILURE;
   }
 
   AppendUTF8toUTF16(countryCodeUTF8, aCountryCode);
   return NS_OK;
 }