1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
/*
* Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "PageClientImpl.h"
#if USE(DICTATION_ALTERNATIVES)
#import <AppKit/NSTextAlternatives.h>
#endif
#import "AttributedString.h"
#import "ColorSpaceData.h"
#import "DataReference.h"
#import "DictionaryPopupInfo.h"
#import "FindIndicator.h"
#import "NativeWebKeyboardEvent.h"
#import "WKAPICast.h"
#import "WKStringCF.h"
#import "WKViewInternal.h"
#import "StringUtilities.h"
#import "WebContextMenuProxyMac.h"
#import "WebEditCommandProxy.h"
#import "WebPopupMenuProxyMac.h"
#import <WebCore/AlternativeTextUIController.h>
#import <WebCore/BitmapImage.h>
#import <WebCore/Cursor.h>
#import <WebCore/FloatRect.h>
#import <WebCore/FoundationExtras.h>
#import <WebCore/GraphicsContext.h>
#import <WebCore/Image.h>
#import <WebCore/KeyboardEvent.h>
#import <WebCore/NotImplemented.h>
#import <WebCore/SharedBuffer.h>
#import <wtf/PassOwnPtr.h>
#import <wtf/text/CString.h>
#import <wtf/text/WTFString.h>
#import <WebKitSystemInterface.h>
@interface NSApplication (WebNSApplicationDetails)
- (NSCursor *)_cursorRectCursor;
@end
#if HAVE(LAYER_HOSTING_IN_WINDOW_SERVER)
@interface NSWindow (WebNSWindowDetails)
- (BOOL)_hostsLayersInWindowServer;
@end
#endif
using namespace WebCore;
using namespace WebKit;
@interface WKEditCommandObjC : NSObject
{
RefPtr<WebEditCommandProxy> m_command;
}
- (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command;
- (WebEditCommandProxy*)command;
@end
@interface WKEditorUndoTargetObjC : NSObject
- (void)undoEditing:(id)sender;
- (void)redoEditing:(id)sender;
@end
@implementation WKEditCommandObjC
- (id)initWithWebEditCommandProxy:(PassRefPtr<WebEditCommandProxy>)command
{
self = [super init];
if (!self)
return nil;
m_command = command;
return self;
}
- (WebEditCommandProxy*)command
{
return m_command.get();
}
@end
@implementation WKEditorUndoTargetObjC
- (void)undoEditing:(id)sender
{
ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
[sender command]->unapply();
}
- (void)redoEditing:(id)sender
{
ASSERT([sender isKindOfClass:[WKEditCommandObjC class]]);
[sender command]->reapply();
}
@end
namespace WebKit {
PassOwnPtr<PageClientImpl> PageClientImpl::create(WKView* wkView)
{
return adoptPtr(new PageClientImpl(wkView));
}
PageClientImpl::PageClientImpl(WKView* wkView)
: m_wkView(wkView)
, m_undoTarget(adoptNS([[WKEditorUndoTargetObjC alloc] init]))
#if USE(DICTATION_ALTERNATIVES)
, m_alternativeTextUIController(adoptPtr(new AlternativeTextUIController))
#endif
{
}
PageClientImpl::~PageClientImpl()
{
}
PassOwnPtr<DrawingAreaProxy> PageClientImpl::createDrawingAreaProxy()
{
return [m_wkView _createDrawingAreaProxy];
}
void PageClientImpl::setViewNeedsDisplay(const WebCore::IntRect& rect)
{
[m_wkView setNeedsDisplayInRect:rect];
}
void PageClientImpl::displayView()
{
[m_wkView displayIfNeeded];
}
bool PageClientImpl::canScrollView()
{
// -scrollRect:by: does nothing in layer-backed views <rdar://problem/12961719>.
return ![m_wkView layer];
}
void PageClientImpl::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
{
NSRect clippedScrollRect = NSIntersectionRect(scrollRect, NSOffsetRect(scrollRect, -scrollOffset.width(), -scrollOffset.height()));
[m_wkView _cacheWindowBottomCornerRect];
[m_wkView translateRectsNeedingDisplayInRect:clippedScrollRect by:scrollOffset];
[m_wkView scrollRect:clippedScrollRect by:scrollOffset];
}
IntSize PageClientImpl::viewSize()
{
return IntSize([m_wkView bounds].size);
}
bool PageClientImpl::isViewWindowActive()
{
return [[m_wkView window] isKeyWindow] || [NSApp keyWindow] == [m_wkView window];
}
bool PageClientImpl::isViewFocused()
{
return [m_wkView _isFocused];
}
void PageClientImpl::makeFirstResponder()
{
[[m_wkView window] makeFirstResponder:m_wkView];
}
bool PageClientImpl::isViewVisible()
{
if (![m_wkView window])
return false;
if (![[m_wkView window] isVisible])
return false;
#if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
// Mountain Lion and previous do not support occlusion notifications, and as such will
// continue to report as "visible" when not on the active space.
if (![[m_wkView window] isOnActiveSpace])
return false;
#endif
if ([m_wkView isHiddenOrHasHiddenAncestor])
return false;
if ([m_wkView _isWindowOccluded])
return false;
return true;
}
bool PageClientImpl::isViewInWindow()
{
return [m_wkView window];
}
void PageClientImpl::viewWillMoveToAnotherWindow()
{
clearAllEditCommands();
}
LayerHostingMode PageClientImpl::viewLayerHostingMode()
{
#if HAVE(LAYER_HOSTING_IN_WINDOW_SERVER)
if (![m_wkView window])
return LayerHostingModeDefault;
return [[m_wkView window] _hostsLayersInWindowServer] ? LayerHostingModeInWindowServer : LayerHostingModeDefault;
#else
return LayerHostingModeDefault;
#endif
}
ColorSpaceData PageClientImpl::colorSpace()
{
return [m_wkView _colorSpace];
}
void PageClientImpl::processDidCrash()
{
[m_wkView _processDidCrash];
}
void PageClientImpl::pageClosed()
{
[m_wkView _pageClosed];
#if USE(DICTATION_ALTERNATIVES)
m_alternativeTextUIController->clear();
#endif
}
void PageClientImpl::didRelaunchProcess()
{
[m_wkView _didRelaunchProcess];
}
void PageClientImpl::preferencesDidChange()
{
[m_wkView _preferencesDidChange];
}
void PageClientImpl::toolTipChanged(const String& oldToolTip, const String& newToolTip)
{
[m_wkView _toolTipChangedFrom:nsStringFromWebCoreString(oldToolTip) to:nsStringFromWebCoreString(newToolTip)];
}
void PageClientImpl::setCursor(const WebCore::Cursor& cursor)
{
if (![NSApp _cursorRectCursor])
[m_wkView _setCursor:cursor.platformCursor()];
}
void PageClientImpl::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
{
[NSCursor setHiddenUntilMouseMoves:hiddenUntilMouseMoves];
}
void PageClientImpl::didChangeViewportProperties(const WebCore::ViewportAttributes&)
{
}
void PageClientImpl::registerEditCommand(PassRefPtr<WebEditCommandProxy> prpCommand, WebPageProxy::UndoOrRedo undoOrRedo)
{
RefPtr<WebEditCommandProxy> command = prpCommand;
RetainPtr<WKEditCommandObjC> commandObjC = adoptNS([[WKEditCommandObjC alloc] initWithWebEditCommandProxy:command]);
String actionName = WebEditCommandProxy::nameForEditAction(command->editAction());
NSUndoManager *undoManager = [m_wkView undoManager];
[undoManager registerUndoWithTarget:m_undoTarget.get() selector:((undoOrRedo == WebPageProxy::Undo) ? @selector(undoEditing:) : @selector(redoEditing:)) object:commandObjC.get()];
if (!actionName.isEmpty())
[undoManager setActionName:(NSString *)actionName];
}
void PageClientImpl::clearAllEditCommands()
{
[[m_wkView undoManager] removeAllActionsWithTarget:m_undoTarget.get()];
}
bool PageClientImpl::canUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
{
return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] canUndo] : [[m_wkView undoManager] canRedo];
}
void PageClientImpl::executeUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
{
return (undoOrRedo == WebPageProxy::Undo) ? [[m_wkView undoManager] undo] : [[m_wkView undoManager] redo];
}
bool PageClientImpl::interpretKeyEvent(const NativeWebKeyboardEvent& event, Vector<WebCore::KeypressCommand>& commands)
{
return [m_wkView _interpretKeyEvent:event.nativeEvent() savingCommandsTo:commands];
}
void PageClientImpl::setDragImage(const IntPoint& clientPosition, PassRefPtr<ShareableBitmap> dragImage, bool isLinkDrag)
{
RetainPtr<CGImageRef> dragCGImage = dragImage->makeCGImage();
RetainPtr<NSImage> dragNSImage = adoptNS([[NSImage alloc] initWithCGImage:dragCGImage.get() size:dragImage->size()]);
[m_wkView _setDragImage:dragNSImage.get() at:clientPosition linkDrag:isLinkDrag];
}
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)
{
RefPtr<Image> image = BitmapImage::create();
image->setData(imageBuffer.get(), true);
[m_wkView _setPromisedData:image.get() withFileName:filename withExtension:extension withTitle:title withURL:url withVisibleURL:visibleUrl withArchive:archiveBuffer.get() forPasteboard:pasteboardName];
}
void PageClientImpl::updateSecureInputState()
{
[m_wkView _updateSecureInputState];
}
void PageClientImpl::resetSecureInputState()
{
[m_wkView _resetSecureInputState];
}
void PageClientImpl::notifyInputContextAboutDiscardedComposition()
{
[m_wkView _notifyInputContextAboutDiscardedComposition];
}
FloatRect PageClientImpl::convertToDeviceSpace(const FloatRect& rect)
{
return [m_wkView _convertToDeviceSpace:rect];
}
FloatRect PageClientImpl::convertToUserSpace(const FloatRect& rect)
{
return [m_wkView _convertToUserSpace:rect];
}
IntPoint PageClientImpl::screenToWindow(const IntPoint& point)
{
NSPoint windowCoord = [[m_wkView window] convertScreenToBase:point];
return IntPoint([m_wkView convertPoint:windowCoord fromView:nil]);
}
IntRect PageClientImpl::windowToScreen(const IntRect& rect)
{
NSRect tempRect = rect;
tempRect = [m_wkView convertRect:tempRect toView:nil];
tempRect.origin = [[m_wkView window] convertBaseToScreen:tempRect.origin];
return enclosingIntRect(tempRect);
}
#if ENABLE(GESTURE_EVENTS)
void PageClientImpl::doneWithGestureEvent(const WebGestureEvent&, bool wasEventHandled)
{
notImplemented();
}
#endif
void PageClientImpl::doneWithKeyEvent(const NativeWebKeyboardEvent& event, bool eventWasHandled)
{
[m_wkView _doneWithKeyEvent:event.nativeEvent() eventWasHandled:eventWasHandled];
}
PassRefPtr<WebPopupMenuProxy> PageClientImpl::createPopupMenuProxy(WebPageProxy* page)
{
return WebPopupMenuProxyMac::create(m_wkView, page);
}
PassRefPtr<WebContextMenuProxy> PageClientImpl::createContextMenuProxy(WebPageProxy* page)
{
return WebContextMenuProxyMac::create(m_wkView, page);
}
#if ENABLE(INPUT_TYPE_COLOR)
PassRefPtr<WebColorPicker> PageClientImpl::createColorPicker(WebPageProxy*, const WebCore::Color&, const WebCore::IntRect&)
{
notImplemented();
return 0;
}
#endif
void PageClientImpl::setFindIndicator(PassRefPtr<FindIndicator> findIndicator, bool fadeOut, bool animate)
{
[m_wkView _setFindIndicator:findIndicator fadeOut:fadeOut animate:animate];
}
void PageClientImpl::accessibilityWebProcessTokenReceived(const CoreIPC::DataReference& data)
{
NSData* remoteToken = [NSData dataWithBytes:data.data() length:data.size()];
[m_wkView _setAccessibilityWebProcessToken:remoteToken];
}
void PageClientImpl::enterAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
{
ASSERT(!layerTreeContext.isEmpty());
CALayer *renderLayer = WKMakeRenderLayer(layerTreeContext.contextID);
[m_wkView _setAcceleratedCompositingModeRootLayer:renderLayer];
}
void PageClientImpl::exitAcceleratedCompositingMode()
{
[m_wkView _setAcceleratedCompositingModeRootLayer:nil];
}
void PageClientImpl::updateAcceleratedCompositingMode(const LayerTreeContext& layerTreeContext)
{
ASSERT(!layerTreeContext.isEmpty());
CALayer *renderLayer = WKMakeRenderLayer(layerTreeContext.contextID);
[m_wkView _setAcceleratedCompositingModeRootLayer:renderLayer];
}
void PageClientImpl::setAcceleratedCompositingRootLayer(CALayer *rootLayer)
{
[m_wkView _setAcceleratedCompositingModeRootLayer:rootLayer];
}
void PageClientImpl::pluginFocusOrWindowFocusChanged(uint64_t pluginComplexTextInputIdentifier, bool pluginHasFocusAndWindowHasFocus)
{
[m_wkView _pluginFocusOrWindowFocusChanged:pluginHasFocusAndWindowHasFocus pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
}
void PageClientImpl::setPluginComplexTextInputState(uint64_t pluginComplexTextInputIdentifier, PluginComplexTextInputState pluginComplexTextInputState)
{
[m_wkView _setPluginComplexTextInputState:pluginComplexTextInputState pluginComplexTextInputIdentifier:pluginComplexTextInputIdentifier];
}
CGContextRef PageClientImpl::containingWindowGraphicsContext()
{
NSWindow *window = [m_wkView window];
// Don't try to get the graphics context if the NSWindow doesn't have a window device.
if ([window windowNumber] <= 0)
return 0;
return static_cast<CGContextRef>([[window graphicsContext] graphicsPort]);
}
void PageClientImpl::flashBackingStoreUpdates(const Vector<IntRect>&)
{
notImplemented();
}
void PageClientImpl::didPerformDictionaryLookup(const AttributedString& text, const DictionaryPopupInfo& dictionaryPopupInfo)
{
RetainPtr<NSAttributedString> attributedString = text.string;
NSPoint textBaselineOrigin = dictionaryPopupInfo.origin;
// Convert to screen coordinates.
textBaselineOrigin = [m_wkView convertPoint:textBaselineOrigin toView:nil];
textBaselineOrigin = [m_wkView.window convertRectToScreen:NSMakeRect(textBaselineOrigin.x, textBaselineOrigin.y, 0, 0)].origin;
WKShowWordDefinitionWindow(attributedString.get(), textBaselineOrigin, (NSDictionary *)dictionaryPopupInfo.options.get());
}
void PageClientImpl::dismissDictionaryLookupPanel()
{
// FIXME: We don't know which panel we are dismissing, it may not even be in the current page (see <rdar://problem/13875766>).
WKHideWordDefinitionWindow();
}
void PageClientImpl::showCorrectionPanel(AlternativeTextType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings)
{
#if USE(AUTOCORRECTION_PANEL)
if (!isViewVisible() || !isViewInWindow())
return;
m_correctionPanel.show(m_wkView, type, boundingBoxOfReplacedString, replacedString, replacementString, alternativeReplacementStrings);
#endif
}
void PageClientImpl::dismissCorrectionPanel(ReasonForDismissingAlternativeText reason)
{
#if USE(AUTOCORRECTION_PANEL)
m_correctionPanel.dismiss(reason);
#endif
}
String PageClientImpl::dismissCorrectionPanelSoon(WebCore::ReasonForDismissingAlternativeText reason)
{
#if USE(AUTOCORRECTION_PANEL)
return m_correctionPanel.dismiss(reason);
#else
return String();
#endif
}
void PageClientImpl::recordAutocorrectionResponse(AutocorrectionResponseType responseType, const String& replacedString, const String& replacementString)
{
NSCorrectionResponse response = responseType == AutocorrectionReverted ? NSCorrectionResponseReverted : NSCorrectionResponseEdited;
CorrectionPanel::recordAutocorrectionResponse(m_wkView, response, replacedString, replacementString);
}
void PageClientImpl::recommendedScrollbarStyleDidChange(int32_t newStyle)
{
NSArray *trackingAreas = [m_wkView trackingAreas];
NSUInteger count = [trackingAreas count];
ASSERT(count == 1);
for (NSUInteger i = 0; i < count; ++i)
[m_wkView removeTrackingArea:[trackingAreas objectAtIndex:i]];
// Now re-create a tracking area with the appropriate options given the new scrollbar style
NSTrackingAreaOptions options = NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect;
if (newStyle == NSScrollerStyleLegacy)
options |= NSTrackingActiveAlways;
else
options |= NSTrackingActiveInKeyWindow;
NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:[m_wkView frame]
options:options
owner:m_wkView
userInfo:nil];
[m_wkView addTrackingArea:trackingArea];
[trackingArea release];
}
void PageClientImpl::intrinsicContentSizeDidChange(const IntSize& intrinsicContentSize)
{
[m_wkView _setIntrinsicContentSize:intrinsicContentSize];
}
bool PageClientImpl::executeSavedCommandBySelector(const String& selectorString)
{
return [m_wkView _executeSavedCommandBySelector:NSSelectorFromString(selectorString)];
}
#if USE(DICTATION_ALTERNATIVES)
uint64_t PageClientImpl::addDictationAlternatives(const RetainPtr<NSTextAlternatives>& alternatives)
{
return m_alternativeTextUIController->addAlternatives(alternatives);
}
void PageClientImpl::removeDictationAlternatives(uint64_t dictationContext)
{
m_alternativeTextUIController->removeAlternatives(dictationContext);
}
void PageClientImpl::showDictationAlternativeUI(const WebCore::FloatRect& boundingBoxOfDictatedText, uint64_t dictationContext)
{
if (!isViewVisible() || !isViewInWindow())
return;
m_alternativeTextUIController->showAlternatives(m_wkView, boundingBoxOfDictatedText, dictationContext, ^(NSString* acceptedAlternative){
[m_wkView handleAcceptedAlternativeText:acceptedAlternative];
});
}
Vector<String> PageClientImpl::dictationAlternatives(uint64_t dictationContext)
{
return m_alternativeTextUIController->alternativesForContext(dictationContext);
}
#endif
} // namespace WebKit
|