REGRESSION (r172975): navigator.language unable to tell region for Traditional Chines...
authorap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Jun 2015 04:25:18 +0000 (04:25 +0000)
committerap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Jun 2015 04:25:18 +0000 (04:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=146121
rdar://problem/21395180

Reviewed by Darin Adler.

Source/WebCore:

Revert to previous behavior, which is wrong in many ways, but not as wrong as the new one.

* platform/mac/Language.mm:
(WebCore::httpStyleLanguageCode):
(WebCore::platformUserPreferredLanguages):
* platform/spi/cf/CFBundleSPI.h:

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/mac/NavigatorLanguage.mm: Added.
(-[NavigatorLanguageDelegate webView:didFinishLoadForFrame:]):
(TestWebKitAPI::overrideAppleLanguages):
(TestWebKitAPI::languageForSystemLanguage):
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@185816 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/mac/Language.mm
Source/WebCore/platform/spi/cf/CFBundleSPI.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/mac/NavigatorLanguage.mm [new file with mode: 0644]

index b7dc9185a9c6d1741fbe4e11b106cf3ec9574569..190e794e07321301139819338470d31e97735dba 100644 (file)
@@ -1,3 +1,18 @@
+2015-06-21  Alexey Proskuryakov  <ap@apple.com>
+
+        REGRESSION (r172975): navigator.language unable to tell region for Traditional Chinese users
+        https://bugs.webkit.org/show_bug.cgi?id=146121
+        rdar://problem/21395180
+
+        Reviewed by Darin Adler.
+
+        Revert to previous behavior, which is wrong in many ways, but not as wrong as the new one.
+
+        * platform/mac/Language.mm:
+        (WebCore::httpStyleLanguageCode):
+        (WebCore::platformUserPreferredLanguages):
+        * platform/spi/cf/CFBundleSPI.h:
+
 2015-06-19  Andy Estes  <aestes@apple.com>
 
         Give Node::didNotifySubtreeInsertions() a better name
index fa2542b31b30bbb4bc97ce744db90ac7d8ddba1f..fef1fa2279fbf79bae46c0a44d45dd65a2a1ff6c 100644 (file)
@@ -27,6 +27,7 @@
 #import "Language.h"
 
 #import "BlockExceptions.h"
+#import "CFBundleSPI.h"
 #import "WebCoreNSStringExtras.h"
 #import <mutex>
 #import <wtf/Assertions.h>
@@ -79,7 +80,31 @@ namespace WebCore {
 
 static String httpStyleLanguageCode(NSString *language)
 {
-    return [[NSLocale canonicalLanguageIdentifierFromString:canonicalLocaleName(language)] lowercaseString];
+    SInt32 languageCode;
+    SInt32 regionCode; 
+    SInt32 scriptCode; 
+    CFStringEncoding stringEncoding; 
+
+    // FIXME: This transformation is very wrong:
+    // 1. There is no reason why CFBundle localization names would be at all related to language names as used on the Web.
+    // 2. Script Manager codes cannot represent all languages that are now supported by the platform, so the conversion is lossy.
+    // 3. This should probably match what is sent by the network layer as Accept-Language, but currently, that's implemented separately.
+    CFBundleGetLocalizationInfoForLocalization((CFStringRef)language, &languageCode, &regionCode, &scriptCode, &stringEncoding);
+    RetainPtr<CFStringRef> preferredLanguageCode = adoptCF(CFBundleCopyLocalizationForLocalizationInfo(languageCode, regionCode, scriptCode, stringEncoding));
+    if (preferredLanguageCode)
+        language = (NSString *)preferredLanguageCode.get();
+
+    // Make the string lowercase.
+    NSString *lowercaseLanguageCode = [language lowercaseString];
+
+    // Turn a '_' into a '-' if it appears after a 2-letter language code.
+    if ([lowercaseLanguageCode length] >= 3 && [lowercaseLanguageCode characterAtIndex:2] == '_') {
+        RetainPtr<NSMutableString> mutableLanguageCode = adoptNS([lowercaseLanguageCode mutableCopy]);
+        [mutableLanguageCode.get() replaceCharactersInRange:NSMakeRange(2, 1) withString:@"-"];
+        return mutableLanguageCode.get();
+    }
+
+    return lowercaseLanguageCode;
 }
 
 Vector<String> platformUserPreferredLanguages()
index 58b94d1cd845a60a82378ee5fa0be7a03f2c173a..4ef0ae3a605b8ac0ae7d21407edbdacf674591ff 100644 (file)
@@ -36,6 +36,9 @@
 
 extern "C" {
 void _CFBundleSetupXPCBootstrap(xpc_object_t bootstrap);
+
+Boolean CFBundleGetLocalizationInfoForLocalization(CFStringRef localizationName, SInt32 *languageCode, SInt32 *regionCode, SInt32 *scriptCode, CFStringEncoding *stringEncoding);
+CFStringRef CFBundleCopyLocalizationForLocalizationInfo(SInt32 languageCode, SInt32 regionCode, SInt32 scriptCode, CFStringEncoding stringEncoding);
 }
 
 #endif
index 32ac04b11f0c008f89324c8fa2dde66d823d7cc1..01f5458372853cdc5800221ff25c5a279ef5d0c3 100644 (file)
@@ -1,3 +1,18 @@
+2015-06-21  Alexey Proskuryakov  <ap@apple.com>
+
+        REGRESSION (r172975): navigator.language unable to tell region for Traditional Chinese users
+        https://bugs.webkit.org/show_bug.cgi?id=146121
+        rdar://problem/21395180
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/mac/NavigatorLanguage.mm: Added.
+        (-[NavigatorLanguageDelegate webView:didFinishLoadForFrame:]):
+        (TestWebKitAPI::overrideAppleLanguages):
+        (TestWebKitAPI::languageForSystemLanguage):
+        (TestWebKitAPI::TEST):
+
 2015-06-21  Michael Catanzaro  <mcatanzaro@igalia.com>
 
         [CMake] Ignore warnings from system headers when compiling Tools
index 91c0492efdee99356d9628a39b7679e666604c4a..b60aae82038509c2067a6b47d3a22bb863cf5e62 100644 (file)
                CEA6CF2819CCF69D0064F5A7 /* open-and-close-window.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = CEA6CF2719CCF69D0064F5A7 /* open-and-close-window.html */; };
                E1220DCA155B28AA0013E2FC /* MemoryCacheDisableWithinResourceLoadDelegate.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = E1220DC9155B287D0013E2FC /* MemoryCacheDisableWithinResourceLoadDelegate.html */; };
                E194E1BD177E53C7009C4D4E /* StopLoadingFromDidReceiveResponse.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = E194E1BC177E534A009C4D4E /* StopLoadingFromDidReceiveResponse.html */; };
