Update ANGLE
[WebKit-https.git] / Source / WebCore / platform / Language.cpp
index 65578d0..ec4b349 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2010, 2013, 2016 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -28,6 +28,7 @@
 
 #include <wtf/HashMap.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/PlatformUserPreferredLanguages.h>
 #include <wtf/RetainPtr.h>
 #include <wtf/text/WTFString.h>
 
 
 namespace WebCore {
 
+static StaticLock userPreferredLanguagesMutex;
+
+static void registerLanguageDidChangeCallbackIfNecessary()
+{
+    static std::once_flag once;
+    std::call_once(
+        once,
+        [] {
+            setPlatformUserPreferredLanguagesChangedCallback(languageDidChange);
+        });
+}
+
 typedef HashMap<void*, LanguageChangeObserverFunction> ObserverMap;
 static ObserverMap& observerMap()
 {
@@ -46,6 +59,7 @@ static ObserverMap& observerMap()
 
 void addLanguageChangeObserver(void* context, LanguageChangeObserverFunction customObserver)
 {
+    registerLanguageDidChangeCallbackIfNecessary();
     observerMap().set(context, customObserver);
 }
 
@@ -87,19 +101,32 @@ void overrideUserPreferredLanguages(const Vector<String>& override)
     preferredLanguagesOverride() = override;
     languageDidChange();
 }
-    
+
+static Vector<String> isolatedCopy(const Vector<String>& strings)
+{
+    Vector<String> copy;
+    copy.reserveInitialCapacity(strings.size());
+    for (auto& language : strings)
+        copy.uncheckedAppend(language.isolatedCopy());
+    return copy;
+}
+
 Vector<String> userPreferredLanguages()
 {
-    Vector<String>& override = preferredLanguagesOverride();
-    if (!override.isEmpty())
-        return override;
+    {
+        std::lock_guard<StaticLock> lock(userPreferredLanguagesMutex);
+        Vector<String>& override = preferredLanguagesOverride();
+        if (!override.isEmpty())
+            return isolatedCopy(override);
+    }
     
+    registerLanguageDidChangeCallbackIfNecessary();
     return platformUserPreferredLanguages();
 }
 
 static String canonicalLanguageIdentifier(const String& languageCode)
 {
-    String lowercaseLanguageCode = languageCode.lower();
+    String lowercaseLanguageCode = languageCode.convertToASCIILowercase();
     
     if (lowercaseLanguageCode.length() >= 3 && lowercaseLanguageCode[2] == '_')
         lowercaseLanguageCode.replace(2, 1, "-");
@@ -109,7 +136,7 @@ static String canonicalLanguageIdentifier(const String& languageCode)
 
 size_t indexOfBestMatchingLanguageInList(const String& language, const Vector<String>& languageList, bool& exactMatch)
 {
-    String lowercaseLanguage = language.lower();
+    String lowercaseLanguage = language.convertToASCIILowercase();
     String languageWithoutLocaleMatch;
     String languageMatchButNotLocale;
     size_t languageWithoutLocaleMatchIndex = 0;