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