+               E19DB9791B32137C00DB38D4 /* NavigatorLanguage.mm in Sources */ = {isa = PBXBuildFile; fileRef = E19DB9781B32137C00DB38D4 /* NavigatorLanguage.mm */; };
                E40019331ACE9B88001B0A2A /* BloomFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E40019301ACE9B5C001B0A2A /* BloomFilter.cpp */; };
                F660AA1115A5F631003A1243 /* GetInjectedBundleInitializationUserDataCallback_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F660AA0F15A5F624003A1243 /* GetInjectedBundleInitializationUserDataCallback_Bundle.cpp */; };
                F660AA1515A61ABF003A1243 /* InjectedBundleInitializationUserDataCallbackWins_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F660AA1415A61ABF003A1243 /* InjectedBundleInitializationUserDataCallbackWins_Bundle.cpp */; };
                E1220DC9155B287D0013E2FC /* MemoryCacheDisableWithinResourceLoadDelegate.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = MemoryCacheDisableWithinResourceLoadDelegate.html; sourceTree = "<group>"; };
                E194E1BA177E5145009C4D4E /* StopLoadingFromDidReceiveResponse.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = StopLoadingFromDidReceiveResponse.mm; sourceTree = "<group>"; };
                E194E1BC177E534A009C4D4E /* StopLoadingFromDidReceiveResponse.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = StopLoadingFromDidReceiveResponse.html; sourceTree = "<group>"; };
+               E19DB9781B32137C00DB38D4 /* NavigatorLanguage.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = NavigatorLanguage.mm; sourceTree = "<group>"; };
                E40019301ACE9B5C001B0A2A /* BloomFilter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BloomFilter.cpp; sourceTree = "<group>"; };
                E490296714E2E3A4002BEDD1 /* TypingStyleCrash.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TypingStyleCrash.mm; sourceTree = "<group>"; };
                E4A757D3178AEA5B00B5D7A4 /* Deque.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Deque.cpp; sourceTree = "<group>"; };
                                E1220D9F155B25480013E2FC /* MemoryCacheDisableWithinResourceLoadDelegate.mm */,
                                517E7DFB15110EA600D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.mm */,
                                7A99D9931AD4A29D00373141 /* MenuTypesForMouseEvents.mm */,
+                               E19DB9781B32137C00DB38D4 /* NavigatorLanguage.mm */,
                                A57A34EF16AF677200C2501F /* PageVisibilityStateWithWindowChanges.mm */,
                                00BC16851680FE810065F1E5 /* PublicSuffix.mm */,
                                37C784DE197C8F2E0010A496 /* RenderedImageFromDOMNode.mm */,
                                26F6E1F01ADC749B00DE696B /* DFAMinimizer.cpp in Sources */,
                                260BA5791B1D2E7B004FA07C /* DFACombiner.cpp in Sources */,
                                7A99D9941AD4A29D00373141 /* MenuTypesForMouseEvents.mm in Sources */,
