Occasional assertion failure under recommendedScrollbarStyleDidChange()
[WebKit-https.git] / Source / WebKit2 / UIProcess / mac / PageClientImpl.mm
1 /*
2  * Copyright (C) 2010, 2011 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 "PageClientImpl.h"
28
29 #if PLATFORM(MAC)
30
31 #import "AttributedString.h"
32 #import "ColorSpaceData.h"
33 #import "DataReference.h"
34 #import "DictionaryPopupInfo.h"
35 #import "DownloadProxy.h"
36 #import "NativeWebKeyboardEvent.h"
37 #import "NativeWebWheelEvent.h"
38 #import "NavigationState.h"
39 #import "StringUtilities.h"
40 #import "TextIndicator.h"
41 #import "ViewSnapshotStore.h"
42 #import "WKAPICast.h"
43 #import "WKFullScreenWindowController.h"
44 #import "WKStringCF.h"
45 #import "WKViewInternal.h"
46 #import "WKWebViewInternal.h"
47 #import "WebColorPickerMac.h"
48 #import "WebContextMenuProxyMac.h"
49 #import "WebEditCommandProxy.h"
50 #import "WebPopupMenuProxyMac.h"
51 #import "WindowServerConnection.h"
52 #import "_WKDownloadInternal.h"
53 #import "_WKThumbnailView.h"
54 #import <WebCore/AlternativeTextUIController.h>
55 #import <WebCore/BitmapImage.h>
56 #import <WebCore/Cursor.h>
57 #import <WebCore/FloatRect.h>
58 #import <WebCore/GraphicsContext.h>
59 #import <WebCore/Image.h>
60 #import <WebCore/KeyboardEvent.h>
61 #import <WebCore/NotImplemented.h>
62 #import <WebCore/SharedBuffer.h>
63 #import <WebCore/TextUndoInsertionMarkupMac.h>
64 #import <WebKitSystemInterface.h>
65 #import <wtf/text/CString.h>
66 #import <wtf/text/WTFString.h>
67
68 #if USE(DICTATION_ALTERNATIVES)
69 #import <AppKit/NSTextAlternatives.h>
70 #endif
71
72 @interface NSApplication (WebNSApplicationDetails)
73 - (NSCursor *)_cursorRectCursor;
74 @end
75
76 #if HAVE(OUT_OF_PROCESS_LAYER_HOSTING)
77 @interface NSWindow (WebNSWindowDetails)
78 - (BOOL)_hostsLayersInWindowServer;
79 @end
80 #endif
81
82 using namespace WebCore;
83 using namespace WebKit;
84
85 @interface WKEditCommandObjC : NSObject
86 {
87     RefPtr<WebEditCommandProxy> m_command;
88 }
89 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command;
90 - (WebEditCommandProxy*)command;
91 @end
92
93 @interface WKEditorUndoTargetObjC : NSObject
94 - (void)undoEditing:(id)sender;
95 - (void)redoEditing:(id)sender;
96 @end
97
98 @implementation WKEditCommandObjC
99
100 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command
101 {
102     self = [super init];
103     if (!self)
104         return nil;
105
106     m_command = command;
107     return self;
108 }
109
110 - (WebEditCommandProxy*)command
111 {
112     return m_command.get();
113 }
114
115 @end
116
117 @implementation WKEditorUndoTargetObjC
118
119 - (void)undoEditing:(id)sender
120 {
121     ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
122     [sender command]->unapply();
123 }
124
125 - (void)redoEditing:(id)sender
126 {
127     ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
128     [sender command]->reapply();
129 }
130
131 @end
132
133 namespace WebKit {
134
135 PageClientImpl::PageClientImpl(WKView* wkView, WKWebView *webView)
136     : m_wkView(wkView)
137     , m_webView(webView)
138     , m_undoTarget(adoptNS([[WKEditorUndoTargetObjC alloc] init]))
139 #if USE(DICTATION_ALTERNATIVES)
140     , m_alternativeTextUIController(adoptPtr(new AlternativeTextUIController))
141 #endif
142 {
143 #if !WK_API_ENABLED
144     ASSERT_UNUSED(m_webView, !m_webView);
145 #endif
146 }
147
148 PageClientImpl::~PageClientImpl()
149 {
150 }
151
152 std::unique_ptr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
153 {
154     return [m_wkView _createDrawingAreaProxy];
155 }
156
157 void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
158 {
159     ASSERT_NOT_REACHED();
160 }
161
162 void PageClientImpl::displayView()
163 {
164     ASSERT_NOT_REACHED();
165 }
166
167 bool PageClientImpl::canScrollView()
168 {
169     return false;
170 }
171
172 void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
173 {
174     ASSERT_NOT_REACHED();
175 }
176
177 void PageClientImpl::requestScroll(const FloatPoint& scrollPosition, bool isProgrammaticScroll)
178 {
179     ASSERT_NOT_REACHED();
180 }
181
182 IntSize PageClientImpl::viewSize()
183 {
184     return IntSize([m_wkView bounds].size);
185 }
186
187 NSView *PageClientImpl::activeView() const
188 {
189 #if WK_API_ENABLED
190     return m_wkView._thumbnailView ? (NSView *)m_wkView._thumbnailView : (NSView *)m_wkView;
191 #else
192     return m_wkView;
193 #endif
194 }
195
196 bool PageClientImpl::isViewWindowActive()
197 {
198     NSWindow *activeViewWindow = activeView().window;
199     return activeViewWindow.isKeyWindow || [NSApp keyWindow] == activeViewWindow;
200 }
201
202 bool PageClientImpl::isViewFocused()
203 {
204     return [m_wkView _isFocused];
205 }
206
207 void PageClientImpl::makeFirstResponder()
208 {
209      [[m_wkView window] makeFirstResponder:m_wkView];
210 }
211     
212 bool PageClientImpl::isViewVisible()
213 {
214     NSView *activeView = this->activeView();
215     NSWindow *activeViewWindow = activeView.window;
216
217     if (!activeViewWindow)
218         return false;
219
220     if (!activeViewWindow.isVisible)
221         return false;
222
223 #if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
224     // Mountain Lion and previous do not support occlusion notifications, and as such will
225     // continue to report as "visible" when not on the active space.
226     if (!activeViewWindow.isOnActiveSpace)
227         return false;
228 #endif
229
230     if (activeView.isHiddenOrHasHiddenAncestor)
231         return false;
232
233 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
234     if ([m_wkView windowOcclusionDetectionEnabled] && (activeViewWindow.occlusionState & NSWindowOcclusionStateVisible) != NSWindowOcclusionStateVisible)
235         return false;
236 #endif
237
238     return true;
239 }
240
241 bool PageClientImpl::isViewVisibleOrOccluded()
242 {
243     return activeView().window.isVisible;
244 }
245
246 bool PageClientImpl::isViewInWindow()
247 {
248     return activeView().window;
249 }
250
251 bool PageClientImpl::isVisuallyIdle()
252 {
253     return WindowServerConnection::shared().applicationWindowModificationsHaveStopped() || !isViewVisible();
254 }
255
256 LayerHostingMode PageClientImpl::viewLayerHostingMode()
257 {
258 #if HAVE(OUT_OF_PROCESS_LAYER_HOSTING)
259     if ([activeView().window _hostsLayersInWindowServer])
260         return LayerHostingMode::OutOfProcess;
261 #endif
262     return LayerHostingMode::InProcess;
263 }
264
265 void PageClientImpl::viewWillMoveToAnotherWindow()
266 {
267     clearAllEditCommands();
268 }
269
270 ColorSpaceData PageClientImpl::colorSpace()
271 {
272     return [m_wkView _colorSpace];
273 }
274
275 void PageClientImpl::processDidExit()
276 {
277     [m_wkView _processDidExit];
278 }
279
280 void PageClientImpl::pageClosed()
281 {
282     [m_wkView _pageClosed];
283 #if USE(DICTATION_ALTERNATIVES)
284     m_alternativeTextUIController->clear();
285 #endif
286 }
287
288 void PageClientImpl::didRelaunchProcess()
289 {
290     [m_wkView _didRelaunchProcess];
291 }
292
293 void PageClientImpl::preferencesDidChange()
294 {
295     [m_wkView _preferencesDidChange];
296 }
297
298 void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
299 {
300     [m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
301 }
302
303 void PageClientImpl::didCommitLoadForMainFrame(const String& mimeType, bool useCustomContentProvider)
304 {
305 }
306
307 void PageClientImpl::didFinishLoadingDataForCustomContentProvider(const String& suggestedFilename, const IPC::DataReference& dataReference)
308 {
309 }
310
311 void PageClientImpl::handleDownloadRequest(DownloadProxy* download)
312 {
313     ASSERT_ARG(download, download);
314 #if WK_API_ENABLED
315     ASSERT([download->wrapper() isKindOfClass:[_WKDownload class]]);
316     [static_cast<_WKDownload *>(download->wrapper()) setOriginatingWebView:m_webView];
317 #endif
318 }
319
320 void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
321 {
322     // FIXME: Would be nice to share this code with WebKit1's WebChromeClient.
323
324     if ([NSApp _cursorRectCursor])
325         return;
326
327     if (!m_wkView)
328         return;
329
330     NSWindow *window = [m_wkView window];
331     if (!window)
332         return;
333
334     if ([window windowNumber] != [NSWindow windowNumberAtPoint:[NSEvent mouseLocation] belowWindowWithWindowNumber:0])
335         return;
336
337     NSCursor *platformCursor = cursor.platformCursor();
338     if ([NSCursor currentCursor] == platformCursor)
339         return;
340
341     [platformCursor set];
342 }
343
344 void PageClientImpl::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
345 {
346     [NSCursor setHiddenUntilMouseMoves:hiddenUntilMouseMoves];
347 }
348
349 void PageClientImpl::didChangeViewportProperties(const WebCore::ViewportAttributes&)
350 {
351 }
352
353 void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
354 {
355     RefPtr<WebEditCommandProxy> command = prpCommand;
356
357     RetainPtr<WKEditCommandObjC> commandObjC = adoptNS([[WKEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
358     String actionName = WebEditCommandProxy::nameForEditAction(command->editAction());
359
360     NSUndoManager *undoManager = [m_wkView undoManager];
361     [undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
362     if (!actionName.isEmpty())
363         [undoManager setActionName:(NSString *)actionName];
364 }
365
366 #if USE(INSERTION_UNDO_GROUPING)
367 void PageClientImpl::registerInsertionUndoGrouping()
368 {
369     registerInsertionUndoGroupingWithUndoManager([m_wkView undoManager]);
370 }
371 #endif
372
373 void PageClientImpl::clearAllEditCommands()
374 {
375     [[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
376 }
377
378 bool PageClientImpl::canUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
379 {
380     return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] canUndo] : [[m_wkView undoManager] canRedo];
381 }
382
383 void PageClientImpl::executeUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
384 {
385     return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] undo] : [[m_wkView undoManager] redo];
386 }
387
388 void PageClientImpl::setDragImage(const IntPoint& clientPosition, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
389 {
390     RetainPtr<CGImageRef> dragCGImage = dragImage->makeCGImage();
391     RetainPtr<NSImage> dragNSImage = adoptNS([[NSImage alloc] initWithCGImage:dragCGImage.get() size:dragImage->size()]);
392
393     [m_wkView _setDragImage:dragNSImage.get() at:clientPosition linkDrag:isLinkDrag];
394 }
395
396 void PageClientImpl::setPromisedData(const String& pasteboardName, PassRefPtr<SharedBuffer> imageBuffer, const String& filename, const String& extension, const String& title, const String& url, const String& visibleUrl, PassRefPtr<SharedBuffer> archiveBuffer)
397 {
398     RefPtr<Image> image = BitmapImage::create();
399     image->setData(imageBuffer.get(), true);
400     [m_wkView _setPromisedData:image.get() withFileName:filename withExtension:extension withTitle:title withURL:url withVisibleURL:visibleUrl withArchive:archiveBuffer.get() forPasteboard:pasteboardName];
401 }
402
403 void PageClientImpl::updateSecureInputState()
404 {
405     [m_wkView _updateSecureInputState];
406 }
407
408 void PageClientImpl::resetSecureInputState()
409 {
410     [m_wkView _resetSecureInputState];
411 }
412
413 void PageClientImpl::notifyInputContextAboutDiscardedComposition()
414 {
415     [m_wkView _notifyInputContextAboutDiscardedComposition];
416 }
417
418 FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
419 {
420     return [m_wkView _convertToDeviceSpace:rect];
421 }
422
423 FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
424 {
425     return [m_wkView _convertToUserSpace:rect];
426 }
427    
428 IntPoint PageClientImpl::screenToRootView(const IntPoint& point)
429 {
430 #pragma clang diagnostic push
431 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
432     NSPoint windowCoord = [[m_wkView window] convertScreenToBase:point];
433 #pragma clang diagnostic pop
434     return IntPoint([m_wkView convertPoint:windowCoord fromView:nil]);
435 }
436     
437 IntRect PageClientImpl::rootViewToScreen(const IntRect& rect)
438 {
439     NSRect tempRect = rect;
440     tempRect = [m_wkView convertRect:tempRect toView:nil];
441 #pragma clang diagnostic push
442 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
443     tempRect.origin = [[m_wkView window] convertBaseToScreen:tempRect.origin];
444 #pragma clang diagnostic pop
445     return enclosingIntRect(tempRect);
446 }
447
448 void PageClientImpl::doneWithKeyEvent(const NativeWebKeyboardEvent& event, bool eventWasHandled)
449 {
450     [m_wkView _doneWithKeyEvent:event.nativeEvent() eventWasHandled:eventWasHandled];
451 }
452
453 PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
454 {
455     return WebPopupMenuProxyMac::create(m_wkView, page);
456 }
457
458 PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
459 {
460     return WebContextMenuProxyMac::create(m_wkView, page);
461 }
462
463 #if ENABLE(INPUT_TYPE_COLOR)
464 PassRefPtr<WebColorPicker> PageClientImpl::createColorPicker(WebPageProxy* page, const WebCore::Color& initialColor,  const WebCore::IntRect& rect)
465 {
466     return WebColorPickerMac::create(page, initialColor, rect, wkView());
467 }
468 #endif
469
470 void PageClientImpl::setTextIndicator(PassRefPtr<TextIndicator> textIndicator, bool fadeOut, bool animate)
471 {
472     [m_wkView _setTextIndicator:textIndicator fadeOut:fadeOut animate:animate];
473 }
474
475 void PageClientImpl::accessibilityWebProcessTokenReceived(const IPC::DataReference& data)
476 {
477     NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
478     [m_wkView _setAccessibilityWebProcessToken:remoteToken];
479 }
480     
481 void PageClientImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
482 {
483     ASSERT(!layerTreeContext.isEmpty());
484
485     CALayer *renderLayer = WKMakeRenderLayer(layerTreeContext.contextID);
486     [m_wkView _setAcceleratedCompositingModeRootLayer:renderLayer];
487 }
488
489 void PageClientImpl::exitAcceleratedCompositingMode()
490 {
491     [m_wkView _setAcceleratedCompositingModeRootLayer:nil];
492 }
493
494 void PageClientImpl::updateAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
495 {
496     ASSERT(!layerTreeContext.isEmpty());
497
498     CALayer *renderLayer = WKMakeRenderLayer(layerTreeContext.contextID);
499     [m_wkView _setAcceleratedCompositingModeRootLayer:renderLayer];
500 }
501
502 void PageClientImpl::setAcceleratedCompositingRootLayer(CALayer *rootLayer)
503 {
504     [m_wkView _setAcceleratedCompositingModeRootLayer:rootLayer];
505 }
506
507 CALayer *PageClientImpl::acceleratedCompositingRootLayer() const
508 {
509     return m_wkView._acceleratedCompositingModeRootLayer;
510 }
511
512 PassRefPtr<ViewSnapshot> PageClientImpl::takeViewSnapshot()
513 {
514     return [m_wkView _takeViewSnapshot];
515 }
516
517 void PageClientImpl::wheelEventWasNotHandledByWebCore(const NativeWebWheelEvent& event)
518 {
519     [m_wkView _wheelEventWasNotHandledByWebCore:event.nativeEvent()];
520 }
521
522 void PageClientImpl::pluginFocusOrWindowFocusChanged(uint64_t pluginComplexTextInputIdentifier, bool pluginHasFocusAndWindowHasFocus)
523 {
524     [m_wkView _pluginFocusOrWindowFocusChanged:pluginHasFocusAndWindowHasFocus pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
525 }
526
527 void PageClientImpl::setPluginComplexTextInputState(uint64_t pluginComplexTextInputIdentifier, PluginComplexTextInputState pluginComplexTextInputState)
528 {
529     [m_wkView _setPluginComplexTextInputState:pluginComplexTextInputState pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
530 }
531
532 void PageClientImpl::didPerformDictionaryLookup(const AttributedString& text, const DictionaryPopupInfo& dictionaryPopupInfo)
533 {
534     RetainPtr<NSAttributedString> attributedString = text.string;
535     NSPoint textBaselineOrigin = dictionaryPopupInfo.origin;
536
537     // Convert to screen coordinates.
538     textBaselineOrigin = [m_wkView convertPoint:textBaselineOrigin toView:nil];
539     textBaselineOrigin = [m_wkView.window convertRectToScreen:NSMakeRect(textBaselineOrigin.x, textBaselineOrigin.y, 0, 0)].origin;
540
541     WKShowWordDefinitionWindow(attributedString.get(), textBaselineOrigin, (NSDictionary *)dictionaryPopupInfo.options.get());
542 }
543
544 void PageClientImpl::dismissDictionaryLookupPanel()
545 {
546     // FIXME: We don't know which panel we are dismissing, it may not even be in the current page (see <rdar://problem/13875766>).
547     WKHideWordDefinitionWindow();
548 }
549
550 void PageClientImpl::dismissActionMenuPopovers()
551 {
552     [m_wkView _dismissActionMenuPopovers];
553 }
554
555 void PageClientImpl::showCorrectionPanel(AlternativeTextType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings)
556 {
557 #if USE(AUTOCORRECTION_PANEL)
558     if (!isViewVisible() || !isViewInWindow())
559         return;
560     m_correctionPanel.show(m_wkView, type, boundingBoxOfReplacedString, replacedString, replacementString, alternativeReplacementStrings);
561 #endif
562 }
563
564 void PageClientImpl::dismissCorrectionPanel(ReasonForDismissingAlternativeText reason)
565 {
566 #if USE(AUTOCORRECTION_PANEL)
567     m_correctionPanel.dismiss(reason);
568 #endif
569 }
570
571 String PageClientImpl::dismissCorrectionPanelSoon(WebCore::ReasonForDismissingAlternativeText reason)
572 {
573 #if USE(AUTOCORRECTION_PANEL)
574     return m_correctionPanel.dismiss(reason);
575 #else
576     return String();
577 #endif
578 }
579
580 void PageClientImpl::recordAutocorrectionResponse(AutocorrectionResponseType responseType, const String& replacedString, const String& replacementString)
581 {
582     NSCorrectionResponse response = responseType == AutocorrectionReverted ? NSCorrectionResponseReverted : NSCorrectionResponseEdited;
583     CorrectionPanel::recordAutocorrectionResponse(m_wkView, response, replacedString, replacementString);
584 }
585
586 void PageClientImpl::recommendedScrollbarStyleDidChange(int32_t newStyle)
587 {
588     // Now re-create a tracking area with the appropriate options given the new scrollbar style
589     NSTrackingAreaOptions options = NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect;
590     if (newStyle == NSScrollerStyleLegacy)
591         options |= NSTrackingActiveAlways;
592     else
593         options |= NSTrackingActiveInKeyWindow;
594
595     RetainPtr<NSTrackingArea> trackingArea = adoptNS([[NSTrackingArea alloc] initWithRect:[m_wkView frame] options:options owner:m_wkView userInfo:nil]);
596     [m_wkView _setPrimaryTrackingArea:trackingArea.get()];
597 }
598
599 void PageClientImpl::intrinsicContentSizeDidChange(const IntSize& intrinsicContentSize)
600 {
601     [m_wkView _setIntrinsicContentSize:intrinsicContentSize];
602 }
603
604 bool PageClientImpl::executeSavedCommandBySelector(const String& selectorString)
605 {
606     return [m_wkView _executeSavedCommandBySelector:NSSelectorFromString(selectorString)];
607 }
608
609 #if USE(DICTATION_ALTERNATIVES)
610 uint64_t PageClientImpl::addDictationAlternatives(const RetainPtr<NSTextAlternatives>& alternatives)
611 {
612     return m_alternativeTextUIController->addAlternatives(alternatives);
613 }
614
615 void PageClientImpl::removeDictationAlternatives(uint64_t dictationContext)
616 {
617     m_alternativeTextUIController->removeAlternatives(dictationContext);
618 }
619
620 void PageClientImpl::showDictationAlternativeUI(const WebCore::FloatRect& boundingBoxOfDictatedText, uint64_t dictationContext)
621 {
622     if (!isViewVisible() || !isViewInWindow())
623         return;
624     m_alternativeTextUIController->showAlternatives(m_wkView, boundingBoxOfDictatedText, dictationContext, ^(NSString* acceptedAlternative){
625         [m_wkView handleAcceptedAlternativeText:acceptedAlternative];
626     });
627 }
628
629 Vector<String> PageClientImpl::dictationAlternatives(uint64_t dictationContext)
630 {
631     return m_alternativeTextUIController->alternativesForContext(dictationContext);
632 }
633 #endif
634
635 #if ENABLE(FULLSCREEN_API)
636
637 WebFullScreenManagerProxyClient& PageClientImpl::fullScreenManagerProxyClient()
638 {
639     return *this;
640 }
641
642 // WebFullScreenManagerProxyClient
643
644 void PageClientImpl::closeFullScreenManager()
645 {
646     [m_wkView _closeFullScreenWindowController];
647 }
648
649 bool PageClientImpl::isFullScreen()
650 {
651     if (!m_wkView._hasFullScreenWindowController)
652         return false;
653
654     return m_wkView._fullScreenWindowController.isFullScreen;
655 }
656
657 void PageClientImpl::enterFullScreen()
658 {
659     [m_wkView._fullScreenWindowController enterFullScreen:nil];
660 }
661
662 void PageClientImpl::exitFullScreen()
663 {
664     [m_wkView._fullScreenWindowController exitFullScreen];
665 }
666
667 void PageClientImpl::beganEnterFullScreen(const IntRect& initialFrame, const IntRect& finalFrame)
668 {
669     [m_wkView._fullScreenWindowController beganEnterFullScreenWithInitialFrame:initialFrame finalFrame:finalFrame];
670 }
671
672 void PageClientImpl::beganExitFullScreen(const IntRect& initialFrame, const IntRect& finalFrame)
673 {
674     [m_wkView._fullScreenWindowController beganExitFullScreenWithInitialFrame:initialFrame finalFrame:finalFrame];
675 }
676
677 #endif // ENABLE(FULLSCREEN_API)
678
679 void PageClientImpl::navigationGestureDidBegin()
680 {
681     // Hide the text indicator and action menu popovers if they are visible.
682     setTextIndicator(nullptr, false, false);
683     dismissActionMenuPopovers();
684
685 #if WK_API_ENABLED
686     if (m_webView)
687         NavigationState::fromWebPage(*m_webView->_page).navigationGestureDidBegin();
688 #endif
689 }
690
691 void PageClientImpl::navigationGestureWillEnd(bool willNavigate, WebBackForwardListItem& item)
692 {
693 #if WK_API_ENABLED
694     if (m_webView)
695         NavigationState::fromWebPage(*m_webView->_page).navigationGestureWillEnd(willNavigate, item);
696 #else
697     UNUSED_PARAM(willNavigate);
698     UNUSED_PARAM(item);
699 #endif
700 }
701
702 void PageClientImpl::navigationGestureDidEnd(bool willNavigate, WebBackForwardListItem& item)
703 {
704 #if WK_API_ENABLED
705     if (m_webView)
706         NavigationState::fromWebPage(*m_webView->_page).navigationGestureDidEnd(willNavigate, item);
707 #else
708     UNUSED_PARAM(willNavigate);
709     UNUSED_PARAM(item);
710 #endif
711 }
712
713 void PageClientImpl::willRecordNavigationSnapshot(WebBackForwardListItem& item)
714 {
715 #if WK_API_ENABLED
716     if (m_webView)
717         NavigationState::fromWebPage(*m_webView->_page).willRecordNavigationSnapshot(item);
718 #else
719     UNUSED_PARAM(item);
720 #endif
721 }
722
723 void PageClientImpl::didFirstVisuallyNonEmptyLayoutForMainFrame()
724 {
725     [m_wkView _didFirstVisuallyNonEmptyLayoutForMainFrame];
726 }
727
728 void PageClientImpl::didFinishLoadForMainFrame()
729 {
730     [m_wkView _didFinishLoadForMainFrame];
731 }
732
733 void PageClientImpl::didSameDocumentNavigationForMainFrame(SameDocumentNavigationType type)
734 {
735     [m_wkView _didSameDocumentNavigationForMainFrame:type];
736 }
737
738 void PageClientImpl::removeNavigationGestureSnapshot()
739 {
740     [m_wkView _removeNavigationGestureSnapshot];
741 }
742
743 CGRect PageClientImpl::boundsOfLayerInLayerBackedWindowCoordinates(CALayer *layer) const
744 {
745     CALayer *windowContentLayer = static_cast<NSView *>(m_wkView.window.contentView).layer;
746     ASSERT(windowContentLayer);
747
748     return [windowContentLayer convertRect:layer.bounds fromLayer:layer];
749 }
750
751 void PageClientImpl::didPerformActionMenuHitTest(const ActionMenuHitTestResult& result, API::Object* userData)
752 {
753 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
754     [m_wkView _didPerformActionMenuHitTest:result userData:userData];
755 #endif
756 }
757
758 void PageClientImpl::showPlatformContextMenu(NSMenu *menu, IntPoint location)
759 {
760     [menu popUpMenuPositioningItem:nil atLocation:location inView:m_wkView];
761 }
762
763
764 } // namespace WebKit
765
766 #endif // PLATFORM(MAC)