Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright 2009, The Android Open Source Project
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 
     28 #if ENABLE(DATABASE)
     29 
     30 #include "ApplicationCacheStorage.h"
     31 #include "DatabaseTracker.h"
     32 #include "JNIUtility.h"
     33 #include "JavaSharedClient.h"
     34 #include "KURL.h"
     35 #include "PageGroup.h"
     36 #include "SecurityOrigin.h"
     37 #include "WebCoreJni.h"
     38 
     39 #include <JNIHelp.h>
     40 
     41 namespace android {
     42 
     43 static jobject GetOrigins(JNIEnv* env, jobject obj)
     44 {
     45     Vector<RefPtr<WebCore::SecurityOrigin> > coreOrigins;
     46     WebCore::DatabaseTracker::tracker().origins(coreOrigins);
     47     Vector<WebCore::KURL> manifestUrls;
     48     if (WebCore::cacheStorage().manifestURLs(&manifestUrls)) {
     49         int size = manifestUrls.size();
     50         for (int i = 0; i < size; ++i) {
     51             RefPtr<WebCore::SecurityOrigin> manifestOrigin = WebCore::SecurityOrigin::create(manifestUrls[i]);
     52             if (manifestOrigin.get() == 0)
     53                 continue;
     54             coreOrigins.append(manifestOrigin);
     55         }
     56     }
     57 
     58     jclass setClass = env->FindClass("java/util/HashSet");
     59     jmethodID cid = env->GetMethodID(setClass, "<init>", "()V");
     60     jmethodID mid = env->GetMethodID(setClass, "add", "(Ljava/lang/Object;)Z");
     61     jobject set = env->NewObject(setClass, cid);
     62     env->DeleteLocalRef(setClass);
     63 
     64     for (unsigned i = 0; i < coreOrigins.size(); ++i) {
     65         WebCore::SecurityOrigin* origin = coreOrigins[i].get();
     66         WTF::String url = origin->toString();
     67         jstring jUrl = wtfStringToJstring(env, url);
     68         env->CallBooleanMethod(set, mid, jUrl);
     69         env->DeleteLocalRef(jUrl);
     70     }
     71 
     72     return set;
     73 }
     74 
     75 static unsigned long long GetQuotaForOrigin(JNIEnv* env, jobject obj, jstring origin)
     76 {
     77     WTF::String originStr = jstringToWtfString(env, origin);
     78     RefPtr<WebCore::SecurityOrigin> securityOrigin = WebCore::SecurityOrigin::createFromString(originStr);
     79     unsigned long long quota = WebCore::DatabaseTracker::tracker().quotaForOrigin(securityOrigin.get());
     80     return quota;
     81 }
     82 
     83 static unsigned long long GetUsageForOrigin(JNIEnv* env, jobject obj, jstring origin)
     84 {
     85     WTF::String originStr = jstringToWtfString(env, origin);
     86     RefPtr<WebCore::SecurityOrigin> securityOrigin = WebCore::SecurityOrigin::createFromString(originStr);
     87     unsigned long long usage = WebCore::DatabaseTracker::tracker().usageForOrigin(securityOrigin.get());
     88     Vector<WebCore::KURL> manifestUrls;
     89     if (!WebCore::cacheStorage().manifestURLs(&manifestUrls))
     90         return usage;
     91     int size = manifestUrls.size();
     92     for (int i = 0; i < size; ++i) {
     93         RefPtr<WebCore::SecurityOrigin> manifestOrigin = WebCore::SecurityOrigin::create(manifestUrls[i]);
     94         if (manifestOrigin.get() == 0)
     95             continue;
     96         if (manifestOrigin->isSameSchemeHostPort(securityOrigin.get())) {
     97             int64_t cacheSize = 0;
     98             WebCore::cacheStorage().cacheGroupSize(manifestUrls[i].string(), &cacheSize);
     99             usage += cacheSize;
    100         }
    101     }
    102     return usage;
    103 }
    104 
    105 static void SetQuotaForOrigin(JNIEnv* env, jobject obj, jstring origin, unsigned long long quota)
    106 {
    107     WTF::String originStr = jstringToWtfString(env, origin);
    108     RefPtr<WebCore::SecurityOrigin> securityOrigin = WebCore::SecurityOrigin::createFromString(originStr);
    109     WebCore::DatabaseTracker::tracker().setQuota(securityOrigin.get(), quota);
    110 }
    111 
    112 static void DeleteOrigin(JNIEnv* env, jobject obj, jstring origin)
    113 {
    114     WTF::String originStr = jstringToWtfString(env, origin);
    115     RefPtr<WebCore::SecurityOrigin> securityOrigin = WebCore::SecurityOrigin::createFromString(originStr);
    116     WebCore::DatabaseTracker::tracker().deleteOrigin(securityOrigin.get());
    117 
    118     Vector<WebCore::KURL> manifestUrls;
    119     if (!WebCore::cacheStorage().manifestURLs(&manifestUrls))
    120         return;
    121     int size = manifestUrls.size();
    122     for (int i = 0; i < size; ++i) {
    123         RefPtr<WebCore::SecurityOrigin> manifestOrigin = WebCore::SecurityOrigin::create(manifestUrls[i]);
    124         if (manifestOrigin.get() == 0)
    125             continue;
    126         if (manifestOrigin->isSameSchemeHostPort(securityOrigin.get()))
    127             WebCore::cacheStorage().deleteCacheGroup(manifestUrls[i]);
    128     }
    129 }
    130 
    131 static void DeleteAllData(JNIEnv* env, jobject obj)
    132 {
    133     WebCore::DatabaseTracker::tracker().deleteAllDatabases();
    134 
    135     Vector<WebCore::KURL> manifestUrls;
    136     if (!WebCore::cacheStorage().manifestURLs(&manifestUrls))
    137         return;
    138     int size = manifestUrls.size();
    139     for (int i = 0; i < size; ++i)
    140         WebCore::cacheStorage().deleteCacheGroup(manifestUrls[i]);
    141 
    142     // FIXME: this is a workaround for eliminating any DOM Storage data (both
    143     // session and local storage) as there is no functionality inside WebKit at the
    144     // moment to do it. That functionality is a WIP in https://bugs.webkit.org/show_bug.cgi?id=51878
    145     // and when that patch lands and we merge it, we should move towards that approach instead.
    146     WebCore::PageGroup::clearDomStorage();
    147 }
    148 
    149 static void SetAppCacheMaximumSize(JNIEnv* env, jobject obj, unsigned long long size)
    150 {
    151     WebCore::cacheStorage().setMaximumSize(size);
    152 }
    153 
    154 /*
    155  * JNI registration
    156  */
    157 static JNINativeMethod gWebStorageMethods[] = {
    158     { "nativeGetOrigins", "()Ljava/util/Set;",
    159         (void*) GetOrigins },
    160     { "nativeGetUsageForOrigin", "(Ljava/lang/String;)J",
    161         (void*) GetUsageForOrigin },
    162     { "nativeGetQuotaForOrigin", "(Ljava/lang/String;)J",
    163         (void*) GetQuotaForOrigin },
    164     { "nativeSetQuotaForOrigin", "(Ljava/lang/String;J)V",
    165         (void*) SetQuotaForOrigin },
    166     { "nativeDeleteOrigin", "(Ljava/lang/String;)V",
    167         (void*) DeleteOrigin },
    168     { "nativeDeleteAllData", "()V",
    169         (void*) DeleteAllData },
    170     { "nativeSetAppCacheMaximumSize", "(J)V",
    171         (void*) SetAppCacheMaximumSize }
    172 };
    173 
    174 int registerWebStorage(JNIEnv* env)
    175 {
    176 #ifndef NDEBUG
    177     jclass webStorage = env->FindClass("android/webkit/WebStorage");
    178     LOG_ASSERT(webStorage, "Unable to find class android.webkit.WebStorage");
    179     env->DeleteLocalRef(webStorage);
    180 #endif
    181 
    182     return jniRegisterNativeMethods(env, "android/webkit/WebStorage",
    183             gWebStorageMethods, NELEM(gWebStorageMethods));
    184 }
    185 
    186 }
    187 
    188 #endif //ENABLE(DATABASE)
    189