2 * Copyright 2007, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #define LOG_TAG "webviewglue"
30 #include "AndroidAnimation.h"
31 #include "AndroidLog.h"
32 #include "BaseLayerAndroid.h"
33 #include "CachedFrame.h"
34 #include "CachedNode.h"
35 #include "CachedRoot.h"
36 #include "DrawExtra.h"
37 #include "FindCanvas.h"
39 #include "GraphicsJNI.h"
40 #include "HTMLInputElement.h"
43 #include "LayerAndroid.h"
45 #include "utils/Functor.h"
46 #include "private/hwui/DrawGlInfo.h"
47 #include "PlatformGraphicsContext.h"
48 #include "PlatformString.h"
49 #include "ScrollableLayerAndroid.h"
50 #include "SelectText.h"
52 #include "SkDumpCanvas.h"
53 #include "SkPicture.h"
56 #ifdef ANDROID_INSTRUMENT
57 #include "TimeCounter.h"
59 #include "TilesManager.h"
60 #include "WebCoreJni.h"
61 #include "WebRequestContext.h"
62 #include "WebViewCore.h"
63 #include "android_graphics.h"
65 #ifdef GET_NATIVE_VIEW
66 #undef GET_NATIVE_VIEW
69 #define GET_NATIVE_VIEW(env, obj) ((WebView*)env->GetIntField(obj, gWebViewField))
71 #include <JNIUtility.h>
74 #include <ui/KeycodeLabels.h>
75 #include <wtf/text/AtomicString.h>
76 #include <wtf/text/CString.h>
78 // Free as much as we possible can
79 #define TRIM_MEMORY_COMPLETE 80
80 // Free a lot (all textures gone)
81 #define TRIM_MEMORY_MODERATE 60
82 // More moderate free (keep bare minimum to restore quickly-ish - possibly clear all textures)
83 #define TRIM_MEMORY_BACKGROUND 40
84 // Moderate free (clear cached tiles, keep visible ones)
85 #define TRIM_MEMORY_UI_HIDDEN 20
86 // Duration to show the pressed cursor ring
87 #define PRESSED_STATE_DURATION 400
91 static jfieldID gWebViewField;
93 //-------------------------------------
95 static jmethodID GetJMethod(JNIEnv* env, jclass clazz, const char name[], const char signature[])
97 jmethodID m = env->GetMethodID(clazz, name, signature);
98 LOG_ASSERT(m, "Could not find method %s", name);
102 //-------------------------------------
103 // This class provides JNI for making calls into native code from the UI side
104 // of the multi-threaded WebView.
108 enum FrameCachePermission {
113 enum DrawExtras { // keep this in sync with WebView.java
116 DrawExtrasSelection = 2,
117 DrawExtrasCursorRing = 3
122 jmethodID m_calcOurContentVisibleRectF;
123 jmethodID m_overrideLoading;
124 jmethodID m_scrollBy;
125 jmethodID m_sendMoveFocus;
126 jmethodID m_sendMoveMouse;
127 jmethodID m_sendMoveMouseIfLatest;
128 jmethodID m_sendMotionUp;
129 jmethodID m_domChangedFocus;
130 jmethodID m_getScaledMaxXScroll;
131 jmethodID m_getScaledMaxYScroll;
132 jmethodID m_getVisibleRect;
133 jmethodID m_rebuildWebTextView;
134 jmethodID m_viewInvalidate;
135 jmethodID m_viewInvalidateRect;
136 jmethodID m_postInvalidateDelayed;
137 jmethodID m_pageSwapCallback;
138 jmethodID m_inFullScreenMode;
141 jmethodID m_rectWidth;
142 jmethodID m_rectHeight;
143 jfieldID m_rectFLeft;
145 jmethodID m_rectFWidth;
146 jmethodID m_rectFHeight;
147 jmethodID m_getTextHandleScale;
148 AutoJObject object(JNIEnv* env) {
149 return getRealObject(env, m_obj);
153 WebView(JNIEnv* env, jobject javaWebView, int viewImpl, WTF::String drawableDir) :
154 m_ring((WebViewCore*) viewImpl)
156 jclass clazz = env->FindClass("android/webkit/WebView");
157 // m_javaGlue = new JavaGlue;
158 m_javaGlue.m_obj = env->NewWeakGlobalRef(javaWebView);
159 m_javaGlue.m_scrollBy = GetJMethod(env, clazz, "setContentScrollBy", "(IIZ)Z");
160 m_javaGlue.m_calcOurContentVisibleRectF = GetJMethod(env, clazz, "calcOurContentVisibleRectF", "(Landroid/graphics/RectF;)V");
161 m_javaGlue.m_overrideLoading = GetJMethod(env, clazz, "overrideLoading", "(Ljava/lang/String;)V");
162 m_javaGlue.m_sendMoveFocus = GetJMethod(env, clazz, "sendMoveFocus", "(II)V");
163 m_javaGlue.m_sendMoveMouse = GetJMethod(env, clazz, "sendMoveMouse", "(IIII)V");
164 m_javaGlue.m_sendMoveMouseIfLatest = GetJMethod(env, clazz, "sendMoveMouseIfLatest", "(ZZ)V");
165 m_javaGlue.m_sendMotionUp = GetJMethod(env, clazz, "sendMotionUp", "(IIIII)V");
166 m_javaGlue.m_domChangedFocus = GetJMethod(env, clazz, "domChangedFocus", "()V");
167 m_javaGlue.m_getScaledMaxXScroll = GetJMethod(env, clazz, "getScaledMaxXScroll", "()I");
168 m_javaGlue.m_getScaledMaxYScroll = GetJMethod(env, clazz, "getScaledMaxYScroll", "()I");
169 m_javaGlue.m_getVisibleRect = GetJMethod(env, clazz, "sendOurVisibleRect", "()Landroid/graphics/Rect;");
170 m_javaGlue.m_rebuildWebTextView = GetJMethod(env, clazz, "rebuildWebTextView", "()V");
171 m_javaGlue.m_viewInvalidate = GetJMethod(env, clazz, "viewInvalidate", "()V");
172 m_javaGlue.m_viewInvalidateRect = GetJMethod(env, clazz, "viewInvalidate", "(IIII)V");
173 m_javaGlue.m_postInvalidateDelayed = GetJMethod(env, clazz,
174 "viewInvalidateDelayed", "(JIIII)V");
175 m_javaGlue.m_pageSwapCallback = GetJMethod(env, clazz, "pageSwapCallback", "()V");
176 m_javaGlue.m_inFullScreenMode = GetJMethod(env, clazz, "inFullScreenMode", "()Z");
177 m_javaGlue.m_getTextHandleScale = GetJMethod(env, clazz, "getTextHandleScale", "()F");
178 env->DeleteLocalRef(clazz);
180 jclass rectClass = env->FindClass("android/graphics/Rect");
181 LOG_ASSERT(rectClass, "Could not find Rect class");
182 m_javaGlue.m_rectLeft = env->GetFieldID(rectClass, "left", "I");
183 m_javaGlue.m_rectTop = env->GetFieldID(rectClass, "top", "I");
184 m_javaGlue.m_rectWidth = GetJMethod(env, rectClass, "width", "()I");
185 m_javaGlue.m_rectHeight = GetJMethod(env, rectClass, "height", "()I");
186 env->DeleteLocalRef(rectClass);
188 jclass rectClassF = env->FindClass("android/graphics/RectF");
189 LOG_ASSERT(rectClassF, "Could not find RectF class");
190 m_javaGlue.m_rectFLeft = env->GetFieldID(rectClassF, "left", "F");
191 m_javaGlue.m_rectFTop = env->GetFieldID(rectClassF, "top", "F");
192 m_javaGlue.m_rectFWidth = GetJMethod(env, rectClassF, "width", "()F");
193 m_javaGlue.m_rectFHeight = GetJMethod(env, rectClassF, "height", "()F");
194 env->DeleteLocalRef(rectClassF);
196 env->SetIntField(javaWebView, gWebViewField, (jint)this);
197 m_viewImpl = (WebViewCore*) viewImpl;
201 m_heightCanMeasure = false;
204 m_ringAnimationEnd = 0;
207 m_buttonSkin = drawableDir.isEmpty() ? 0 : new RenderSkinButton(drawableDir);
208 #if USE(ACCELERATED_COMPOSITING)
209 m_glWebViewState = 0;
210 m_pageSwapCallbackRegistered = false;
216 if (m_javaGlue.m_obj)
218 JNIEnv* env = JSC::Bindings::getJNIEnv();
219 env->DeleteWeakGlobalRef(m_javaGlue.m_obj);
220 m_javaGlue.m_obj = 0;
222 #if USE(ACCELERATED_COMPOSITING)
223 // We must remove the m_glWebViewState prior to deleting m_baseLayer. If we
224 // do not remove it here, we risk having BaseTiles trying to paint using a
225 // deallocated base layer.
228 delete m_frameCacheUI;
229 delete m_navPictureUI;
230 SkSafeUnref(m_baseLayer);
231 delete m_glDrawFunctor;
237 #if USE(ACCELERATED_COMPOSITING)
238 delete m_glWebViewState;
239 m_glWebViewState = 0;
243 WebViewCore* getWebViewCore() const {
247 float getTextHandleScale()
249 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
250 JNIEnv* env = JSC::Bindings::getJNIEnv();
251 AutoJObject javaObject = m_javaGlue.object(env);
252 if (!javaObject.get())
254 float result = env->CallFloatMethod(javaObject.get(), m_javaGlue.m_getTextHandleScale);
259 void updateSelectionHandles()
263 // Adjust for device density & scale
264 m_selectText.updateHandleScale(getTextHandleScale());
267 // removes the cursor altogether (e.g., when going to a new page)
270 CachedRoot* root = getFrameCache(AllowNewer);
274 m_viewImpl->m_hasCursorBounds = false;
279 // leaves the cursor where it is, but suppresses drawing it
282 CachedRoot* root = getFrameCache(AllowNewer);
290 void hideCursor(CachedRoot* root)
292 DBG_NAV_LOG("inner");
293 m_viewImpl->m_hasCursorBounds = false;
300 CachedRoot* root = getFrameCache(DontAllowNewer);
302 root->mDebug.print();
306 // Traverse our stored array of buttons that are in our picture, and update
307 // their subpictures according to their current state.
308 // Called from the UI thread. This is the one place in the UI thread where we
309 // access the buttons stored in the WebCore thread.
310 // hasFocus keeps track of whether the WebView has focus && windowFocus.
311 // If not, we do not want to draw the button in a selected or pressed state
312 void nativeRecordButtons(bool hasFocus, bool pressed, bool invalidate)
314 bool cursorIsOnButton = false;
315 const CachedFrame* cachedFrame;
316 const CachedNode* cachedCursor = 0;
317 // Lock the mutex, since we now share with the WebCore thread.
318 m_viewImpl->gButtonMutex.lock();
319 if (m_viewImpl->m_buttons.size() && m_buttonSkin) {
320 // FIXME: In a future change, we should keep track of whether the selection
321 // has changed to short circuit (note that we would still need to update
322 // if we received new buttons from the WebCore thread).
323 WebCore::Node* cursor = 0;
324 CachedRoot* root = getFrameCache(DontAllowNewer);
326 cachedCursor = root->currentCursor(&cachedFrame);
328 cursor = (WebCore::Node*) cachedCursor->nodePointer();
331 // Traverse the array, and update each button, depending on whether it
333 Container* end = m_viewImpl->m_buttons.end();
334 for (Container* ptr = m_viewImpl->m_buttons.begin(); ptr != end; ptr++) {
335 RenderSkinAndroid::State state = RenderSkinAndroid::kNormal;
336 if (ptr->matches(cursor)) {
337 cursorIsOnButton = true;
338 // If the WebView is out of focus/window focus, set the state to
339 // normal, but still keep track of the fact that the selected is a
342 if (pressed || m_ring.m_isPressed)
343 state = RenderSkinAndroid::kPressed;
344 else if (SkTime::GetMSecs() < m_ringAnimationEnd)
345 state = RenderSkinAndroid::kFocused;
348 ptr->updateFocusState(state, m_buttonSkin);
351 m_viewImpl->gButtonMutex.unlock();
352 if (invalidate && cachedCursor && cursorIsOnButton) {
353 const WebCore::IntRect& b = cachedCursor->bounds(cachedFrame);
354 viewInvalidateRect(b.x(), b.y(), b.maxX(), b.maxY());
358 void scrollToCurrentMatch()
360 if (!m_findOnPage.currentMatchIsInLayer()) {
361 scrollRectOnScreen(m_findOnPage.currentMatchBounds());
365 SkRect matchBounds = m_findOnPage.currentMatchBounds();
366 LayerAndroid* rootLayer = getFrameCache(DontAllowNewer)->rootLayer();
367 Layer* layerContainingMatch = rootLayer->findById(m_findOnPage.currentMatchLayerId());
368 ASSERT(layerContainingMatch);
370 // If the match is in a fixed position layer, there's nothing to do.
371 if (layerContainingMatch->shouldInheritFromRootTransform())
374 // If the match is in a scrollable layer or a descendant of such a layer,
375 // there may be a range of of scroll configurations that will make the
376 // current match visible. Our approach is the simplest possible. Starting at
377 // the layer in which the match is found, we move up the layer tree,
378 // scrolling any scrollable layers as little as possible to make sure that
379 // the current match is in view. This approach has the disadvantage that we
380 // may end up scrolling a larger number of elements than is necessary, which
381 // may be visually jarring. However, minimising the number of layers
382 // scrolled would complicate the code significantly.
384 bool didScrollLayer = false;
385 for (Layer* layer = layerContainingMatch; layer; layer = layer->getParent()) {
386 ASSERT(layer->getParent() || layer == rootLayer);
388 if (layer->contentIsScrollable()) {
389 // Convert the match location to layer's local space and scroll it.
390 // Repeatedly calling Layer::localToAncestor() is inefficient as
391 // each call repeats part of the calculation. It would be more
392 // efficient to maintain the transform here and update it on each
393 // iteration, but that would mean duplicating logic from
394 // Layer::localToAncestor() and would complicate things.
396 layerContainingMatch->localToAncestor(layer, &transform);
397 SkRect transformedMatchBounds;
398 transform.mapRect(&transformedMatchBounds, matchBounds);
399 SkIRect roundedTransformedMatchBounds;
400 transformedMatchBounds.roundOut(&roundedTransformedMatchBounds);
401 // Only ScrollableLayerAndroid returns true for contentIsScrollable().
402 didScrollLayer |= static_cast<ScrollableLayerAndroid*>(layer)->scrollRectIntoView(roundedTransformedMatchBounds);
405 // Invalidate, as the call below to scroll the main page may be a no-op.
409 // Convert matchBounds to the global space so we can scroll the main page.
411 layerContainingMatch->localToGlobal(&transform);
412 SkRect transformedMatchBounds;
413 transform.mapRect(&transformedMatchBounds, matchBounds);
414 SkIRect roundedTransformedMatchBounds;
415 transformedMatchBounds.roundOut(&roundedTransformedMatchBounds);
416 scrollRectOnScreen(roundedTransformedMatchBounds);
419 void scrollRectOnScreen(const IntRect& rect)
424 calcOurContentVisibleRect(&visible);
427 int right = rect.maxX();
428 if (left < visible.fLeft) {
429 dx = left - visible.fLeft;
430 // Only scroll right if the entire width can fit on screen.
431 } else if (right > visible.fRight && right - left < visible.width()) {
432 dx = right - visible.fRight;
436 int bottom = rect.maxY();
437 if (top < visible.fTop) {
438 dy = top - visible.fTop;
439 // Only scroll down if the entire height can fit on screen
440 } else if (bottom > visible.fBottom && bottom - top < visible.height()) {
441 dy = bottom - visible.fBottom;
443 if ((dx|dy) == 0 || !scrollBy(dx, dy))
448 void calcOurContentVisibleRect(SkRect* r)
450 JNIEnv* env = JSC::Bindings::getJNIEnv();
451 AutoJObject javaObject = m_javaGlue.object(env);
452 if (!javaObject.get())
454 jclass rectClass = env->FindClass("android/graphics/RectF");
455 jmethodID init = env->GetMethodID(rectClass, "<init>", "(FFFF)V");
456 jobject jRect = env->NewObject(rectClass, init, 0, 0, 0, 0);
457 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_calcOurContentVisibleRectF, jRect);
458 r->fLeft = env->GetFloatField(jRect, m_javaGlue.m_rectFLeft);
459 r->fTop = env->GetFloatField(jRect, m_javaGlue.m_rectFTop);
460 r->fRight = r->fLeft + env->CallFloatMethod(jRect, m_javaGlue.m_rectFWidth);
461 r->fBottom = r->fTop + env->CallFloatMethod(jRect, m_javaGlue.m_rectFHeight);
462 env->DeleteLocalRef(rectClass);
463 env->DeleteLocalRef(jRect);
467 void resetCursorRing()
469 m_ringAnimationEnd = 0;
470 m_viewImpl->m_hasCursorBounds = false;
473 bool drawCursorPreamble(CachedRoot* root)
475 if (!root) return false;
476 const CachedFrame* frame;
477 const CachedNode* node = root->currentCursor(&frame);
479 DBG_NAV_LOGV("%s", "!node");
483 m_ring.setIsButton(node);
484 if (node->isHidden()) {
485 DBG_NAV_LOG("node->isHidden()");
486 m_viewImpl->m_hasCursorBounds = false;
489 #if USE(ACCELERATED_COMPOSITING)
490 if (node->isInLayer() && root->rootLayer()) {
491 LayerAndroid* layer = root->rootLayer();
493 calcOurContentVisibleRect(&visible);
494 layer->updateFixedLayersPositions(visible);
495 layer->updatePositions();
498 setVisibleRect(root);
499 m_ring.m_root = root;
500 m_ring.m_frame = frame;
501 m_ring.m_node = node;
502 SkMSec time = SkTime::GetMSecs();
503 m_ring.m_isPressed = time < m_ringAnimationEnd
504 && m_ringAnimationEnd != UINT_MAX;
508 void drawCursorPostamble()
510 if (m_ringAnimationEnd == UINT_MAX)
512 SkMSec time = SkTime::GetMSecs();
513 if (time < m_ringAnimationEnd) {
514 // views assume that inval bounds coordinates are non-negative
515 WebCore::IntRect invalBounds(0, 0, INT_MAX, INT_MAX);
516 invalBounds.intersect(m_ring.m_absBounds);
517 postInvalidateDelayed(m_ringAnimationEnd - time, invalBounds);
519 hideCursor(const_cast<CachedRoot*>(m_ring.m_root));
523 bool drawGL(WebCore::IntRect& viewRect, WebCore::IntRect* invalRect, WebCore::IntRect& webViewRect,
524 int titleBarHeight, WebCore::IntRect& clip, float scale, int extras)
526 #if USE(ACCELERATED_COMPOSITING)
527 if (!m_baseLayer || inFullScreenMode())
530 if (!m_glWebViewState) {
531 m_glWebViewState = new GLWebViewState(&m_viewImpl->gButtonMutex);
532 m_glWebViewState->glExtras()->setCursorRingExtra(&m_ring);
533 m_glWebViewState->glExtras()->setFindOnPageExtra(&m_findOnPage);
534 if (m_baseLayer->content()) {
537 rect.set(0, 0, m_baseLayer->content()->width(), m_baseLayer->content()->height());
538 region.setRect(rect);
539 m_glWebViewState->setBaseLayer(m_baseLayer, region, false, true);
543 CachedRoot* root = getFrameCache(AllowNewer);
545 DBG_NAV_LOG("!root");
546 if (extras == DrawExtrasCursorRing)
549 DrawExtra* extra = 0;
552 extra = &m_findOnPage;
554 case DrawExtrasSelection:
555 // This will involve a JNI call, but under normal circumstances we will
556 // not hit this anyway. Only if USE_JAVA_TEXT_SELECTION is disabled
557 // in WebView.java will we hit this (so really debug only)
558 updateSelectionHandles();
559 extra = &m_selectText;
561 case DrawExtrasCursorRing:
562 if (drawCursorPreamble(root) && m_ring.setup()) {
563 if (m_ring.m_isPressed || m_ringAnimationEnd == UINT_MAX)
565 drawCursorPostamble();
572 unsigned int pic = m_glWebViewState->currentPictureCounter();
574 if (extra == &m_ring) {
575 if (m_ring.m_isButton || root != m_ring.m_frame) {
576 // TODO: Fix the navcache to work with layers correctly
577 // In the meantime, this works around the bug. However, the rings
578 // it produces are not as nice for some reason, thus we use
579 // m_ring.rings() as produced by navcache for the base layer and
580 // for layers we generate a new ring set
581 m_ring.rings().clear();
582 for (size_t i = 0; i < m_ring.m_node->rings().size(); i++) {
583 IntRect rect = m_ring.m_node->rings().at(i);
584 rect = m_ring.m_frame->adjustBounds(m_ring.m_node, rect);
585 if (!m_ring.m_isButton)
587 m_ring.rings().append(rect);
591 m_glWebViewState->glExtras()->setDrawExtra(extra);
593 LayerAndroid* compositeLayer = compositeRoot();
595 compositeLayer->setExtra(0);
598 calcOurContentVisibleRect(&visibleRect);
599 // Make sure we have valid coordinates. We might not have valid coords
600 // if the zoom manager is still initializing. We will be redrawn
601 // once the correct scale is set
602 if (!visibleRect.hasValidCoordinates())
604 bool pagesSwapped = false;
605 bool ret = m_glWebViewState->drawGL(viewRect, visibleRect, invalRect,
606 webViewRect, titleBarHeight, clip, scale,
608 if (m_pageSwapCallbackRegistered && pagesSwapped) {
609 m_pageSwapCallbackRegistered = false;
610 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
611 JNIEnv* env = JSC::Bindings::getJNIEnv();
612 AutoJObject javaObject = m_javaGlue.object(env);
613 if (javaObject.get()) {
614 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_pageSwapCallback);
618 if (ret || m_glWebViewState->currentPictureCounter() != pic)
624 PictureSet* draw(SkCanvas* canvas, SkColor bgColor, int extras, bool split)
628 canvas->drawColor(bgColor);
632 // draw the content of the base layer first
633 PictureSet* content = m_baseLayer->content();
634 int sc = canvas->save(SkCanvas::kClip_SaveFlag);
635 canvas->clipRect(SkRect::MakeLTRB(0, 0, content->width(),
636 content->height()), SkRegion::kDifference_Op);
637 canvas->drawColor(bgColor);
638 canvas->restoreToCount(sc);
639 if (content->draw(canvas))
640 ret = split ? new PictureSet(*content) : 0;
642 CachedRoot* root = getFrameCache(AllowNewer);
644 DBG_NAV_LOG("!root");
645 if (extras == DrawExtrasCursorRing)
648 LayerAndroid mainPicture(m_navPictureUI);
649 DrawExtra* extra = 0;
652 extra = &m_findOnPage;
654 case DrawExtrasSelection:
655 // This will involve a JNI call, but under normal circumstances we will
656 // not hit this anyway. Only if USE_JAVA_TEXT_SELECTION is disabled
657 // in WebView.java will we hit this (so really debug only)
658 updateSelectionHandles();
659 extra = &m_selectText;
661 case DrawExtrasCursorRing:
662 if (drawCursorPreamble(root) && m_ring.setup()) {
663 if (!m_ring.m_isButton)
665 drawCursorPostamble();
672 IntRect dummy; // inval area, unused for now
673 extra->draw(canvas, &mainPicture, &dummy);
675 #if USE(ACCELERATED_COMPOSITING)
676 LayerAndroid* compositeLayer = compositeRoot();
679 compositeLayer->setExtra(extra);
681 calcOurContentVisibleRect(&visible);
682 // call this to be sure we've adjusted for any scrolling or animations
683 // before we actually draw
684 compositeLayer->updateFixedLayersPositions(visible);
685 compositeLayer->updatePositions();
686 // We have to set the canvas' matrix on the base layer
687 // (to have fixed layers work as intended)
688 SkAutoCanvasRestore restore(canvas, true);
689 m_baseLayer->setMatrix(canvas->getTotalMatrix());
690 canvas->resetMatrix();
691 m_baseLayer->draw(canvas);
697 bool cursorIsTextInput(FrameCachePermission allowNewer)
699 CachedRoot* root = getFrameCache(allowNewer);
701 DBG_NAV_LOG("!root");
704 const CachedNode* cursor = root->currentCursor();
706 DBG_NAV_LOG("!cursor");
709 DBG_NAV_LOGD("%s", cursor->isTextInput() ? "true" : "false");
710 return cursor->isTextInput();
713 void cursorRingBounds(WebCore::IntRect* bounds)
715 DBG_NAV_LOGD("%s", "");
716 CachedRoot* root = getFrameCache(DontAllowNewer);
718 const CachedFrame* cachedFrame;
719 const CachedNode* cachedNode = root->currentCursor(&cachedFrame);
721 *bounds = cachedNode->cursorRingBounds(cachedFrame);
722 DBG_NAV_LOGD("bounds={%d,%d,%d,%d}", bounds->x(), bounds->y(),
723 bounds->width(), bounds->height());
727 *bounds = WebCore::IntRect(0, 0, 0, 0);
732 m_viewImpl->gCursorBoundsMutex.lock();
733 bool hasCursorBounds = m_viewImpl->m_hasCursorBounds;
734 IntRect bounds = m_viewImpl->m_cursorBounds;
735 m_viewImpl->gCursorBoundsMutex.unlock();
736 if (!hasCursorBounds)
739 const CachedFrame* frame;
740 const CachedNode* node = m_frameCacheUI->findAt(bounds, &frame, &x, &y, true);
743 // require that node have approximately the same bounds (+/- 4) and the same
745 IntPoint oldCenter = IntPoint(bounds.x() + (bounds.width() >> 1),
746 bounds.y() + (bounds.height() >> 1));
747 IntRect newBounds = node->bounds(frame);
748 IntPoint newCenter = IntPoint(newBounds.x() + (newBounds.width() >> 1),
749 newBounds.y() + (newBounds.height() >> 1));
750 DBG_NAV_LOGD("oldCenter=(%d,%d) newCenter=(%d,%d)"
751 " bounds=(%d,%d,w=%d,h=%d) newBounds=(%d,%d,w=%d,h=%d)",
752 oldCenter.x(), oldCenter.y(), newCenter.x(), newCenter.y(),
753 bounds.x(), bounds.y(), bounds.width(), bounds.height(),
754 newBounds.x(), newBounds.y(), newBounds.width(), newBounds.height());
755 if (abs(oldCenter.x() - newCenter.x()) > 2)
757 if (abs(oldCenter.y() - newCenter.y()) > 2)
759 if (abs(bounds.x() - newBounds.x()) > 4)
761 if (abs(bounds.y() - newBounds.y()) > 4)
763 if (abs(bounds.maxX() - newBounds.maxX()) > 4)
765 if (abs(bounds.maxY() - newBounds.maxY()) > 4)
767 DBG_NAV_LOGD("node=%p frame=%p x=%d y=%d bounds=(%d,%d,w=%d,h=%d)",
768 node, frame, x, y, bounds.x(), bounds.y(), bounds.width(),
770 m_frameCacheUI->setCursor(const_cast<CachedFrame*>(frame),
771 const_cast<CachedNode*>(node));
774 CachedRoot* getFrameCache(FrameCachePermission allowNewer)
776 if (!m_viewImpl->m_updatedFrameCache) {
777 DBG_NAV_LOGV("%s", "!m_viewImpl->m_updatedFrameCache");
778 return m_frameCacheUI;
780 if (allowNewer == DontAllowNewer && m_viewImpl->m_lastGeneration < m_generation) {
781 DBG_NAV_LOGD("allowNewer==DontAllowNewer m_viewImpl->m_lastGeneration=%d"
782 " < m_generation=%d", m_viewImpl->m_lastGeneration, m_generation);
783 return m_frameCacheUI;
785 DBG_NAV_LOGD("%s", "m_viewImpl->m_updatedFrameCache == true");
786 const CachedFrame* oldCursorFrame;
787 const CachedNode* oldCursorNode = m_frameCacheUI ?
788 m_frameCacheUI->currentCursor(&oldCursorFrame) : 0;
789 #if USE(ACCELERATED_COMPOSITING)
791 if (oldCursorNode && oldCursorNode->isInLayer()) {
792 const LayerAndroid* cursorLayer = oldCursorFrame->layer(oldCursorNode)
793 ->layer(m_frameCacheUI->rootLayer());
795 layerId = cursorLayer->uniqueId();
798 // get id from old layer and use to find new layer
799 bool oldFocusIsTextInput = false;
800 void* oldFocusNodePointer = 0;
801 if (m_frameCacheUI) {
802 const CachedNode* oldFocus = m_frameCacheUI->currentFocus();
804 oldFocusIsTextInput = oldFocus->isTextInput();
805 oldFocusNodePointer = oldFocus->nodePointer();
808 m_viewImpl->gFrameCacheMutex.lock();
809 delete m_frameCacheUI;
810 SkSafeUnref(m_navPictureUI);
811 m_viewImpl->m_updatedFrameCache = false;
812 m_frameCacheUI = m_viewImpl->m_frameCacheKit;
813 m_navPictureUI = m_viewImpl->m_navPictureKit;
814 m_viewImpl->m_frameCacheKit = 0;
815 m_viewImpl->m_navPictureKit = 0;
816 m_viewImpl->gFrameCacheMutex.unlock();
818 m_frameCacheUI->setRootLayer(compositeRoot());
819 #if USE(ACCELERATED_COMPOSITING)
822 calcOurContentVisibleRect(&visible);
823 LayerAndroid* layer = const_cast<LayerAndroid*>(
824 m_frameCacheUI->rootLayer());
826 layer->updateFixedLayersPositions(visible);
827 layer->updatePositions();
832 if (oldFocusIsTextInput) {
833 const CachedNode* newFocus = m_frameCacheUI->currentFocus();
834 if (newFocus && oldFocusNodePointer != newFocus->nodePointer()
835 && newFocus->isTextInput()
836 && newFocus != m_frameCacheUI->currentCursor()) {
837 // The focus has changed. We may need to update things.
838 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
839 JNIEnv* env = JSC::Bindings::getJNIEnv();
840 AutoJObject javaObject = m_javaGlue.object(env);
841 if (javaObject.get()) {
842 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_domChangedFocus);
847 if (oldCursorNode && (!m_frameCacheUI || !m_frameCacheUI->currentCursor()))
848 viewInvalidate(); // redraw in case cursor ring is still visible
849 return m_frameCacheUI;
852 int getScaledMaxXScroll()
854 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
855 JNIEnv* env = JSC::Bindings::getJNIEnv();
856 AutoJObject javaObject = m_javaGlue.object(env);
857 if (!javaObject.get())
859 int result = env->CallIntMethod(javaObject.get(), m_javaGlue.m_getScaledMaxXScroll);
864 int getScaledMaxYScroll()
866 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
867 JNIEnv* env = JSC::Bindings::getJNIEnv();
868 AutoJObject javaObject = m_javaGlue.object(env);
869 if (!javaObject.get())
871 int result = env->CallIntMethod(javaObject.get(), m_javaGlue.m_getScaledMaxYScroll);
876 IntRect getVisibleRect()
879 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
880 JNIEnv* env = JSC::Bindings::getJNIEnv();
881 AutoJObject javaObject = m_javaGlue.object(env);
882 if (!javaObject.get())
884 jobject jRect = env->CallObjectMethod(javaObject.get(), m_javaGlue.m_getVisibleRect);
886 rect.setX(env->GetIntField(jRect, m_javaGlue.m_rectLeft));
888 rect.setY(env->GetIntField(jRect, m_javaGlue.m_rectTop));
890 rect.setWidth(env->CallIntMethod(jRect, m_javaGlue.m_rectWidth));
892 rect.setHeight(env->CallIntMethod(jRect, m_javaGlue.m_rectHeight));
894 env->DeleteLocalRef(jRect);
899 static CachedFrame::Direction KeyToDirection(int32_t keyCode)
902 case AKEYCODE_DPAD_RIGHT:
903 DBG_NAV_LOGD("keyCode=%s", "right");
904 return CachedFrame::RIGHT;
905 case AKEYCODE_DPAD_LEFT:
906 DBG_NAV_LOGD("keyCode=%s", "left");
907 return CachedFrame::LEFT;
908 case AKEYCODE_DPAD_DOWN:
909 DBG_NAV_LOGD("keyCode=%s", "down");
910 return CachedFrame::DOWN;
911 case AKEYCODE_DPAD_UP:
912 DBG_NAV_LOGD("keyCode=%s", "up");
913 return CachedFrame::UP;
915 DBG_NAV_LOGD("bad key %d sent", keyCode);
916 return CachedFrame::UNINITIALIZED;
920 WTF::String imageURI(int x, int y)
922 const CachedRoot* root = getFrameCache(DontAllowNewer);
923 return root ? root->imageURI(x, y) : WTF::String();
926 bool cursorWantsKeyEvents()
928 const CachedRoot* root = getFrameCache(DontAllowNewer);
930 const CachedNode* focus = root->currentCursor();
932 return focus->wantsKeyEvents();
938 /* returns true if the key had no effect (neither scrolled nor changed cursor) */
939 bool moveCursor(int keyCode, int count, bool ignoreScroll)
941 CachedRoot* root = getFrameCache(AllowNewer);
943 DBG_NAV_LOG("!root");
947 m_viewImpl->m_moveGeneration++;
948 CachedFrame::Direction direction = KeyToDirection(keyCode);
949 const CachedFrame* cachedFrame, * oldFrame = 0;
950 const CachedNode* cursor = root->currentCursor(&oldFrame);
951 WebCore::IntPoint cursorLocation = root->cursorLocation();
952 DBG_NAV_LOGD("old cursor %d (nativeNode=%p) cursorLocation={%d, %d}",
953 cursor ? cursor->index() : 0,
954 cursor ? cursor->nodePointer() : 0, cursorLocation.x(), cursorLocation.y());
955 WebCore::IntRect visibleRect = setVisibleRect(root);
956 int xMax = getScaledMaxXScroll();
957 int yMax = getScaledMaxYScroll();
958 root->setMaxScroll(xMax, yMax);
959 const CachedNode* cachedNode = 0;
963 while (--counter >= 0) {
964 WebCore::IntPoint scroll = WebCore::IntPoint(0, 0);
965 cachedNode = root->moveCursor(direction, &cachedFrame, &scroll);
969 DBG_NAV_LOGD("new cursor %d (nativeNode=%p) cursorLocation={%d, %d}"
970 "bounds={%d,%d,w=%d,h=%d}", cachedNode ? cachedNode->index() : 0,
971 cachedNode ? cachedNode->nodePointer() : 0,
972 root->cursorLocation().x(), root->cursorLocation().y(),
973 cachedNode ? cachedNode->bounds(cachedFrame).x() : 0,
974 cachedNode ? cachedNode->bounds(cachedFrame).y() : 0,
975 cachedNode ? cachedNode->bounds(cachedFrame).width() : 0,
976 cachedNode ? cachedNode->bounds(cachedFrame).height() : 0);
977 // If !m_heightCanMeasure (such as in the browser), we want to scroll no
979 if (!ignoreScroll && (!m_heightCanMeasure ||
981 (cursor && cursor->nodePointer() == cachedNode->nodePointer())))
983 if (count == 1 && dx != 0 && dy == 0 && -m_lastDx == dx &&
984 SkTime::GetMSecs() - m_lastDxTime < 1000)
985 root->checkForJiggle(&dx);
986 DBG_NAV_LOGD("scrollBy %d,%d", dx, dy);
988 this->scrollBy(dx, dy);
990 m_lastDxTime = SkTime::GetMSecs();
995 m_viewImpl->updateCursorBounds(root, cachedFrame, cachedNode);
996 root->setCursor(const_cast<CachedFrame*>(cachedFrame),
997 const_cast<CachedNode*>(cachedNode));
998 const CachedNode* focus = root->currentFocus();
999 bool clearTextEntry = cachedNode != focus && focus
1000 && cachedNode->nodePointer() != focus->nodePointer() && focus->isTextInput();
1001 // Stop painting the caret if the old focus was a text input and so is the new cursor.
1002 bool stopPaintingCaret = clearTextEntry && cachedNode->wantsKeyEvents();
1003 sendMoveMouseIfLatest(clearTextEntry, stopPaintingCaret);
1005 int docHeight = root->documentHeight();
1006 int docWidth = root->documentWidth();
1007 if (visibleRect.maxY() + dy > docHeight)
1008 dy = docHeight - visibleRect.maxY();
1009 else if (visibleRect.y() + dy < 0)
1010 dy = -visibleRect.y();
1011 if (visibleRect.maxX() + dx > docWidth)
1012 dx = docWidth - visibleRect.maxX();
1013 else if (visibleRect.x() < 0)
1014 dx = -visibleRect.x();
1015 result = direction == CachedFrame::LEFT ? dx >= 0 :
1016 direction == CachedFrame::RIGHT ? dx <= 0 :
1017 direction == CachedFrame::UP ? dy >= 0 : dy <= 0;
1022 void notifyProgressFinished()
1024 DBG_NAV_LOGD("cursorIsTextInput=%d", cursorIsTextInput(DontAllowNewer));
1025 rebuildWebTextView();
1027 if (m_frameCacheUI) {
1028 const CachedNode* focus = m_frameCacheUI->currentFocus();
1029 DBG_NAV_LOGD("focus %d (nativeNode=%p)",
1030 focus ? focus->index() : 0,
1031 focus ? focus->nodePointer() : 0);
1036 const CachedNode* findAt(CachedRoot* root, const WebCore::IntRect& rect,
1037 const CachedFrame** framePtr, int* rxPtr, int* ryPtr)
1044 setVisibleRect(root);
1045 return root->findAt(rect, framePtr, rxPtr, ryPtr, true);
1048 IntRect setVisibleRect(CachedRoot* root)
1050 IntRect visibleRect = getVisibleRect();
1051 DBG_NAV_LOGD("getVisibleRect %d,%d,%d,%d",
1052 visibleRect.x(), visibleRect.y(), visibleRect.width(), visibleRect.height());
1053 root->setVisibleRect(visibleRect);
1057 void selectBestAt(const WebCore::IntRect& rect)
1059 const CachedFrame* frame;
1061 CachedRoot* root = getFrameCache(AllowNewer);
1064 const CachedNode* node = findAt(root, rect, &frame, &rx, &ry);
1066 DBG_NAV_LOGD("no nodes found root=%p", root);
1067 root->rootHistory()->setMouseBounds(rect);
1068 m_viewImpl->m_hasCursorBounds = false;
1069 root->setCursor(0, 0);
1072 DBG_NAV_LOGD("CachedNode:%p (%d)", node, node->index());
1073 WebCore::IntRect bounds = node->bounds(frame);
1074 root->rootHistory()->setMouseBounds(bounds);
1075 m_viewImpl->updateCursorBounds(root, frame, node);
1077 root->setCursor(const_cast<CachedFrame*>(frame),
1078 const_cast<CachedNode*>(node));
1080 sendMoveMouseIfLatest(false, false);
1083 const CachedNode* m_cacheHitNode;
1084 const CachedFrame* m_cacheHitFrame;
1086 bool pointInNavCache(int x, int y, int slop)
1088 CachedRoot* root = getFrameCache(AllowNewer);
1091 IntRect rect = IntRect(x - slop, y - slop, slop * 2, slop * 2);
1093 return (m_cacheHitNode = findAt(root, rect, &m_cacheHitFrame, &rx, &ry));
1096 bool motionUp(int x, int y, int slop)
1098 bool pageScrolled = false;
1099 IntRect rect = IntRect(x - slop, y - slop, slop * 2, slop * 2);
1101 CachedRoot* root = getFrameCache(AllowNewer);
1104 const CachedFrame* frame = 0;
1105 const CachedNode* result = findAt(root, rect, &frame, &rx, &ry);
1106 CachedHistory* history = root->rootHistory();
1108 DBG_NAV_LOGD("no nodes found root=%p", root);
1109 history->setNavBounds(rect);
1110 m_viewImpl->m_hasCursorBounds = false;
1112 int dx = root->checkForCenter(x, y);
1115 pageScrolled = true;
1117 sendMotionUp(frame ? (WebCore::Frame*) frame->framePointer() : 0,
1120 return pageScrolled;
1122 DBG_NAV_LOGD("CachedNode:%p (%d) x=%d y=%d rx=%d ry=%d", result,
1123 result->index(), x, y, rx, ry);
1124 WebCore::IntRect navBounds = WebCore::IntRect(rx, ry, 1, 1);
1125 history->setNavBounds(navBounds);
1126 history->setMouseBounds(navBounds);
1127 m_viewImpl->updateCursorBounds(root, frame, result);
1128 root->setCursor(const_cast<CachedFrame*>(frame),
1129 const_cast<CachedNode*>(result));
1130 if (result->isSyntheticLink())
1131 overrideUrlLoading(result->getExport());
1134 (WebCore::Frame*) frame->framePointer(),
1135 (WebCore::Node*) result->nodePointer(), rx, ry);
1137 if (result->isTextInput() || result->isSelect()
1138 || result->isContentEditable()) {
1139 showCursorUntimed();
1142 return pageScrolled;
1145 #if USE(ACCELERATED_COMPOSITING)
1146 static const ScrollableLayerAndroid* findScrollableLayer(
1147 const LayerAndroid* parent, int x, int y, SkIRect* foundBounds) {
1149 parent->bounds(&bounds);
1150 // Check the parent bounds first; this will clip to within a masking layer's
1152 if (parent->masksToBounds() && !bounds.contains(x, y))
1154 // Move the hit test local to parent.
1157 int count = parent->countChildren();
1159 const LayerAndroid* child = parent->getChild(count);
1160 const ScrollableLayerAndroid* result = findScrollableLayer(child, x, y,
1163 foundBounds->offset(bounds.fLeft, bounds.fTop);
1164 if (parent->masksToBounds()) {
1165 if (bounds.width() < foundBounds->width())
1166 foundBounds->fRight = foundBounds->fLeft + bounds.width();
1167 if (bounds.height() < foundBounds->height())
1168 foundBounds->fBottom = foundBounds->fTop + bounds.height();
1173 if (parent->contentIsScrollable()) {
1174 foundBounds->set(0, 0, bounds.width(), bounds.height());
1175 return static_cast<const ScrollableLayerAndroid*>(parent);
1181 int scrollableLayer(int x, int y, SkIRect* layerRect, SkIRect* bounds)
1183 #if USE(ACCELERATED_COMPOSITING)
1184 const LayerAndroid* layerRoot = compositeRoot();
1187 const ScrollableLayerAndroid* result = findScrollableLayer(layerRoot, x, y,
1190 result->getScrollRect(layerRect);
1191 return result->uniqueId();
1197 int getBlockLeftEdge(int x, int y, float scale)
1199 CachedRoot* root = getFrameCache(AllowNewer);
1201 return root->getBlockLeftEdge(x, y, scale);
1205 void overrideUrlLoading(const WTF::String& url)
1207 JNIEnv* env = JSC::Bindings::getJNIEnv();
1208 AutoJObject javaObject = m_javaGlue.object(env);
1209 if (!javaObject.get())
1211 jstring jName = wtfStringToJstring(env, url);
1212 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_overrideLoading, jName);
1213 env->DeleteLocalRef(jName);
1216 void setFindIsUp(bool up)
1218 DBG_NAV_LOGD("up=%d", up);
1219 m_viewImpl->m_findIsUp = up;
1222 void setFindIsEmpty()
1225 m_findOnPage.clearCurrentLocation();
1228 void showCursorTimed()
1231 m_ringAnimationEnd = SkTime::GetMSecs() + PRESSED_STATE_DURATION;
1235 void showCursorUntimed()
1238 m_ring.m_isPressed = false;
1239 m_ringAnimationEnd = UINT_MAX;
1243 void setHeightCanMeasure(bool measure)
1245 m_heightCanMeasure = measure;
1248 String getSelection()
1250 return m_selectText.getSelection();
1253 void moveSelection(int x, int y)
1255 m_selectText.moveSelection(getVisibleRect(), x, y);
1258 IntPoint selectableText()
1260 const CachedRoot* root = getFrameCache(DontAllowNewer);
1262 return IntPoint(0, 0);
1263 return m_selectText.selectableText(root);
1268 m_selectText.selectAll();
1273 return m_selectText.selectionX();
1278 return m_selectText.selectionY();
1281 void resetSelection()
1283 m_selectText.reset();
1286 bool startSelection(int x, int y)
1288 const CachedRoot* root = getFrameCache(DontAllowNewer);
1291 updateSelectionHandles();
1292 return m_selectText.startSelection(root, getVisibleRect(), x, y);
1295 bool wordSelection(int x, int y)
1297 const CachedRoot* root = getFrameCache(DontAllowNewer);
1300 updateSelectionHandles();
1301 return m_selectText.wordSelection(root, getVisibleRect(), x, y);
1304 bool extendSelection(int x, int y)
1306 m_selectText.extendSelection(getVisibleRect(), x, y);
1310 bool hitSelection(int x, int y)
1312 updateSelectionHandles();
1313 return m_selectText.hitSelection(x, y);
1316 void setExtendSelection()
1318 m_selectText.setExtendSelection(true);
1321 void setSelectionPointer(bool set, float scale, int x, int y)
1323 m_selectText.setDrawPointer(set);
1326 m_selectText.m_inverseScale = scale;
1327 m_selectText.m_selectX = x;
1328 m_selectText.m_selectY = y;
1331 void sendMoveFocus(WebCore::Frame* framePtr, WebCore::Node* nodePtr)
1333 DBG_NAV_LOGD("framePtr=%p nodePtr=%p", framePtr, nodePtr);
1334 JNIEnv* env = JSC::Bindings::getJNIEnv();
1335 AutoJObject javaObject = m_javaGlue.object(env);
1336 if (!javaObject.get())
1338 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_sendMoveFocus, (jint) framePtr, (jint) nodePtr);
1339 checkException(env);
1342 void sendMoveMouse(WebCore::Frame* framePtr, WebCore::Node* nodePtr, int x, int y)
1344 DBG_NAV_LOGD("framePtr=%p nodePtr=%p x=%d y=%d", framePtr, nodePtr, x, y);
1345 JNIEnv* env = JSC::Bindings::getJNIEnv();
1346 AutoJObject javaObject = m_javaGlue.object(env);
1347 if (!javaObject.get())
1349 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_sendMoveMouse, reinterpret_cast<jint>(framePtr), reinterpret_cast<jint>(nodePtr), x, y);
1350 checkException(env);
1353 void sendMoveMouseIfLatest(bool clearTextEntry, bool stopPaintingCaret)
1355 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
1356 JNIEnv* env = JSC::Bindings::getJNIEnv();
1357 AutoJObject javaObject = m_javaGlue.object(env);
1358 if (!javaObject.get())
1360 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_sendMoveMouseIfLatest, clearTextEntry, stopPaintingCaret);
1361 checkException(env);
1364 void sendMotionUp(WebCore::Frame* framePtr, WebCore::Node* nodePtr, int x, int y)
1366 DBG_NAV_LOGD("m_generation=%d framePtr=%p nodePtr=%p x=%d y=%d", m_generation, framePtr, nodePtr, x, y);
1367 LOG_ASSERT(m_javaGlue.m_obj, "A WebView was not associated with this WebViewNative!");
1369 JNIEnv* env = JSC::Bindings::getJNIEnv();
1370 AutoJObject javaObject = m_javaGlue.object(env);
1371 if (!javaObject.get())
1373 m_viewImpl->m_touchGeneration = ++m_generation;
1374 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_sendMotionUp, m_generation, (jint) framePtr, (jint) nodePtr, x, y);
1375 checkException(env);
1378 void findNext(bool forward)
1380 m_findOnPage.findNext(forward);
1381 scrollToCurrentMatch();
1385 // With this call, WebView takes ownership of matches, and is responsible for
1387 void setMatches(WTF::Vector<MatchInfo>* matches, jboolean sameAsLastSearch)
1389 // If this search is the same as the last one, check against the old
1390 // location to determine whether to scroll. If the same word is found
1391 // in the same place, then do not scroll.
1392 IntRect oldLocation;
1393 bool checkAgainstOldLocation = false;
1394 if (sameAsLastSearch && m_findOnPage.isCurrentLocationValid()) {
1395 oldLocation = m_findOnPage.currentMatchBounds();
1396 checkAgainstOldLocation = true;
1399 m_findOnPage.setMatches(matches);
1401 if (!checkAgainstOldLocation || oldLocation != m_findOnPage.currentMatchBounds())
1402 scrollToCurrentMatch();
1406 int currentMatchIndex()
1408 return m_findOnPage.currentMatchIndex();
1411 bool scrollBy(int dx, int dy)
1413 LOG_ASSERT(m_javaGlue.m_obj, "A java object was not associated with this native WebView!");
1415 JNIEnv* env = JSC::Bindings::getJNIEnv();
1416 AutoJObject javaObject = m_javaGlue.object(env);
1417 if (!javaObject.get())
1419 bool result = env->CallBooleanMethod(javaObject.get(), m_javaGlue.m_scrollBy, dx, dy, true);
1420 checkException(env);
1424 void setIsScrolling(bool isScrolling)
1426 #if USE(ACCELERATED_COMPOSITING)
1427 if (m_glWebViewState)
1428 m_glWebViewState->setIsScrolling(isScrolling);
1432 bool hasCursorNode()
1434 CachedRoot* root = getFrameCache(DontAllowNewer);
1436 DBG_NAV_LOG("!root");
1439 const CachedNode* cursorNode = root->currentCursor();
1440 DBG_NAV_LOGD("cursorNode=%d (nodePointer=%p)",
1441 cursorNode ? cursorNode->index() : -1,
1442 cursorNode ? cursorNode->nodePointer() : 0);
1448 CachedRoot* root = getFrameCache(DontAllowNewer);
1450 DBG_NAV_LOG("!root");
1453 const CachedNode* focusNode = root->currentFocus();
1454 DBG_NAV_LOGD("focusNode=%d (nodePointer=%p)",
1455 focusNode ? focusNode->index() : -1,
1456 focusNode ? focusNode->nodePointer() : 0);
1460 void rebuildWebTextView()
1462 JNIEnv* env = JSC::Bindings::getJNIEnv();
1463 AutoJObject javaObject = m_javaGlue.object(env);
1464 if (!javaObject.get())
1466 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_rebuildWebTextView);
1467 checkException(env);
1470 void viewInvalidate()
1472 JNIEnv* env = JSC::Bindings::getJNIEnv();
1473 AutoJObject javaObject = m_javaGlue.object(env);
1474 if (!javaObject.get())
1476 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_viewInvalidate);
1477 checkException(env);
1480 void viewInvalidateRect(int l, int t, int r, int b)
1482 JNIEnv* env = JSC::Bindings::getJNIEnv();
1483 AutoJObject javaObject = m_javaGlue.object(env);
1484 if (!javaObject.get())
1486 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_viewInvalidateRect, l, r, t, b);
1487 checkException(env);
1490 void postInvalidateDelayed(int64_t delay, const WebCore::IntRect& bounds)
1492 JNIEnv* env = JSC::Bindings::getJNIEnv();
1493 AutoJObject javaObject = m_javaGlue.object(env);
1494 if (!javaObject.get())
1496 env->CallVoidMethod(javaObject.get(), m_javaGlue.m_postInvalidateDelayed,
1497 delay, bounds.x(), bounds.y(), bounds.maxX(), bounds.maxY());
1498 checkException(env);
1501 bool inFullScreenMode()
1503 JNIEnv* env = JSC::Bindings::getJNIEnv();
1504 AutoJObject javaObject = m_javaGlue.object(env);
1505 if (!javaObject.get())
1507 jboolean result = env->CallBooleanMethod(javaObject.get(), m_javaGlue.m_inFullScreenMode);
1508 checkException(env);
1512 int moveGeneration()
1514 return m_viewImpl->m_moveGeneration;
1517 LayerAndroid* compositeRoot() const
1519 LOG_ASSERT(!m_baseLayer || m_baseLayer->countChildren() == 1,
1520 "base layer can't have more than one child %s", __FUNCTION__);
1521 if (m_baseLayer && m_baseLayer->countChildren() == 1)
1522 return static_cast<LayerAndroid*>(m_baseLayer->getChild(0));
1527 #if ENABLE(ANDROID_OVERFLOW_SCROLL)
1528 static void copyScrollPositionRecursive(const LayerAndroid* from,
1533 for (int i = 0; i < from->countChildren(); i++) {
1534 const LayerAndroid* l = from->getChild(i);
1535 if (l->contentIsScrollable()) {
1536 const SkPoint& pos = l->getPosition();
1537 LayerAndroid* match = root->findById(l->uniqueId());
1538 if (match && match->contentIsScrollable())
1539 match->setPosition(pos.fX, pos.fY);
1541 copyScrollPositionRecursive(l, root);
1546 void registerPageSwapCallback()
1548 m_pageSwapCallbackRegistered = true;
1551 void setBaseLayer(BaseLayerAndroid* layer, SkRegion& inval, bool showVisualIndicator,
1552 bool isPictureAfterFirstLayout, bool registerPageSwapCallback)
1554 #if USE(ACCELERATED_COMPOSITING)
1555 if (m_glWebViewState)
1556 m_glWebViewState->setBaseLayer(layer, inval, showVisualIndicator,
1557 isPictureAfterFirstLayout);
1558 m_pageSwapCallbackRegistered |= registerPageSwapCallback;
1561 #if ENABLE(ANDROID_OVERFLOW_SCROLL)
1563 LayerAndroid* newCompositeRoot = static_cast<LayerAndroid*>(layer->getChild(0));
1564 copyScrollPositionRecursive(compositeRoot(), newCompositeRoot);
1567 SkSafeUnref(m_baseLayer);
1568 m_baseLayer = layer;
1569 CachedRoot* root = getFrameCache(DontAllowNewer);
1572 root->resetLayers();
1573 root->setRootLayer(compositeRoot());
1576 void getTextSelectionRegion(SkRegion *region)
1578 m_selectText.getSelectionRegion(getVisibleRect(), region);
1581 void replaceBaseContent(PictureSet* set)
1585 m_baseLayer->setContent(*set);
1589 void copyBaseContentToPicture(SkPicture* picture)
1593 PictureSet* content = m_baseLayer->content();
1594 m_baseLayer->drawCanvas(picture->beginRecording(content->width(), content->height(),
1595 SkPicture::kUsePathBoundsForClip_RecordingFlag));
1596 picture->endRecording();
1602 return !m_baseLayer->content()->isEmpty();
1605 void setFunctor(Functor* functor) {
1606 delete m_glDrawFunctor;
1607 m_glDrawFunctor = functor;
1610 Functor* getFunctor() {
1611 return m_glDrawFunctor;
1614 BaseLayerAndroid* getBaseLayer() {
1618 private: // local state for WebView
1619 // private to getFrameCache(); other functions operate in a different thread
1620 CachedRoot* m_frameCacheUI; // navigation data ready for use
1621 WebViewCore* m_viewImpl;
1622 int m_generation; // associate unique ID with sent kit focus to match with ui
1623 SkPicture* m_navPictureUI;
1624 SkMSec m_ringAnimationEnd;
1625 // Corresponds to the same-named boolean on the java side.
1626 bool m_heightCanMeasure;
1628 SkMSec m_lastDxTime;
1629 SelectText m_selectText;
1630 FindOnPage m_findOnPage;
1632 BaseLayerAndroid* m_baseLayer;
1633 Functor* m_glDrawFunctor;
1634 #if USE(ACCELERATED_COMPOSITING)
1635 GLWebViewState* m_glWebViewState;
1636 bool m_pageSwapCallbackRegistered;
1638 RenderSkinButton* m_buttonSkin;
1639 }; // end of WebView class
1643 * This class holds a function pointer and parameters for calling drawGL into a specific
1644 * viewport. The pointer to the Functor will be put on a framework display list to be called
1645 * when the display list is replayed.
1647 class GLDrawFunctor : Functor {
1649 GLDrawFunctor(WebView* _wvInstance,
1650 bool(WebView::*_funcPtr)(WebCore::IntRect&, WebCore::IntRect*, WebCore::IntRect&, int, WebCore::IntRect&, jfloat, jint),
1651 WebCore::IntRect _viewRect, float _scale, int _extras) {
1652 wvInstance = _wvInstance;
1654 viewRect = _viewRect;
1658 status_t operator()(int messageId, void* data) {
1659 if (viewRect.isEmpty()) {
1660 // NOOP operation if viewport is empty
1664 WebCore::IntRect inval;
1665 int titlebarHeight = webViewRect.height() - viewRect.height();
1667 uirenderer::DrawGlInfo* info = reinterpret_cast<uirenderer::DrawGlInfo*>(data);
1668 WebCore::IntRect localViewRect = viewRect;
1670 localViewRect.move(-1 * localViewRect.x(), -1 * localViewRect.y());
1672 WebCore::IntRect clip(info->clipLeft, info->clipTop,
1673 info->clipRight - info->clipLeft,
1674 info->clipBottom - info->clipTop);
1676 bool retVal = (*wvInstance.*funcPtr)(localViewRect, &inval, webViewRect, titlebarHeight, clip, scale, extras);
1679 if (inval.isEmpty()) {
1680 finalInval = webViewRect;
1683 finalInval.setX(webViewRect.x() + inval.x());
1684 finalInval.setY(webViewRect.y() + titlebarHeight + inval.y());
1685 finalInval.setWidth(inval.width());
1686 finalInval.setHeight(inval.height());
1688 info->dirtyLeft = finalInval.x();
1689 info->dirtyTop = finalInval.y();
1690 info->dirtyRight = finalInval.maxX();
1691 info->dirtyBottom = finalInval.maxY();
1693 // return 1 if invalidation needed, 0 otherwise
1694 return retVal ? 1 : 0;
1696 void updateRect(WebCore::IntRect& _viewRect) {
1697 viewRect = _viewRect;
1699 void updateViewRect(WebCore::IntRect& _viewRect) {
1700 webViewRect = _viewRect;
1703 WebView* wvInstance;
1704 bool (WebView::*funcPtr)(WebCore::IntRect&, WebCore::IntRect*, WebCore::IntRect&, int, WebCore::IntRect&, float, int);
1705 WebCore::IntRect viewRect;
1706 WebCore::IntRect webViewRect;
1711 static jobject createJavaRect(JNIEnv* env, int x, int y, int right, int bottom)
1713 jclass rectClass = env->FindClass("android/graphics/Rect");
1714 jmethodID init = env->GetMethodID(rectClass, "<init>", "(IIII)V");
1715 jobject rect = env->NewObject(rectClass, init, x, y, right, bottom);
1716 env->DeleteLocalRef(rectClass);
1721 * Native JNI methods
1723 static int nativeCacheHitFramePointer(JNIEnv *env, jobject obj)
1725 return reinterpret_cast<int>(GET_NATIVE_VIEW(env, obj)
1726 ->m_cacheHitFrame->framePointer());
1729 static jobject nativeCacheHitNodeBounds(JNIEnv *env, jobject obj)
1731 WebCore::IntRect bounds = GET_NATIVE_VIEW(env, obj)
1732 ->m_cacheHitNode->originalAbsoluteBounds();
1733 return createJavaRect(env, bounds.x(), bounds.y(),
1734 bounds.maxX(), bounds.maxY());
1737 static int nativeCacheHitNodePointer(JNIEnv *env, jobject obj)
1739 return reinterpret_cast<int>(GET_NATIVE_VIEW(env, obj)
1740 ->m_cacheHitNode->nodePointer());
1743 static bool nativeCacheHitIsPlugin(JNIEnv *env, jobject obj)
1745 return GET_NATIVE_VIEW(env, obj)->m_cacheHitNode->isPlugin();
1748 static void nativeClearCursor(JNIEnv *env, jobject obj)
1750 WebView* view = GET_NATIVE_VIEW(env, obj);
1751 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
1752 view->clearCursor();
1755 static void nativeCreate(JNIEnv *env, jobject obj, int viewImpl, jstring drawableDir)
1757 WTF::String dir = jstringToWtfString(env, drawableDir);
1758 WebView* webview = new WebView(env, obj, viewImpl, dir);
1759 // NEED THIS OR SOMETHING LIKE IT!
1763 static jint nativeCursorFramePointer(JNIEnv *env, jobject obj)
1765 WebView* view = GET_NATIVE_VIEW(env, obj);
1766 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1769 const CachedFrame* frame = 0;
1770 (void) root->currentCursor(&frame);
1771 return reinterpret_cast<int>(frame ? frame->framePointer() : 0);
1774 static const CachedNode* getCursorNode(JNIEnv *env, jobject obj)
1776 WebView* view = GET_NATIVE_VIEW(env, obj);
1777 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1778 return root ? root->currentCursor() : 0;
1781 static const CachedNode* getCursorNode(JNIEnv *env, jobject obj,
1782 const CachedFrame** frame)
1784 WebView* view = GET_NATIVE_VIEW(env, obj);
1785 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1786 return root ? root->currentCursor(frame) : 0;
1789 static const CachedNode* getFocusCandidate(JNIEnv *env, jobject obj,
1790 const CachedFrame** frame)
1792 WebView* view = GET_NATIVE_VIEW(env, obj);
1793 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1796 const CachedNode* cursor = root->currentCursor(frame);
1797 if (cursor && cursor->wantsKeyEvents())
1799 return root->currentFocus(frame);
1802 static bool focusCandidateHasNextTextfield(JNIEnv *env, jobject obj)
1804 WebView* view = GET_NATIVE_VIEW(env, obj);
1805 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1808 const CachedNode* cursor = root->currentCursor();
1809 if (!cursor || !cursor->isTextInput())
1810 cursor = root->currentFocus();
1811 if (!cursor || !cursor->isTextInput()) return false;
1812 return root->nextTextField(cursor, 0);
1815 static const CachedNode* getFocusNode(JNIEnv *env, jobject obj)
1817 WebView* view = GET_NATIVE_VIEW(env, obj);
1818 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1819 return root ? root->currentFocus() : 0;
1822 static const CachedNode* getFocusNode(JNIEnv *env, jobject obj,
1823 const CachedFrame** frame)
1825 WebView* view = GET_NATIVE_VIEW(env, obj);
1826 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1827 return root ? root->currentFocus(frame) : 0;
1830 static const CachedInput* getInputCandidate(JNIEnv *env, jobject obj)
1832 WebView* view = GET_NATIVE_VIEW(env, obj);
1833 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1836 const CachedFrame* frame;
1837 const CachedNode* cursor = root->currentCursor(&frame);
1838 if (!cursor || !cursor->wantsKeyEvents())
1839 cursor = root->currentFocus(&frame);
1840 return cursor ? frame->textInput(cursor) : 0;
1843 static jboolean nativePageShouldHandleShiftAndArrows(JNIEnv *env, jobject obj)
1845 const CachedNode* focus = getFocusNode(env, obj);
1846 if (!focus) return false;
1847 // Plugins handle shift and arrows whether or not they have focus.
1848 if (focus->isPlugin()) return true;
1849 const CachedNode* cursor = getCursorNode(env, obj);
1850 // ContentEditable nodes should only receive shift and arrows if they have
1851 // both the cursor and the focus.
1852 return cursor && cursor->nodePointer() == focus->nodePointer()
1853 && cursor->isContentEditable();
1856 static jobject nativeCursorNodeBounds(JNIEnv *env, jobject obj)
1858 const CachedFrame* frame;
1859 const CachedNode* node = getCursorNode(env, obj, &frame);
1860 WebCore::IntRect bounds = node ? node->bounds(frame)
1861 : WebCore::IntRect(0, 0, 0, 0);
1862 return createJavaRect(env, bounds.x(), bounds.y(),
1863 bounds.maxX(), bounds.maxY());
1866 static jint nativeCursorNodePointer(JNIEnv *env, jobject obj)
1868 const CachedNode* node = getCursorNode(env, obj);
1869 return reinterpret_cast<int>(node ? node->nodePointer() : 0);
1872 static jobject nativeCursorPosition(JNIEnv *env, jobject obj)
1874 WebView* view = GET_NATIVE_VIEW(env, obj);
1875 const CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
1876 WebCore::IntPoint pos = WebCore::IntPoint(0, 0);
1878 root->getSimulatedMousePosition(&pos);
1879 jclass pointClass = env->FindClass("android/graphics/Point");
1880 jmethodID init = env->GetMethodID(pointClass, "<init>", "(II)V");
1881 jobject point = env->NewObject(pointClass, init, pos.x(), pos.y());
1882 env->DeleteLocalRef(pointClass);
1886 static WebCore::IntRect jrect_to_webrect(JNIEnv* env, jobject obj)
1889 GraphicsJNI::get_jrect(env, obj, &L, &T, &R, &B);
1890 return WebCore::IntRect(L, T, R - L, B - T);
1893 static bool nativeCursorIntersects(JNIEnv *env, jobject obj, jobject visRect)
1895 const CachedFrame* frame;
1896 const CachedNode* node = getCursorNode(env, obj, &frame);
1897 return node ? node->bounds(frame).intersects(
1898 jrect_to_webrect(env, visRect)) : false;
1901 static bool nativeCursorIsAnchor(JNIEnv *env, jobject obj)
1903 const CachedNode* node = getCursorNode(env, obj);
1904 return node ? node->isAnchor() : false;
1907 static bool nativeCursorIsTextInput(JNIEnv *env, jobject obj)
1909 const CachedNode* node = getCursorNode(env, obj);
1910 return node ? node->isTextInput() : false;
1913 static jobject nativeCursorText(JNIEnv *env, jobject obj)
1915 const CachedNode* node = getCursorNode(env, obj);
1918 WTF::String value = node->getExport();
1919 return wtfStringToJstring(env, value);
1922 static void nativeDebugDump(JNIEnv *env, jobject obj)
1925 WebView* view = GET_NATIVE_VIEW(env, obj);
1926 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
1931 static jint nativeDraw(JNIEnv *env, jobject obj, jobject canv, jint color,
1932 jint extras, jboolean split) {
1933 SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, canv);
1934 return reinterpret_cast<jint>(GET_NATIVE_VIEW(env, obj)->draw(canvas, color, extras, split));
1937 static jint nativeGetDrawGLFunction(JNIEnv *env, jobject obj, jobject jrect, jobject jviewrect,
1938 jfloat scale, jint extras) {
1939 WebCore::IntRect viewRect;
1940 if (jrect == NULL) {
1941 viewRect = WebCore::IntRect();
1943 viewRect = jrect_to_webrect(env, jrect);
1945 WebView *wvInstance = GET_NATIVE_VIEW(env, obj);
1946 GLDrawFunctor* functor = new GLDrawFunctor(wvInstance, &android::WebView::drawGL,
1947 viewRect, scale, extras);
1948 wvInstance->setFunctor((Functor*) functor);
1950 WebCore::IntRect webViewRect;
1951 if (jviewrect == NULL) {
1952 webViewRect = WebCore::IntRect();
1954 webViewRect = jrect_to_webrect(env, jviewrect);
1956 functor->updateViewRect(webViewRect);
1958 return (jint)functor;
1961 static void nativeUpdateDrawGLFunction(JNIEnv *env, jobject obj, jobject jrect, jobject jviewrect) {
1962 WebView *wvInstance = GET_NATIVE_VIEW(env, obj);
1963 if (wvInstance != NULL) {
1964 GLDrawFunctor* functor = (GLDrawFunctor*) wvInstance->getFunctor();
1965 if (functor != NULL) {
1966 WebCore::IntRect viewRect;
1967 if (jrect == NULL) {
1968 viewRect = WebCore::IntRect();
1970 viewRect = jrect_to_webrect(env, jrect);
1972 functor->updateRect(viewRect);
1974 WebCore::IntRect webViewRect;
1975 if (jviewrect == NULL) {
1976 webViewRect = WebCore::IntRect();
1978 webViewRect = jrect_to_webrect(env, jviewrect);
1980 functor->updateViewRect(webViewRect);
1985 static bool nativeEvaluateLayersAnimations(JNIEnv *env, jobject obj)
1987 #if USE(ACCELERATED_COMPOSITING)
1988 LayerAndroid* root = GET_NATIVE_VIEW(env, obj)->compositeRoot();
1990 return root->evaluateAnimations();
1995 static void nativeSetBaseLayer(JNIEnv *env, jobject obj, jint layer, jobject inval,
1996 jboolean showVisualIndicator,
1997 jboolean isPictureAfterFirstLayout,
1998 jboolean registerPageSwapCallback)
2000 BaseLayerAndroid* layerImpl = reinterpret_cast<BaseLayerAndroid*>(layer);
2001 SkRegion invalRegion;
2003 invalRegion = *GraphicsJNI::getNativeRegion(env, inval);
2004 GET_NATIVE_VIEW(env, obj)->setBaseLayer(layerImpl, invalRegion, showVisualIndicator,
2005 isPictureAfterFirstLayout,
2006 registerPageSwapCallback);
2009 static void nativeGetTextSelectionRegion(JNIEnv *env, jobject obj, jobject region)
2013 SkRegion* nregion = GraphicsJNI::getNativeRegion(env, region);
2014 GET_NATIVE_VIEW(env, obj)->getTextSelectionRegion(nregion);
2017 static BaseLayerAndroid* nativeGetBaseLayer(JNIEnv *env, jobject obj)
2019 return GET_NATIVE_VIEW(env, obj)->getBaseLayer();
2022 static void nativeReplaceBaseContent(JNIEnv *env, jobject obj, jint content)
2024 PictureSet* set = reinterpret_cast<PictureSet*>(content);
2025 GET_NATIVE_VIEW(env, obj)->replaceBaseContent(set);
2028 static void nativeCopyBaseContentToPicture(JNIEnv *env, jobject obj, jobject pict)
2030 SkPicture* picture = GraphicsJNI::getNativePicture(env, pict);
2031 GET_NATIVE_VIEW(env, obj)->copyBaseContentToPicture(picture);
2034 static bool nativeHasContent(JNIEnv *env, jobject obj)
2036 return GET_NATIVE_VIEW(env, obj)->hasContent();
2039 static jobject nativeImageURI(JNIEnv *env, jobject obj, jint x, jint y)
2041 WebView* view = GET_NATIVE_VIEW(env, obj);
2042 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2043 WTF::String uri = view->imageURI(x, y);
2044 return wtfStringToJstring(env, uri);
2047 static jint nativeFocusCandidateFramePointer(JNIEnv *env, jobject obj)
2049 WebView* view = GET_NATIVE_VIEW(env, obj);
2050 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
2053 const CachedFrame* frame = 0;
2054 const CachedNode* cursor = root->currentCursor(&frame);
2055 if (!cursor || !cursor->wantsKeyEvents())
2056 (void) root->currentFocus(&frame);
2057 return reinterpret_cast<int>(frame ? frame->framePointer() : 0);
2060 static bool nativeFocusCandidateIsPassword(JNIEnv *env, jobject obj)
2062 const CachedInput* input = getInputCandidate(env, obj);
2063 return input && input->getType() == CachedInput::PASSWORD;
2066 static bool nativeFocusCandidateIsRtlText(JNIEnv *env, jobject obj)
2068 const CachedInput* input = getInputCandidate(env, obj);
2069 return input ? input->isRtlText() : false;
2072 static bool nativeFocusCandidateIsTextInput(JNIEnv *env, jobject obj)
2074 const CachedNode* node = getFocusCandidate(env, obj, 0);
2075 return node ? node->isTextInput() : false;
2078 static jint nativeFocusCandidateMaxLength(JNIEnv *env, jobject obj)
2080 const CachedInput* input = getInputCandidate(env, obj);
2081 return input ? input->maxLength() : false;
2084 static jint nativeFocusCandidateIsAutoComplete(JNIEnv *env, jobject obj)
2086 const CachedInput* input = getInputCandidate(env, obj);
2087 return input ? input->autoComplete() : false;
2090 static jobject nativeFocusCandidateName(JNIEnv *env, jobject obj)
2092 const CachedInput* input = getInputCandidate(env, obj);
2095 const WTF::String& name = input->name();
2096 return wtfStringToJstring(env, name);
2099 static jobject nativeFocusCandidateNodeBounds(JNIEnv *env, jobject obj)
2101 const CachedFrame* frame;
2102 const CachedNode* node = getFocusCandidate(env, obj, &frame);
2103 WebCore::IntRect bounds = node ? node->bounds(frame)
2104 : WebCore::IntRect(0, 0, 0, 0);
2105 // Inset the rect by 1 unit, so that the focus candidate's border can still
2106 // be seen behind it.
2107 return createJavaRect(env, bounds.x() + 1, bounds.y() + 1,
2108 bounds.maxX() - 1, bounds.maxY() - 1);
2111 static jobject nativeFocusCandidatePaddingRect(JNIEnv *env, jobject obj)
2113 const CachedInput* input = getInputCandidate(env, obj);
2116 // Note that the Java Rect is being used to pass four integers, rather than
2117 // being used as an actual rectangle.
2118 return createJavaRect(env, input->paddingLeft(), input->paddingTop(),
2119 input->paddingRight(), input->paddingBottom());
2122 static jint nativeFocusCandidatePointer(JNIEnv *env, jobject obj)
2124 const CachedNode* node = getFocusCandidate(env, obj, 0);
2125 return reinterpret_cast<int>(node ? node->nodePointer() : 0);
2128 static jobject nativeFocusCandidateText(JNIEnv *env, jobject obj)
2130 const CachedNode* node = getFocusCandidate(env, obj, 0);
2133 WTF::String value = node->getExport();
2134 return wtfStringToJstring(env, value);
2137 static int nativeFocusCandidateLineHeight(JNIEnv *env, jobject obj)
2139 const CachedInput* input = getInputCandidate(env, obj);
2140 return input ? input->lineHeight() : 0;
2143 static jfloat nativeFocusCandidateTextSize(JNIEnv *env, jobject obj)
2145 const CachedInput* input = getInputCandidate(env, obj);
2146 return input ? input->textSize() : 0.f;
2149 static int nativeFocusCandidateType(JNIEnv *env, jobject obj)
2151 const CachedInput* input = getInputCandidate(env, obj);
2153 return CachedInput::NONE;
2155 if (input->isTextArea())
2156 return CachedInput::TEXT_AREA;
2158 return input->getType();
2161 static bool nativeFocusIsPlugin(JNIEnv *env, jobject obj)
2163 const CachedNode* node = getFocusNode(env, obj);
2164 return node ? node->isPlugin() : false;
2167 static jobject nativeFocusNodeBounds(JNIEnv *env, jobject obj)
2169 const CachedFrame* frame;
2170 const CachedNode* node = getFocusNode(env, obj, &frame);
2171 WebCore::IntRect bounds = node ? node->bounds(frame)
2172 : WebCore::IntRect(0, 0, 0, 0);
2173 return createJavaRect(env, bounds.x(), bounds.y(),
2174 bounds.maxX(), bounds.maxY());
2177 static jint nativeFocusNodePointer(JNIEnv *env, jobject obj)
2179 const CachedNode* node = getFocusNode(env, obj);
2180 return node ? reinterpret_cast<int>(node->nodePointer()) : 0;
2183 static bool nativeCursorWantsKeyEvents(JNIEnv* env, jobject jwebview) {
2184 WebView* view = GET_NATIVE_VIEW(env, jwebview);
2185 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2186 return view->cursorWantsKeyEvents();
2189 static void nativeHideCursor(JNIEnv *env, jobject obj)
2191 WebView* view = GET_NATIVE_VIEW(env, obj);
2192 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2196 static void nativeInstrumentReport(JNIEnv *env, jobject obj)
2198 #ifdef ANDROID_INSTRUMENT
2199 TimeCounter::reportNow();
2203 static void nativeSelectBestAt(JNIEnv *env, jobject obj, jobject jrect)
2205 WebView* view = GET_NATIVE_VIEW(env, obj);
2206 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2207 WebCore::IntRect rect = jrect_to_webrect(env, jrect);
2208 view->selectBestAt(rect);
2211 static void nativeSelectAt(JNIEnv *env, jobject obj, jint x, jint y)
2213 WebView* view = GET_NATIVE_VIEW(env, obj);
2214 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2215 WebCore::IntRect rect = IntRect(x, y , 1, 1);
2216 view->selectBestAt(rect);
2217 if (view->hasCursorNode())
2218 view->showCursorUntimed();
2221 static jobject nativeLayerBounds(JNIEnv* env, jobject obj, jint jlayer)
2224 #if USE(ACCELERATED_COMPOSITING)
2225 LayerAndroid* layer = (LayerAndroid*) jlayer;
2226 r = layer->bounds();
2232 jclass rectClass = env->FindClass("android/graphics/Rect");
2233 jmethodID init = env->GetMethodID(rectClass, "<init>", "(IIII)V");
2234 jobject rect = env->NewObject(rectClass, init, irect.fLeft, irect.fTop,
2235 irect.fRight, irect.fBottom);
2236 env->DeleteLocalRef(rectClass);
2240 static jobject nativeSubtractLayers(JNIEnv* env, jobject obj, jobject jrect)
2242 SkIRect irect = jrect_to_webrect(env, jrect);
2243 #if USE(ACCELERATED_COMPOSITING)
2244 LayerAndroid* root = GET_NATIVE_VIEW(env, obj)->compositeRoot();
2248 rect = root->subtractLayers(rect);
2252 jclass rectClass = env->FindClass("android/graphics/Rect");
2253 jmethodID init = env->GetMethodID(rectClass, "<init>", "(IIII)V");
2254 jobject rect = env->NewObject(rectClass, init, irect.fLeft, irect.fTop,
2255 irect.fRight, irect.fBottom);
2256 env->DeleteLocalRef(rectClass);
2260 static jint nativeTextGeneration(JNIEnv *env, jobject obj)
2262 WebView* view = GET_NATIVE_VIEW(env, obj);
2263 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
2264 return root ? root->textGeneration() : 0;
2267 static bool nativePointInNavCache(JNIEnv *env, jobject obj,
2268 int x, int y, int slop)
2270 return GET_NATIVE_VIEW(env, obj)->pointInNavCache(x, y, slop);
2273 static bool nativeMotionUp(JNIEnv *env, jobject obj,
2274 int x, int y, int slop)
2276 WebView* view = GET_NATIVE_VIEW(env, obj);
2277 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2278 return view->motionUp(x, y, slop);
2281 static bool nativeHasCursorNode(JNIEnv *env, jobject obj)
2283 return GET_NATIVE_VIEW(env, obj)->hasCursorNode();
2286 static bool nativeHasFocusNode(JNIEnv *env, jobject obj)
2288 return GET_NATIVE_VIEW(env, obj)->hasFocusNode();
2291 static bool nativeMoveCursor(JNIEnv *env, jobject obj,
2292 int key, int count, bool ignoreScroll)
2294 WebView* view = GET_NATIVE_VIEW(env, obj);
2295 DBG_NAV_LOGD("env=%p obj=%p view=%p", env, obj, view);
2296 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2297 return view->moveCursor(key, count, ignoreScroll);
2300 static void nativeRecordButtons(JNIEnv* env, jobject obj, bool hasFocus,
2301 bool pressed, bool invalidate)
2303 WebView* view = GET_NATIVE_VIEW(env, obj);
2304 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2305 view->nativeRecordButtons(hasFocus, pressed, invalidate);
2308 static void nativeSetFindIsUp(JNIEnv *env, jobject obj, jboolean isUp)
2310 WebView* view = GET_NATIVE_VIEW(env, obj);
2311 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2312 view->setFindIsUp(isUp);
2315 static void nativeSetFindIsEmpty(JNIEnv *env, jobject obj)
2317 GET_NATIVE_VIEW(env, obj)->setFindIsEmpty();
2320 static void nativeShowCursorTimed(JNIEnv *env, jobject obj)
2322 GET_NATIVE_VIEW(env, obj)->showCursorTimed();
2325 static void nativeSetHeightCanMeasure(JNIEnv *env, jobject obj, bool measure)
2327 WebView* view = GET_NATIVE_VIEW(env, obj);
2328 LOG_ASSERT(view, "view not set in nativeSetHeightCanMeasure");
2329 view->setHeightCanMeasure(measure);
2332 static jobject nativeGetCursorRingBounds(JNIEnv *env, jobject obj)
2334 WebView* view = GET_NATIVE_VIEW(env, obj);
2335 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2336 jclass rectClass = env->FindClass("android/graphics/Rect");
2337 LOG_ASSERT(rectClass, "Could not find Rect class!");
2338 jmethodID init = env->GetMethodID(rectClass, "<init>", "(IIII)V");
2339 LOG_ASSERT(init, "Could not find constructor for Rect");
2340 WebCore::IntRect webRect;
2341 view->cursorRingBounds(&webRect);
2342 jobject rect = env->NewObject(rectClass, init, webRect.x(),
2343 webRect.y(), webRect.maxX(), webRect.maxY());
2344 env->DeleteLocalRef(rectClass);
2348 static int nativeFindAll(JNIEnv *env, jobject obj, jstring findLower,
2349 jstring findUpper, jboolean sameAsLastSearch)
2351 // If one or the other is null, do not search.
2352 if (!(findLower && findUpper))
2354 // Obtain the characters for both the lower case string and the upper case
2355 // string representing the same word.
2356 const jchar* findLowerChars = env->GetStringChars(findLower, 0);
2357 const jchar* findUpperChars = env->GetStringChars(findUpper, 0);
2358 // If one or the other is null, do not search.
2359 if (!(findLowerChars && findUpperChars)) {
2361 env->ReleaseStringChars(findLower, findLowerChars);
2363 env->ReleaseStringChars(findUpper, findUpperChars);
2364 checkException(env);
2367 WebView* view = GET_NATIVE_VIEW(env, obj);
2368 LOG_ASSERT(view, "view not set in nativeFindAll");
2369 CachedRoot* root = view->getFrameCache(WebView::AllowNewer);
2371 env->ReleaseStringChars(findLower, findLowerChars);
2372 env->ReleaseStringChars(findUpper, findUpperChars);
2373 checkException(env);
2376 int length = env->GetStringLength(findLower);
2377 // If the lengths of the strings do not match, then they are not the same
2378 // word, so do not search.
2379 if (!length || env->GetStringLength(findUpper) != length) {
2380 env->ReleaseStringChars(findLower, findLowerChars);
2381 env->ReleaseStringChars(findUpper, findUpperChars);
2382 checkException(env);
2385 int width = root->documentWidth();
2386 int height = root->documentHeight();
2387 // Create a FindCanvas, which allows us to fake draw into it so we can
2388 // figure out where our search string is rendered (and how many times).
2389 FindCanvas canvas(width, height, (const UChar*) findLowerChars,
2390 (const UChar*) findUpperChars, length << 1);
2392 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
2393 canvas.setBitmapDevice(bitmap);
2395 WTF::Vector<MatchInfo>* matches = canvas.detachMatches();
2396 // With setMatches, the WebView takes ownership of matches
2397 view->setMatches(matches, sameAsLastSearch);
2399 env->ReleaseStringChars(findLower, findLowerChars);
2400 env->ReleaseStringChars(findUpper, findUpperChars);
2401 checkException(env);
2402 return canvas.found();
2405 static void nativeFindNext(JNIEnv *env, jobject obj, bool forward)
2407 WebView* view = GET_NATIVE_VIEW(env, obj);
2408 LOG_ASSERT(view, "view not set in nativeFindNext");
2409 view->findNext(forward);
2412 static int nativeFindIndex(JNIEnv *env, jobject obj)
2414 WebView* view = GET_NATIVE_VIEW(env, obj);
2415 LOG_ASSERT(view, "view not set in nativeFindIndex");
2416 return view->currentMatchIndex();
2419 static void nativeUpdateCachedTextfield(JNIEnv *env, jobject obj, jstring updatedText, jint generation)
2421 WebView* view = GET_NATIVE_VIEW(env, obj);
2422 LOG_ASSERT(view, "view not set in nativeUpdateCachedTextfield");
2423 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
2426 const CachedNode* cachedFocusNode = root->currentFocus();
2427 if (!cachedFocusNode || !cachedFocusNode->isTextInput())
2429 WTF::String webcoreString = jstringToWtfString(env, updatedText);
2430 (const_cast<CachedNode*>(cachedFocusNode))->setExport(webcoreString);
2431 root->setTextGeneration(generation);
2432 checkException(env);
2435 static jint nativeGetBlockLeftEdge(JNIEnv *env, jobject obj, jint x, jint y,
2438 WebView* view = GET_NATIVE_VIEW(env, obj);
2439 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2442 return view->getBlockLeftEdge(x, y, scale);
2445 static void nativeDestroy(JNIEnv *env, jobject obj)
2447 WebView* view = GET_NATIVE_VIEW(env, obj);
2448 LOGD("nativeDestroy view: %p", view);
2449 LOG_ASSERT(view, "view not set in nativeDestroy");
2453 static void nativeStopGL(JNIEnv *env, jobject obj)
2455 GET_NATIVE_VIEW(env, obj)->stopGL();
2458 static bool nativeMoveCursorToNextTextInput(JNIEnv *env, jobject obj)
2460 WebView* view = GET_NATIVE_VIEW(env, obj);
2461 CachedRoot* root = view->getFrameCache(WebView::DontAllowNewer);
2464 const CachedNode* current = root->currentCursor();
2465 if (!current || !current->isTextInput())
2466 current = root->currentFocus();
2467 if (!current || !current->isTextInput())
2469 const CachedFrame* frame;
2470 const CachedNode* next = root->nextTextField(current, &frame);
2473 const WebCore::IntRect& bounds = next->bounds(frame);
2474 root->rootHistory()->setMouseBounds(bounds);
2475 view->getWebViewCore()->updateCursorBounds(root, frame, next);
2476 view->showCursorUntimed();
2477 root->setCursor(const_cast<CachedFrame*>(frame),
2478 const_cast<CachedNode*>(next));
2479 view->sendMoveFocus(static_cast<WebCore::Frame*>(frame->framePointer()),
2480 static_cast<WebCore::Node*>(next->nodePointer()));
2481 if (!next->isInLayer())
2482 view->scrollRectOnScreen(bounds);
2483 view->getWebViewCore()->m_moveGeneration++;
2487 static int nativeMoveGeneration(JNIEnv *env, jobject obj)
2489 WebView* view = GET_NATIVE_VIEW(env, obj);
2492 return view->moveGeneration();
2495 static void nativeMoveSelection(JNIEnv *env, jobject obj, int x, int y)
2497 GET_NATIVE_VIEW(env, obj)->moveSelection(x, y);
2500 static void nativeResetSelection(JNIEnv *env, jobject obj)
2502 return GET_NATIVE_VIEW(env, obj)->resetSelection();
2505 static jobject nativeSelectableText(JNIEnv* env, jobject obj)
2507 IntPoint pos = GET_NATIVE_VIEW(env, obj)->selectableText();
2508 jclass pointClass = env->FindClass("android/graphics/Point");
2509 jmethodID init = env->GetMethodID(pointClass, "<init>", "(II)V");
2510 jobject point = env->NewObject(pointClass, init, pos.x(), pos.y());
2511 env->DeleteLocalRef(pointClass);
2515 static void nativeSelectAll(JNIEnv* env, jobject obj)
2517 GET_NATIVE_VIEW(env, obj)->selectAll();
2520 static void nativeSetExtendSelection(JNIEnv *env, jobject obj)
2522 GET_NATIVE_VIEW(env, obj)->setExtendSelection();
2525 static jboolean nativeStartSelection(JNIEnv *env, jobject obj, int x, int y)
2527 return GET_NATIVE_VIEW(env, obj)->startSelection(x, y);
2530 static jboolean nativeWordSelection(JNIEnv *env, jobject obj, int x, int y)
2532 return GET_NATIVE_VIEW(env, obj)->wordSelection(x, y);
2535 static void nativeExtendSelection(JNIEnv *env, jobject obj, int x, int y)
2537 GET_NATIVE_VIEW(env, obj)->extendSelection(x, y);
2540 static jobject nativeGetSelection(JNIEnv *env, jobject obj)
2542 WebView* view = GET_NATIVE_VIEW(env, obj);
2543 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2544 String selection = view->getSelection();
2545 return wtfStringToJstring(env, selection);
2548 static jboolean nativeHitSelection(JNIEnv *env, jobject obj, int x, int y)
2550 return GET_NATIVE_VIEW(env, obj)->hitSelection(x, y);
2553 static jint nativeSelectionX(JNIEnv *env, jobject obj)
2555 return GET_NATIVE_VIEW(env, obj)->selectionX();
2558 static jint nativeSelectionY(JNIEnv *env, jobject obj)
2560 return GET_NATIVE_VIEW(env, obj)->selectionY();
2563 static void nativeSetSelectionPointer(JNIEnv *env, jobject obj, jboolean set,
2564 jfloat scale, jint x, jint y)
2566 GET_NATIVE_VIEW(env, obj)->setSelectionPointer(set, scale, x, y);
2569 static void nativeRegisterPageSwapCallback(JNIEnv *env, jobject obj)
2571 GET_NATIVE_VIEW(env, obj)->registerPageSwapCallback();
2574 static void nativeTileProfilingStart(JNIEnv *env, jobject obj)
2576 TilesManager::instance()->getProfiler()->start();
2579 static float nativeTileProfilingStop(JNIEnv *env, jobject obj)
2581 return TilesManager::instance()->getProfiler()->stop();
2584 static void nativeTileProfilingClear(JNIEnv *env, jobject obj)
2586 TilesManager::instance()->getProfiler()->clear();
2589 static int nativeTileProfilingNumFrames(JNIEnv *env, jobject obj)
2591 return TilesManager::instance()->getProfiler()->numFrames();
2594 static int nativeTileProfilingNumTilesInFrame(JNIEnv *env, jobject obj, int frame)
2596 return TilesManager::instance()->getProfiler()->numTilesInFrame(frame);
2599 static int nativeTileProfilingGetInt(JNIEnv *env, jobject obj, int frame, int tile, jstring jkey)
2601 WTF::String key = jstringToWtfString(env, jkey);
2602 TileProfileRecord* record = TilesManager::instance()->getProfiler()->getTile(frame, tile);
2605 return record->left;
2609 return record->right;
2610 if (key == "bottom")
2611 return record->bottom;
2613 return record->level;
2614 if (key == "isReady")
2615 return record->isReady ? 1 : 0;
2619 static float nativeTileProfilingGetFloat(JNIEnv *env, jobject obj, int frame, int tile, jstring jkey)
2621 TileProfileRecord* record = TilesManager::instance()->getProfiler()->getTile(frame, tile);
2622 return record->scale;
2625 #ifdef ANDROID_DUMP_DISPLAY_TREE
2626 static void dumpToFile(const char text[], void* file) {
2627 fwrite(text, 1, strlen(text), reinterpret_cast<FILE*>(file));
2628 fwrite("\n", 1, 1, reinterpret_cast<FILE*>(file));
2632 static bool nativeSetProperty(JNIEnv *env, jobject obj, jstring jkey, jstring jvalue)
2634 WTF::String key = jstringToWtfString(env, jkey);
2635 WTF::String value = jstringToWtfString(env, jvalue);
2636 if (key == "inverted") {
2637 if (value == "true")
2638 TilesManager::instance()->setInvertedScreen(true);
2640 TilesManager::instance()->setInvertedScreen(false);
2643 if (key == "inverted_contrast") {
2644 float contrast = value.toFloat();
2645 TilesManager::instance()->setInvertedScreenContrast(contrast);
2648 if (key == "enable_cpu_upload_path") {
2649 TilesManager::instance()->transferQueue()->setTextureUploadType(
2650 value == "true" ? CpuUpload : GpuUpload);
2656 static jstring nativeGetProperty(JNIEnv *env, jobject obj, jstring key)
2661 static void nativeOnTrimMemory(JNIEnv *env, jobject obj, jint level)
2663 if (TilesManager::hardwareAccelerationEnabled()) {
2664 bool freeAllTextures = (level > TRIM_MEMORY_UI_HIDDEN);
2665 TilesManager::instance()->deallocateTextures(freeAllTextures);
2669 static void nativeDumpDisplayTree(JNIEnv* env, jobject jwebview, jstring jurl)
2671 #ifdef ANDROID_DUMP_DISPLAY_TREE
2672 WebView* view = GET_NATIVE_VIEW(env, jwebview);
2673 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2675 if (view && view->getWebViewCore()) {
2676 FILE* file = fopen(DISPLAY_TREE_LOG_FILE, "w");
2678 SkFormatDumper dumper(dumpToFile, file);
2681 const char* str = env->GetStringUTFChars(jurl, 0);
2682 SkDebugf("Dumping %s to %s\n", str, DISPLAY_TREE_LOG_FILE);
2683 dumpToFile(str, file);
2684 env->ReleaseStringUTFChars(jurl, str);
2686 // now dump the display tree
2687 SkDumpCanvas canvas(&dumper);
2688 // this will playback the picture into the canvas, which will
2689 // spew its contents to the dumper
2690 view->draw(&canvas, 0, 0, false);
2691 // we're done with the file now
2692 fwrite("\n", 1, 1, file);
2695 #if USE(ACCELERATED_COMPOSITING)
2696 const LayerAndroid* rootLayer = view->compositeRoot();
2698 FILE* file = fopen(LAYERS_TREE_LOG_FILE,"w");
2700 rootLayer->dumpLayers(file, 0);
2709 static int nativeScrollableLayer(JNIEnv* env, jobject jwebview, jint x, jint y,
2710 jobject rect, jobject bounds)
2712 WebView* view = GET_NATIVE_VIEW(env, jwebview);
2713 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2714 SkIRect nativeRect, nativeBounds;
2715 int id = view->scrollableLayer(x, y, &nativeRect, &nativeBounds);
2717 GraphicsJNI::irect_to_jrect(nativeRect, env, rect);
2719 GraphicsJNI::irect_to_jrect(nativeBounds, env, bounds);
2723 static bool nativeScrollLayer(JNIEnv* env, jobject obj, jint layerId, jint x,
2726 #if ENABLE(ANDROID_OVERFLOW_SCROLL)
2727 WebView* view = GET_NATIVE_VIEW(env, obj);
2728 LayerAndroid* root = view->compositeRoot();
2731 LayerAndroid* layer = root->findById(layerId);
2732 if (!layer || !layer->contentIsScrollable())
2734 return static_cast<ScrollableLayerAndroid*>(layer)->scrollTo(x, y);
2739 static void nativeSetIsScrolling(JNIEnv* env, jobject jwebview, jboolean isScrolling)
2741 WebView* view = GET_NATIVE_VIEW(env, jwebview);
2742 LOG_ASSERT(view, "view not set in %s", __FUNCTION__);
2743 view->setIsScrolling(isScrolling);
2746 static void nativeUseHardwareAccelSkia(JNIEnv*, jobject, jboolean enabled)
2748 BaseRenderer::setCurrentRendererType(enabled ? BaseRenderer::Ganesh : BaseRenderer::Raster);
2751 static int nativeGetBackgroundColor(JNIEnv* env, jobject obj)
2753 WebView* view = GET_NATIVE_VIEW(env, obj);
2754 BaseLayerAndroid* baseLayer = view->getBaseLayer();
2756 WebCore::Color color = baseLayer->getBackgroundColor();
2757 if (color.isValid())
2758 return SkColorSetARGB(color.alpha(), color.red(),
2759 color.green(), color.blue());
2761 return SK_ColorWHITE;
2767 static JNINativeMethod gJavaWebViewMethods[] = {
2768 { "nativeCacheHitFramePointer", "()I",
2769 (void*) nativeCacheHitFramePointer },
2770 { "nativeCacheHitIsPlugin", "()Z",
2771 (void*) nativeCacheHitIsPlugin },
2772 { "nativeCacheHitNodeBounds", "()Landroid/graphics/Rect;",
2773 (void*) nativeCacheHitNodeBounds },
2774 { "nativeCacheHitNodePointer", "()I",
2775 (void*) nativeCacheHitNodePointer },
2776 { "nativeClearCursor", "()V",
2777 (void*) nativeClearCursor },
2778 { "nativeCreate", "(ILjava/lang/String;)V",
2779 (void*) nativeCreate },
2780 { "nativeCursorFramePointer", "()I",
2781 (void*) nativeCursorFramePointer },
2782 { "nativePageShouldHandleShiftAndArrows", "()Z",
2783 (void*) nativePageShouldHandleShiftAndArrows },
2784 { "nativeCursorNodeBounds", "()Landroid/graphics/Rect;",
2785 (void*) nativeCursorNodeBounds },
2786 { "nativeCursorNodePointer", "()I",
2787 (void*) nativeCursorNodePointer },
2788 { "nativeCursorIntersects", "(Landroid/graphics/Rect;)Z",
2789 (void*) nativeCursorIntersects },
2790 { "nativeCursorIsAnchor", "()Z",
2791 (void*) nativeCursorIsAnchor },
2792 { "nativeCursorIsTextInput", "()Z",
2793 (void*) nativeCursorIsTextInput },
2794 { "nativeCursorPosition", "()Landroid/graphics/Point;",
2795 (void*) nativeCursorPosition },
2796 { "nativeCursorText", "()Ljava/lang/String;",
2797 (void*) nativeCursorText },
2798 { "nativeCursorWantsKeyEvents", "()Z",
2799 (void*)nativeCursorWantsKeyEvents },
2800 { "nativeDebugDump", "()V",
2801 (void*) nativeDebugDump },
2802 { "nativeDestroy", "()V",
2803 (void*) nativeDestroy },
2804 { "nativeDraw", "(Landroid/graphics/Canvas;IIZ)I",
2805 (void*) nativeDraw },
2806 { "nativeGetDrawGLFunction", "(Landroid/graphics/Rect;Landroid/graphics/Rect;FI)I",
2807 (void*) nativeGetDrawGLFunction },
2808 { "nativeUpdateDrawGLFunction", "(Landroid/graphics/Rect;Landroid/graphics/Rect;)V",
2809 (void*) nativeUpdateDrawGLFunction },
2810 { "nativeDumpDisplayTree", "(Ljava/lang/String;)V",
2811 (void*) nativeDumpDisplayTree },
2812 { "nativeEvaluateLayersAnimations", "()Z",
2813 (void*) nativeEvaluateLayersAnimations },
2814 { "nativeExtendSelection", "(II)V",
2815 (void*) nativeExtendSelection },
2816 { "nativeFindAll", "(Ljava/lang/String;Ljava/lang/String;Z)I",
2817 (void*) nativeFindAll },
2818 { "nativeFindNext", "(Z)V",
2819 (void*) nativeFindNext },
2820 { "nativeFindIndex", "()I",
2821 (void*) nativeFindIndex},
2822 { "nativeFocusCandidateFramePointer", "()I",
2823 (void*) nativeFocusCandidateFramePointer },
2824 { "nativeFocusCandidateHasNextTextfield", "()Z",
2825 (void*) focusCandidateHasNextTextfield },
2826 { "nativeFocusCandidateIsPassword", "()Z",
2827 (void*) nativeFocusCandidateIsPassword },
2828 { "nativeFocusCandidateIsRtlText", "()Z",
2829 (void*) nativeFocusCandidateIsRtlText },
2830 { "nativeFocusCandidateIsTextInput", "()Z",
2831 (void*) nativeFocusCandidateIsTextInput },
2832 { "nativeFocusCandidateLineHeight", "()I",
2833 (void*) nativeFocusCandidateLineHeight },
2834 { "nativeFocusCandidateMaxLength", "()I",
2835 (void*) nativeFocusCandidateMaxLength },
2836 { "nativeFocusCandidateIsAutoComplete", "()Z",
2837 (void*) nativeFocusCandidateIsAutoComplete },
2838 { "nativeFocusCandidateName", "()Ljava/lang/String;",
2839 (void*) nativeFocusCandidateName },
2840 { "nativeFocusCandidateNodeBounds", "()Landroid/graphics/Rect;",
2841 (void*) nativeFocusCandidateNodeBounds },
2842 { "nativeFocusCandidatePaddingRect", "()Landroid/graphics/Rect;",
2843 (void*) nativeFocusCandidatePaddingRect },
2844 { "nativeFocusCandidatePointer", "()I",
2845 (void*) nativeFocusCandidatePointer },
2846 { "nativeFocusCandidateText", "()Ljava/lang/String;",
2847 (void*) nativeFocusCandidateText },
2848 { "nativeFocusCandidateTextSize", "()F",
2849 (void*) nativeFocusCandidateTextSize },
2850 { "nativeFocusCandidateType", "()I",
2851 (void*) nativeFocusCandidateType },
2852 { "nativeFocusIsPlugin", "()Z",
2853 (void*) nativeFocusIsPlugin },
2854 { "nativeFocusNodeBounds", "()Landroid/graphics/Rect;",
2855 (void*) nativeFocusNodeBounds },
2856 { "nativeFocusNodePointer", "()I",
2857 (void*) nativeFocusNodePointer },
2858 { "nativeGetCursorRingBounds", "()Landroid/graphics/Rect;",
2859 (void*) nativeGetCursorRingBounds },
2860 { "nativeGetSelection", "()Ljava/lang/String;",
2861 (void*) nativeGetSelection },
2862 { "nativeHasCursorNode", "()Z",
2863 (void*) nativeHasCursorNode },
2864 { "nativeHasFocusNode", "()Z",
2865 (void*) nativeHasFocusNode },
2866 { "nativeHideCursor", "()V",
2867 (void*) nativeHideCursor },
2868 { "nativeHitSelection", "(II)Z",
2869 (void*) nativeHitSelection },
2870 { "nativeImageURI", "(II)Ljava/lang/String;",
2871 (void*) nativeImageURI },
2872 { "nativeInstrumentReport", "()V",
2873 (void*) nativeInstrumentReport },
2874 { "nativeLayerBounds", "(I)Landroid/graphics/Rect;",
2875 (void*) nativeLayerBounds },
2876 { "nativeMotionUp", "(III)Z",
2877 (void*) nativeMotionUp },
2878 { "nativeMoveCursor", "(IIZ)Z",
2879 (void*) nativeMoveCursor },
2880 { "nativeMoveCursorToNextTextInput", "()Z",
2881 (void*) nativeMoveCursorToNextTextInput },
2882 { "nativeMoveGeneration", "()I",
2883 (void*) nativeMoveGeneration },
2884 { "nativeMoveSelection", "(II)V",
2885 (void*) nativeMoveSelection },
2886 { "nativePointInNavCache", "(III)Z",
2887 (void*) nativePointInNavCache },
2888 { "nativeRecordButtons", "(ZZZ)V",
2889 (void*) nativeRecordButtons },
2890 { "nativeResetSelection", "()V",
2891 (void*) nativeResetSelection },
2892 { "nativeSelectableText", "()Landroid/graphics/Point;",
2893 (void*) nativeSelectableText },
2894 { "nativeSelectAll", "()V",
2895 (void*) nativeSelectAll },
2896 { "nativeSelectBestAt", "(Landroid/graphics/Rect;)V",
2897 (void*) nativeSelectBestAt },
2898 { "nativeSelectAt", "(II)V",
2899 (void*) nativeSelectAt },
2900 { "nativeSelectionX", "()I",
2901 (void*) nativeSelectionX },
2902 { "nativeSelectionY", "()I",
2903 (void*) nativeSelectionY },
2904 { "nativeSetExtendSelection", "()V",
2905 (void*) nativeSetExtendSelection },
2906 { "nativeSetFindIsEmpty", "()V",
2907 (void*) nativeSetFindIsEmpty },
2908 { "nativeSetFindIsUp", "(Z)V",
2909 (void*) nativeSetFindIsUp },
2910 { "nativeSetHeightCanMeasure", "(Z)V",
2911 (void*) nativeSetHeightCanMeasure },
2912 { "nativeSetBaseLayer", "(ILandroid/graphics/Region;ZZZ)V",
2913 (void*) nativeSetBaseLayer },
2914 { "nativeGetTextSelectionRegion", "(Landroid/graphics/Region;)V",
2915 (void*) nativeGetTextSelectionRegion },
2916 { "nativeGetBaseLayer", "()I",
2917 (void*) nativeGetBaseLayer },
2918 { "nativeReplaceBaseContent", "(I)V",
2919 (void*) nativeReplaceBaseContent },
2920 { "nativeCopyBaseContentToPicture", "(Landroid/graphics/Picture;)V",
2921 (void*) nativeCopyBaseContentToPicture },
2922 { "nativeHasContent", "()Z",
2923 (void*) nativeHasContent },
2924 { "nativeSetSelectionPointer", "(ZFII)V",
2925 (void*) nativeSetSelectionPointer },
2926 { "nativeShowCursorTimed", "()V",
2927 (void*) nativeShowCursorTimed },
2928 { "nativeRegisterPageSwapCallback", "()V",
2929 (void*) nativeRegisterPageSwapCallback },
2930 { "nativeTileProfilingStart", "()V",
2931 (void*) nativeTileProfilingStart },
2932 { "nativeTileProfilingStop", "()F",
2933 (void*) nativeTileProfilingStop },
2934 { "nativeTileProfilingClear", "()V",
2935 (void*) nativeTileProfilingClear },
2936 { "nativeTileProfilingNumFrames", "()I",
2937 (void*) nativeTileProfilingNumFrames },
2938 { "nativeTileProfilingNumTilesInFrame", "(I)I",
2939 (void*) nativeTileProfilingNumTilesInFrame },
2940 { "nativeTileProfilingGetInt", "(IILjava/lang/String;)I",
2941 (void*) nativeTileProfilingGetInt },
2942 { "nativeTileProfilingGetFloat", "(IILjava/lang/String;)F",
2943 (void*) nativeTileProfilingGetFloat },
2944 { "nativeStartSelection", "(II)Z",
2945 (void*) nativeStartSelection },
2946 { "nativeStopGL", "()V",
2947 (void*) nativeStopGL },
2948 { "nativeSubtractLayers", "(Landroid/graphics/Rect;)Landroid/graphics/Rect;",
2949 (void*) nativeSubtractLayers },
2950 { "nativeTextGeneration", "()I",
2951 (void*) nativeTextGeneration },
2952 { "nativeUpdateCachedTextfield", "(Ljava/lang/String;I)V",
2953 (void*) nativeUpdateCachedTextfield },
2954 { "nativeWordSelection", "(II)Z",
2955 (void*) nativeWordSelection },
2956 { "nativeGetBlockLeftEdge", "(IIF)I",
2957 (void*) nativeGetBlockLeftEdge },
2958 { "nativeScrollableLayer", "(IILandroid/graphics/Rect;Landroid/graphics/Rect;)I",
2959 (void*) nativeScrollableLayer },
2960 { "nativeScrollLayer", "(III)Z",
2961 (void*) nativeScrollLayer },
2962 { "nativeSetIsScrolling", "(Z)V",
2963 (void*) nativeSetIsScrolling },
2964 { "nativeUseHardwareAccelSkia", "(Z)V",
2965 (void*) nativeUseHardwareAccelSkia },
2966 { "nativeGetBackgroundColor", "()I",
2967 (void*) nativeGetBackgroundColor },
2968 { "nativeSetProperty", "(Ljava/lang/String;Ljava/lang/String;)Z",
2969 (void*) nativeSetProperty },
2970 { "nativeGetProperty", "(Ljava/lang/String;)Ljava/lang/String;",
2971 (void*) nativeGetProperty },
2972 { "nativeOnTrimMemory", "(I)V",
2973 (void*) nativeOnTrimMemory },
2976 int registerWebView(JNIEnv* env)
2978 jclass clazz = env->FindClass("android/webkit/WebView");
2979 LOG_ASSERT(clazz, "Unable to find class android/webkit/WebView");
2980 gWebViewField = env->GetFieldID(clazz, "mNativeClass", "I");
2981 LOG_ASSERT(gWebViewField, "Unable to find android/webkit/WebView.mNativeClass");
2982 env->DeleteLocalRef(clazz);
2984 return jniRegisterNativeMethods(env, "android/webkit/WebView", gJavaWebViewMethods, NELEM(gJavaWebViewMethods));
2987 } // namespace android