1 /*
2 * Copyright 2006, 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 APPLE COMPUTER, INC. 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 #define LOG_TAG "favicons"
27
28 #include <config.h>
29 #include <wtf/Platform.h>
30
31 #include "WebIconDatabase.h"
32
33 #include "IconDatabase.h"
34 #include "Image.h"
35 #include "IntRect.h"
36 #include "JavaSharedClient.h"
37 #include "jni_utility.h"
38 #include "KURL.h"
39 #include "WebCoreJni.h"
40
41 #include <pthread.h>
42 #include "GraphicsJNI.h"
43 #include <SkBitmap.h>
44 #include <SkImageDecoder.h>
45 #include <SkTemplates.h>
46 #include <utils/misc.h>
47 #include <JNIHelp.h>
48
49 namespace android {
50
webcoreImageToJavaBitmap(JNIEnv * env,WebCore::Image * icon)51 jobject webcoreImageToJavaBitmap(JNIEnv* env, WebCore::Image* icon)
52 {
53 if (!icon)
54 return NULL;
55 SkBitmap bm;
56 WebCore::SharedBuffer* buffer = icon->data();
57 if (!buffer || !SkImageDecoder::DecodeMemory(buffer->data(), buffer->size(),
58 &bm, SkBitmap::kNo_Config,
59 SkImageDecoder::kDecodePixels_Mode))
60 return NULL;
61
62 return GraphicsJNI::createBitmap(env, new SkBitmap(bm), false, NULL);
63 }
64
65 static WebIconDatabase* gIconDatabaseClient = new WebIconDatabase();
66
67 // XXX: Called by the IconDatabase thread
dispatchDidAddIconForPageURL(const WebCore::String & pageURL)68 void WebIconDatabase::dispatchDidAddIconForPageURL(const WebCore::String& pageURL)
69 {
70 // If there are no clients currently, drop this message.
71 if (mClients.size() == 0)
72 return;
73
74 // Attempt to attach to the current vm.
75 JavaVM* vm = JSC::Bindings::getJavaVM();
76 JavaVMAttachArgs args;
77
78 args.version = JNI_VERSION_1_4;
79 args.name = "IconDatabase";
80 args.group = NULL;
81
82 JNIEnv* env;
83 bool threadIsAttached = true;
84 if (vm->AttachCurrentThread(&env, (void*) &args) != JNI_OK) {
85 LOGE("Could not attach IconDatabase thread to the VM");
86 threadIsAttached = false;
87 }
88
89 mNotificationsMutex.lock();
90 mNotifications.append(pageURL);
91 if (!mDeliveryRequested) {
92 if (threadIsAttached) {
93 mDeliveryRequested = true;
94 JavaSharedClient::EnqueueFunctionPtr(DeliverNotifications, this);
95 }
96 }
97 mNotificationsMutex.unlock();
98 }
99
100 // Called in the WebCore thread
RegisterForIconNotification(WebIconDatabaseClient * client)101 void WebIconDatabase::RegisterForIconNotification(WebIconDatabaseClient* client)
102 {
103 gIconDatabaseClient->mClientsMutex.lock();
104 gIconDatabaseClient->mClients.append(client);
105 gIconDatabaseClient->mClientsMutex.unlock();
106 }
107
108 // Called in the WebCore thread
UnregisterForIconNotification(WebIconDatabaseClient * client)109 void WebIconDatabase::UnregisterForIconNotification(WebIconDatabaseClient* client)
110 {
111 WebIconDatabase* db = gIconDatabaseClient;
112 db->mClientsMutex.lock();
113 for (unsigned i = 0; i < db->mClients.size(); ++i) {
114 if (db->mClients[i] == client) {
115 db->mClients.remove(i);
116 break;
117 }
118 }
119 db->mClientsMutex.unlock();
120 }
121
122 // Called in the WebCore thread
DeliverNotifications(void * v)123 void WebIconDatabase::DeliverNotifications(void* v)
124 {
125 ASSERT(v);
126 ((WebIconDatabase*)v)->deliverNotifications();
127 }
128
129 // Called in the WebCore thread
deliverNotifications()130 void WebIconDatabase::deliverNotifications()
131 {
132 ASSERT(mDeliveryRequested);
133
134 // Swap the notifications queue
135 Vector<WebCore::String> queue;
136 mNotificationsMutex.lock();
137 queue.swap(mNotifications);
138 mDeliveryRequested = false;
139 mNotificationsMutex.unlock();
140
141 // Swap the clients queue
142 Vector<WebIconDatabaseClient*> clients;
143 mClientsMutex.lock();
144 clients.swap(mClients);
145 mClientsMutex.unlock();
146
147 for (unsigned i = 0; i < queue.size(); ++i) {
148 for (unsigned j = 0; j < clients.size(); ++j) {
149 clients[j]->didAddIconForPageUrl(queue[i]);
150 }
151 }
152 }
153
Open(JNIEnv * env,jobject obj,jstring path)154 static void Open(JNIEnv* env, jobject obj, jstring path)
155 {
156 WebCore::IconDatabase* iconDb = WebCore::iconDatabase();
157 if (iconDb->isOpen())
158 return;
159 iconDb->setEnabled(true);
160 iconDb->setClient(gIconDatabaseClient);
161 LOG_ASSERT(path, "No path given to nativeOpen");
162 WebCore::String pathStr = to_string(env, path);
163 LOGV("Opening WebIconDatabase file '%s'", pathStr.latin1().data());
164 bool res = iconDb->open(pathStr);
165 if (!res)
166 LOGE("Open failed!");
167 }
168
Close(JNIEnv * env,jobject obj)169 static void Close(JNIEnv* env, jobject obj)
170 {
171 WebCore::iconDatabase()->close();
172 }
173
RemoveAllIcons(JNIEnv * env,jobject obj)174 static void RemoveAllIcons(JNIEnv* env, jobject obj)
175 {
176 LOGV("Removing all icons");
177 WebCore::iconDatabase()->removeAllIcons();
178 }
179
IconForPageUrl(JNIEnv * env,jobject obj,jstring url)180 static jobject IconForPageUrl(JNIEnv* env, jobject obj, jstring url)
181 {
182 LOG_ASSERT(url, "No url given to iconForPageUrl");
183 WebCore::String urlStr = to_string(env, url);
184
185 WebCore::Image* icon = WebCore::iconDatabase()->iconForPageURL(urlStr,
186 WebCore::IntSize(16, 16));
187 LOGV("Retrieving icon for '%s' %p", urlStr.latin1().data(), icon);
188 return webcoreImageToJavaBitmap(env, icon);
189 }
190
RetainIconForPageUrl(JNIEnv * env,jobject obj,jstring url)191 static void RetainIconForPageUrl(JNIEnv* env, jobject obj, jstring url)
192 {
193 LOG_ASSERT(url, "No url given to retainIconForPageUrl");
194 WebCore::String urlStr = to_string(env, url);
195
196 LOGV("Retaining icon for '%s'", urlStr.latin1().data());
197 WebCore::iconDatabase()->retainIconForPageURL(urlStr);
198 }
199
ReleaseIconForPageUrl(JNIEnv * env,jobject obj,jstring url)200 static void ReleaseIconForPageUrl(JNIEnv* env, jobject obj, jstring url)
201 {
202 LOG_ASSERT(url, "No url given to releaseIconForPageUrl");
203 WebCore::String urlStr = to_string(env, url);
204
205 LOGV("Releasing icon for '%s'", urlStr.latin1().data());
206 WebCore::iconDatabase()->releaseIconForPageURL(urlStr);
207 }
208
209 /*
210 * JNI registration
211 */
212 static JNINativeMethod gWebIconDatabaseMethods[] = {
213 { "nativeOpen", "(Ljava/lang/String;)V",
214 (void*) Open },
215 { "nativeClose", "()V",
216 (void*) Close },
217 { "nativeRemoveAllIcons", "()V",
218 (void*) RemoveAllIcons },
219 { "nativeIconForPageUrl", "(Ljava/lang/String;)Landroid/graphics/Bitmap;",
220 (void*) IconForPageUrl },
221 { "nativeRetainIconForPageUrl", "(Ljava/lang/String;)V",
222 (void*) RetainIconForPageUrl },
223 { "nativeReleaseIconForPageUrl", "(Ljava/lang/String;)V",
224 (void*) ReleaseIconForPageUrl }
225 };
226
register_webicondatabase(JNIEnv * env)227 int register_webicondatabase(JNIEnv* env)
228 {
229 jclass webIconDB = env->FindClass("android/webkit/WebIconDatabase");
230 LOG_ASSERT(webIconDB, "Unable to find class android.webkit.WebIconDatabase");
231
232 return jniRegisterNativeMethods(env, "android/webkit/WebIconDatabase",
233 gWebIconDatabaseMethods, NELEM(gWebIconDatabaseMethods));
234 }
235
236 }
237