2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. 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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "FindController.h"
29 #include "ShareableBitmap.h"
31 #include "WebCoreArgumentCoders.h"
33 #include "WebPageProxyMessages.h"
34 #include "WebProcess.h"
35 #include <WebCore/DocumentMarkerController.h>
36 #include <WebCore/FloatQuad.h>
37 #include <WebCore/FocusController.h>
38 #include <WebCore/Frame.h>
39 #include <WebCore/FrameView.h>
40 #include <WebCore/GraphicsContext.h>
41 #include <WebCore/Page.h>
44 using namespace WebCore;
48 static WebCore::FindOptions core(FindOptions options)
50 return (options & FindOptionsCaseInsensitive ? CaseInsensitive : 0)
51 | (options & FindOptionsAtWordStarts ? AtWordStarts : 0)
52 | (options & FindOptionsTreatMedialCapitalAsWordStart ? TreatMedialCapitalAsWordStart : 0)
53 | (options & FindOptionsBackwards ? Backwards : 0)
54 | (options & FindOptionsWrapAround ? WrapAround : 0);
57 FindController::FindController(WebPage* webPage)
59 , m_findPageOverlay(0)
60 , m_isShowingFindIndicator(false)
61 #if ENABLE(TIZEN_FIND_STRING)
62 , m_activeMatchIndexInCurrentFrame(0)
67 FindController::~FindController()
71 void FindController::countStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
73 if (maxMatchCount == numeric_limits<unsigned>::max())
76 unsigned matchCount = m_webPage->corePage()->markAllMatchesForText(string, core(options), false, maxMatchCount + 1);
77 m_webPage->corePage()->unmarkAllTextMatches();
79 // Check if we have more matches than allowed.
80 if (matchCount > maxMatchCount)
81 matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
83 m_webPage->send(Messages::WebPageProxy::DidCountStringMatches(string, matchCount));
86 static Frame* frameWithSelection(Page* page)
88 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
89 if (frame->selection()->isRange())
96 void FindController::updateFindUIAfterPageScroll(bool found, const String& string, FindOptions options, unsigned maxMatchCount)
98 Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
100 bool shouldShowOverlay = false;
103 m_webPage->corePage()->unmarkAllTextMatches();
105 // Clear the selection.
107 selectedFrame->selection()->clear();
111 m_webPage->send(Messages::WebPageProxy::DidFailToFindString(string));
113 shouldShowOverlay = options & FindOptionsShowOverlay;
114 bool shouldShowHighlight = options & FindOptionsShowHighlight;
115 unsigned matchCount = 1;
117 if (shouldShowOverlay || shouldShowHighlight) {
119 if (maxMatchCount == numeric_limits<unsigned>::max())
122 m_webPage->corePage()->unmarkAllTextMatches();
123 matchCount = m_webPage->corePage()->markAllMatchesForText(string, core(options), shouldShowHighlight, maxMatchCount + 1);
125 // Check if we have more matches than allowed.
126 if (matchCount > maxMatchCount) {
127 shouldShowOverlay = false;
128 matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
132 m_webPage->send(Messages::WebPageProxy::DidFindString(string, matchCount));
134 if (!(options & FindOptionsShowFindIndicator) || !updateFindIndicator(selectedFrame, shouldShowOverlay)) {
135 // Either we shouldn't show the find indicator, or we couldn't update it.
140 if (!shouldShowOverlay) {
141 if (m_findPageOverlay) {
142 // Get rid of the overlay.
143 m_webPage->uninstallPageOverlay(m_findPageOverlay, false);
146 ASSERT(!m_findPageOverlay);
148 if (!m_findPageOverlay) {
149 RefPtr<PageOverlay> findPageOverlay = PageOverlay::create(this);
150 m_findPageOverlay = findPageOverlay.get();
151 m_webPage->installPageOverlay(findPageOverlay.release());
153 // The page overlay needs to be repainted.
154 m_findPageOverlay->setNeedsDisplay();
159 #if ENABLE(TIZEN_FIND_STRING)
160 void FindController::setActiveMatchHighlight(int activeMatchIndex)
162 if (activeMatchIndex >= static_cast<int>(m_findMatches.size()))
163 m_activeMatchIndexInCurrentFrame = 0;
164 else if (activeMatchIndex < 0)
165 m_activeMatchIndexInCurrentFrame = m_findMatches.size() - 1;
167 m_activeMatchIndexInCurrentFrame = activeMatchIndex;
169 Frame* frame = m_findMatches[m_activeMatchIndexInCurrentFrame]->startContainer()->document()->frame();
173 m_activeMatch = m_findMatches[m_activeMatchIndexInCurrentFrame];
175 // Scroll to the position of the active matching text.
176 m_activeMatch->firstNode()->renderer()->scrollRectToVisible(m_activeMatch->boundingBox(),
177 ScrollAlignment::alignCenterIfNeeded, ScrollAlignment::alignCenterIfNeeded);
179 frame->document()->markers()->addTextMatchMarker(m_activeMatch.get(), true);
180 frame->document()->markers()->setMarkersActive(m_activeMatch.get(), true);
184 void FindController::findString(const String& string, FindOptions options, unsigned maxMatchCount)
186 #if ENABLE(TIZEN_FIND_STRING)
187 m_findMatches.clear();
188 int indexForSelection;
190 m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
192 bool found = !m_findMatches.isEmpty();
194 // highlight the text matches found.
195 m_webPage->drawingArea()->dispatchAfterEnsuringUpdatedScrollPosition(WTF::bind(&FindController::updateFindUIAfterPageScroll, this, found, string, options, maxMatchCount));
198 m_activeMatchIndexInCurrentFrame = -1;
202 if (!m_oldString || m_oldString != string) {
203 m_oldString = string;
204 if (options & FindOptionsBackwards)
205 setActiveMatchHighlight(m_findMatches.size() - 1); // Should we use m_lastMatchCount instead of m_findMatches.size()
207 setActiveMatchHighlight(0);
209 if (options & FindOptionsBackwards)
210 setActiveMatchHighlight(m_activeMatchIndexInCurrentFrame - 1);
212 setActiveMatchHighlight(m_activeMatchIndexInCurrentFrame + 1);
215 bool found = m_webPage->corePage()->findString(string, core(options));
217 m_webPage->drawingArea()->dispatchAfterEnsuringUpdatedScrollPosition(WTF::bind(&FindController::updateFindUIAfterPageScroll, this, found, string, options, maxMatchCount));
221 void FindController::findStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
223 m_findMatches.clear();
224 int indexForSelection;
226 m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
228 Vector<Vector<IntRect> > matchRects;
229 for (size_t i = 0; i < m_findMatches.size(); ++i) {
230 Vector<IntRect> rects;
231 m_findMatches[i]->textRects(rects);
232 matchRects.append(rects);
235 m_webPage->send(Messages::WebPageProxy::DidFindStringMatches(string, matchRects, indexForSelection));
238 bool FindController::getFindIndicatorBitmapAndRect(Frame* frame, ShareableBitmap::Handle& handle, IntRect& selectionRect)
240 selectionRect = enclosingIntRect(frame->selection()->bounds());
242 // Selection rect can be empty for matches that are currently obscured from view.
243 if (selectionRect.isEmpty())
246 IntSize backingStoreSize = selectionRect.size();
247 backingStoreSize.scale(m_webPage->corePage()->deviceScaleFactor());
249 // Create a backing store and paint the find indicator text into it.
250 RefPtr<ShareableBitmap> findIndicatorTextBackingStore = ShareableBitmap::createShareable(backingStoreSize, ShareableBitmap::SupportsAlpha);
251 if (!findIndicatorTextBackingStore)
254 OwnPtr<GraphicsContext> graphicsContext = findIndicatorTextBackingStore->createGraphicsContext();
255 graphicsContext->scale(FloatSize(m_webPage->corePage()->deviceScaleFactor(), m_webPage->corePage()->deviceScaleFactor()));
257 IntRect paintRect = selectionRect;
258 paintRect.move(frame->view()->frameRect().x(), frame->view()->frameRect().y());
259 paintRect.move(-frame->view()->scrollOffset());
261 graphicsContext->translate(-paintRect.x(), -paintRect.y());
262 frame->view()->setPaintBehavior(PaintBehaviorSelectionOnly | PaintBehaviorForceBlackText | PaintBehaviorFlattenCompositingLayers);
263 frame->document()->updateLayout();
265 frame->view()->paint(graphicsContext.get(), paintRect);
266 frame->view()->setPaintBehavior(PaintBehaviorNormal);
268 if (!findIndicatorTextBackingStore->createHandle(handle))
273 void FindController::getImageForFindMatch(uint32_t matchIndex)
275 if (matchIndex >= m_findMatches.size())
277 Frame* frame = m_findMatches[matchIndex]->startContainer()->document()->frame();
281 VisibleSelection oldSelection = frame->selection()->selection();
282 frame->selection()->setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
284 IntRect selectionRect;
285 ShareableBitmap::Handle handle;
286 getFindIndicatorBitmapAndRect(frame, handle, selectionRect);
288 frame->selection()->setSelection(oldSelection);
290 m_webPage->send(Messages::WebPageProxy::DidGetImageForFindMatch(handle, matchIndex));
293 void FindController::selectFindMatch(uint32_t matchIndex)
295 if (matchIndex >= m_findMatches.size())
297 Frame* frame = m_findMatches[matchIndex]->startContainer()->document()->frame();
300 frame->selection()->setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
303 void FindController::hideFindUI()
305 m_findMatches.clear();
306 if (m_findPageOverlay)
307 m_webPage->uninstallPageOverlay(m_findPageOverlay, false);
309 m_webPage->corePage()->unmarkAllTextMatches();
313 bool FindController::updateFindIndicator(Frame* selectedFrame, bool isShowingOverlay, bool shouldAnimate)
318 IntRect selectionRect;
319 ShareableBitmap::Handle handle;
320 if (!getFindIndicatorBitmapAndRect(selectedFrame, handle, selectionRect))
323 // We want the selection rect in window coordinates.
324 IntRect selectionRectInWindowCoordinates = selectedFrame->view()->contentsToWindow(selectionRect);
326 Vector<FloatRect> textRects;
327 selectedFrame->selection()->getClippedVisibleTextRectangles(textRects);
329 // We want the text rects in selection rect coordinates.
330 Vector<FloatRect> textRectsInSelectionRectCoordinates;
332 for (size_t i = 0; i < textRects.size(); ++i) {
333 IntRect textRectInSelectionRectCoordinates = selectedFrame->view()->contentsToWindow(enclosingIntRect(textRects[i]));
334 textRectInSelectionRectCoordinates.move(-selectionRectInWindowCoordinates.x(), -selectionRectInWindowCoordinates.y());
336 textRectsInSelectionRectCoordinates.append(textRectInSelectionRectCoordinates);
339 m_webPage->send(Messages::WebPageProxy::SetFindIndicator(selectionRectInWindowCoordinates, textRectsInSelectionRectCoordinates, m_webPage->corePage()->deviceScaleFactor(), handle, !isShowingOverlay, shouldAnimate));
340 m_findIndicatorRect = selectionRectInWindowCoordinates;
341 m_isShowingFindIndicator = true;
346 void FindController::hideFindIndicator()
348 if (!m_isShowingFindIndicator)
351 ShareableBitmap::Handle handle;
352 m_webPage->send(Messages::WebPageProxy::SetFindIndicator(FloatRect(), Vector<FloatRect>(), m_webPage->corePage()->deviceScaleFactor(), handle, false, true));
353 m_isShowingFindIndicator = false;
356 void FindController::showFindIndicatorInSelection()
358 Frame* selectedFrame = m_webPage->corePage()->focusController()->focusedOrMainFrame();
362 updateFindIndicator(selectedFrame, false);
365 void FindController::deviceScaleFactorDidChange()
367 ASSERT(isShowingOverlay());
369 Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
373 updateFindIndicator(selectedFrame, true, false);
376 Vector<IntRect> FindController::rectsForTextMatches()
378 Vector<IntRect> rects;
380 for (Frame* frame = m_webPage->corePage()->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
381 Document* document = frame->document();
385 IntRect visibleRect = frame->view()->visibleContentRect();
386 Vector<IntRect> frameRects = document->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
388 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC)
389 IntPoint frameOffset(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
390 frameOffset = frame->view()->convertToContainingWindow(frameOffset);
392 IntPoint frameOffset;
393 if (!frame->view()->delegatesScrolling()) {
394 frameOffset = IntPoint(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
395 frameOffset = frame->view()->convertToContainingWindow(frameOffset);
399 for (Vector<IntRect>::iterator it = frameRects.begin(), end = frameRects.end(); it != end; ++it) {
400 it->intersect(visibleRect);
401 it->move(frameOffset.x(), frameOffset.y());
409 void FindController::pageOverlayDestroyed(PageOverlay*)
413 void FindController::willMoveToWebPage(PageOverlay*, WebPage* webPage)
418 // The page overlay is moving away from the web page, reset it.
419 ASSERT(m_findPageOverlay);
420 m_findPageOverlay = 0;
423 void FindController::didMoveToWebPage(PageOverlay*, WebPage*)
427 static const float shadowOffsetX = 0.0;
428 static const float shadowOffsetY = 1.0;
429 static const float shadowBlurRadius = 2.0;
430 static const float whiteFrameThickness = 1.0;
432 static const float overlayBackgroundRed = 0.1;
433 static const float overlayBackgroundGreen = 0.1;
434 static const float overlayBackgroundBlue = 0.1;
435 static const float overlayBackgroundAlpha = 0.25;
437 static Color overlayBackgroundColor(float fractionFadedIn)
439 return Color(overlayBackgroundRed, overlayBackgroundGreen, overlayBackgroundBlue, overlayBackgroundAlpha * fractionFadedIn);
442 static Color holeShadowColor(float fractionFadedIn)
444 return Color(0.0f, 0.0f, 0.0f, fractionFadedIn);
447 static Color holeFillColor(float fractionFadedIn)
449 return Color(1.0f, 1.0f, 1.0f, fractionFadedIn);
452 void FindController::drawRect(PageOverlay* pageOverlay, GraphicsContext& graphicsContext, const IntRect& dirtyRect)
454 float fractionFadedIn = pageOverlay->fractionFadedIn();
456 Vector<IntRect> rects = rectsForTextMatches();
458 // Draw the background.
459 graphicsContext.fillRect(dirtyRect, overlayBackgroundColor(fractionFadedIn), ColorSpaceSRGB);
462 GraphicsContextStateSaver stateSaver(graphicsContext);
464 graphicsContext.setShadow(FloatSize(shadowOffsetX, shadowOffsetY), shadowBlurRadius, holeShadowColor(fractionFadedIn), ColorSpaceSRGB);
465 graphicsContext.setFillColor(holeFillColor(fractionFadedIn), ColorSpaceSRGB);
467 // Draw white frames around the holes.
468 for (size_t i = 0; i < rects.size(); ++i) {
469 IntRect whiteFrameRect = rects[i];
470 whiteFrameRect.inflate(1);
472 graphicsContext.fillRect(whiteFrameRect);
476 graphicsContext.setFillColor(Color::transparent, ColorSpaceSRGB);
478 // Clear out the holes.
479 for (size_t i = 0; i < rects.size(); ++i)
480 graphicsContext.fillRect(rects[i]);
482 if (!m_isShowingFindIndicator)
485 if (Frame* selectedFrame = frameWithSelection(m_webPage->corePage())) {
486 IntRect findIndicatorRect = selectedFrame->view()->contentsToWindow(enclosingIntRect(selectedFrame->selection()->bounds()));
488 if (findIndicatorRect != m_findIndicatorRect)
493 bool FindController::mouseEvent(PageOverlay* pageOverlay, const WebMouseEvent& mouseEvent)
495 // If we get a mouse down event inside the page overlay we should hide the find UI.
496 if (mouseEvent.type() == WebEvent::MouseDown) {
497 // Dismiss the overlay.
504 } // namespace WebKit