ed3fd488616b44791309cbe2273e387463dd3612
[WebKit-https.git] / Source / WebKit2 / UIProcess / ios / WebPageProxyIOS.mm
1 /*
2  * Copyright (C) 2012 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 #import "config.h"
27 #import "WebPageProxy.h"
28
29 #if PLATFORM(IOS)
30
31 #import "APIUIClient.h"
32 #import "DataReference.h"
33 #import "EditingRange.h"
34 #import "NativeWebKeyboardEvent.h"
35 #import "PageClient.h"
36 #import "RemoteLayerTreeDrawingAreaProxy.h"
37 #import "RemoteLayerTreeDrawingAreaProxyMessages.h"
38 #import "RemoteLayerTreeTransaction.h"
39 #import "UIKitSPI.h"
40 #import "UserData.h"
41 #import "ViewUpdateDispatcherMessages.h"
42 #import "WKBrowsingContextControllerInternal.h"
43 #import "WebKitSystemInterfaceIOS.h"
44 #import "WebPageMessages.h"
45 #import "WebProcessProxy.h"
46 #import "WebVideoFullscreenManagerProxy.h"
47 #import <WebCore/FrameView.h>
48 #import <WebCore/NotImplemented.h>
49 #import <WebCore/SharedBuffer.h>
50 #import <WebCore/UserAgent.h>
51
52 #if USE(QUICK_LOOK)
53 #import "APILoaderClient.h"
54 #import "APINavigationClient.h"
55 #import <wtf/text/WTFString.h>
56 #endif
57
58 using namespace WebCore;
59
60 namespace WebKit {
61
62 void WebPageProxy::platformInitialize()
63 {
64 }
65
66 static String webKitBundleVersionString()
67 {
68     return [[NSBundle bundleForClass:NSClassFromString(@"WKWebView")] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
69 }
70
71 String WebPageProxy::standardUserAgent(const String& applicationNameForUserAgent)
72 {
73     return standardUserAgentWithApplicationName(applicationNameForUserAgent, webKitBundleVersionString());
74 }
75
76 void WebPageProxy::getIsSpeaking(bool&)
77 {
78     notImplemented();
79 }
80
81 void WebPageProxy::speak(const String&)
82 {
83     notImplemented();
84 }
85
86 void WebPageProxy::stopSpeaking()
87 {
88     notImplemented();
89 }
90
91 void WebPageProxy::searchWithSpotlight(const String&)
92 {
93     notImplemented();
94 }
95
96 void WebPageProxy::searchTheWeb(const String&)
97 {
98     notImplemented();
99 }
100
101 void WebPageProxy::windowAndViewFramesChanged(const FloatRect&, const FloatPoint&)
102 {
103     notImplemented();
104 }
105
106 String WebPageProxy::stringSelectionForPasteboard()
107 {
108     notImplemented();
109     return String();
110 }
111
112 RefPtr<WebCore::SharedBuffer> WebPageProxy::dataSelectionForPasteboard(const String&)
113 {
114     notImplemented();
115     return nullptr;
116 }
117
118 bool WebPageProxy::readSelectionFromPasteboard(const String&)
119 {
120     notImplemented();
121     return false;
122 }
123
124 void WebPageProxy::performDictionaryLookupAtLocation(const WebCore::FloatPoint&)
125 {
126     notImplemented();
127 }
128
129 void WebPageProxy::gestureCallback(const WebCore::IntPoint& point, uint32_t gestureType, uint32_t gestureState, uint32_t flags, uint64_t callbackID)
130 {
131     auto callback = m_callbacks.take<GestureCallback>(callbackID);
132     if (!callback) {
133         ASSERT_NOT_REACHED();
134         return;
135     }
136     
137     callback->performCallbackWithReturnValue(point, gestureType, gestureState, flags);
138 }
139
140 void WebPageProxy::touchesCallback(const WebCore::IntPoint& point, uint32_t touches, uint64_t callbackID)
141 {
142     auto callback = m_callbacks.take<TouchesCallback>(callbackID);
143     if (!callback) {
144         ASSERT_NOT_REACHED();
145         return;
146     }
147
148     callback->performCallbackWithReturnValue(point, touches);
149 }
150
151 void WebPageProxy::autocorrectionDataCallback(const Vector<WebCore::FloatRect>& rects, const String& fontName, float fontSize, uint64_t fontTraits, uint64_t callbackID)
152 {
153     auto callback = m_callbacks.take<AutocorrectionDataCallback>(callbackID);
154     if (!callback) {
155         ASSERT_NOT_REACHED();
156         return;
157     }
158
159     callback->performCallbackWithReturnValue(rects, fontName, fontSize, fontTraits);
160 }
161
162 void WebPageProxy::dictationContextCallback(const String& selectedText, const String& beforeText, const String& afterText, uint64_t callbackID)
163 {
164     auto callback = m_callbacks.take<DictationContextCallback>(callbackID);
165     if (!callback) {
166         ASSERT_NOT_REACHED();
167         return;
168     }
169
170     callback->performCallbackWithReturnValue(selectedText, beforeText, afterText);
171 }
172
173 void WebPageProxy::autocorrectionContextCallback(const String& beforeText, const String& markedText, const String& selectedText, const String& afterText, uint64_t location, uint64_t length, uint64_t callbackID)
174 {
175     auto callback = m_callbacks.take<AutocorrectionContextCallback>(callbackID);
176     if (!callback) {
177         ASSERT_NOT_REACHED();
178         return;
179     }
180
181     callback->performCallbackWithReturnValue(beforeText, markedText, selectedText, afterText, location, length);
182 }
183
184 void WebPageProxy::updateVisibleContentRects(const WebCore::FloatRect& exposedRect, const WebCore::FloatRect& unobscuredRect, const WebCore::FloatRect& unobscuredRectInScrollViewCoordinates, const WebCore::FloatRect& customFixedPositionRect, double scale, bool inStableState, bool isChangingObscuredInsetsInteractively, double timestamp, double horizontalVelocity, double verticalVelocity, double scaleChangeRate)
185 {
186     if (!isValid())
187         return;
188
189     VisibleContentRectUpdateInfo visibleContentRectUpdateInfo(exposedRect, unobscuredRect, unobscuredRectInScrollViewCoordinates, customFixedPositionRect, scale, inStableState, isChangingObscuredInsetsInteractively, timestamp, horizontalVelocity, verticalVelocity, scaleChangeRate, downcast<RemoteLayerTreeDrawingAreaProxy>(*drawingArea()).lastCommittedLayerTreeTransactionID());
190
191     if (visibleContentRectUpdateInfo == m_lastVisibleContentRectUpdate)
192         return;
193
194     m_lastVisibleContentRectUpdate = visibleContentRectUpdateInfo;
195     m_process->send(Messages::ViewUpdateDispatcher::VisibleContentRectUpdate(m_pageID, visibleContentRectUpdateInfo), 0);
196 }
197
198 void WebPageProxy::resendLastVisibleContentRects()
199 {
200     m_process->send(Messages::ViewUpdateDispatcher::VisibleContentRectUpdate(m_pageID, m_lastVisibleContentRectUpdate), 0);
201 }
202
203 static inline float adjustedUnexposedEdge(float documentEdge, float exposedRectEdge, float factor)
204 {
205     if (exposedRectEdge < documentEdge)
206         return documentEdge - factor * (documentEdge - exposedRectEdge);
207     
208     return exposedRectEdge;
209 }
210
211 static inline float adjustedUnexposedMaxEdge(float documentEdge, float exposedRectEdge, float factor)
212 {
213     if (exposedRectEdge > documentEdge)
214         return documentEdge + factor * (exposedRectEdge - documentEdge);
215     
216     return exposedRectEdge;
217 }
218
219 WebCore::FloatRect WebPageProxy::computeCustomFixedPositionRect(const FloatRect& unobscuredContentRect, double displayedContentScale, UnobscuredRectConstraint constraint) const
220 {
221     FloatRect constrainedUnobscuredRect = unobscuredContentRect;
222     FloatRect documentRect = m_pageClient.documentRect();
223
224     if (m_pageClient.isAssistingNode())
225         return documentRect;
226
227     if (constraint == UnobscuredRectConstraint::ConstrainedToDocumentRect)
228         constrainedUnobscuredRect.intersect(documentRect);
229
230     double minimumScale = m_pageClient.minimumZoomScale();
231     
232     if (displayedContentScale < minimumScale) {
233         const CGFloat slope = 12;
234         CGFloat factor = std::max<CGFloat>(1 - slope * (minimumScale - displayedContentScale), 0);
235             
236         constrainedUnobscuredRect.setX(adjustedUnexposedEdge(documentRect.x(), constrainedUnobscuredRect.x(), factor));
237         constrainedUnobscuredRect.setY(adjustedUnexposedEdge(documentRect.y(), constrainedUnobscuredRect.y(), factor));
238         constrainedUnobscuredRect.setWidth(adjustedUnexposedMaxEdge(documentRect.maxX(), constrainedUnobscuredRect.maxX(), factor) - constrainedUnobscuredRect.x());
239         constrainedUnobscuredRect.setHeight(adjustedUnexposedMaxEdge(documentRect.maxY(), constrainedUnobscuredRect.maxY(), factor) - constrainedUnobscuredRect.y());
240     }
241     
242     return FrameView::rectForViewportConstrainedObjects(enclosingLayoutRect(constrainedUnobscuredRect), LayoutSize(documentRect.size()), displayedContentScale, false, StickToViewportBounds);
243 }
244
245 void WebPageProxy::overflowScrollViewWillStartPanGesture()
246 {
247     m_pageClient.overflowScrollViewWillStartPanGesture();
248 }
249
250 void WebPageProxy::overflowScrollViewDidScroll()
251 {
252     m_pageClient.overflowScrollViewDidScroll();
253 }
254
255 void WebPageProxy::overflowScrollWillStartScroll()
256 {
257     m_pageClient.overflowScrollWillStartScroll();
258 }
259
260 void WebPageProxy::overflowScrollDidEndScroll()
261 {
262     m_pageClient.overflowScrollDidEndScroll();
263 }
264
265 void WebPageProxy::dynamicViewportSizeUpdate(const FloatSize& minimumLayoutSize, const WebCore::FloatSize& maximumUnobscuredSize, const FloatRect& targetExposedContentRect, const FloatRect& targetUnobscuredRect, const FloatRect& targetUnobscuredRectInScrollViewCoordinates,  double targetScale, int32_t deviceOrientation)
266 {
267     if (!isValid())
268         return;
269
270     m_dynamicViewportSizeUpdateWaitingForTarget = true;
271     m_dynamicViewportSizeUpdateWaitingForLayerTreeCommit = true;
272     m_process->send(Messages::WebPage::DynamicViewportSizeUpdate(minimumLayoutSize, maximumUnobscuredSize, targetExposedContentRect, targetUnobscuredRect, targetUnobscuredRectInScrollViewCoordinates, targetScale, deviceOrientation, ++m_currentDynamicViewportSizeUpdateID), m_pageID);
273 }
274
275 void WebPageProxy::synchronizeDynamicViewportUpdate()
276 {
277     if (!isValid())
278         return;
279
280     if (m_dynamicViewportSizeUpdateWaitingForTarget) {
281         // We do not want the UIProcess to finish animated resize with the old content size, scale, etc.
282         // If that happens, the UIProcess would start pushing new VisibleContentRectUpdateInfo to the WebProcess with
283         // invalid informations.
284         //
285         // Ideally, the animated resize should just be transactional, and the UIProcess would remain in the "resize" state
286         // until both DynamicViewportUpdateChangedTarget and the associated commitLayerTree are finished.
287         // The tricky part with such implementation is if a second animated resize starts before the end of the previous one.
288         // In that case, the values used for the target state needs to be computed from the output of the previous animated resize.
289         //
290         // The following is a workaround to have the UIProcess in a consistent state.
291         // Instead of handling nested resize, we block the UIProcess until the animated resize finishes.
292         double newScale;
293         FloatPoint newScrollPosition;
294         uint64_t nextValidLayerTreeTransactionID;
295         if (m_process->sendSync(Messages::WebPage::SynchronizeDynamicViewportUpdate(), Messages::WebPage::SynchronizeDynamicViewportUpdate::Reply(newScale, newScrollPosition, nextValidLayerTreeTransactionID), m_pageID, std::chrono::seconds(2))) {
296             m_dynamicViewportSizeUpdateWaitingForTarget = false;
297             m_dynamicViewportSizeUpdateLayerTreeTransactionID = nextValidLayerTreeTransactionID;
298             m_pageClient.dynamicViewportUpdateChangedTarget(newScale, newScrollPosition, nextValidLayerTreeTransactionID);
299         }
300
301     }
302
303     // If m_dynamicViewportSizeUpdateWaitingForTarget is false, we are waiting for the next valid frame with the hope it is the one for the new target.
304     // If m_dynamicViewportSizeUpdateWaitingForTarget is still true, this is a desesperate attempt to get the valid frame before finishing the animation.
305     if (m_dynamicViewportSizeUpdateWaitingForLayerTreeCommit)
306         m_process->connection()->waitForAndDispatchImmediately<Messages::RemoteLayerTreeDrawingAreaProxy::CommitLayerTree>(m_pageID, std::chrono::seconds(1), IPC::InterruptWaitingIfSyncMessageArrives);
307
308     m_dynamicViewportSizeUpdateWaitingForTarget = false;
309     m_dynamicViewportSizeUpdateWaitingForLayerTreeCommit = false;
310 }
311
312 void WebPageProxy::setViewportConfigurationMinimumLayoutSize(const WebCore::FloatSize& size)
313 {
314     m_process->send(Messages::WebPage::SetViewportConfigurationMinimumLayoutSize(size), m_pageID);
315 }
316
317 void WebPageProxy::setMaximumUnobscuredSize(const WebCore::FloatSize& size)
318 {
319     m_process->send(Messages::WebPage::SetMaximumUnobscuredSize(size), m_pageID);
320 }
321
322 void WebPageProxy::setDeviceOrientation(int32_t deviceOrientation)
323 {
324     if (deviceOrientation != m_deviceOrientation) {
325         m_deviceOrientation = deviceOrientation;
326         if (isValid())
327             m_process->send(Messages::WebPage::SetDeviceOrientation(deviceOrientation), m_pageID);
328     }
329 }
330
331 static bool exceedsRenderTreeSizeSizeThreshold(uint64_t thresholdSize, uint64_t committedSize)
332 {
333     const double thesholdSizeFraction = 0.5; // Empirically-derived.
334     return committedSize > thresholdSize * thesholdSizeFraction;
335 }
336
337 void WebPageProxy::didCommitLayerTree(const WebKit::RemoteLayerTreeTransaction& layerTreeTransaction)
338 {
339     m_pageExtendedBackgroundColor = layerTreeTransaction.pageExtendedBackgroundColor();
340
341     if (!m_hasReceivedLayerTreeTransactionAfterDidCommitLoad) {
342         if (layerTreeTransaction.transactionID() >= m_firstLayerTreeTransactionIdAfterDidCommitLoad) {
343             m_hasReceivedLayerTreeTransactionAfterDidCommitLoad = true;
344             m_lastVisibleContentRectUpdate = VisibleContentRectUpdateInfo();
345         }
346     }
347
348     if (!m_dynamicViewportSizeUpdateWaitingForTarget && m_dynamicViewportSizeUpdateWaitingForLayerTreeCommit) {
349         if (layerTreeTransaction.transactionID() >= m_dynamicViewportSizeUpdateLayerTreeTransactionID)
350             m_dynamicViewportSizeUpdateWaitingForLayerTreeCommit = false;
351     }
352
353     m_pageClient.didCommitLayerTree(layerTreeTransaction);
354
355     // FIXME: Remove this special mechanism and fold it into the transaction's layout milestones.
356     if (m_wantsSessionRestorationRenderTreeSizeThresholdEvent && !m_hitRenderTreeSizeThreshold
357         && exceedsRenderTreeSizeSizeThreshold(m_sessionRestorationRenderTreeSize, layerTreeTransaction.renderTreeSize())) {
358         m_hitRenderTreeSizeThreshold = true;
359         didLayout(WebCore::ReachedSessionRestorationRenderTreeSizeThreshold);
360     }
361 }
362
363 void WebPageProxy::selectWithGesture(const WebCore::IntPoint point, WebCore::TextGranularity granularity, uint32_t gestureType, uint32_t gestureState, std::function<void (const WebCore::IntPoint&, uint32_t, uint32_t, uint32_t, CallbackBase::Error)> callbackFunction)
364 {
365     if (!isValid()) {
366         callbackFunction(WebCore::IntPoint(), 0, 0, 0, CallbackBase::Error::Unknown);
367         return;
368     }
369
370     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
371     m_process->send(Messages::WebPage::SelectWithGesture(point, (uint32_t)granularity, gestureType, gestureState, callbackID), m_pageID);
372 }
373
374 void WebPageProxy::updateSelectionWithTouches(const WebCore::IntPoint point, uint32_t touches, bool baseIsStart, std::function<void (const WebCore::IntPoint&, uint32_t, CallbackBase::Error)> callbackFunction)
375 {
376     if (!isValid()) {
377         callbackFunction(WebCore::IntPoint(), 0, CallbackBase::Error::Unknown);
378         return;
379     }
380
381     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
382     m_process->send(Messages::WebPage::UpdateSelectionWithTouches(point, touches, baseIsStart, callbackID), m_pageID);
383 }
384     
385 void WebPageProxy::replaceDictatedText(const String& oldText, const String& newText)
386 {
387     m_process->send(Messages::WebPage::ReplaceDictatedText(oldText, newText), m_pageID);
388 }
389
390 void WebPageProxy::replaceSelectedText(const String& oldText, const String& newText)
391 {
392     m_process->send(Messages::WebPage::ReplaceSelectedText(oldText, newText), m_pageID);
393 }
394
395 void WebPageProxy::requestAutocorrectionData(const String& textForAutocorrection, std::function<void (const Vector<WebCore::FloatRect>&, const String&, double, uint64_t, CallbackBase::Error)> callbackFunction)
396 {
397     if (!isValid()) {
398         callbackFunction(Vector<WebCore::FloatRect>(), String(), 0, 0, CallbackBase::Error::Unknown);
399         return;
400     }
401
402     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
403     m_process->send(Messages::WebPage::RequestAutocorrectionData(textForAutocorrection, callbackID), m_pageID);
404 }
405
406 void WebPageProxy::applyAutocorrection(const String& correction, const String& originalText, std::function<void (const String&, CallbackBase::Error)> callbackFunction)
407 {
408     if (!isValid()) {
409         callbackFunction(String(), CallbackBase::Error::Unknown);
410         return;
411     }
412
413     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
414     m_process->send(Messages::WebPage::ApplyAutocorrection(correction, originalText, callbackID), m_pageID);
415 }
416
417 void WebPageProxy::executeEditCommand(const String& commandName, std::function<void (CallbackBase::Error)> callbackFunction)
418 {
419     if (!isValid()) {
420         callbackFunction(CallbackBase::Error::Unknown);
421         return;
422     }
423     
424     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
425     m_process->send(Messages::WebPage::ExecuteEditCommandWithCallback(commandName, callbackID), m_pageID);
426 }
427
428 bool WebPageProxy::applyAutocorrection(const String& correction, const String& originalText)
429 {
430     bool autocorrectionApplied = false;
431     m_process->sendSync(Messages::WebPage::SyncApplyAutocorrection(correction, originalText), Messages::WebPage::SyncApplyAutocorrection::Reply(autocorrectionApplied), m_pageID);
432     return autocorrectionApplied;
433 }
434
435 void WebPageProxy::selectTextWithGranularityAtPoint(const WebCore::IntPoint point, WebCore::TextGranularity granularity, std::function<void (CallbackBase::Error)> callbackFunction)
436 {
437     if (!isValid()) {
438         callbackFunction(CallbackBase::Error::Unknown);
439         return;
440     }
441     
442     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
443     m_process->send(Messages::WebPage::SelectTextWithGranularityAtPoint(point, static_cast<uint32_t>(granularity), callbackID), m_pageID);
444 }
445
446 void WebPageProxy::selectPositionAtBoundaryWithDirection(const WebCore::IntPoint point, WebCore::TextGranularity granularity, WebCore::SelectionDirection direction, std::function<void (CallbackBase::Error)> callbackFunction)
447 {
448     if (!isValid()) {
449         callbackFunction(CallbackBase::Error::Unknown);
450         return;
451     }
452     
453     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
454     m_process->send(Messages::WebPage::SelectPositionAtBoundaryWithDirection(point, static_cast<uint32_t>(granularity), static_cast<uint32_t>(direction), callbackID), m_pageID);
455 }
456
457 void WebPageProxy::moveSelectionAtBoundaryWithDirection(WebCore::TextGranularity granularity, WebCore::SelectionDirection direction, std::function<void(CallbackBase::Error)> callbackFunction)
458 {
459     if (!isValid()) {
460         callbackFunction(CallbackBase::Error::Unknown);
461         return;
462     }
463     
464     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
465     m_process->send(Messages::WebPage::MoveSelectionAtBoundaryWithDirection(static_cast<uint32_t>(granularity), static_cast<uint32_t>(direction), callbackID), m_pageID);
466 }
467     
468 void WebPageProxy::selectPositionAtPoint(const WebCore::IntPoint point, std::function<void (CallbackBase::Error)> callbackFunction)
469 {
470     if (!isValid()) {
471         callbackFunction(CallbackBase::Error::Unknown);
472         return;
473     }
474     
475     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
476     m_process->send(Messages::WebPage::SelectPositionAtPoint(point, callbackID), m_pageID);
477 }
478
479 void WebPageProxy::beginSelectionInDirection(WebCore::SelectionDirection direction, std::function<void (uint64_t, CallbackBase::Error)> callbackFunction)
480 {
481     if (!isValid()) {
482         callbackFunction(0, CallbackBase::Error::Unknown);
483         return;
484     }
485     
486     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
487     m_process->send(Messages::WebPage::BeginSelectionInDirection(direction, callbackID), m_pageID);
488 }
489
490 void WebPageProxy::updateSelectionWithExtentPoint(const WebCore::IntPoint point, std::function<void (uint64_t, CallbackBase::Error)> callbackFunction)
491 {
492     if (!isValid()) {
493         callbackFunction(0, CallbackBase::Error::Unknown);
494         return;
495     }
496     
497     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
498     m_process->send(Messages::WebPage::UpdateSelectionWithExtentPoint(point, callbackID), m_pageID);
499     
500 }
501
502 void WebPageProxy::updateSelectionWithExtentPointAndBoundary(const WebCore::IntPoint point, WebCore::TextGranularity granularity, std::function<void(uint64_t, CallbackBase::Error)> callbackFunction)
503 {
504     if (!isValid()) {
505         callbackFunction(0, CallbackBase::Error::Unknown);
506         return;
507     }
508     
509     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
510     m_process->send(Messages::WebPage::UpdateSelectionWithExtentPointAndBoundary(point, granularity, callbackID), m_pageID);
511     
512 }
513
514 void WebPageProxy::requestDictationContext(std::function<void (const String&, const String&, const String&, CallbackBase::Error)> callbackFunction)
515 {
516     if (!isValid()) {
517         callbackFunction(String(), String(), String(), CallbackBase::Error::Unknown);
518         return;
519     }
520
521     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
522     m_process->send(Messages::WebPage::RequestDictationContext(callbackID), m_pageID);
523 }
524
525 void WebPageProxy::requestAutocorrectionContext(std::function<void (const String&, const String&, const String&, const String&, uint64_t, uint64_t, CallbackBase::Error)> callbackFunction)
526 {
527     if (!isValid()) {
528         callbackFunction(String(), String(), String(), String(), 0, 0, CallbackBase::Error::Unknown);
529         return;
530     }
531
532     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
533     m_process->send(Messages::WebPage::RequestAutocorrectionContext(callbackID), m_pageID);
534 }
535
536 void WebPageProxy::getAutocorrectionContext(String& beforeContext, String& markedText, String& selectedText, String& afterContext, uint64_t& location, uint64_t& length)
537 {
538     m_process->sendSync(Messages::WebPage::GetAutocorrectionContext(), Messages::WebPage::GetAutocorrectionContext::Reply(beforeContext, markedText, selectedText, afterContext, location, length), m_pageID);
539 }
540
541 void WebPageProxy::getLookupContextAtPoint(const WebCore::IntPoint& point, std::function<void(const String&, CallbackBase::Error)> callbackFunction)
542 {
543     if (!isValid()) {
544         callbackFunction(String(), CallbackBase::Error::Unknown);
545         return;
546     }
547     
548     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
549     m_process->send(Messages::WebPage::GetLookupContextAtPoint(point, callbackID), m_pageID);
550 }
551
552 void WebPageProxy::selectWithTwoTouches(const WebCore::IntPoint from, const WebCore::IntPoint to, uint32_t gestureType, uint32_t gestureState, std::function<void (const WebCore::IntPoint&, uint32_t, uint32_t, uint32_t, CallbackBase::Error)> callbackFunction)
553 {
554     if (!isValid()) {
555         callbackFunction(WebCore::IntPoint(), 0, 0, 0, CallbackBase::Error::Unknown);
556         return;
557     }
558
559     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
560     m_process->send(Messages::WebPage::SelectWithTwoTouches(from, to, gestureType, gestureState, callbackID), m_pageID);
561 }
562
563 void WebPageProxy::updateBlockSelectionWithTouch(const WebCore::IntPoint point, uint32_t touch, uint32_t handlePosition)
564 {
565     m_process->send(Messages::WebPage::UpdateBlockSelectionWithTouch(point, touch, handlePosition), m_pageID);
566 }
567
568 void WebPageProxy::didReceivePositionInformation(const InteractionInformationAtPosition& info)
569 {
570     m_pageClient.positionInformationDidChange(info);
571 }
572
573 void WebPageProxy::getPositionInformation(const WebCore::IntPoint& point, InteractionInformationAtPosition& info)
574 {
575     m_process->sendSync(Messages::WebPage::GetPositionInformation(point), Messages::WebPage::GetPositionInformation::Reply(info), m_pageID);
576 }
577
578 void WebPageProxy::requestPositionInformation(const WebCore::IntPoint& point)
579 {
580     m_process->send(Messages::WebPage::RequestPositionInformation(point), m_pageID);
581 }
582
583 void WebPageProxy::startInteractionWithElementAtPosition(const WebCore::IntPoint& point)
584 {
585     m_process->send(Messages::WebPage::StartInteractionWithElementAtPosition(point), m_pageID);
586 }
587
588 void WebPageProxy::stopInteraction()
589 {
590     m_process->send(Messages::WebPage::StopInteraction(), m_pageID);
591 }
592
593 void WebPageProxy::performActionOnElement(uint32_t action)
594 {
595     m_process->send(Messages::WebPage::PerformActionOnElement(action), m_pageID);
596 }
597
598 void WebPageProxy::saveImageToLibrary(const SharedMemory::Handle& imageHandle, uint64_t imageSize)
599 {
600     RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::map(imageHandle, SharedMemory::Protection::ReadOnly);
601     RefPtr<SharedBuffer> buffer = SharedBuffer::create(static_cast<unsigned char*>(sharedMemoryBuffer->data()), imageSize);
602     m_pageClient.saveImageToLibrary(buffer);
603 }
604
605 void WebPageProxy::didUpdateBlockSelectionWithTouch(uint32_t touch, uint32_t flags, float growThreshold, float shrinkThreshold)
606 {
607     m_pageClient.didUpdateBlockSelectionWithTouch(touch, flags, growThreshold, shrinkThreshold);
608 }
609
610 void WebPageProxy::applicationDidEnterBackground()
611 {
612     bool isSuspendedUnderLock = [UIApp isSuspendedUnderLock];
613     m_process->send(Messages::WebPage::ApplicationDidEnterBackground(isSuspendedUnderLock), m_pageID);
614 }
615
616 void WebPageProxy::applicationWillEnterForeground()
617 {
618     m_process->send(Messages::WebPage::ApplicationWillEnterForeground(), m_pageID);
619 }
620
621 void WebPageProxy::applicationWillResignActive()
622 {
623     m_process->send(Messages::WebPage::ApplicationWillResignActive(), m_pageID);
624 }
625
626 void WebPageProxy::applicationDidBecomeActive()
627 {
628     m_process->send(Messages::WebPage::ApplicationDidBecomeActive(), m_pageID);
629 }
630
631 void WebPageProxy::extendSelection(WebCore::TextGranularity granularity)
632 {
633     m_process->send(Messages::WebPage::ExtendSelection(static_cast<uint32_t>(granularity)), m_pageID);
634 }
635
636 void WebPageProxy::selectWordBackward()
637 {
638     m_process->send(Messages::WebPage::SelectWordBackward(), m_pageID);
639 }
640
641 void WebPageProxy::moveSelectionByOffset(int32_t offset, std::function<void (CallbackBase::Error)> callbackFunction)
642 {
643     if (!isValid()) {
644         callbackFunction(CallbackBase::Error::Unknown);
645         return;
646     }
647     
648     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
649     m_process->send(Messages::WebPage::MoveSelectionByOffset(offset, callbackID), m_pageID);
650 }
651
652 void WebPageProxy::interpretKeyEvent(const EditorState& state, bool isCharEvent, bool& handled)
653 {
654     m_editorState = state;
655     handled = m_pageClient.interpretKeyEvent(m_keyEventQueue.first(), isCharEvent);
656 }
657
658 // Complex text input support for plug-ins.
659 void WebPageProxy::sendComplexTextInputToPlugin(uint64_t, const String&)
660 {
661     notImplemented();
662 }
663
664 void WebPageProxy::setSmartInsertDeleteEnabled(bool)
665 {
666     notImplemented();
667 }
668
669 void WebPageProxy::registerWebProcessAccessibilityToken(const IPC::DataReference& data)
670 {
671     m_pageClient.accessibilityWebProcessTokenReceived(data);
672 }    
673
674 void WebPageProxy::makeFirstResponder()
675 {
676     notImplemented();
677 }
678
679 void WebPageProxy::registerUIProcessAccessibilityTokens(const IPC::DataReference& elementToken, const IPC::DataReference& windowToken)
680 {
681     if (!isValid())
682         return;
683     
684     process().send(Messages::WebPage::RegisterUIProcessAccessibilityTokens(elementToken, windowToken), m_pageID);
685 }
686
687 void WebPageProxy::pluginFocusOrWindowFocusChanged(uint64_t, bool)
688 {
689     notImplemented();
690 }
691
692 void WebPageProxy::setPluginComplexTextInputState(uint64_t, uint64_t)
693 {
694     notImplemented();
695 }
696
697 void WebPageProxy::executeSavedCommandBySelector(const String&, bool&)
698 {
699     notImplemented();
700 }
701
702 bool WebPageProxy::shouldDelayWindowOrderingForEvent(const WebKit::WebMouseEvent&)
703 {
704     notImplemented();
705     return false;
706 }
707
708 bool WebPageProxy::acceptsFirstMouse(int, const WebKit::WebMouseEvent&)
709 {
710     notImplemented();
711     return false;
712 }
713
714 void WebPageProxy::willStartUserTriggeredZooming()
715 {
716     process().send(Messages::WebPage::WillStartUserTriggeredZooming(), m_pageID);
717 }
718
719 void WebPageProxy::potentialTapAtPosition(const WebCore::FloatPoint& position, uint64_t& requestID)
720 {
721     process().send(Messages::WebPage::PotentialTapAtPosition(requestID, position), m_pageID);
722 }
723
724 void WebPageProxy::commitPotentialTap()
725 {
726     process().send(Messages::WebPage::CommitPotentialTap(m_layerTreeTransactionIdAtLastTouchStart), m_pageID);
727 }
728
729 void WebPageProxy::cancelPotentialTap()
730 {
731     process().send(Messages::WebPage::CancelPotentialTap(), m_pageID);
732 }
733
734 void WebPageProxy::tapHighlightAtPosition(const WebCore::FloatPoint& position, uint64_t& requestID)
735 {
736     process().send(Messages::WebPage::TapHighlightAtPosition(requestID, position), m_pageID);
737 }
738
739 void WebPageProxy::handleTap(const FloatPoint& location)
740 {
741     process().send(Messages::WebPage::HandleTap(roundedIntPoint(location), m_layerTreeTransactionIdAtLastTouchStart), m_pageID);
742 }
743
744 void WebPageProxy::inspectorNodeSearchMovedToPosition(const WebCore::FloatPoint& position)
745 {
746     process().send(Messages::WebPage::InspectorNodeSearchMovedToPosition(position), m_pageID);
747 }
748
749 void WebPageProxy::inspectorNodeSearchEndedAtPosition(const WebCore::FloatPoint& position)
750 {
751     process().send(Messages::WebPage::InspectorNodeSearchEndedAtPosition(position), m_pageID);
752 }
753
754 void WebPageProxy::blurAssistedNode()
755 {
756     process().send(Messages::WebPage::BlurAssistedNode(), m_pageID);
757 }
758
759 FloatSize WebPageProxy::screenSize()
760 {
761     return FloatSize(WKGetScreenSize());
762 }
763
764 FloatSize WebPageProxy::availableScreenSize()
765 {
766     return FloatSize(WKGetAvailableScreenSize());
767 }
768     
769 float WebPageProxy::textAutosizingWidth()
770 {
771     return WKGetScreenSize().width;
772 }
773
774 void WebPageProxy::dynamicViewportUpdateChangedTarget(double newScale, const WebCore::FloatPoint& newScrollPosition, uint64_t dynamicViewportSizeUpdateID)
775 {
776     if (dynamicViewportSizeUpdateID != m_currentDynamicViewportSizeUpdateID)
777         return;
778
779     if (m_dynamicViewportSizeUpdateWaitingForTarget) {
780         m_dynamicViewportSizeUpdateLayerTreeTransactionID = downcast<RemoteLayerTreeDrawingAreaProxy>(*drawingArea()).nextLayerTreeTransactionID();
781         m_dynamicViewportSizeUpdateWaitingForTarget = false;
782         m_pageClient.dynamicViewportUpdateChangedTarget(newScale, newScrollPosition, m_dynamicViewportSizeUpdateLayerTreeTransactionID);
783     }
784 }
785
786 void WebPageProxy::couldNotRestorePageState()
787 {
788     m_pageClient.couldNotRestorePageState();
789 }
790
791 void WebPageProxy::restorePageState(const WebCore::FloatRect& exposedRect, double scale)
792 {
793     m_pageClient.restorePageState(exposedRect, scale);
794 }
795
796 void WebPageProxy::restorePageCenterAndScale(const WebCore::FloatPoint& center, double scale)
797 {
798     m_pageClient.restorePageCenterAndScale(center, scale);
799 }
800
801 void WebPageProxy::didGetTapHighlightGeometries(uint64_t requestID, const WebCore::Color& color, const Vector<WebCore::FloatQuad>& highlightedQuads, const WebCore::IntSize& topLeftRadius, const WebCore::IntSize& topRightRadius, const WebCore::IntSize& bottomLeftRadius, const WebCore::IntSize& bottomRightRadius)
802 {
803     m_pageClient.didGetTapHighlightGeometries(requestID, color, highlightedQuads, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
804 }
805
806 void WebPageProxy::startAssistingNode(const AssistedNodeInformation& information, bool userIsInteracting, bool blurPreviousNode, const UserData& userData)
807 {
808     m_pageClient.startAssistingNode(information, userIsInteracting, blurPreviousNode, process().transformHandlesToObjects(userData.object()).get());
809 }
810
811 void WebPageProxy::stopAssistingNode()
812 {
813     m_pageClient.stopAssistingNode();
814 }
815
816 void WebPageProxy::showInspectorHighlight(const WebCore::Highlight& highlight)
817 {
818     m_pageClient.showInspectorHighlight(highlight);
819 }
820
821 void WebPageProxy::hideInspectorHighlight()
822 {
823     m_pageClient.hideInspectorHighlight();
824 }
825
826 void WebPageProxy::showInspectorIndication()
827 {
828     m_pageClient.showInspectorIndication();
829 }
830
831 void WebPageProxy::hideInspectorIndication()
832 {
833     m_pageClient.hideInspectorIndication();
834 }
835
836 void WebPageProxy::enableInspectorNodeSearch()
837 {
838     m_pageClient.enableInspectorNodeSearch();
839 }
840
841 void WebPageProxy::disableInspectorNodeSearch()
842 {
843     m_pageClient.disableInspectorNodeSearch();
844 }
845
846 void WebPageProxy::focusNextAssistedNode(bool isForward, std::function<void (CallbackBase::Error)> callbackFunction)
847 {
848     if (!isValid()) {
849         callbackFunction(CallbackBase::Error::Unknown);
850         return;
851     }
852     
853     uint64_t callbackID = m_callbacks.put(WTF::move(callbackFunction), m_process->throttler().backgroundActivityToken());
854     process().send(Messages::WebPage::FocusNextAssistedNode(isForward, callbackID), m_pageID);
855 }
856
857 void WebPageProxy::setAssistedNodeValue(const String& value)
858 {
859     process().send(Messages::WebPage::SetAssistedNodeValue(value), m_pageID);
860 }
861
862 void WebPageProxy::setAssistedNodeValueAsNumber(double value)
863 {
864     process().send(Messages::WebPage::SetAssistedNodeValueAsNumber(value), m_pageID);
865 }
866
867 void WebPageProxy::setAssistedNodeSelectedIndex(uint32_t index, bool allowMultipleSelection)
868 {
869     process().send(Messages::WebPage::SetAssistedNodeSelectedIndex(index, allowMultipleSelection), m_pageID);
870 }
871
872 void WebPageProxy::didPerformDictionaryLookup(const DictionaryPopupInfo&)
873 {
874     notImplemented();
875 }
876
877 void WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication(const String&, const String&, const IPC::DataReference&, const String&)
878 {
879     notImplemented();
880 }
881
882 void WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplicationRaw(const String&, const String&, const uint8_t*, unsigned long, const String&)
883 {
884     notImplemented();
885 }
886
887 void WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication(const String&)
888 {
889     notImplemented();
890 }
891
892 void WebPageProxy::setAcceleratedCompositingRootLayer(LayerOrView* rootLayer)
893 {
894     m_pageClient.setAcceleratedCompositingRootLayer(rootLayer);
895 }
896
897 void WebPageProxy::showPlaybackTargetPicker(bool hasVideo, const IntRect& elementRect)
898 {
899     m_pageClient.showPlaybackTargetPicker(hasVideo, elementRect);
900 }
901
902 void WebPageProxy::zoomToRect(FloatRect rect, double minimumScale, double maximumScale)
903 {
904     m_pageClient.zoomToRect(rect, minimumScale, maximumScale);
905 }
906
907 void WebPageProxy::commitPotentialTapFailed()
908 {
909     m_pageClient.commitPotentialTapFailed();
910 }
911
912 void WebPageProxy::didNotHandleTapAsClick(const WebCore::IntPoint& point)
913 {
914     m_uiClient->didNotHandleTapAsClick(point);
915 }
916
917 void WebPageProxy::viewportMetaTagWidthDidChange(float width)
918 {
919     m_pageClient.didChangeViewportMetaTagWidth(width);
920 }
921
922 void WebPageProxy::didFinishDrawingPagesToPDF(const IPC::DataReference& pdfData)
923 {
924     m_pageClient.didFinishDrawingPagesToPDF(pdfData);
925 }
926
927 void WebPageProxy::contentSizeCategoryDidChange(const String& contentSizeCategory)
928 {
929     process().send(Messages::WebPage::ContentSizeCategoryDidChange(contentSizeCategory), m_pageID);
930 }
931
932 void WebPageProxy::editorStateChanged(const EditorState& editorState)
933 {
934     bool couldChangeSecureInputState = m_editorState.isInPasswordField != editorState.isInPasswordField || m_editorState.selectionIsNone;
935     
936     m_editorState = editorState;
937     
938     // Selection being none is a temporary state when editing. Flipping secure input state too quickly was causing trouble (not fully understood).
939     if (couldChangeSecureInputState && !editorState.selectionIsNone)
940         m_pageClient.updateSecureInputState();
941     
942     if (editorState.shouldIgnoreCompositionSelectionChange)
943         return;
944     
945     // We always need to notify the client on iOS to make sure the selection is redrawn,
946     // even during composition to support phrase boundary gesture.
947     m_pageClient.selectionDidChange();
948 }
949
950 #if USE(QUICK_LOOK)
951     
952 void WebPageProxy::didStartLoadForQuickLookDocumentInMainFrame(const String& fileName, const String& uti)
953 {
954     // Ensure that fileName isn't really a path name
955     static_assert(notFound + 1 == 0, "The following line assumes WTF::notFound equals -1");
956     if (m_navigationClient)
957         m_navigationClient->didStartLoadForQuickLookDocumentInMainFrame(fileName.substring(fileName.reverseFind('/') + 1), uti);
958     else
959         m_loaderClient->didStartLoadForQuickLookDocumentInMainFrame(fileName.substring(fileName.reverseFind('/') + 1), uti);
960 }
961
962 void WebPageProxy::didFinishLoadForQuickLookDocumentInMainFrame(const QuickLookDocumentData& data)
963 {
964     if (m_navigationClient)
965         m_navigationClient->didFinishLoadForQuickLookDocumentInMainFrame(data);
966     else
967         m_loaderClient->didFinishLoadForQuickLookDocumentInMainFrame(data);
968 }
969 #endif
970
971 } // namespace WebKit
972
973 #endif // PLATFORM(IOS)