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 mNotificationsMutex.lock();
75 mNotifications.append(pageURL);
76 if (!mDeliveryRequested) {
77 mDeliveryRequested = true;
78 JavaSharedClient::EnqueueFunctionPtr(DeliverNotifications, this);
79 }
80 mNotificationsMutex.unlock();
81 }
82
83 // Called in the WebCore thread
RegisterForIconNotification(WebIconDatabaseClient * client)84 void WebIconDatabase::RegisterForIconNotification(WebIconDatabaseClient* client)
85 {
86 gIconDatabaseClient->mClientsMutex.lock();
87 gIconDatabaseClient->mClients.append(client);
88 gIconDatabaseClient->mClientsMutex.unlock();
89 }
90
91 // Called in the WebCore thread
UnregisterForIconNotification(WebIconDatabaseClient * client)92 void WebIconDatabase::UnregisterForIconNotification(WebIconDatabaseClient* client)
93 {
94 WebIconDatabase* db = gIconDatabaseClient;
95 db->mClientsMutex.lock();
96 for (unsigned i = 0; i < db->mClients.size(); ++i) {
97 if (db->mClients[i] == client) {
98 db->mClients.remove(i);
99 break;
100 }
101 }
102 db->mClientsMutex.unlock();
103 }
104
105 // Called in the WebCore thread
DeliverNotifications(void * v)106 void WebIconDatabase::DeliverNotifications(void* v)
107 {
108 ASSERT(v);
109 ((WebIconDatabase*)v)->deliverNotifications();
110 }
111
112 // Called in the WebCore thread
deliverNotifications()113 void WebIconDatabase::deliverNotifications()
114 {
115 ASSERT(mDeliveryRequested);
116
117 // Swap the notifications queue
118 Vector<WebCore::String> queue;
119 mNotificationsMutex.lock();
120 queue.swap(mNotifications);
121 mDeliveryRequested = false;
122 mNotificationsMutex.unlock();
123
124 // Swap the clients queue
125 Vector<WebIconDatabaseClient*> clients;
126 mClientsMutex.lock();
127 clients.swap(mClients);
128 mClientsMutex.unlock();
129
130 for (unsigned i = 0; i < queue.size(); ++i) {
131 for (unsigned j = 0; j < clients.size(); ++j) {
132 clients[j]->didAddIconForPageUrl(queue[i]);
133 }
134 }
135 }
136
Open(JNIEnv * env,jobject obj,jstring path)137 static void Open(JNIEnv* env, jobject obj, jstring path)
138 {
139 WebCore::IconDatabase* iconDb = WebCore::iconDatabase();
140 if (iconDb->isOpen())
141 return;
142 iconDb->setEnabled(true);
143 iconDb->setClient(gIconDatabaseClient);
144 LOG_ASSERT(path, "No path given to nativeOpen");
145 WebCore::String pathStr = to_string(env, path);
146 LOGV("Opening WebIconDatabase file '%s'", pathStr.latin1().data());
147 bool res = iconDb->open(pathStr);
148 if (!res)
149 LOGE("Open failed!");
150 }
151
Close(JNIEnv * env,jobject obj)152 static void Close(JNIEnv* env, jobject obj)
153 {
154 WebCore::iconDatabase()->close();
155 }
156
RemoveAllIcons(JNIEnv * env,jobject obj)157 static void RemoveAllIcons(JNIEnv* env, jobject obj)
158 {
159 LOGV("Removing all icons");
160 WebCore::iconDatabase()->removeAllIcons();
161 }
162
IconForPageUrl(JNIEnv * env,jobject obj,jstring url)163 static jobject IconForPageUrl(JNIEnv* env, jobject obj, jstring url)
164 {
165 LOG_ASSERT(url, "No url given to iconForPageUrl");
166 WebCore::String urlStr = to_string(env, url);
167
168 WebCore::Image* icon = WebCore::iconDatabase()->iconForPageURL(urlStr,
169 WebCore::IntSize(16, 16));
170 LOGV("Retrieving icon for '%s' %p", urlStr.latin1().data(), icon);
171 return webcoreImageToJavaBitmap(env, icon);
172 }
173
RetainIconForPageUrl(JNIEnv * env,jobject obj,jstring url)174 static void RetainIconForPageUrl(JNIEnv* env, jobject obj, jstring url)
175 {
176 LOG_ASSERT(url, "No url given to retainIconForPageUrl");
177 WebCore::String urlStr = to_string(env, url);
178
179 LOGV("Retaining icon for '%s'", urlStr.latin1().data());
180 WebCore::iconDatabase()->retainIconForPageURL(urlStr);
181 }
182
ReleaseIconForPageUrl(JNIEnv * env,jobject obj,jstring url)183 static void ReleaseIconForPageUrl(JNIEnv* env, jobject obj, jstring url)
184 {
185 LOG_ASSERT(url, "No url given to releaseIconForPageUrl");
186 WebCore::String urlStr = to_string(env, url);
187
188 LOGV("Releasing icon for '%s'", urlStr.latin1().data());
189 WebCore::iconDatabase()->releaseIconForPageURL(urlStr);
190 }
191
192 /*
193 * JNI registration
194 */
195 static JNINativeMethod gWebIconDatabaseMethods[] = {
196 { "nativeOpen", "(Ljava/lang/String;)V",
197 (void*) Open },
198 { "nativeClose", "()V",
199 (void*) Close },
200 { "nativeRemoveAllIcons", "()V",
201 (void*) RemoveAllIcons },
202 { "nativeIconForPageUrl", "(Ljava/lang/String;)Landroid/graphics/Bitmap;",
203 (void*) IconForPageUrl },
204 { "nativeRetainIconForPageUrl", "(Ljava/lang/String;)V",
205 (void*) RetainIconForPageUrl },
206 { "nativeReleaseIconForPageUrl", "(Ljava/lang/String;)V",
207 (void*) ReleaseIconForPageUrl }
208 };
209
register_webicondatabase(JNIEnv * env)210 int register_webicondatabase(JNIEnv* env)
211 {
212 jclass webIconDB = env->FindClass("android/webkit/WebIconDatabase");
213 LOG_ASSERT(webIconDB, "Unable to find class android.webkit.WebIconDatabase");
214
215 return jniRegisterNativeMethods(env, "android/webkit/WebIconDatabase",
216 gWebIconDatabaseMethods, NELEM(gWebIconDatabaseMethods));
217 }
218
219 }
220