ASSERT(isMainThread()) hit under platformUserPreferredLanguages() with WebKit1 and...
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 1 Mar 2014 03:11:49 +0000 (03:11 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 1 Mar 2014 03:11:49 +0000 (03:11 +0000)
https://bugs.webkit.org/show_bug.cgi?id=129528

Reviewed by Andreas Kling.

* platform/mac/Language.mm:
(WebCore::preferredLanguagesMutex):
(WebCore::preferredLanguages):
(+[WebLanguageChangeObserver languagePreferencesDidChange:]):
(WebCore::httpStyleLanguageCode):
(WebCore::platformUserPreferredLanguages):
Add a mutex and do an isolated copy of the strings to allow this function to be called from multiple threads.

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

Source/WebCore/ChangeLog
Source/WebCore/platform/mac/Language.mm

index 6957ffd05766392c962fc5e5e99869d52f7f061f..925d1e5e3db79d2706816d0846c975d7ae7f34cc 100644 (file)
@@ -1,3 +1,18 @@
+2014-02-28  Sam Weinig  <sam@webkit.org>
+
+        ASSERT(isMainThread()) hit under platformUserPreferredLanguages() with WebKit1 and WebKit2 in the same process
+        https://bugs.webkit.org/show_bug.cgi?id=129528
+
+        Reviewed by Andreas Kling.
+
+        * platform/mac/Language.mm:
+        (WebCore::preferredLanguagesMutex):
+        (WebCore::preferredLanguages):
+        (+[WebLanguageChangeObserver languagePreferencesDidChange:]):
+        (WebCore::httpStyleLanguageCode):
+        (WebCore::platformUserPreferredLanguages):
+        Add a mutex and do an isolated copy of the strings to allow this function to be called from multiple threads.
+
 2014-02-28  Dean Jackson  <dino@apple.com>
 
         Crash at ebay.com when viewing auction items at com.apple.WebCore: WebCore::GraphicsContext3D::getExtensions + 10
index 1afcc3706e786b3c8ea9f394c5ef53ac0ba82c4c..bc72c49147327c555fdcc82df6a905702769a2d1 100644 (file)
 
 #import "BlockExceptions.h"
 #import "WebCoreSystemInterface.h"
+#import <mutex>
 #import <wtf/Assertions.h>
-#import <wtf/MainThread.h>
+#import <wtf/NeverDestroyed.h>
 #import <wtf/RetainPtr.h>
 #import <wtf/text/WTFString.h>
 
-using namespace WebCore;
+namespace WebCore {
+
+static std::mutex& preferredLanguagesMutex()
+{
+    static dispatch_once_t onceToken;
+    static std::mutex* mutex;
+
+    dispatch_once(&onceToken, ^{
+        mutex = std::make_unique<std::mutex>().release();
+    });
 
-static BOOL useCachedPreferredLanguages;
+    return *mutex;
+}
+
+static Vector<String>& preferredLanguages()
+{
+    static NeverDestroyed<Vector<String>> languages;
+    return languages;
+}
 
-@interface WebLanguageChangeObserver : NSObject {
 }
+
+@interface WebLanguageChangeObserver : NSObject
 @end
 
 @implementation WebLanguageChangeObserver
 
-+ (void)_webkit_languagePreferencesDidChange
++ (void)languagePreferencesDidChange:(NSNotification *)notification
 {
-    ASSERT(isMainThread());
+    UNUSED_PARAM(notification);
 
-    useCachedPreferredLanguages = NO;
+    {
+        std::lock_guard<std::mutex> lock(WebCore::preferredLanguagesMutex());
+        WebCore::preferredLanguages().clear();
+    }
 
-    languageDidChange();
+    WebCore::languageDidChange();
 }
 
 @end
@@ -58,8 +79,6 @@ namespace WebCore {
 
 static String httpStyleLanguageCode(NSString *languageCode)
 {
-    ASSERT(isMainThread());
-
     // Look up the language code using CFBundle.
     RetainPtr<CFStringRef> preferredLanguageCode = adoptCF(wkCopyCFLocalizationPreferredName((CFStringRef)languageCode));
 
@@ -81,16 +100,19 @@ static String httpStyleLanguageCode(NSString *languageCode)
 
 Vector<String> platformUserPreferredLanguages()
 {
-    DEFINE_STATIC_LOCAL(Vector<String>, userPreferredLanguages, ());
-
-    ASSERT(isMainThread());
+#if PLATFORM(MAC)
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        [[NSDistributedNotificationCenter defaultCenter] addObserver:[WebLanguageChangeObserver self] selector:@selector(languagePreferencesDidChange:) name:@"AppleLanguagePreferencesChangedNotification" object:nil];
+    });
+#endif
 
     BEGIN_BLOCK_OBJC_EXCEPTIONS;
 
-    if (!useCachedPreferredLanguages) {
-        useCachedPreferredLanguages = YES;
-        userPreferredLanguages.clear();
+    std::lock_guard<std::mutex> lock(preferredLanguagesMutex());
+    Vector<String>& userPreferredLanguages = preferredLanguages();
 
+    if (userPreferredLanguages.isEmpty()) {
         RetainPtr<CFArrayRef> languages = adoptCF(CFLocaleCopyPreferredLanguages());
         CFIndex languageCount = CFArrayGetCount(languages.get());
         if (!languageCount)
@@ -101,18 +123,13 @@ Vector<String> platformUserPreferredLanguages()
         }
     }
 
-#if !PLATFORM(IOS)
-    static bool languageChangeObserverAdded;
-    if (!languageChangeObserverAdded) {
-        [[NSDistributedNotificationCenter defaultCenter] addObserver:[WebLanguageChangeObserver self]
-                                                            selector:@selector(_webkit_languagePreferencesDidChange)
-                                                                name:@"AppleLanguagePreferencesChangedNotification"
-                                                              object:nil];
-        languageChangeObserverAdded = true;
-    }
-#endif // !PLATFORM(IOS)
-    
-    return userPreferredLanguages;
+    Vector<String> userPreferredLanguagesCopy;
+    userPreferredLanguagesCopy.reserveInitialCapacity(userPreferredLanguages.size());
+
+    for (auto& language : userPreferredLanguages)
+        userPreferredLanguagesCopy.uncheckedAppend(language.isolatedCopy());
+
+    return userPreferredLanguagesCopy;
 
     END_BLOCK_OBJC_EXCEPTIONS;