OSDN Git Service

Fix WebRequestContext::get() to be threadsafe
authorSteve Block <steveblock@google.com>
Wed, 27 Oct 2010 08:58:13 +0000 (09:58 +0100)
committerSteve Block <steveblock@google.com>
Wed, 27 Oct 2010 13:07:30 +0000 (14:07 +0100)
This allows the method to be called from both the WebCore thread and
the UI thread, as required to hook up CookieManager.

Change-Id: I895d14909b14f0baba77fb2cb75926d9103ee01f

WebKit/android/WebCoreSupport/WebRequestContext.cpp
WebKit/android/WebCoreSupport/WebRequestContext.h
WebKit/android/jni/CookieManager.cpp

index 87b6893..5484979 100644 (file)
@@ -171,7 +171,10 @@ WebRequestContext* WebRequestContext::getContextForPath(const char* cookieFilena
 
 WebRequestContext* WebRequestContext::getRegularContext()
 {
+    static WTF::Mutex regularContextMutex;
     static scoped_refptr<WebRequestContext> regularContext(0);
+
+    MutexLocker lock(regularContextMutex);
     if (!regularContext)
         regularContext = getContextForPath(kCookiesDatabaseFilename, kCacheDirectory);
     return regularContext;
index 3f5631f..788998e 100644 (file)
@@ -38,6 +38,9 @@ public:
     virtual const std::string& GetAcceptLanguage() const;
 
     // Lazily create the relevant context. This class holds a reference.
+    // This may be called on any thread. The context returned, however, is not
+    // threadsafe, and should only be used on a single thread (the network stack
+    // IO thread), with the exception of the methods below.
     static WebRequestContext* get(bool isPrivateBrowsing);
 
     // These methods are threadsafe.
index f16ffd7..34e2cb6 100644 (file)
@@ -46,6 +46,16 @@ static bool useChromiumHttpStack(JNIEnv*, jobject) {
 
 static void removeAllCookie(JNIEnv*, jobject) {
 #if USE(CHROME_NETWORK_STACK)
+    // Though WebRequestContext::get() is threadsafe, the context itself, in
+    // general, is not. The context is generally used on the HTTP stack IO
+    // thread, but this call is on the UI thread. However, the cookie_store()
+    // getter just returns a pointer which is only set when the context is first
+    // constructed. The CookieMonster is threadsafe, so the call below is safe
+    // overall.
+    //
+    // TODO: It's possible that this call is made before the WebKit thread has
+    // started up and the settings have been synced to the BrowserFrame. This
+    // will cause creation of the context to fail.
     WebRequestContext::get(false)->cookie_store()->GetCookieMonster()->DeleteAllCreatedAfter(Time(), true);
     // This will lazily create a new private browsing context. However, if the
     // context doesn't already exist, there's no need to create it, as cookies