From 965d128a2f803da81a8a6d04c3d79382ff827bb9 Mon Sep 17 00:00:00 2001 From: Steve Block Date: Mon, 25 Oct 2010 12:20:52 +0100 Subject: [PATCH] Hook up CookieManager.acceptCookie() and setAcceptCookie() for the Chromium HTTP stack We also update PlatformBridge::cookiesEnabled() to query the Chromium HTTP stack directly. This avoids calling CookieClient::cookiesEnabled(), which calls the Java CookieManager::acceptCookie() which in turns calls back to native code. Also requires a change to frameworks/base ... https://android-git.corp.google.com/g/76065 Bug: 3116410 Change-Id: Id853463f3bcef76b220e8c44dd2b30c0d6752624 --- WebKit/android/WebCoreSupport/ChromiumIncludes.h | 1 + WebKit/android/WebCoreSupport/PlatformBridge.cpp | 5 ++++ .../android/WebCoreSupport/WebRequestContext.cpp | 26 ++++++++++++++++++++- WebKit/android/WebCoreSupport/WebRequestContext.h | 13 ++++++++++- WebKit/android/jni/CookieManager.cpp | 27 ++++++++++++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) diff --git a/WebKit/android/WebCoreSupport/ChromiumIncludes.h b/WebKit/android/WebCoreSupport/ChromiumIncludes.h index ad9317956..377aa2266 100644 --- a/WebKit/android/WebCoreSupport/ChromiumIncludes.h +++ b/WebKit/android/WebCoreSupport/ChromiumIncludes.h @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #include diff --git a/WebKit/android/WebCoreSupport/PlatformBridge.cpp b/WebKit/android/WebCoreSupport/PlatformBridge.cpp index 03a791247..092676b58 100644 --- a/WebKit/android/WebCoreSupport/PlatformBridge.cpp +++ b/WebKit/android/WebCoreSupport/PlatformBridge.cpp @@ -101,11 +101,16 @@ String PlatformBridge::cookies(const Document* document, const KURL& url) bool PlatformBridge::cookiesEnabled(const Document* document) { +#if USE(CHROME_NETWORK_STACK) + bool isPrivateBrowsing = document->settings() && document->settings()->privateBrowsingEnabled(); + return WebRequestContext::get(isPrivateBrowsing)->allowCookies(); +#else CookieClient* client = JavaSharedClient::GetCookieClient(); if (!client) return false; return client->cookiesEnabled(); +#endif } NPObject* PlatformBridge::pluginScriptableObject(Widget* widget) diff --git a/WebKit/android/WebCoreSupport/WebRequestContext.cpp b/WebKit/android/WebCoreSupport/WebRequestContext.cpp index 2ff3ae80a..aa5311f66 100644 --- a/WebKit/android/WebCoreSupport/WebRequestContext.cpp +++ b/WebKit/android/WebCoreSupport/WebRequestContext.cpp @@ -37,7 +37,6 @@ #include #include #include -#include namespace { // TODO: The userAgent should not be a static, as it can be set per WebView. @@ -65,6 +64,7 @@ static scoped_refptr privateBrowsingContext(0); static WTF::Mutex privateBrowsingContextMutex; WebRequestContext::WebRequestContext() + : m_allowCookies(true) { // Also hardcoded in FrameLoader.java accept_charset_ = "utf-8, iso-8859-1, utf-16, *;q=0.7"; @@ -163,6 +163,7 @@ WebRequestContext* WebRequestContext::getContextForPath(const char* cookieFilena net::CookieMonster::EnableFileScheme(); context->cookie_store_ = new net::CookieMonster(cookieDb.get(), 0); + context->cookie_policy_ = context; return context; } @@ -238,4 +239,27 @@ bool WebRequestContext::cleanupPrivateBrowsingFiles(const std::string& databaseD return false; } +int WebRequestContext::CanGetCookies(const GURL&, const GURL&, net::CompletionCallback*) +{ + MutexLocker lock(m_allowCookiesMutex); + return m_allowCookies ? net::OK : net::ERR_ACCESS_DENIED; +} + +int WebRequestContext::CanSetCookie(const GURL&, const GURL&, const std::string&, net::CompletionCallback*) +{ + MutexLocker lock(m_allowCookiesMutex); + return m_allowCookies ? net::OK : net::ERR_ACCESS_DENIED; +} + +bool WebRequestContext::allowCookies() { + MutexLocker lock(m_allowCookiesMutex); + return m_allowCookies; +} + +void WebRequestContext::setAllowCookies(bool allow) +{ + MutexLocker lock(m_allowCookiesMutex); + m_allowCookies = allow; +} + } // namespace android diff --git a/WebKit/android/WebCoreSupport/WebRequestContext.h b/WebKit/android/WebCoreSupport/WebRequestContext.h index 788998e4c..f4a579cc3 100644 --- a/WebKit/android/WebCoreSupport/WebRequestContext.h +++ b/WebKit/android/WebCoreSupport/WebRequestContext.h @@ -29,14 +29,20 @@ #include "ChromiumIncludes.h" #include "PlatformString.h" +#include + namespace android { -class WebRequestContext : public URLRequestContext { +class WebRequestContext : public URLRequestContext, net::CookiePolicy { public: // URLRequestContext overrides. virtual const std::string& GetUserAgent(const GURL&) const; virtual const std::string& GetAcceptLanguage() const; + // CookiePolicy implementation. + virtual int CanGetCookies(const GURL& url, const GURL& first_party_for_cookies, net::CompletionCallback*); + virtual int CanSetCookie(const GURL& url, const GURL& first_party_for_cookies, const std::string& cookie_line, net::CompletionCallback*); + // 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 @@ -47,6 +53,8 @@ public: static bool cleanupPrivateBrowsingFiles(const std::string& databaseDirectory, const std::string& cacheDirectory); static void setUserAgent(WTF::String); static void setAcceptLanguage(WTF::String); + bool allowCookies(); + void setAllowCookies(bool allow); private: WebRequestContext(); @@ -55,6 +63,9 @@ private: static WebRequestContext* getContextForPath(const char* cookieFilename, const char* cacheFilename); static WebRequestContext* getRegularContext(); static WebRequestContext* getPrivateBrowsingContext(); + + bool m_allowCookies; + WTF::Mutex m_allowCookiesMutex; }; } // namespace android diff --git a/WebKit/android/jni/CookieManager.cpp b/WebKit/android/jni/CookieManager.cpp index 7139286ce..e1e139e11 100644 --- a/WebKit/android/jni/CookieManager.cpp +++ b/WebKit/android/jni/CookieManager.cpp @@ -44,6 +44,21 @@ static bool useChromiumHttpStack(JNIEnv*, jobject) { #endif } +static bool acceptCookie(JNIEnv*, jobject) { +#if USE(CHROME_NETWORK_STACK) + // This is a static method which gets the cookie policy for all WebViews. We + // always apply the same configuration to the contexts for both regular and + // private browsing, so expect the same result here. + bool regularAcceptCookies = WebRequestContext::get(false)->allowCookies(); + ASSERT(regularAcceptCookies == WebRequestContext::get(true)->allowCookies()); + return regularAcceptCookies; +#else + // The Android HTTP stack is implemented Java-side. + ASSERT_NOT_REACHED(); + return false; +#endif +} + static void removeAllCookie(JNIEnv*, jobject) { #if USE(CHROME_NETWORK_STACK) // Though WebRequestContext::get() is threadsafe, the context itself, in @@ -62,9 +77,21 @@ static void removeAllCookie(JNIEnv*, jobject) { #endif } +static void setAcceptCookie(JNIEnv*, jobject, jboolean accept) { +#if USE(CHROME_NETWORK_STACK) + // This is a static method which configures the cookie policy for all + // WebViews, so we configure the contexts for both regular and private + // browsing. + WebRequestContext::get(false)->setAllowCookies(accept); + WebRequestContext::get(true)->setAllowCookies(accept); +#endif +} + static JNINativeMethod gCookieManagerMethods[] = { { "nativeUseChromiumHttpStack", "()Z", (void*) useChromiumHttpStack }, + { "nativeAcceptCookie", "()Z", (void*) acceptCookie }, { "nativeRemoveAllCookie", "()V", (void*) removeAllCookie }, + { "nativeSetAcceptCookie", "(Z)V", (void*) setAcceptCookie }, }; int registerCookieManager(JNIEnv* env) -- 2.11.0