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
GetOrigins(JNIEnv * env,jobject obj)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
GetQuotaForOrigin(JNIEnv * env,jobject obj,jstring origin)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
GetUsageForOrigin(JNIEnv * env,jobject obj,jstring origin)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
SetQuotaForOrigin(JNIEnv * env,jobject obj,jstring origin,unsigned long long quota)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
DeleteOrigin(JNIEnv * env,jobject obj,jstring origin)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
DeleteAllData(JNIEnv * env,jobject obj)131 static void DeleteAllData(JNIEnv* env, jobject obj)
132 {
133 // delete WebSQL database
134 WebCore::DatabaseTracker::tracker().deleteAllDatabases();
135 // delete AppCache
136 WebCore::cacheStorage().deleteAllEntries();
137
138 // FIXME: this is a workaround for eliminating any DOM Storage data (both
139 // session and local storage) as there is no functionality inside WebKit at the
140 // moment to do it. That functionality is a WIP in https://bugs.webkit.org/show_bug.cgi?id=51878
141 // and when that patch lands and we merge it, we should move towards that approach instead.
142 WebCore::PageGroup::clearDomStorage();
143 }
144
SetAppCacheMaximumSize(JNIEnv * env,jobject obj,unsigned long long size)145 static void SetAppCacheMaximumSize(JNIEnv* env, jobject obj, unsigned long long size)
146 {
147 WebCore::cacheStorage().setMaximumSize(size);
148 }
149
150 /*
151 * JNI registration
152 */
153 static JNINativeMethod gWebStorageMethods[] = {
154 { "nativeGetOrigins", "()Ljava/util/Set;",
155 (void*) GetOrigins },
156 { "nativeGetUsageForOrigin", "(Ljava/lang/String;)J",
157 (void*) GetUsageForOrigin },
158 { "nativeGetQuotaForOrigin", "(Ljava/lang/String;)J",
159 (void*) GetQuotaForOrigin },
160 { "nativeSetQuotaForOrigin", "(Ljava/lang/String;J)V",
161 (void*) SetQuotaForOrigin },
162 { "nativeDeleteOrigin", "(Ljava/lang/String;)V",
163 (void*) DeleteOrigin },
164 { "nativeDeleteAllData", "()V",
165 (void*) DeleteAllData },
166 { "nativeSetAppCacheMaximumSize", "(J)V",
167 (void*) SetAppCacheMaximumSize }
168 };
169
registerWebStorage(JNIEnv * env)170 int registerWebStorage(JNIEnv* env)
171 {
172 #ifndef NDEBUG
173 jclass webStorage = env->FindClass("android/webkit/WebStorageClassic");
174 ALOG_ASSERT(webStorage, "Unable to find class android.webkit.WebStorageClassic");
175 env->DeleteLocalRef(webStorage);
176 #endif
177
178 return jniRegisterNativeMethods(env, "android/webkit/WebStorageClassic",
179 gWebStorageMethods, NELEM(gWebStorageMethods));
180 }
181
182 }
183
184 #endif //ENABLE(DATABASE)
185