+                               E19DB9791B32137C00DB38D4 /* NavigatorLanguage.mm in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
diff --git a/Tools/TestWebKitAPI/Tests/mac/NavigatorLanguage.mm b/Tools/TestWebKitAPI/Tests/mac/NavigatorLanguage.mm
new file mode 100644 (file)
index 0000000..c4d756b
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "PlatformUtilities.h"
+#import <WebKit/WebViewPrivate.h>
+#import <WebKit/DOM.h>
+#import <wtf/RetainPtr.h>
+
+@interface NavigatorLanguageDelegate : NSObject <WebFrameLoadDelegate> {
+}
+@end
+
+static bool didFinishLoad;
+
+@implementation NavigatorLanguageDelegate
+
+- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
+{
+    didFinishLoad = true;
+}
+
+@end
+
+namespace TestWebKitAPI {
+
+void overrideAppleLanguages(NSString *primaryLanguage)
+{
+    RetainPtr<NSMutableDictionary> argumentDomain = adoptNS([[[NSUserDefaults standardUserDefaults] volatileDomainForName:NSArgumentDomain] mutableCopy]);
+    if (!argumentDomain)
+        argumentDomain = adoptNS([[NSMutableDictionary alloc] init]);
+    
+    [argumentDomain addEntriesFromDictionary:@{
+        @"AppleLanguages": @[primaryLanguage]
+    }];
+    [[NSUserDefaults standardUserDefaults] setVolatileDomain:argumentDomain.get() forName:NSArgumentDomain];
+
+    [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"AppleLanguagePreferencesChangedNotification" object:nil userInfo:nil options:NSNotificationDeliverImmediately];
+
+    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+}
+
+static NSString *languageForSystemLanguage(WebView* webView, NSString *systemLanguage)
+{
+    overrideAppleLanguages(systemLanguage);
+    return [webView stringByEvaluatingJavaScriptFromString:@"navigator.language"];
+}
+
+// These tests document current behavior. Some of the current results may not be right.
+NSArray *tests = @[
+    @[@"ru", @"ru-ru"], // This does not match other browsers or CFNetwork's Accept-Language, which all use "ru".
+    @[@"en", @"en-us"],
+    @[@"en-GB", @"en-gb"],
+    @[@"en-US", @"en-us"],
+    @[@"ja", @"ja-jp"],
+    @[@"hi", @"hi-in"],
+    @[@"zh-TW", @"zh-tw"], // This should not map to the generic zh-hant, see rdar://problem/21395180.
+    @[@"zh-HK", @"zh-tw"],
+    @[@"es", @"es-es"],
+    @[@"es-MX", @"es-xl"],
+    @[@"es-ES", @"es-es"],
+    @[@"es-419", @"es-xl"],
+    @[@"zh-Hans", @"zh-cn"],
+    @[@"zh-Hant", @"zh-tw"],
+    @[@"pt-BR", @"pt-br"],
+    @[@"pt-PT", @"pt-pt"],
+    @[@"fr", @"fr-fr"],
+    @[@"fr-CA", @"fr-ca"],
+];
+
+TEST(WebKit1, NavigatorLanguage)
+{
+    RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSZeroRect frameName:nil groupName:nil]);
+    RetainPtr<NavigatorLanguageDelegate> frameLoadDelegate = adoptNS([NavigatorLanguageDelegate new]);
+
+    webView.get().frameLoadDelegate = frameLoadDelegate.get();
+    [[webView.get() mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
+
+    Util::run(&didFinishLoad);
+    for (NSArray *test in tests)
+        EXPECT_WK_STREQ([test objectAtIndex:1], languageForSystemLanguage(webView.get(), [test objectAtIndex:0]));
+}
+
+#if WK_API_ENABLED
+
+TEST(WKWebView, NavigatorLanguage)
+{
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]];
+    [webView loadRequest:request];
+    __block bool isDone = false;
+
+    __block void (^runTest)(NSUInteger) = ^(NSUInteger index) {
+        NSArray *test = [tests objectAtIndex:index];
+        overrideAppleLanguages([test objectAtIndex:0]);
+        [webView evaluateJavaScript:@"navigator.language" completionHandler:^(id result, NSError *) {
+            EXPECT_WK_STREQ([test objectAtIndex:1], result);
+            if (index + 1 < [tests count])
+                runTest(index + 1);
+            else
+                isDone = true;
+        }];
+    };
+
+    runTest(0);
+
+    TestWebKitAPI::Util::run(&isDone);
+}
+
+#endif
+
+} // namespace TestWebKitAPI