summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/android/jni/WebIconDatabase.cpp
blob: 2d5c39c7e6e303537bf153276a7f1b963a8201d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#define LOG_TAG "favicons"

#include <config.h>
#include <wtf/Platform.h>

#include "WebCoreJni.h"
#include "WebIconDatabase.h"

#include "IconDatabase.h"
#include "Image.h"
#include "IntRect.h"
#include "JavaSharedClient.h"
#include "KURL.h"

#include <pthread.h>
#include "GraphicsJNI.h"
#include <SkBitmap.h>
#include <SkImageDecoder.h>
#include <SkTemplates.h>
#undef LOG
#include <utils/Log.h>
#include <utils/misc.h>
#include <JNIHelp.h>

namespace android {

jobject webcoreImageToJavaBitmap(JNIEnv* env, WebCore::Image* icon)
{
    if (!icon)
        return NULL;
    SkBitmap bm;
    WebCore::SharedBuffer* buffer = icon->data();
    if (!buffer || !SkImageDecoder::DecodeMemory(buffer->data(), buffer->size(),
                                                 &bm, SkBitmap::kNo_Config,
                                            SkImageDecoder::kDecodePixels_Mode))
        return NULL;

    return GraphicsJNI::createBitmap(env, new SkBitmap(bm), false, NULL);
}

static WebIconDatabase* gIconDatabaseClient = new WebIconDatabase();

// XXX: Called by the IconDatabase thread
void WebIconDatabase::dispatchDidAddIconForPageURL(const WebCore::String& pageURL)
{
    // Attempt to attach to the current vm.
    JavaVM* vm = WebCoreJni::getJavaVM();
    JavaVMAttachArgs args;

    args.version = JNI_VERSION_1_4;
    args.name = "IconDatabase";
    args.group = NULL;

    JNIEnv* env;
    bool threadIsAttached = true;
    if (vm->AttachCurrentThread(&env, (void*) &args) != JNI_OK) {
        LOGE("Could not attach IconDatabase thread to the VM");
        threadIsAttached = false;
    }

    mNotificationsMutex.lock();
    mNotifications.append(pageURL);
    if (!mDeliveryRequested) {
        if (threadIsAttached) {
            mDeliveryRequested = true;
            WebCore::JavaSharedClient::EnqueueFunctionPtr(DeliverNotifications, this);
        }
    }
    mNotificationsMutex.unlock();
}

// Called in the WebCore thread
void WebIconDatabase::RegisterForIconNotification(WebIconDatabaseClient* client)
{
    gIconDatabaseClient->mClientsMutex.lock();
    gIconDatabaseClient->mClients.append(client);
    gIconDatabaseClient->mClientsMutex.unlock();
}

// Called in the WebCore thread
void WebIconDatabase::UnregisterForIconNotification(WebIconDatabaseClient* client)
{
    WebIconDatabase* db = gIconDatabaseClient;
    db->mClientsMutex.lock();
    for (unsigned i = 0; i < db->mClients.size(); ++i) {
        if (db->mClients[i] == client) {
            db->mClients.remove(i);
            break;
        }
    }
    db->mClientsMutex.unlock();
}

// Called in the WebCore thread
void WebIconDatabase::DeliverNotifications(void* v)
{
    ASSERT(v);
    ((WebIconDatabase*)v)->deliverNotifications();
}

// Called in the WebCore thread
void WebIconDatabase::deliverNotifications()
{
    ASSERT(mDeliveryRequested);

    // Swap the notifications queue
    Vector<WebCore::String> queue;
    mNotificationsMutex.lock();
    queue.swap(mNotifications);
    mDeliveryRequested = false;
    mNotificationsMutex.unlock();

    // Swap the clients queue
    Vector<WebIconDatabaseClient*> clients;
    mClientsMutex.lock();
    clients.swap(mClients);
    mClientsMutex.unlock();

    for (unsigned i = 0; i < queue.size(); ++i) {
        for (unsigned j = 0; j < clients.size(); ++j) {
            clients[j]->didAddIconForPageUrl(queue[i]);
        }
    }
}

static void Open(JNIEnv* env, jobject obj, jstring path)
{
    WebCore::IconDatabase* iconDb = WebCore::iconDatabase();
    if (iconDb->isOpen())
        return;
    iconDb->setEnabled(true);
    iconDb->setClient(gIconDatabaseClient);
    LOG_ASSERT(path, "No path given to nativeOpen");
    const char* pathStr = env->GetStringUTFChars(path, NULL);
    LOG_ASSERT(pathStr, "GetStringUTFChars failed for the path");
    LOGV("Opening WebIconDatabase file '%s'", pathStr);
    bool res = iconDb->open(pathStr);
    if (!res)
        LOGE("Open failed!");
    env->ReleaseStringUTFChars(path, pathStr);
}

static void Close(JNIEnv* env, jobject obj)
{
    WebCore::iconDatabase()->close();
}

static void RemoveAllIcons(JNIEnv* env, jobject obj)
{
    LOGV("Removing all icons");
    WebCore::iconDatabase()->removeAllIcons();
}

static jobject IconForPageUrl(JNIEnv* env, jobject obj, jstring url)
{
    LOG_ASSERT(url, "No url given to iconForPageUrl");
    const char* urlStr = env->GetStringUTFChars(url, NULL);
    LOG_ASSERT(urlStr, "GetStringUTFChars failed for url");

    WebCore::Image* icon = WebCore::iconDatabase()->iconForPageURL(urlStr,
            WebCore::IntSize(16, 16));
    LOGV("Retrieving icon for '%s' %p", urlStr, icon);
    env->ReleaseStringUTFChars(url, urlStr);
    return webcoreImageToJavaBitmap(env, icon);
}

static void RetainIconForPageUrl(JNIEnv* env, jobject obj, jstring url)
{
    LOG_ASSERT(url, "No url given to retainIconForPageUrl");
    const char* urlStr = env->GetStringUTFChars(url, NULL);
    LOG_ASSERT(urlStr, "GetStringUTFChars failed for url");

    LOGV("Retaining icon for '%s'", urlStr);
    WebCore::iconDatabase()->retainIconForPageURL(urlStr);
    env->ReleaseStringUTFChars(url, urlStr);
}

static void ReleaseIconForPageUrl(JNIEnv* env, jobject obj, jstring url)
{
    LOG_ASSERT(url, "No url given to releaseIconForPageUrl");
    const char* urlStr = env->GetStringUTFChars(url, NULL);
    LOG_ASSERT(urlStr, "GetStringUTFChars failed for url");

    LOGV("Releasing icon for '%s'", urlStr);
    WebCore::iconDatabase()->releaseIconForPageURL(urlStr);
    env->ReleaseStringUTFChars(url, urlStr);
}

/*
 * JNI registration
 */
static JNINativeMethod gWebIconDatabaseMethods[] = {
    { "nativeOpen", "(Ljava/lang/String;)V",
        (void*) Open },
    { "nativeClose", "()V",
        (void*) Close },
    { "nativeRemoveAllIcons", "()V",
        (void*) RemoveAllIcons },
    { "nativeIconForPageUrl", "(Ljava/lang/String;)Landroid/graphics/Bitmap;",
        (void*) IconForPageUrl },
    { "nativeRetainIconForPageUrl", "(Ljava/lang/String;)V",
        (void*) RetainIconForPageUrl },
    { "nativeReleaseIconForPageUrl", "(Ljava/lang/String;)V",
        (void*) ReleaseIconForPageUrl }
};

int register_webicondatabase(JNIEnv* env)
{
    jclass webIconDB = env->FindClass("android/webkit/WebIconDatabase");
    LOG_ASSERT(webIconDB, "Unable to find class android.webkit.WebIconDatabase");

    return jniRegisterNativeMethods(env, "android/webkit/WebIconDatabase",
            gWebIconDatabaseMethods, NELEM(gWebIconDatabaseMethods));
}

}