1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 #include "config.h"
   5 
   6 #if COMPILER(GCC)
   7 #pragma GCC diagnostic ignored "-Wunused-parameter"
   8 #endif
   9 
  10 #include "PlatformCookieJar.h"
  11 #include "URL.h"
  12 
  13 #include <wtf/java/JavaEnv.h>
  14 #include "NotImplemented.h"
  15 
  16 
  17 namespace WebCore {
  18 
  19 namespace CookieJarJavaInternal {
  20 static JGClass cookieJarClass;
  21 static jmethodID getMethod;
  22 static jmethodID putMethod;
  23 
  24 static void initRefs(JNIEnv* env)
  25 {
  26     if (!cookieJarClass) {
  27         cookieJarClass = JLClass(env->FindClass(
  28                 "com/sun/webkit/network/CookieJar"));
  29         ASSERT(cookieJarClass);
  30 
  31         getMethod = env->GetStaticMethodID(
  32                 cookieJarClass,
  33                 "fwkGet",
  34                 "(Ljava/lang/String;Z)Ljava/lang/String;");
  35         ASSERT(getMethod);
  36 
  37         putMethod = env->GetStaticMethodID(
  38                 cookieJarClass,
  39                 "fwkPut",
  40                 "(Ljava/lang/String;Ljava/lang/String;)V");
  41         ASSERT(putMethod);
  42     }
  43 }
  44 
  45 static String getCookies(const URL& url, bool includeHttpOnlyCookies)
  46 {
  47     using namespace CookieJarJavaInternal;
  48     JNIEnv* env = WebCore_GetJavaEnv();
  49     initRefs(env);
  50 
  51     JLString result = static_cast<jstring>(env->CallStaticObjectMethod(
  52             cookieJarClass,
  53             getMethod,
  54             (jstring) url.string().toJavaString(env),
  55             bool_to_jbool(includeHttpOnlyCookies)));
  56     CheckAndClearException(env);
  57 
  58     return result ? String(env, result) : emptyString();
  59 }
  60 }
  61 
  62 void setCookiesFromDOM(const NetworkStorageSession&, const URL&, const URL& url, std::optional<uint64_t>, std::optional<uint64_t>, const String& value)
  63 {
  64     using namespace CookieJarJavaInternal;
  65     JNIEnv* env = WebCore_GetJavaEnv();
  66     initRefs(env);
  67 
  68     env->CallStaticVoidMethod(
  69             cookieJarClass,
  70             putMethod,
  71             (jstring) url.string().toJavaString(env),
  72             (jstring) value.toJavaString(env));
  73     CheckAndClearException(env);
  74 }
  75 
  76 std::pair<String, bool> cookiesForDOM(const NetworkStorageSession&, const URL&, const URL& url, std::optional<uint64_t>, std::optional<uint64_t>, IncludeSecureCookies)
  77 {
  78     using namespace CookieJarJavaInternal;
  79     // 'HttpOnly' cookies should no be accessible from scripts, so we filter them out here.
  80     return { getCookies(url, false), false };
  81 }
  82 
  83 std::pair<String, bool> cookieRequestHeaderFieldValue(const NetworkStorageSession&, const URL& /*firstParty*/, const URL& url, std::optional<uint64_t>, std::optional<uint64_t>, IncludeSecureCookies)
  84 {
  85     using namespace CookieJarJavaInternal;
  86     return { getCookies(url, true), true };
  87 }
  88 
  89 bool cookiesEnabled(const NetworkStorageSession&)
  90 {
  91     return true;
  92 }
  93 
  94 bool getRawCookies(const NetworkStorageSession&, const URL&, const URL&, std::optional<uint64_t>, std::optional<uint64_t>, Vector<Cookie>&)
  95 {
  96     notImplemented();
  97     return false;
  98 }
  99 
 100 void deleteCookie(const NetworkStorageSession&, const URL&, const String&)
 101 {
 102     notImplemented();
 103 }
 104 
 105 void getHostnamesWithCookies(const NetworkStorageSession&, HashSet<String>&)
 106 {
 107     notImplemented();
 108 }
 109 
 110 void deleteCookiesForHostname(const NetworkStorageSession&, const String& /*hostname*/)
 111 {
 112     notImplemented();
 113 }
 114 
 115 void deleteAllCookies(const NetworkStorageSession&)
 116 {
 117     notImplemented();
 118 }
 119 
 120 } // namespace WebCore