OSDN Git Service

Hook up CookieManager.getCookie() for Chromium HTTP stack
authorSteve Block <steveblock@google.com>
Wed, 27 Oct 2010 15:59:53 +0000 (16:59 +0100)
committerSteve Block <steveblock@google.com>
Wed, 27 Oct 2010 19:27:10 +0000 (20:27 +0100)
Requires a change to frameworks/base ...
https://android-git.corp.google.com/g/76605

Bug: 3116410
Change-Id: I1b66c1ac9f9aaf5388ad1da92f54b1f16d4a3626

WebKit/android/jni/CookieManager.cpp

index e1e139e..154dc5d 100644 (file)
 
 #include "ChromiumIncludes.h"
 #include "WebRequestContext.h"
+#include "WebCoreJni.h"
 #include <JNIHelp.h>
 
 using namespace base;
+using namespace net;
 
 namespace android {
 
 // JNI for android.webkit.CookieManager
 static const char* javaCookieManagerClass = "android/webkit/CookieManager";
 
-static bool useChromiumHttpStack(JNIEnv*, jobject) {
+// Though WebRequestContext::get() is threadsafe, the context itself, in
+// general, is not. The context is generally used on the HTTP stack IO
+// thread, but calls to the methods of this class are made on the UI thread.
+// We ensure thread safety as follows ...
+// - The cookie_store() getter just returns a pointer which is only set when the
+//   context is first constructed. The CookieMonster itself is threadsafe, so
+//   using it from the UI thread is safe.
+// - Calls to the other WebRequestContext methods used here are explicitly
+//   threadsafe.
+
+static bool useChromiumHttpStack(JNIEnv*, jobject)
+{
 #if USE(CHROME_NETWORK_STACK)
     return true;
 #else
@@ -44,7 +57,8 @@ static bool useChromiumHttpStack(JNIEnv*, jobject) {
 #endif
 }
 
-static bool acceptCookie(JNIEnv*, jobject) {
+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
@@ -59,14 +73,22 @@ static bool acceptCookie(JNIEnv*, jobject) {
 #endif
 }
 
-static void removeAllCookie(JNIEnv*, jobject) {
+static jstring getCookie(JNIEnv* env, jobject, jstring url)
+{
+#if USE(CHROME_NETWORK_STACK)
+    GURL gurl(jstringToStdString(env, url));
+    std::string cookies = WebRequestContext::get(false)->cookie_store()->GetCookieMonster()->GetCookiesWithOptions(gurl, CookieOptions());
+    return env->NewStringUTF(cookies.c_str());
+#else
+    // The Android HTTP stack is implemented Java-side.
+    ASSERT_NOT_REACHED();
+    return jstring();
+#endif
+}
+
+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.
     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
@@ -77,7 +99,8 @@ static void removeAllCookie(JNIEnv*, jobject) {
 #endif
 }
 
-static void setAcceptCookie(JNIEnv*, jobject, jboolean accept) {
+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
@@ -90,6 +113,7 @@ static void setAcceptCookie(JNIEnv*, jobject, jboolean accept) {
 static JNINativeMethod gCookieManagerMethods[] = {
     { "nativeUseChromiumHttpStack", "()Z", (void*) useChromiumHttpStack },
     { "nativeAcceptCookie", "()Z", (void*) acceptCookie },
+    { "nativeGetCookie", "(Ljava/lang/String;)Ljava/lang/String;", (void*) getCookie },
     { "nativeRemoveAllCookie", "()V", (void*) removeAllCookie },
     { "nativeSetAcceptCookie", "(Z)V", (void*) setAcceptCookie },
 };