summaryrefslogtreecommitdiffstats
path: root/Source/WebKit/android/jni/AndroidHitTestResult.cpp
blob: a135c42042bb819de8d263a6c93bb4e8c1f3fcb4 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright 2012, The Android Open Source Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define LOG_TAG "AndroidHitTestResult"

#include "config.h"
#include "AndroidHitTestResult.h"

#include "content/address_detector.h"
#include "content/PhoneEmailDetector.h"
#include "android/WebHitTestInfo.h"
#include "Document.h"
#include "Element.h"
#include "Frame.h"
#include "HitTestResult.h"
#include "KURL.h"
#include "LayerAndroid.h"
#include "PlatformString.h"
#include "Range.h"
#include "RenderLayer.h"
#include "RenderLayerBacking.h"
#include "RenderObject.h"
#include "WebCoreJni.h"
#include "WebViewCore.h"

#include <cutils/log.h>
#include <JNIHelp.h>
#include <JNIUtility.h>

namespace android {

using namespace WebCore;

static bool gJniInitialized = false;
static struct {
    jmethodID m_Init;
    jfieldID m_LinkUrl;
    jfieldID m_AnchorText;
    jfieldID m_ImageUrl;
    jfieldID m_AltDisplayString;
    jfieldID m_Title;
    jfieldID m_Editable;
    jfieldID m_TouchRects;
    jfieldID m_TapHighlightColor;
    jfieldID m_EnclosingParentRects;
    jfieldID m_HasFocus;
    jfieldID m_IntentUrl;
} gHitTestGlue;

struct field {
    jclass m_class;
    const char *m_fieldName;
    const char *m_fieldType;
    jfieldID *m_jfield;
};

static void InitJni(JNIEnv* env)
{
    if (gJniInitialized)
        return;

    jclass rectClass = env->FindClass("android/graphics/Rect");
    ALOG_ASSERT(rectClass, "Could not find android/graphics/Rect");
    jclass hitTestClass = env->FindClass("android/webkit/WebViewCore$WebKitHitTest");
    ALOG_ASSERT(hitTestClass, "Could not find android/webkit/WebViewCore$WebKitHitTest");

    gHitTestGlue.m_Init = env->GetMethodID(hitTestClass, "<init>",  "()V");
    ALOG_ASSERT(gHitTestGlue.m_Init, "Could not find init method on android/webkit/WebViewCore$WebKitHitTest");

    field fields[] = {
        { hitTestClass, "mTouchRects", "[Landroid/graphics/Rect;", &gHitTestGlue.m_TouchRects },
        { hitTestClass, "mEditable", "Z", &gHitTestGlue.m_Editable },
        { hitTestClass, "mLinkUrl", "Ljava/lang/String;", &gHitTestGlue.m_LinkUrl },
        { hitTestClass, "mIntentUrl", "Ljava/lang/String;", &gHitTestGlue.m_IntentUrl },
        { hitTestClass, "mAnchorText", "Ljava/lang/String;", &gHitTestGlue.m_AnchorText },
        { hitTestClass, "mImageUrl", "Ljava/lang/String;", &gHitTestGlue.m_ImageUrl },
        { hitTestClass, "mAltDisplayString", "Ljava/lang/String;", &gHitTestGlue.m_AltDisplayString },
        { hitTestClass, "mTitle", "Ljava/lang/String;", &gHitTestGlue.m_Title },
        { hitTestClass, "mTapHighlightColor", "I", &gHitTestGlue.m_TapHighlightColor },
        { hitTestClass, "mEnclosingParentRects", "[Landroid/graphics/Rect;", &gHitTestGlue.m_EnclosingParentRects },
        { hitTestClass, "mHasFocus", "Z", &gHitTestGlue.m_HasFocus },
        {0, 0, 0, 0},
    };

    for (int i = 0; fields[i].m_jfield; i++) {
        field *f = &fields[i];
        jfieldID field = env->GetFieldID(f->m_class, f->m_fieldName, f->m_fieldType);
        ALOG_ASSERT(field, "Can't find %s", f->m_fieldName);
        *(f->m_jfield) = field;
    }

    gJniInitialized = true;
}

AndroidHitTestResult::AndroidHitTestResult(WebViewCore* webViewCore, WebCore::HitTestResult& hitTestResult)
    : m_webViewCore(webViewCore)
    , m_hitTestResult(hitTestResult)
{
    buildHighlightRects();
}

void AndroidHitTestResult::setURLElement(Element* element)
{
    m_hitTestResult.setURLElement(element);
    buildHighlightRects();
}

void AndroidHitTestResult::buildHighlightRects()
{
    m_highlightRects.clear();
    Node* node = m_hitTestResult.URLElement();
    if (!node || !node->renderer())
        node = m_hitTestResult.innerNode();
    if (!node || !node->renderer())
        return;
    if (!WebViewCore::nodeIsClickableOrFocusable(node))
        return;
    Frame* frame = node->document()->frame();
    IntPoint frameOffset = m_webViewCore->convertGlobalContentToFrameContent(IntPoint(), frame);
    RenderObject* renderer = node->renderer();
    Vector<FloatQuad> quads;
    if (renderer->isInline())
        renderer->absoluteFocusRingQuads(quads);
    if (!quads.size())
        renderer->absoluteQuads(quads); // No fancy rings, grab a bounding box
    for (size_t i = 0; i < quads.size(); i++) {
        IntRect boundingBox = quads[i].enclosingBoundingBox();
        boundingBox.move(-frameOffset.x(), -frameOffset.y());
        m_highlightRects.append(boundingBox);
    }
}

void AndroidHitTestResult::searchContentDetectors()
{
    AddressDetector address;
    PhoneEmailDetector phoneEmail;
    Node* node = m_hitTestResult.innerNode();
    if (!node || !node->isTextNode())
        return;
    if (!m_hitTestResult.absoluteLinkURL().isEmpty())
        return;
    WebKit::WebHitTestInfo webHitTest(m_hitTestResult);
    m_searchResult = address.FindTappedContent(webHitTest);
    if (!m_searchResult.valid) {
        m_searchResult = phoneEmail.FindTappedContent(webHitTest);
    }
    if (m_searchResult.valid) {
        m_highlightRects.clear();
        RefPtr<Range> range = (PassRefPtr<Range>) m_searchResult.range;
        range->textRects(m_highlightRects, true);
    }
}

void setStringField(JNIEnv* env, jobject obj, jfieldID field, const String& str)
{
    jstring jstr = wtfStringToJstring(env, str, false);
    env->SetObjectField(obj, field, jstr);
    env->DeleteLocalRef(jstr);
}

void setStringField(JNIEnv* env, jobject obj, jfieldID field, const GURL& url)
{
    jstring jstr = stdStringToJstring(env, url.spec(), false);
    env->SetObjectField(obj, field, jstr);
    env->DeleteLocalRef(jstr);
}

void setRectArray(JNIEnv* env, jobject obj, jfieldID field, Vector<IntRect> &rects)
{
    jobjectArray array = intRectVectorToRectArray(env, rects);
    env->SetObjectField(obj, field, array);
    env->DeleteLocalRef(array);
}

// Some helper macros specific to setting hitTest fields
#define _SET(jtype, jfield, value) env->Set ## jtype ## Field(hitTest, gHitTestGlue.m_ ## jfield, value)
#define SET_BOOL(jfield, value) _SET(Boolean, jfield, value)
#define SET_STRING(jfield, value) setStringField(env, hitTest, gHitTestGlue.m_ ## jfield, value)
#define SET_INT(jfield, value) _SET(Int, jfield, value)

jobject AndroidHitTestResult::createJavaObject(JNIEnv* env)
{
    InitJni(env);
    jclass hitTestClass = env->FindClass("android/webkit/WebViewCore$WebKitHitTest");
    ALOG_ASSERT(hitTestClass, "Could not find android/webkit/WebViewCore$WebKitHitTest");

    jobject hitTest = env->NewObject(hitTestClass, gHitTestGlue.m_Init);
    setRectArray(env, hitTest, gHitTestGlue.m_TouchRects, m_highlightRects);

    Vector<IntRect> rects = enclosingParentRects(m_hitTestResult.innerNode());
    setRectArray(env, hitTest, gHitTestGlue.m_EnclosingParentRects, rects);

    SET_BOOL(Editable, m_hitTestResult.isContentEditable());
    SET_STRING(LinkUrl, m_hitTestResult.absoluteLinkURL().string());
    if (m_searchResult.valid)
        SET_STRING(IntentUrl, m_searchResult.intent_url);
    SET_STRING(ImageUrl, m_hitTestResult.absoluteImageURL().string());
    SET_STRING(AltDisplayString, m_hitTestResult.altDisplayString());
    TextDirection titleTextDirection;
    SET_STRING(Title, m_hitTestResult.title(titleTextDirection));
    if (m_hitTestResult.URLElement()) {
        Element* urlElement = m_hitTestResult.URLElement();
        SET_STRING(AnchorText, urlElement->innerText());
        if (urlElement->renderer()) {
            SET_INT(TapHighlightColor,
                    urlElement->renderer()->style()->tapHighlightColor().rgb());
        }
    }
    Node* focusedNode = m_webViewCore->focusedFrame()->document()->focusedNode();
    SET_BOOL(HasFocus,
             focusedNode == m_hitTestResult.URLElement()
             || focusedNode == m_hitTestResult.innerNode()
             || focusedNode == m_hitTestResult.innerNonSharedNode());

    env->DeleteLocalRef(hitTestClass);

    return hitTest;
}

Vector<IntRect> AndroidHitTestResult::enclosingParentRects(Node* node)
{
    int count = 0;
    int lastX = 0;
    Vector<IntRect> rects;

    while (node && count < 5) {
        RenderObject* render = node->renderer();
        if (!render || render->isBody())
            break;

        IntPoint frameOffset = m_webViewCore->convertGlobalContentToFrameContent(IntPoint(),
                node->document()->frame());
        IntRect rect = render->absoluteBoundingBoxRect();
        rect.move(-frameOffset.x(), -frameOffset.y());
        if (count == 0 || rect.x() != lastX) {
            rects.append(rect);
            lastX = rect.x();
            count++;
        }

        node = node->parentNode();
    }

    return rects;
}

} /* namespace android */