2 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #import "PageClientImpl.h"
29 #if USE(DICTATION_ALTERNATIVES)
30 #import <AppKit/NSTextAlternatives.h>
32 #import "AttributedString.h"
33 #import "ColorSpaceData.h"
34 #import "DataReference.h"
35 #import "DictionaryPopupInfo.h"
36 #import "FindIndicator.h"
37 #import "NativeWebKeyboardEvent.h"
39 #import "WKStringCF.h"
40 #import "WKViewInternal.h"
41 #import "StringUtilities.h"
42 #import "WebContextMenuProxyMac.h"
43 #import "WebEditCommandProxy.h"
44 #import "WebPopupMenuProxyMac.h"
45 #import <WebCore/AlternativeTextUIController.h>
46 #import <WebCore/BitmapImage.h>
47 #import <WebCore/Cursor.h>
48 #import <WebCore/FloatRect.h>
49 #import <WebCore/FoundationExtras.h>
50 #import <WebCore/GraphicsContext.h>
51 #import <WebCore/Image.h>
52 #import <WebCore/KeyboardEvent.h>
53 #import <WebCore/NotImplemented.h>
54 #import <WebCore/SharedBuffer.h>
55 #import <wtf/PassOwnPtr.h>
56 #import <wtf/text/CString.h>
57 #import <wtf/text/WTFString.h>
58 #import <WebKitSystemInterface.h>
60 @interface NSApplication (WebNSApplicationDetails)
61 - (NSCursor *)_cursorRectCursor;
64 #if HAVE(LAYER_HOSTING_IN_WINDOW_SERVER)
65 @interface NSWindow (WebNSWindowDetails)
66 - (BOOL)_hostsLayersInWindowServer;
70 using namespace WebCore;
71 using namespace WebKit;
73 @interface WKEditCommandObjC : NSObject
75 RefPtr<WebEditCommandProxy> m_command;
77 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command;
78 - (WebEditCommandProxy*)command;
81 @interface WKEditorUndoTargetObjC : NSObject
82 - (void)undoEditing:(id)sender;
83 - (void)redoEditing:(id)sender;
86 @implementation WKEditCommandObjC
88 - (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command
98 - (WebEditCommandProxy*)command
100 return m_command.get();
105 @implementation WKEditorUndoTargetObjC
107 - (void)undoEditing:(id)sender
109 ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
110 [sender command]->unapply();
113 - (void)redoEditing:(id)sender
115 ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
116 [sender command]->reapply();
123 PassOwnPtr<PageClientImpl> PageClientImpl::create(WKView* wkView)
125 return adoptPtr(new PageClientImpl(wkView));
128 PageClientImpl::PageClientImpl(WKView* wkView)
130 , m_undoTarget(adoptNS([[WKEditorUndoTargetObjC alloc] init]))
131 #if USE(DICTATION_ALTERNATIVES)
132 , m_alternativeTextUIController(adoptPtr(new AlternativeTextUIController))
137 PageClientImpl::~PageClientImpl()
141 PassOwnPtr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
143 return [m_wkView _createDrawingAreaProxy];
146 void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
148 [m_wkView setNeedsDisplayInRect:rect];
151 void PageClientImpl::displayView()
153 [m_wkView displayIfNeeded];
156 bool PageClientImpl::canScrollView()
158 // -scrollRect:by: does nothing in layer-backed views <rdar://problem/12961719>.
159 return ![m_wkView layer];
162 void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
164 NSRect clippedScrollRect = NSIntersectionRect(scrollRect, NSOffsetRect(scrollRect, -scrollOffset.width(), -scrollOffset.height()));
166 [m_wkView _cacheWindowBottomCornerRect];
168 [m_wkView translateRectsNeedingDisplayInRect:clippedScrollRect by:scrollOffset];
169 [m_wkView scrollRect:clippedScrollRect by:scrollOffset];
172 IntSize PageClientImpl::viewSize()
174 return IntSize([m_wkView bounds].size);
177 bool PageClientImpl::isViewWindowActive()
179 return [[m_wkView window] isKeyWindow] || [NSApp keyWindow] == [m_wkView window];
182 bool PageClientImpl::isViewFocused()
184 return [m_wkView _isFocused];
187 void PageClientImpl::makeFirstResponder()
189 [[m_wkView window] makeFirstResponder:m_wkView];
192 bool PageClientImpl::isViewVisible()
194 if (![m_wkView window])
197 if (![[m_wkView window] isVisible])
200 #if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
201 // Mountain Lion and previous do not support occlusion notifications, and as such will
202 // continue to report as "visible" when not on the active space.
203 if (![[m_wkView window] isOnActiveSpace])
207 if ([m_wkView isHiddenOrHasHiddenAncestor])
210 if ([m_wkView _isWindowOccluded])
216 bool PageClientImpl::isViewInWindow()
218 return [m_wkView window];
221 void PageClientImpl::viewWillMoveToAnotherWindow()
223 clearAllEditCommands();
226 LayerHostingMode PageClientImpl::viewLayerHostingMode()
228 #if HAVE(LAYER_HOSTING_IN_WINDOW_SERVER)
229 if (![m_wkView window])
230 return LayerHostingModeDefault;
232 return [[m_wkView window] _hostsLayersInWindowServer] ? LayerHostingModeInWindowServer : LayerHostingModeDefault;
234 return LayerHostingModeDefault;
238 ColorSpaceData PageClientImpl::colorSpace()
240 return [m_wkView _colorSpace];
243 void PageClientImpl::processDidCrash()
245 [m_wkView _processDidCrash];
248 void PageClientImpl::pageClosed()
250 [m_wkView _pageClosed];
251 #if USE(DICTATION_ALTERNATIVES)
252 m_alternativeTextUIController->clear();
256 void PageClientImpl::didRelaunchProcess()
258 [m_wkView _didRelaunchProcess];
261 void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
263 [m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
266 void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
268 if (![NSApp _cursorRectCursor])
269 [m_wkView _setCursor:cursor.platformCursor()];
272 void PageClientImpl::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
274 [NSCursor setHiddenUntilMouseMoves:hiddenUntilMouseMoves];
277 void PageClientImpl::didChangeViewportProperties(const WebCore::ViewportAttributes&)
281 void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
283 RefPtr<WebEditCommandProxy> command = prpCommand;
285 RetainPtr<WKEditCommandObjC> commandObjC = adoptNS([[WKEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
286 String actionName = WebEditCommandProxy::nameForEditAction(command->editAction());
288 NSUndoManager *undoManager = [m_wkView undoManager];
289 [undoManager beginUndoGrouping];
290 [undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
291 if (!actionName.isEmpty())
292 [undoManager setActionName:(NSString *)actionName];
293 [undoManager endUndoGrouping];
296 void PageClientImpl::clearAllEditCommands()
298 [[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
301 bool PageClientImpl::canUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
303 return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] canUndo] : [[m_wkView undoManager] canRedo];
306 void PageClientImpl::executeUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
308 return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] undo] : [[m_wkView undoManager] redo];
311 bool PageClientImpl::interpretKeyEvent(const NativeWebKeyboardEvent& event, Vector<WebCore::KeypressCommand>& commands)
313 return [m_wkView _interpretKeyEvent:event.nativeEvent() savingCommandsTo:commands];
316 void PageClientImpl::setDragImage(const IntPoint& clientPosition, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
318 RetainPtr<CGImageRef> dragCGImage = dragImage->makeCGImage();
319 RetainPtr<NSImage> dragNSImage = adoptNS([[NSImage alloc] initWithCGImage:dragCGImage.get() size:dragImage->size()]);
321 [m_wkView _setDragImage:dragNSImage.get() at:clientPosition linkDrag:isLinkDrag];
324 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)
326 RefPtr<Image> image = BitmapImage::create();
327 image->setData(imageBuffer.get(), true);
328 [m_wkView _setPromisedData:image.get() withFileName:filename withExtension:extension withTitle:title withURL:url withVisibleURL:visibleUrl withArchive:archiveBuffer.get() forPasteboard:pasteboardName];
331 void PageClientImpl::updateTextInputState(bool updateSecureInputState)
333 [m_wkView _updateTextInputStateIncludingSecureInputState:updateSecureInputState];
336 void PageClientImpl::resetSecureInputState()
338 [m_wkView _resetSecureInputState];
341 void PageClientImpl::notifyInputContextAboutDiscardedComposition()
343 [m_wkView _notifyInputContextAboutDiscardedComposition];
346 FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
348 return [m_wkView _convertToDeviceSpace:rect];
351 FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
353 return [m_wkView _convertToUserSpace:rect];
356 IntPoint PageClientImpl::screenToWindow(const IntPoint& point)
358 NSPoint windowCoord = [[m_wkView window] convertScreenToBase:point];
359 return IntPoint([m_wkView convertPoint:windowCoord fromView:nil]);
362 IntRect PageClientImpl::windowToScreen(const IntRect& rect)
364 NSRect tempRect = rect;
365 tempRect = [m_wkView convertRect:tempRect toView:nil];
366 tempRect.origin = [[m_wkView window] convertBaseToScreen:tempRect.origin];
367 return enclosingIntRect(tempRect);
370 #if ENABLE(GESTURE_EVENTS)
371 void PageClientImpl::doneWithGestureEvent(const WebGestureEvent&, bool wasEventHandled)
377 void PageClientImpl::doneWithKeyEvent(const NativeWebKeyboardEvent& event, bool eventWasHandled)
379 [m_wkView _doneWithKeyEvent:event.nativeEvent() eventWasHandled:eventWasHandled];
382 PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
384 return WebPopupMenuProxyMac::create(m_wkView, page);
387 PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
389 return WebContextMenuProxyMac::create(m_wkView, page);
392 #if ENABLE(INPUT_TYPE_COLOR)
393 PassRefPtr<WebColorChooserProxy> PageClientImpl::createColorChooserProxy(WebPageProxy*, const WebCore::Color&, const WebCore::IntRect&)
400 void PageClientImpl::setFindIndicator(PassRefPtr<FindIndicator> findIndicator, bool fadeOut, bool animate)
402 [m_wkView _setFindIndicator:findIndicator fadeOut:fadeOut animate:animate];
405 void PageClientImpl::accessibilityWebProcessTokenReceived(const CoreIPC::DataReference& data)
407 NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
408 [m_wkView _setAccessibilityWebProcessToken:remoteToken];
411 void PageClientImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
413 ASSERT(!layerTreeContext.isEmpty());
415 CALayer *renderLayer = WKMakeRenderLayer(layerTreeContext.contextID);
416 [m_wkView _setAcceleratedCompositingModeRootLayer:renderLayer];
419 void PageClientImpl::exitAcceleratedCompositingMode()
421 [m_wkView _setAcceleratedCompositingModeRootLayer:nil];
424 void PageClientImpl::updateAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
426 ASSERT(!layerTreeContext.isEmpty());
428 CALayer *renderLayer = WKMakeRenderLayer(layerTreeContext.contextID);
429 [m_wkView _setAcceleratedCompositingModeRootLayer:renderLayer];
432 void PageClientImpl::setAcceleratedCompositingRootLayer(CALayer *rootLayer)
434 [m_wkView _setAcceleratedCompositingModeRootLayer:rootLayer];
437 void PageClientImpl::pluginFocusOrWindowFocusChanged(uint64_t pluginComplexTextInputIdentifier, bool pluginHasFocusAndWindowHasFocus)
439 [m_wkView _pluginFocusOrWindowFocusChanged:pluginHasFocusAndWindowHasFocus pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
442 void PageClientImpl::setPluginComplexTextInputState(uint64_t pluginComplexTextInputIdentifier, PluginComplexTextInputState pluginComplexTextInputState)
444 [m_wkView _setPluginComplexTextInputState:pluginComplexTextInputState pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
447 CGContextRef PageClientImpl::containingWindowGraphicsContext()
449 NSWindow *window = [m_wkView window];
451 // Don't try to get the graphics context if the NSWindow doesn't have a window device.
452 if ([window windowNumber] <= 0)
455 return static_cast<CGContextRef>([[window graphicsContext] graphicsPort]);
458 void PageClientImpl::didCommitLoadForMainFrame(bool useCustomRepresentation)
460 [m_wkView _setPageHasCustomRepresentation:useCustomRepresentation];
463 void PageClientImpl::didFinishLoadingDataForCustomRepresentation(const String& suggestedFilename, const CoreIPC::DataReference& dataReference)
465 [m_wkView _didFinishLoadingDataForCustomRepresentationWithSuggestedFilename:suggestedFilename dataReference:dataReference];
468 double PageClientImpl::customRepresentationZoomFactor()
470 return [m_wkView _customRepresentationZoomFactor];
473 void PageClientImpl::setCustomRepresentationZoomFactor(double zoomFactor)
475 [m_wkView _setCustomRepresentationZoomFactor:zoomFactor];
478 void PageClientImpl::findStringInCustomRepresentation(const String& string, FindOptions options, unsigned maxMatchCount)
480 [m_wkView _findStringInCustomRepresentation:string withFindOptions:options maxMatchCount:maxMatchCount];
483 void PageClientImpl::countStringMatchesInCustomRepresentation(const String& string, FindOptions options, unsigned maxMatchCount)
485 [m_wkView _countStringMatchesInCustomRepresentation:string withFindOptions:options maxMatchCount:maxMatchCount];
488 void PageClientImpl::flashBackingStoreUpdates(const Vector<IntRect>&)
493 void PageClientImpl::didPerformDictionaryLookup(const AttributedString& text, const DictionaryPopupInfo& dictionaryPopupInfo)
495 RetainPtr<NSAttributedString> attributedString = text.string;
496 NSPoint textBaselineOrigin = dictionaryPopupInfo.origin;
498 // Convert to screen coordinates.
499 textBaselineOrigin = [m_wkView convertPoint:textBaselineOrigin toView:nil];
500 textBaselineOrigin = [m_wkView.window convertRectToScreen:NSMakeRect(textBaselineOrigin.x, textBaselineOrigin.y, 0, 0)].origin;
502 WKShowWordDefinitionWindow(attributedString.get(), textBaselineOrigin, (NSDictionary *)dictionaryPopupInfo.options.get());
505 void PageClientImpl::dismissDictionaryLookupPanel()
507 WKHideWordDefinitionWindow();
510 void PageClientImpl::showCorrectionPanel(AlternativeTextType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings)
512 #if USE(AUTOCORRECTION_PANEL)
513 if (!isViewVisible() || !isViewInWindow())
515 m_correctionPanel.show(m_wkView, type, boundingBoxOfReplacedString, replacedString, replacementString, alternativeReplacementStrings);
519 void PageClientImpl::dismissCorrectionPanel(ReasonForDismissingAlternativeText reason)
521 #if USE(AUTOCORRECTION_PANEL)
522 m_correctionPanel.dismiss(reason);
526 String PageClientImpl::dismissCorrectionPanelSoon(WebCore::ReasonForDismissingAlternativeText reason)
528 #if USE(AUTOCORRECTION_PANEL)
529 return m_correctionPanel.dismiss(reason);
535 void PageClientImpl::recordAutocorrectionResponse(AutocorrectionResponseType responseType, const String& replacedString, const String& replacementString)
537 NSCorrectionResponse response = responseType == AutocorrectionReverted ? NSCorrectionResponseReverted : NSCorrectionResponseEdited;
538 CorrectionPanel::recordAutocorrectionResponse(m_wkView, response, replacedString, replacementString);
541 void PageClientImpl::recommendedScrollbarStyleDidChange(int32_t newStyle)
543 NSArray *trackingAreas = [m_wkView trackingAreas];
544 NSUInteger count = [trackingAreas count];
547 for (NSUInteger i = 0; i < count; ++i)
548 [m_wkView removeTrackingArea:[trackingAreas objectAtIndex:i]];
550 // Now re-create a tracking area with the appropriate options given the new scrollbar style
551 NSTrackingAreaOptions options = NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect;
552 if (newStyle == NSScrollerStyleLegacy)
553 options |= NSTrackingActiveAlways;
555 options |= NSTrackingActiveInKeyWindow;
557 NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:[m_wkView frame]
561 [m_wkView addTrackingArea:trackingArea];
562 [trackingArea release];
565 void PageClientImpl::intrinsicContentSizeDidChange(const IntSize& intrinsicContentSize)
567 [m_wkView _setIntrinsicContentSize:intrinsicContentSize];
570 bool PageClientImpl::executeSavedCommandBySelector(const String& selectorString)
572 return [m_wkView _executeSavedCommandBySelector:NSSelectorFromString(selectorString)];
575 #if USE(DICTATION_ALTERNATIVES)
576 uint64_t PageClientImpl::addDictationAlternatives(const RetainPtr<NSTextAlternatives>& alternatives)
578 return m_alternativeTextUIController->addAlternatives(alternatives);
581 void PageClientImpl::removeDictationAlternatives(uint64_t dictationContext)
583 m_alternativeTextUIController->removeAlternatives(dictationContext);
586 void PageClientImpl::showDictationAlternativeUI(const WebCore::FloatRect& boundingBoxOfDictatedText, uint64_t dictationContext)
588 if (!isViewVisible() || !isViewInWindow())
590 m_alternativeTextUIController->showAlternatives(m_wkView, boundingBoxOfDictatedText, dictationContext, ^(NSString* acceptedAlternative){
591 [m_wkView handleAcceptedAlternativeText:acceptedAlternative];
595 Vector<String> PageClientImpl::dictationAlternatives(uint64_t dictationContext)
597 return m_alternativeTextUIController->alternativesForContext(dictationContext);
601 } // namespace WebKit