4688fed876ce27992485bb34c586c078af2c02e2
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / FindController.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "FindController.h"
28
29 #include "DrawingArea.h"
30 #include "PluginView.h"
31 #include "ShareableBitmap.h"
32 #include "WKPage.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebPage.h"
35 #include "WebPageProxyMessages.h"
36 #include <WebCore/DocumentMarkerController.h>
37 #include <WebCore/FloatQuad.h>
38 #include <WebCore/FocusController.h>
39 #include <WebCore/FrameView.h>
40 #include <WebCore/GraphicsContext.h>
41 #include <WebCore/MainFrame.h>
42 #include <WebCore/Page.h>
43 #include <WebCore/PluginDocument.h>
44
45 using namespace WebCore;
46
47 namespace WebKit {
48
49 static WebCore::FindOptions core(FindOptions options)
50 {
51     return (options & FindOptionsCaseInsensitive ? CaseInsensitive : 0)
52         | (options & FindOptionsAtWordStarts ? AtWordStarts : 0)
53         | (options & FindOptionsTreatMedialCapitalAsWordStart ? TreatMedialCapitalAsWordStart : 0)
54         | (options & FindOptionsBackwards ? Backwards : 0)
55         | (options & FindOptionsWrapAround ? WrapAround : 0);
56 }
57
58 FindController::FindController(WebPage* webPage)
59     : m_webPage(webPage)
60     , m_findPageOverlay(0)
61     , m_isShowingFindIndicator(false)
62     , m_foundStringMatchIndex(-1)
63 {
64 }
65
66 FindController::~FindController()
67 {
68 }
69
70 static PluginView* pluginViewForFrame(Frame* frame)
71 {
72     if (!frame->document()->isPluginDocument())
73         return 0;
74
75     PluginDocument* pluginDocument = static_cast<PluginDocument*>(frame->document());
76     return static_cast<PluginView*>(pluginDocument->pluginWidget());
77 }
78
79 void FindController::countStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
80 {
81     if (maxMatchCount == std::numeric_limits<unsigned>::max())
82         --maxMatchCount;
83     
84     PluginView* pluginView = pluginViewForFrame(m_webPage->mainFrame());
85     
86     unsigned matchCount;
87
88     if (pluginView)
89         matchCount = pluginView->countFindMatches(string, core(options), maxMatchCount + 1);
90     else {
91         matchCount = m_webPage->corePage()->countFindMatches(string, core(options), maxMatchCount + 1);
92         m_webPage->corePage()->unmarkAllTextMatches();
93     }
94
95     if (matchCount > maxMatchCount)
96         matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
97     
98     m_webPage->send(Messages::WebPageProxy::DidCountStringMatches(string, matchCount));
99 }
100
101 static Frame* frameWithSelection(Page* page)
102 {
103     for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
104         if (frame->selection().isRange())
105             return frame;
106     }
107
108     return 0;
109 }
110
111 void FindController::updateFindUIAfterPageScroll(bool found, const String& string, FindOptions options, unsigned maxMatchCount)
112 {
113     Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
114     
115     PluginView* pluginView = pluginViewForFrame(m_webPage->mainFrame());
116
117     bool shouldShowOverlay = false;
118
119     if (!found) {
120         if (!pluginView)
121             m_webPage->corePage()->unmarkAllTextMatches();
122
123         if (selectedFrame)
124             selectedFrame->selection().clear();
125
126         hideFindIndicator();
127         didFailToFindString();
128
129         m_webPage->send(Messages::WebPageProxy::DidFailToFindString(string));
130     } else {
131         shouldShowOverlay = options & FindOptionsShowOverlay;
132         bool shouldShowHighlight = options & FindOptionsShowHighlight;
133         bool shouldDetermineMatchIndex = options & FindOptionsDetermineMatchIndex;
134         unsigned matchCount = 1;
135
136         if (shouldDetermineMatchIndex) {
137             if (pluginView)
138                 matchCount = pluginView->countFindMatches(string, core(options), maxMatchCount + 1);
139             else
140                 matchCount = m_webPage->corePage()->countFindMatches(string, core(options), maxMatchCount + 1);
141         }
142
143         if (shouldShowOverlay || shouldShowHighlight) {
144             if (maxMatchCount == std::numeric_limits<unsigned>::max())
145                 --maxMatchCount;
146
147             if (pluginView) {
148                 if (!shouldDetermineMatchIndex)
149                     matchCount = pluginView->countFindMatches(string, core(options), maxMatchCount + 1);
150                 shouldShowOverlay = false;
151             } else {
152                 m_webPage->corePage()->unmarkAllTextMatches();
153                 matchCount = m_webPage->corePage()->markAllMatchesForText(string, core(options), shouldShowHighlight, maxMatchCount + 1);
154             }
155
156             // If we have a large number of matches, we don't want to take the time to paint the overlay.
157             if (matchCount > maxMatchCount) {
158                 shouldShowOverlay = false;
159                 matchCount = static_cast<unsigned>(kWKMoreThanMaximumMatchCount);
160             }
161         }
162         if (matchCount == static_cast<unsigned>(kWKMoreThanMaximumMatchCount))
163             m_foundStringMatchIndex = -1;
164         else {
165             if (m_foundStringMatchIndex < 0)
166                 m_foundStringMatchIndex += matchCount;
167             if (m_foundStringMatchIndex >= (int) matchCount)
168                 m_foundStringMatchIndex -= matchCount;
169         }
170
171         m_webPage->send(Messages::WebPageProxy::DidFindString(string, matchCount, m_foundStringMatchIndex));
172
173         if (!(options & FindOptionsShowFindIndicator) || !selectedFrame || !updateFindIndicator(*selectedFrame, shouldShowOverlay))
174             hideFindIndicator();
175     }
176
177     if (!shouldShowOverlay) {
178         if (m_findPageOverlay)
179             m_webPage->uninstallPageOverlay(m_findPageOverlay, PageOverlay::FadeMode::Fade);
180     } else {
181         if (!m_findPageOverlay) {
182             RefPtr<PageOverlay> findPageOverlay = PageOverlay::create(this);
183             m_findPageOverlay = findPageOverlay.get();
184             m_webPage->installPageOverlay(findPageOverlay.release(), PageOverlay::FadeMode::Fade);
185         }
186         m_findPageOverlay->setNeedsDisplay();
187     }
188 }
189
190 void FindController::findString(const String& string, FindOptions options, unsigned maxMatchCount)
191 {
192     PluginView* pluginView = pluginViewForFrame(m_webPage->mainFrame());
193
194     WebCore::FindOptions coreOptions = core(options);
195
196     // iOS will reveal the selection through a different mechanism, and
197     // we need to avoid sending the non-painted selection change to the UI process
198     // so that it does not clear the selection out from under us.
199 #if PLATFORM(IOS)
200     coreOptions = static_cast<FindOptions>(coreOptions | DoNotRevealSelection);
201 #endif
202
203     willFindString();
204
205     bool foundStringStartsAfterSelection = false;
206     if (!pluginView) {
207         if (Frame* selectedFrame = frameWithSelection(m_webPage->corePage())) {
208             FrameSelection& fs = selectedFrame->selection();
209             if (fs.selectionBounds().isEmpty()) {
210                 m_findMatches.clear();
211                 int indexForSelection;
212                 m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
213                 m_foundStringMatchIndex = indexForSelection;
214                 foundStringStartsAfterSelection = true;
215             }
216         }
217     }
218
219     bool found;
220     if (pluginView)
221         found = pluginView->findString(string, coreOptions, maxMatchCount);
222     else
223         found = m_webPage->corePage()->findString(string, coreOptions);
224
225     if (found && !foundStringStartsAfterSelection) {
226         if (options & FindOptionsBackwards)
227             m_foundStringMatchIndex--;
228         else
229             m_foundStringMatchIndex++;
230     }
231
232     m_webPage->drawingArea()->dispatchAfterEnsuringUpdatedScrollPosition(WTF::bind(&FindController::updateFindUIAfterPageScroll, this, found, string, options, maxMatchCount));
233 }
234
235 void FindController::findStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
236 {
237     m_findMatches.clear();
238     int indexForSelection;
239
240     m_webPage->corePage()->findStringMatchingRanges(string, core(options), maxMatchCount, &m_findMatches, indexForSelection);
241
242     Vector<Vector<IntRect>> matchRects;
243     for (size_t i = 0; i < m_findMatches.size(); ++i) {
244         Vector<IntRect> rects;
245         m_findMatches[i]->textRects(rects);
246         matchRects.append(rects);
247     }
248
249     m_webPage->send(Messages::WebPageProxy::DidFindStringMatches(string, matchRects, indexForSelection));
250 }
251
252 bool FindController::getFindIndicatorBitmapAndRect(Frame& frame, ShareableBitmap::Handle& handle, IntRect& selectionRect)
253 {
254     selectionRect = enclosingIntRect(frame.selection().selectionBounds());
255
256     // Selection rect can be empty for matches that are currently obscured from view.
257     if (selectionRect.isEmpty())
258         return false;
259
260     IntSize backingStoreSize = selectionRect.size();
261     float deviceScaleFactor = m_webPage->corePage()->deviceScaleFactor();
262     backingStoreSize.scale(deviceScaleFactor);
263
264     // Create a backing store and paint the find indicator text into it.
265     RefPtr<ShareableBitmap> findIndicatorTextBackingStore = ShareableBitmap::createShareable(backingStoreSize, ShareableBitmap::SupportsAlpha);
266     if (!findIndicatorTextBackingStore)
267         return false;
268
269     auto graphicsContext = findIndicatorTextBackingStore->createGraphicsContext();
270     graphicsContext->scale(FloatSize(deviceScaleFactor, deviceScaleFactor));
271
272     IntRect paintRect = selectionRect;
273     paintRect.move(frame.view()->frameRect().x(), frame.view()->frameRect().y());
274     paintRect.move(-frame.view()->scrollOffset());
275
276     graphicsContext->translate(-paintRect.x(), -paintRect.y());
277     frame.view()->setPaintBehavior(PaintBehaviorSelectionOnly | PaintBehaviorForceBlackText | PaintBehaviorFlattenCompositingLayers);
278     frame.document()->updateLayout();
279
280     frame.view()->paint(graphicsContext.get(), paintRect);
281     frame.view()->setPaintBehavior(PaintBehaviorNormal);
282
283     if (!findIndicatorTextBackingStore->createHandle(handle))
284         return false;
285     return true;
286 }
287
288 void FindController::getImageForFindMatch(uint32_t matchIndex)
289 {
290     if (matchIndex >= m_findMatches.size())
291         return;
292     Frame* frame = m_findMatches[matchIndex]->startContainer()->document().frame();
293     if (!frame)
294         return;
295
296     VisibleSelection oldSelection = frame->selection().selection();
297     frame->selection().setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
298
299     IntRect selectionRect;
300     ShareableBitmap::Handle handle;
301     getFindIndicatorBitmapAndRect(*frame, handle, selectionRect);
302
303     frame->selection().setSelection(oldSelection);
304
305     if (handle.isNull())
306         return;
307
308     m_webPage->send(Messages::WebPageProxy::DidGetImageForFindMatch(handle, matchIndex));
309 }
310
311 void FindController::selectFindMatch(uint32_t matchIndex)
312 {
313     if (matchIndex >= m_findMatches.size())
314         return;
315     Frame* frame = m_findMatches[matchIndex]->startContainer()->document().frame();
316     if (!frame)
317         return;
318     frame->selection().setSelection(VisibleSelection(m_findMatches[matchIndex].get()));
319 }
320
321 void FindController::hideFindUI()
322 {
323     m_findMatches.clear();
324     if (m_findPageOverlay)
325         m_webPage->uninstallPageOverlay(m_findPageOverlay, PageOverlay::FadeMode::Fade);
326
327     PluginView* pluginView = pluginViewForFrame(m_webPage->mainFrame());
328     
329     if (pluginView)
330         pluginView->findString(emptyString(), 0, 0);
331     else
332         m_webPage->corePage()->unmarkAllTextMatches();
333     
334     hideFindIndicator();
335 }
336
337 #if !PLATFORM(IOS)
338 bool FindController::updateFindIndicator(Frame& selectedFrame, bool isShowingOverlay, bool shouldAnimate)
339 {
340     IntRect selectionRect;
341     ShareableBitmap::Handle handle;
342     if (!getFindIndicatorBitmapAndRect(selectedFrame, handle, selectionRect))
343         return false;
344
345     // We want the selection rect in window coordinates.
346     IntRect selectionRectInWindowCoordinates = selectedFrame.view()->contentsToWindow(selectionRect);
347
348     Vector<FloatRect> textRects;
349     selectedFrame.selection().getClippedVisibleTextRectangles(textRects);
350
351     // We want the text rects in selection rect coordinates.
352     Vector<FloatRect> textRectsInSelectionRectCoordinates;
353     
354     for (size_t i = 0; i < textRects.size(); ++i) {
355         IntRect textRectInSelectionRectCoordinates = selectedFrame.view()->contentsToWindow(enclosingIntRect(textRects[i]));
356         textRectInSelectionRectCoordinates.move(-selectionRectInWindowCoordinates.x(), -selectionRectInWindowCoordinates.y());
357
358         textRectsInSelectionRectCoordinates.append(textRectInSelectionRectCoordinates);
359     }            
360
361     m_webPage->send(Messages::WebPageProxy::SetFindIndicator(selectionRectInWindowCoordinates, textRectsInSelectionRectCoordinates, m_webPage->corePage()->deviceScaleFactor(), handle, !isShowingOverlay, shouldAnimate));
362     m_findIndicatorRect = selectionRectInWindowCoordinates;
363     m_isShowingFindIndicator = true;
364
365     return true;
366 }
367
368 void FindController::hideFindIndicator()
369 {
370     if (!m_isShowingFindIndicator)
371         return;
372
373     ShareableBitmap::Handle handle;
374     m_webPage->send(Messages::WebPageProxy::SetFindIndicator(FloatRect(), Vector<FloatRect>(), m_webPage->corePage()->deviceScaleFactor(), handle, false, true));
375     m_isShowingFindIndicator = false;
376     m_foundStringMatchIndex = -1;
377     didHideFindIndicator();
378 }
379
380 void FindController::willFindString()
381 {
382 }
383
384 void FindController::didFailToFindString()
385 {
386 }
387
388 void FindController::didHideFindIndicator()
389 {
390 }
391 #endif
392
393 void FindController::showFindIndicatorInSelection()
394 {
395     Frame& selectedFrame = m_webPage->corePage()->focusController().focusedOrMainFrame();
396     updateFindIndicator(selectedFrame, false);
397 }
398
399 void FindController::deviceScaleFactorDidChange()
400 {
401     ASSERT(isShowingOverlay());
402
403     Frame* selectedFrame = frameWithSelection(m_webPage->corePage());
404     if (!selectedFrame)
405         return;
406
407     updateFindIndicator(*selectedFrame, true, false);
408 }
409
410 Vector<IntRect> FindController::rectsForTextMatches()
411 {
412     Vector<IntRect> rects;
413
414     for (Frame* frame = &m_webPage->corePage()->mainFrame(); frame; frame = frame->tree().traverseNext()) {
415         Document* document = frame->document();
416         if (!document)
417             continue;
418
419         IntRect visibleRect = frame->view()->visibleContentRect();
420         Vector<IntRect> frameRects = document->markers().renderedRectsForMarkers(DocumentMarker::TextMatch);
421         IntPoint frameOffset(-frame->view()->scrollOffsetRelativeToDocument().width(), -frame->view()->scrollOffsetRelativeToDocument().height());
422         frameOffset = frame->view()->convertToContainingWindow(frameOffset);
423
424         for (Vector<IntRect>::iterator it = frameRects.begin(), end = frameRects.end(); it != end; ++it) {
425             it->intersect(visibleRect);
426             it->move(frameOffset.x(), frameOffset.y());
427             rects.append(*it);
428         }
429     }
430
431     return rects;
432 }
433
434 void FindController::pageOverlayDestroyed(PageOverlay*)
435 {
436 }
437
438 void FindController::willMoveToWebPage(PageOverlay*, WebPage* webPage)
439 {
440     if (webPage)
441         return;
442
443     ASSERT(m_findPageOverlay);
444     m_findPageOverlay = 0;
445 }
446     
447 void FindController::didMoveToWebPage(PageOverlay*, WebPage*)
448 {
449 }
450
451 static const float shadowOffsetX = 0.0;
452 static const float shadowOffsetY = 1.0;
453 static const float shadowBlurRadius = 2.0;
454
455 static const float overlayBackgroundRed = 0.1;
456 static const float overlayBackgroundGreen = 0.1;
457 static const float overlayBackgroundBlue = 0.1;
458 static const float overlayBackgroundAlpha = 0.25;
459
460 void FindController::drawRect(PageOverlay*, GraphicsContext& graphicsContext, const IntRect& dirtyRect)
461 {
462     Color overlayBackgroundColor(overlayBackgroundRed, overlayBackgroundGreen, overlayBackgroundBlue, overlayBackgroundAlpha);
463
464     Vector<IntRect> rects = rectsForTextMatches();
465
466     // Draw the background.
467     graphicsContext.fillRect(dirtyRect, overlayBackgroundColor, ColorSpaceSRGB);
468
469     {
470         GraphicsContextStateSaver stateSaver(graphicsContext);
471
472         graphicsContext.setShadow(FloatSize(shadowOffsetX, shadowOffsetY), shadowBlurRadius, Color::black, ColorSpaceSRGB);
473         graphicsContext.setFillColor(Color::white, ColorSpaceSRGB);
474
475         // Draw white frames around the holes.
476         for (size_t i = 0; i < rects.size(); ++i) {
477             IntRect whiteFrameRect = rects[i];
478             whiteFrameRect.inflate(1);
479             graphicsContext.fillRect(whiteFrameRect);
480         }
481     }
482
483     // Clear out the holes.
484     for (size_t i = 0; i < rects.size(); ++i)
485         graphicsContext.clearRect(rects[i]);
486
487     if (!m_isShowingFindIndicator)
488         return;
489
490     if (Frame* selectedFrame = frameWithSelection(m_webPage->corePage())) {
491         IntRect findIndicatorRect = selectedFrame->view()->contentsToWindow(enclosingIntRect(selectedFrame->selection().selectionBounds()));
492
493         if (findIndicatorRect != m_findIndicatorRect)
494             hideFindIndicator();
495     }
496 }
497
498 bool FindController::mouseEvent(PageOverlay*, const WebMouseEvent& mouseEvent)
499 {
500     if (mouseEvent.type() == WebEvent::MouseDown)
501         hideFindUI();
502
503     return false;
504 }
505
506 } // namespace WebKit