2 * Copyright (C) 2005, 2006, 2007 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import "WebPDFView.h"
31 #import "WebDataSourceInternal.h"
32 #import "WebDocumentInternal.h"
33 #import "WebDocumentPrivate.h"
35 #import "WebFrameInternal.h"
36 #import "WebFrameView.h"
37 #import "WebLocalizableStrings.h"
38 #import "WebNSArrayExtras.h"
39 #import "WebNSAttributedStringExtras.h"
40 #import "WebNSPasteboardExtras.h"
41 #import "WebNSViewExtras.h"
42 #import "WebPDFRepresentation.h"
43 #import "WebPreferencesPrivate.h"
44 #import "WebUIDelegate.h"
45 #import "WebUIDelegatePrivate.h"
47 #import "WebViewInternal.h"
48 #import <JavaScriptCore/Assertions.h>
49 #import <PDFKit/PDFKit.h>
50 #import <WebCore/EventNames.h>
51 #import <WebCore/FrameLoader.h>
52 #import <WebCore/KURL.h>
53 #import <WebCore/KeyboardEvent.h>
54 #import <WebCore/MouseEvent.h>
55 #import <WebCore/PlatformKeyboardEvent.h>
56 #import <WebKitSystemInterface.h>
58 using namespace WebCore;
59 using namespace EventNames;
61 // Redeclarations of PDFKit notifications. We can't use the API since we use a weak link to the framework.
62 #define _webkit_PDFViewDisplayModeChangedNotification @"PDFViewDisplayModeChanged"
63 #define _webkit_PDFViewScaleChangedNotification @"PDFViewScaleChanged"
64 #define _webkit_PDFViewPageChangedNotification @"PDFViewChangedPage"
66 @interface PDFDocument (PDFKitSecretsIKnow)
67 - (NSPrintOperation *)getPrintOperationForPrintInfo:(NSPrintInfo *)printInfo autoRotate:(BOOL)doRotate;
70 extern "C" NSString *_NSPathForSystemFramework(NSString *framework);
72 @interface WebPDFView (FileInternal)
73 + (Class)_PDFPreviewViewClass;
74 + (Class)_PDFViewClass;
75 - (BOOL)_anyPDFTagsFoundInMenu:(NSMenu *)menu;
76 - (void)_applyPDFDefaults;
77 - (BOOL)_canLookUpInDictionary;
78 - (NSClipView *)_clipViewForPDFDocumentView;
79 - (NSEvent *)_fakeKeyEventWithFunctionKey:(unichar)functionKey;
80 - (NSMutableArray *)_menuItemsFromPDFKitForEvent:(NSEvent *)theEvent;
81 - (PDFSelection *)_nextMatchFor:(NSString *)string direction:(BOOL)forward caseSensitive:(BOOL)caseFlag wrap:(BOOL)wrapFlag fromSelection:(PDFSelection *)initialSelection startInSelection:(BOOL)startInSelection;
82 - (void)_openWithFinder:(id)sender;
84 - (void)_PDFDocumentViewMightHaveScrolled:(NSNotification *)notification;
85 - (BOOL)_pointIsInSelection:(NSPoint)point;
86 - (NSAttributedString *)_scaledAttributedString:(NSAttributedString *)unscaledAttributedString;
87 - (void)_setTextMatches:(NSArray *)array;
88 - (NSString *)_temporaryPDFDirectoryPath;
89 - (void)_trackFirstResponder;
90 - (void)_updatePreferencesSoon;
91 - (NSSet *)_visiblePDFPages;
94 // PDFPrefUpdatingProxy is a class that forwards everything it gets to a target and updates the PDF viewing prefs
95 // after each of those messages. We use it as a way to hook all the places that the PDF viewing attrs change.
96 @interface PDFPrefUpdatingProxy : NSProxy {
99 - (id)initWithView:(WebPDFView *)view;
102 #pragma mark C UTILITY FUNCTIONS
104 static void _applicationInfoForMIMEType(NSString *type, NSString **name, NSImage **image)
108 OSStatus error = LSCopyApplicationForMIMEType((CFStringRef)type, kLSRolesAll, (CFURLRef *)&appURL);
112 NSString *appPath = [appURL path];
115 *image = [[NSWorkspace sharedWorkspace] iconForFile:appPath];
116 [*image setSize:NSMakeSize(16.f,16.f)];
118 NSString *appName = [[NSFileManager defaultManager] displayNameAtPath:appPath];
122 // FIXME 4182876: We can eliminate this function in favor if -isEqual: if [PDFSelection isEqual:] is overridden
123 // to compare contents.
124 static BOOL _PDFSelectionsAreEqual(PDFSelection *selectionA, PDFSelection *selectionB)
126 NSArray *aPages = [selectionA pages];
127 NSArray *bPages = [selectionB pages];
129 if (![aPages isEqual:bPages])
132 int count = [aPages count];
134 for (i = 0; i < count; ++i) {
135 NSRect aBounds = [selectionA boundsForPage:[aPages objectAtIndex:i]];
136 NSRect bBounds = [selectionB boundsForPage:[bPages objectAtIndex:i]];
137 if (!NSEqualRects(aBounds, bBounds)) {
145 @implementation WebPDFView
147 #pragma mark WebPDFView API
149 + (NSBundle *)PDFKitBundle
151 static NSBundle *PDFKitBundle = nil;
152 if (PDFKitBundle == nil) {
153 NSString *PDFKitPath = [_NSPathForSystemFramework(@"Quartz.framework") stringByAppendingString:@"/Frameworks/PDFKit.framework"];
154 if (PDFKitPath == nil) {
155 LOG_ERROR("Couldn't find PDFKit.framework");
158 PDFKitBundle = [NSBundle bundleWithPath:PDFKitPath];
159 if (![PDFKitBundle load]) {
160 LOG_ERROR("Couldn't load PDFKit.framework");
166 + (NSArray *)supportedMIMETypes
168 return [WebPDFRepresentation supportedMIMETypes];
171 - (void)setPDFDocument:(PDFDocument *)doc
173 // Both setDocument: and _applyPDFDefaults will trigger scale and mode-changed notifications.
174 // Those aren't reflecting user actions, so we need to ignore them.
175 _ignoreScaleAndDisplayModeAndPageNotifications = YES;
176 [PDFSubview setDocument:doc];
177 [self _applyPDFDefaults];
178 _ignoreScaleAndDisplayModeAndPageNotifications = NO;
181 #pragma mark NSObject OVERRIDES
185 ASSERT(!trackedFirstResponder);
186 [previewView release];
187 [PDFSubview release];
189 [PDFSubviewProxy release];
190 [textMatches release];
194 #pragma mark NSResponder OVERRIDES
196 - (void)centerSelectionInVisibleArea:(id)sender
198 [PDFSubview scrollSelectionToVisible:nil];
201 - (void)scrollPageDown:(id)sender
203 // PDFView doesn't support this responder method directly, so we pass it a fake key event
204 [PDFSubview keyDown:[self _fakeKeyEventWithFunctionKey:NSPageDownFunctionKey]];
207 - (void)scrollPageUp:(id)sender
209 // PDFView doesn't support this responder method directly, so we pass it a fake key event
210 [PDFSubview keyDown:[self _fakeKeyEventWithFunctionKey:NSPageUpFunctionKey]];
213 - (void)scrollLineDown:(id)sender
215 // PDFView doesn't support this responder method directly, so we pass it a fake key event
216 [PDFSubview keyDown:[self _fakeKeyEventWithFunctionKey:NSDownArrowFunctionKey]];
219 - (void)scrollLineUp:(id)sender
221 // PDFView doesn't support this responder method directly, so we pass it a fake key event
222 [PDFSubview keyDown:[self _fakeKeyEventWithFunctionKey:NSUpArrowFunctionKey]];
225 - (void)scrollToBeginningOfDocument:(id)sender
227 // PDFView doesn't support this responder method directly, so we pass it a fake key event
228 [PDFSubview keyDown:[self _fakeKeyEventWithFunctionKey:NSHomeFunctionKey]];
231 - (void)scrollToEndOfDocument:(id)sender
233 // PDFView doesn't support this responder method directly, so we pass it a fake key event
234 [PDFSubview keyDown:[self _fakeKeyEventWithFunctionKey:NSEndFunctionKey]];
237 // jumpToSelection is the old name for what AppKit now calls centerSelectionInVisibleArea. Safari
238 // was using the old jumpToSelection selector in its menu. Newer versions of Safari will us the
239 // selector centerSelectionInVisibleArea. We'll leave this old selector in place for two reasons:
240 // (1) compatibility between older Safari and newer WebKit; (2) other WebKit-based applications
241 // might be using the jumpToSelection: selector, and we don't want to break them.
242 - (void)jumpToSelection:(id)sender
244 [self centerSelectionInVisibleArea:nil];
247 #pragma mark NSView OVERRIDES
249 - (BOOL)acceptsFirstResponder {
253 - (BOOL)becomeFirstResponder
255 // This works together with setNextKeyView to splice our PDFSubview into
256 // the key loop similar to the way NSScrollView does this.
257 NSWindow *window = [self window];
258 id newFirstResponder = nil;
260 if ([window keyViewSelectionDirection] == NSSelectingPrevious) {
261 NSView *previousValidKeyView = [self previousValidKeyView];
262 if ((previousValidKeyView != self) && (previousValidKeyView != PDFSubview))
263 newFirstResponder = previousValidKeyView;
265 NSView *PDFDocumentView = [PDFSubview documentView];
266 if ([PDFDocumentView acceptsFirstResponder])
267 newFirstResponder = PDFDocumentView;
270 if (!newFirstResponder)
273 if (![window makeFirstResponder:newFirstResponder])
276 [[dataSource webFrame] _clearSelectionInOtherFrames];
281 - (NSView *)hitTest:(NSPoint)point
283 // Override hitTest so we can override menuForEvent.
284 NSEvent *event = [NSApp currentEvent];
285 NSEventType type = [event type];
286 if (type == NSRightMouseDown || (type == NSLeftMouseDown && ([event modifierFlags] & NSControlKeyMask)))
289 return [super hitTest:point];
292 - (id)initWithFrame:(NSRect)frame
294 self = [super initWithFrame:frame];
296 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
298 Class previewViewClass = [[self class] _PDFPreviewViewClass];
300 // We might not have found a previewViewClass, but if we did find it
301 // then we should be able to create an instance.
302 if (previewViewClass) {
303 previewView = [[previewViewClass alloc] initWithFrame:frame];
307 NSView *topLevelPDFKitView = nil;
309 // We'll retain the PDFSubview here so that it is equally retained in all
310 // code paths. That way we don't need to worry about conditionally releasing
312 PDFSubview = [[previewView performSelector:@selector(pdfView)] retain];
313 topLevelPDFKitView = previewView;
315 PDFSubview = [[[[self class] _PDFViewClass] alloc] initWithFrame:frame];
316 topLevelPDFKitView = PDFSubview;
321 [topLevelPDFKitView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
322 [self addSubview:topLevelPDFKitView];
324 [PDFSubview setDelegate:self];
326 // Messaging this proxy is the same as messaging PDFSubview, with the side effect that the
327 // PDF viewing defaults are updated afterwards
328 PDFSubviewProxy = (PDFView *)[[PDFPrefUpdatingProxy alloc] initWithView:self];
334 - (NSMenu *)menuForEvent:(NSEvent *)theEvent
336 // Start with the menu items supplied by PDFKit, with WebKit tags applied
337 NSMutableArray *items = [self _menuItemsFromPDFKitForEvent:theEvent];
339 // Add in an "Open with <default PDF viewer>" item
340 NSString *appName = nil;
341 NSImage *appIcon = nil;
343 _applicationInfoForMIMEType([[dataSource response] MIMEType], &appName, &appIcon);
345 appName = UI_STRING("Finder", "Default application name for Open With context menu");
347 // To match the PDFKit style, we'll add Open with Preview even when there's no document yet to view, and
348 // disable it using validateUserInterfaceItem.
349 NSString *title = [NSString stringWithFormat:UI_STRING("Open with %@", "context menu item for PDF"), appName];
350 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:@selector(_openWithFinder:) keyEquivalent:@""];
351 [item setTag:WebMenuItemTagOpenWithDefaultApplication];
353 [item setImage:appIcon];
354 [items insertObject:item atIndex:0];
357 [items insertObject:[NSMenuItem separatorItem] atIndex:1];
359 // pass the items off to the WebKit context menu mechanism
360 WebView *webView = [[dataSource webFrame] webView];
362 NSMenu *menu = [webView _menuForElement:[self elementAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]] defaultItems:items];
364 // The delegate has now had the opportunity to add items to the standard PDF-related items, or to
365 // remove or modify some of the PDF-related items. In 10.4, the PDF context menu did not go through
366 // the standard WebKit delegate path, and so the standard PDF-related items always appeared. For
367 // clients that create their own context menu by hand-picking specific items from the default list, such as
368 // Safari, none of the PDF-related items will appear until the client is rewritten to explicitly
369 // include these items. For backwards compatibility of tip-of-tree WebKit with the 10.4 version of Safari
370 // (the configuration that people building open source WebKit use), we'll use the entire set of PDFKit-supplied
371 // menu items. This backward-compatibility hack won't work with any non-Safari clients, but this seems OK since
372 // (1) the symptom is fairly minor, and (2) we suspect that non-Safari clients are probably using the entire
373 // set of default items, rather than manually choosing from them. We can remove this code entirely when we
374 // ship a version of Safari that includes the fix for radar 3796579.
375 if (![self _anyPDFTagsFoundInMenu:menu] && [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.apple.Safari"]) {
376 [menu addItem:[NSMenuItem separatorItem]];
377 NSEnumerator *e = [items objectEnumerator];
378 NSMenuItem *menuItem;
379 while ((menuItem = [e nextObject]) != nil) {
380 // copy menuItem since a given menuItem can be in only one menu at a time, and we don't
381 // want to mess with the menu returned from PDFKit.
382 [menu addItem:[menuItem copy]];
389 - (void)setNextKeyView:(NSView *)aView
391 // This works together with becomeFirstResponder to splice PDFSubview into
392 // the key loop similar to the way NSScrollView and NSClipView do this.
393 NSView *documentView = [PDFSubview documentView];
395 [documentView setNextKeyView:aView];
397 // We need to make the documentView be the next view in the keyview loop.
398 // It would seem more sensible to do this in our init method, but it turns out
399 // that [NSClipView setDocumentView] won't call this method if our next key view
400 // is already set, so we wait until we're called before adding this connection.
401 // We'll also clear it when we're called with nil, so this could go through the
402 // same code path more than once successfully.
403 [super setNextKeyView: aView ? documentView : nil];
405 [super setNextKeyView:aView];
408 - (void)viewDidMoveToWindow
410 // FIXME 2573089: we can observe a notification for first responder changes
411 // instead of the very frequent NSWindowDidUpdateNotification if/when 2573089 is addressed.
412 NSWindow *newWindow = [self window];
416 [self _trackFirstResponder];
417 NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
418 [notificationCenter addObserver:self
419 selector:@selector(_trackFirstResponder)
420 name:NSWindowDidUpdateNotification
423 [notificationCenter addObserver:self
424 selector:@selector(_scaleOrDisplayModeOrPageChanged:)
425 name:_webkit_PDFViewScaleChangedNotification
428 [notificationCenter addObserver:self
429 selector:@selector(_scaleOrDisplayModeOrPageChanged:)
430 name:_webkit_PDFViewDisplayModeChangedNotification
433 [notificationCenter addObserver:self
434 selector:@selector(_scaleOrDisplayModeOrPageChanged:)
435 name:_webkit_PDFViewPageChangedNotification
438 [notificationCenter addObserver:self
439 selector:@selector(_PDFDocumentViewMightHaveScrolled:)
440 name:NSViewBoundsDidChangeNotification
441 object:[self _clipViewForPDFDocumentView]];
444 - (void)viewWillMoveToWindow:(NSWindow *)window
446 // FIXME 2573089: we can observe a notification for changes to the first responder
447 // instead of the very frequent NSWindowDidUpdateNotification if/when 2573089 is addressed.
448 NSWindow *oldWindow = [self window];
452 NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
453 [notificationCenter removeObserver:self
454 name:NSWindowDidUpdateNotification
456 [notificationCenter removeObserver:self
457 name:_webkit_PDFViewScaleChangedNotification
459 [notificationCenter removeObserver:self
460 name:_webkit_PDFViewDisplayModeChangedNotification
462 [notificationCenter removeObserver:self
463 name:_webkit_PDFViewPageChangedNotification
466 [notificationCenter removeObserver:self
467 name:NSViewBoundsDidChangeNotification
468 object:[self _clipViewForPDFDocumentView]];
470 [trackedFirstResponder release];
471 trackedFirstResponder = nil;
474 #pragma mark NSUserInterfaceValidations PROTOCOL IMPLEMENTATION
476 - (BOOL)validateUserInterfaceItemWithoutDelegate:(id <NSValidatedUserInterfaceItem>)item
478 SEL action = [item action];
479 if (action == @selector(takeFindStringFromSelection:) || action == @selector(centerSelectionInVisibleArea:) || action == @selector(jumpToSelection:))
480 return [PDFSubview currentSelection] != nil;
482 if (action == @selector(_openWithFinder:))
483 return [PDFSubview document] != nil;
485 if (action == @selector(_lookUpInDictionaryFromMenu:))
486 return [self _canLookUpInDictionary];
491 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
493 BOOL result = [self validateUserInterfaceItemWithoutDelegate:item];
494 return CallUIDelegateReturningBoolean(result, [self _webView], @selector(webView:validateUserInterfaceItem:defaultValidation:), item, result);
497 #pragma mark INTERFACE BUILDER ACTIONS FOR SAFARI
499 // Surprisingly enough, this isn't defined in any superclass, though it is defined in assorted AppKit classes since
500 // it's a standard menu item IBAction.
501 - (IBAction)copy:(id)sender
503 [PDFSubview copy:sender];
506 // This used to be a standard IBAction (for Use Selection For Find), but AppKit now uses performFindPanelAction:
507 // with a menu item tag for this purpose.
508 - (IBAction)takeFindStringFromSelection:(id)sender
510 [NSPasteboard _web_setFindPasteboardString:[[PDFSubview currentSelection] string] withOwner:self];
513 #pragma mark WebFrameView UNDECLARED "DELEGATE METHODS"
515 // This is tested in -[WebFrameView canPrintHeadersAndFooters], but isn't declared anywhere (yuck)
516 - (BOOL)canPrintHeadersAndFooters
521 // This is tested in -[WebFrameView printOperationWithPrintInfo:], but isn't declared anywhere (yuck)
522 - (NSPrintOperation *)printOperationWithPrintInfo:(NSPrintInfo *)printInfo
524 return [[PDFSubview document] getPrintOperationForPrintInfo:printInfo autoRotate:YES];
527 #pragma mark WebDocumentView PROTOCOL IMPLEMENTATION
529 - (void)setDataSource:(WebDataSource *)ds
532 [self setFrame:[[self superview] frame]];
535 - (void)dataSourceUpdated:(WebDataSource *)dataSource
539 - (void)setNeedsLayout:(BOOL)flag
547 - (void)viewWillMoveToHostWindow:(NSWindow *)hostWindow
551 - (void)viewDidMoveToHostWindow
555 #pragma mark WebDocumentElement PROTOCOL IMPLEMENTATION
557 - (NSDictionary *)elementAtPoint:(NSPoint)point
559 WebFrame *frame = [dataSource webFrame];
562 return [NSDictionary dictionaryWithObjectsAndKeys:
563 frame, WebElementFrameKey,
564 [NSNumber numberWithBool:[self _pointIsInSelection:point]], WebElementIsSelectedKey,
568 - (NSDictionary *)elementAtPoint:(NSPoint)point allowShadowContent:(BOOL)allow
570 return [self elementAtPoint:point];
573 #pragma mark WebDocumentSearching PROTOCOL IMPLEMENTATION
575 - (BOOL)searchFor:(NSString *)string direction:(BOOL)forward caseSensitive:(BOOL)caseFlag wrap:(BOOL)wrapFlag
577 return [self searchFor:string direction:forward caseSensitive:caseFlag wrap:wrapFlag startInSelection:NO];
580 #pragma mark WebDocumentIncrementalSearching PROTOCOL IMPLEMENTATION
582 - (BOOL)searchFor:(NSString *)string direction:(BOOL)forward caseSensitive:(BOOL)caseFlag wrap:(BOOL)wrapFlag startInSelection:(BOOL)startInSelection
584 PDFSelection *selection = [self _nextMatchFor:string direction:forward caseSensitive:caseFlag wrap:wrapFlag fromSelection:[PDFSubview currentSelection] startInSelection:startInSelection];
588 [PDFSubview setCurrentSelection:selection];
589 [PDFSubview scrollSelectionToVisible:nil];
593 #pragma mark WebMultipleTextMatches PROTOCOL IMPLEMENTATION
595 - (void)setMarkedTextMatchesAreHighlighted:(BOOL)newValue
597 // This method is part of the WebMultipleTextMatches algorithm, but this class doesn't support
598 // highlighting text matches inline.
601 LOG_ERROR("[WebPDFView setMarkedTextMatchesAreHighlighted:] called with YES, which isn't supported");
605 - (BOOL)markedTextMatchesAreHighlighted
610 - (NSUInteger)markAllMatchesForText:(NSString *)string caseSensitive:(BOOL)caseFlag limit:(NSUInteger)limit
612 PDFSelection *previousMatch = nil;
613 PDFSelection *nextMatch = nil;
614 NSMutableArray *matches = [[NSMutableArray alloc] initWithCapacity:limit];
617 nextMatch = [self _nextMatchFor:string direction:YES caseSensitive:caseFlag wrap:NO fromSelection:previousMatch startInSelection:NO];
621 [matches addObject:nextMatch];
622 previousMatch = nextMatch;
624 if ([matches count] >= limit)
628 [self _setTextMatches:matches];
631 return [matches count];
634 - (void)unmarkAllTextMatches
636 [self _setTextMatches:nil];
639 - (NSArray *)rectsForTextMatches
641 NSMutableArray *result = [NSMutableArray arrayWithCapacity:[textMatches count]];
642 NSSet *visiblePages = [self _visiblePDFPages];
643 NSEnumerator *matchEnumerator = [textMatches objectEnumerator];
646 while ((match = [matchEnumerator nextObject]) != nil) {
647 NSEnumerator *pages = [[match pages] objectEnumerator];
649 while ((page = [pages nextObject]) != nil) {
651 // Skip pages that aren't visible (needed for non-continuous modes, see 5362989)
652 if (![visiblePages containsObject:page])
655 NSRect selectionOnPageInPDFViewCoordinates = [PDFSubview convertRect:[match boundsForPage:page] fromPage:page];
656 [result addObject:[NSValue valueWithRect:selectionOnPageInPDFViewCoordinates]];
663 #pragma mark WebDocumentText PROTOCOL IMPLEMENTATION
665 - (BOOL)supportsTextEncoding
672 return [[PDFSubview document] string];
675 - (NSAttributedString *)attributedString
677 // changing the selection is a hack, but the only way to get an attr string is via PDFSelection
679 // must copy this selection object because we change the selection which seems to release it
680 PDFSelection *savedSelection = [[PDFSubview currentSelection] copy];
681 [PDFSubview selectAll:nil];
682 NSAttributedString *result = [[PDFSubview currentSelection] attributedString];
683 if (savedSelection) {
684 [PDFSubview setCurrentSelection:savedSelection];
685 [savedSelection release];
687 // FIXME: behavior of setCurrentSelection:nil is not documented - check 4182934 for progress
688 // Otherwise, we could collapse this code with the case above.
689 [PDFSubview clearSelection];
692 result = [self _scaledAttributedString:result];
697 - (NSString *)selectedString
699 return [[PDFSubview currentSelection] string];
702 - (NSAttributedString *)selectedAttributedString
704 return [self _scaledAttributedString:[[PDFSubview currentSelection] attributedString]];
709 [PDFSubview selectAll:nil];
714 [PDFSubview clearSelection];
717 #pragma mark WebDocumentViewState PROTOCOL IMPLEMENTATION
719 // Even though to WebKit we are the "docView", in reality a PDFView contains its own scrollview and docView.
720 // And it even turns out there is another PDFKit view between the docView and its enclosing ScrollView, so
721 // we have to be sure to do our calculations based on that view, immediately inside the ClipView. We try
722 // to make as few assumptions about the PDFKit view hierarchy as possible.
724 - (NSPoint)scrollPoint
726 NSView *realDocView = [PDFSubview documentView];
727 NSClipView *clipView = [[realDocView enclosingScrollView] contentView];
728 return [clipView bounds].origin;
731 - (void)setScrollPoint:(NSPoint)p
733 WebFrame *frame = [dataSource webFrame];
734 //FIXME: We only restore scroll state in the non-frames case because otherwise we get a crash due to
735 // PDFKit calling display from within its drawRect:. See bugzilla 4164.
736 if (![frame parentFrame]) {
737 NSView *realDocView = [PDFSubview documentView];
738 [[[realDocView enclosingScrollView] documentView] scrollPoint:p];
744 NSMutableArray *state = [NSMutableArray arrayWithCapacity:4];
745 PDFDisplayMode mode = [PDFSubview displayMode];
746 [state addObject:[NSNumber numberWithInt:mode]];
747 if (mode == kPDFDisplaySinglePage || mode == kPDFDisplayTwoUp) {
748 unsigned int pageIndex = [[PDFSubview document] indexForPage:[PDFSubview currentPage]];
749 [state addObject:[NSNumber numberWithUnsignedInt:pageIndex]];
750 } // else in continuous modes, scroll position gets us to the right page
751 BOOL autoScaleFlag = [PDFSubview autoScales];
752 [state addObject:[NSNumber numberWithBool:autoScaleFlag]];
754 [state addObject:[NSNumber numberWithFloat:[PDFSubview scaleFactor]]];
759 - (void)setViewState:(id)statePList
761 ASSERT([statePList isKindOfClass:[NSArray class]]);
762 NSArray *state = statePList;
764 PDFDisplayMode mode = [[state objectAtIndex:i++] intValue];
765 [PDFSubview setDisplayMode:mode];
766 if (mode == kPDFDisplaySinglePage || mode == kPDFDisplayTwoUp) {
767 unsigned int pageIndex = [[state objectAtIndex:i++] unsignedIntValue];
768 [PDFSubview goToPage:[[PDFSubview document] pageAtIndex:pageIndex]];
769 } // else in continuous modes, scroll position gets us to the right page
770 BOOL autoScaleFlag = [[state objectAtIndex:i++] boolValue];
771 [PDFSubview setAutoScales:autoScaleFlag];
773 [PDFSubview setScaleFactor:[[state objectAtIndex:i++] floatValue]];
776 #pragma mark _WebDocumentTextSizing PROTOCOL IMPLEMENTATION
778 - (IBAction)_makeTextSmaller:(id)sender
780 [PDFSubviewProxy zoomOut:sender];
783 - (IBAction)_makeTextLarger:(id)sender
785 [PDFSubviewProxy zoomIn:sender];
788 - (IBAction)_makeTextStandardSize:(id)sender
790 [PDFSubviewProxy setScaleFactor:1.0f];
793 // never sent because we do not track the common size factor
794 - (void)_textSizeMultiplierChanged { ASSERT_NOT_REACHED(); }
796 - (BOOL)_tracksCommonSizeFactor
798 // We keep our own scale factor instead of tracking the common one in the WebView for a couple reasons.
799 // First, PDFs tend to have visually smaller text because they are laid out for a printed page instead of
800 // the screen. Second, the PDFView feature of AutoScaling means our scaling factor can be quiet variable.
804 - (BOOL)_canMakeTextSmaller
806 return [PDFSubview canZoomOut];
809 - (BOOL)_canMakeTextLarger
811 return [PDFSubview canZoomIn];
814 - (BOOL)_canMakeTextStandardSize
816 return [PDFSubview scaleFactor] != 1.0;
819 #pragma mark WebDocumentSelection PROTOCOL IMPLEMENTATION
821 - (NSRect)selectionRect
823 NSRect result = NSZeroRect;
824 PDFSelection *selection = [PDFSubview currentSelection];
825 NSEnumerator *pages = [[selection pages] objectEnumerator];
827 while ((page = [pages nextObject]) != nil) {
828 NSRect selectionOnPageInPDFViewCoordinates = [PDFSubview convertRect:[selection boundsForPage:page] fromPage:page];
829 if (NSIsEmptyRect(result))
830 result = selectionOnPageInPDFViewCoordinates;
832 result = NSUnionRect(result, selectionOnPageInPDFViewCoordinates);
835 // Convert result to be in documentView (selectionView) coordinates
836 result = [PDFSubview convertRect:result toView:[PDFSubview documentView]];
841 - (NSArray *)selectionTextRects
843 // FIXME: We'd need new PDFKit API/SPI to get multiple text rects for selections that intersect more than one line
844 return [NSArray arrayWithObject:[NSValue valueWithRect:[self selectionRect]]];
847 - (NSView *)selectionView
849 return [PDFSubview documentView];
852 - (NSImage *)selectionImageForcingBlackText:(BOOL)forceBlackText
854 // Convert the selection to an attributed string, and draw that.
855 // FIXME 4621154: this doesn't handle italics (and maybe other styles)
856 // FIXME 4604366: this doesn't handle text at non-actual size
857 NSMutableAttributedString *attributedString = [[self selectedAttributedString] mutableCopy];
858 NSRange wholeStringRange = NSMakeRange(0, [attributedString length]);
860 // Modify the styles in the attributed string to draw black text, no background, and no underline. We draw
861 // no underline because it would look ugly.
862 [attributedString beginEditing];
863 [attributedString removeAttribute:NSBackgroundColorAttributeName range:wholeStringRange];
864 [attributedString removeAttribute:NSUnderlineStyleAttributeName range:wholeStringRange];
866 [attributedString addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithDeviceWhite:0.0f alpha:1.0f] range:wholeStringRange];
867 [attributedString endEditing];
869 NSImage* selectionImage = [[[NSImage alloc] initWithSize:[self selectionRect].size] autorelease];
871 [selectionImage lockFocus];
872 [attributedString drawAtPoint:NSZeroPoint];
873 [selectionImage unlockFocus];
875 [attributedString release];
877 return selectionImage;
880 - (NSImage *)selectionImageForcingWhiteText:(BOOL)forceWhiteText
882 // NOTE: this method is obsolete and doesn't behave as its name suggests.
883 // See comment in WebDocumentPrivate.h.
884 return [self selectionImageForcingBlackText:forceWhiteText];
887 - (NSRect)selectionImageRect
889 // FIXME: deal with clipping?
890 return [self selectionRect];
893 - (NSArray *)pasteboardTypesForSelection
895 return [NSArray arrayWithObjects:NSRTFDPboardType, NSRTFPboardType, NSStringPboardType, nil];
898 - (void)writeSelectionWithPasteboardTypes:(NSArray *)types toPasteboard:(NSPasteboard *)pasteboard
900 NSAttributedString *attributedString = [self selectedAttributedString];
902 if ([types containsObject:NSRTFDPboardType]) {
903 NSData *RTFDData = [attributedString RTFDFromRange:NSMakeRange(0, [attributedString length]) documentAttributes:nil];
904 [pasteboard setData:RTFDData forType:NSRTFDPboardType];
907 if ([types containsObject:NSRTFPboardType]) {
908 if ([attributedString containsAttachments])
909 attributedString = [attributedString _web_attributedStringByStrippingAttachmentCharacters];
911 NSData *RTFData = [attributedString RTFFromRange:NSMakeRange(0, [attributedString length]) documentAttributes:nil];
912 [pasteboard setData:RTFData forType:NSRTFPboardType];
915 if ([types containsObject:NSStringPboardType])
916 [pasteboard setString:[self selectedString] forType:NSStringPboardType];
919 #pragma mark PDFView DELEGATE METHODS
921 - (void)PDFViewWillClickOnLink:(PDFView *)sender withURL:(NSURL *)URL
926 NSWindow *window = [sender window];
927 NSEvent *nsEvent = [window currentEvent];
928 const int noButton = -1;
929 int button = noButton;
931 switch ([nsEvent type]) {
939 button = [nsEvent buttonNumber];
942 PlatformKeyboardEvent pe(nsEvent);
943 event = new KeyboardEvent(keydownEvent, true, true, 0,
944 pe.keyIdentifier(), pe.WindowsKeyCode(),
945 pe.ctrlKey(), pe.altKey(), pe.shiftKey(), pe.metaKey(), false);
950 if (button != noButton)
951 event = new MouseEvent(clickEvent, true, true, 0, [nsEvent clickCount], 0, 0, 0, 0,
952 [nsEvent modifierFlags] & NSControlKeyMask,
953 [nsEvent modifierFlags] & NSAlternateKeyMask,
954 [nsEvent modifierFlags] & NSShiftKeyMask,
955 [nsEvent modifierFlags] & NSCommandKeyMask,
958 // Call to the frame loader because this is where our security checks are made.
959 [[dataSource webFrame] _frameLoader]->load(URL, event.get());
962 - (void)PDFViewOpenPDFInNativeApplication:(PDFView *)sender
964 // Delegate method sent when the user requests opening the PDF file in the system's default app
965 [self _openWithFinder:sender];
968 - (void)PDFViewSavePDFToDownloadFolder:(PDFView *)sender
970 // We don't want to write the file until we have a document to write (see 5267607).
971 if (![PDFSubview document]) {
976 // Delegate method sent when the user requests downloading the PDF file to disk. We pass NO for
977 // showingPanel: so that the PDF file is saved to the standard location without user intervention.
978 CallUIDelegate([self _webView], @selector(webView:saveFrameView:showingPanel:), [[dataSource webFrame] frameView], NO);
983 @implementation WebPDFView (FileInternal)
985 + (Class)_PDFPreviewViewClass
987 static Class PDFPreviewViewClass = nil;
988 static BOOL checkedForPDFPreviewViewClass = NO;
990 if (!checkedForPDFPreviewViewClass) {
991 checkedForPDFPreviewViewClass = YES;
992 PDFPreviewViewClass = [[WebPDFView PDFKitBundle] classNamed:@"PDFPreviewView"];
995 // This class might not be available; callers need to deal with a nil return here.
996 return PDFPreviewViewClass;
999 + (Class)_PDFViewClass
1001 static Class PDFViewClass = nil;
1002 if (PDFViewClass == nil) {
1003 PDFViewClass = [[WebPDFView PDFKitBundle] classNamed:@"PDFView"];
1005 LOG_ERROR("Couldn't find PDFView class in PDFKit.framework");
1007 return PDFViewClass;
1010 - (BOOL)_anyPDFTagsFoundInMenu:(NSMenu *)menu
1012 NSEnumerator *e = [[menu itemArray] objectEnumerator];
1014 while ((item = [e nextObject]) != nil) {
1015 switch ([item tag]) {
1016 case WebMenuItemTagOpenWithDefaultApplication:
1017 case WebMenuItemPDFActualSize:
1018 case WebMenuItemPDFZoomIn:
1019 case WebMenuItemPDFZoomOut:
1020 case WebMenuItemPDFAutoSize:
1021 case WebMenuItemPDFSinglePage:
1022 case WebMenuItemPDFSinglePageScrolling:
1023 case WebMenuItemPDFFacingPages:
1024 case WebMenuItemPDFFacingPagesScrolling:
1025 case WebMenuItemPDFContinuous:
1026 case WebMenuItemPDFNextPage:
1027 case WebMenuItemPDFPreviousPage:
1034 - (void)_applyPDFDefaults
1036 // Set up default viewing params
1037 WebPreferences *prefs = [[dataSource _webView] preferences];
1038 float scaleFactor = [prefs PDFScaleFactor];
1039 if (scaleFactor == 0)
1040 [PDFSubview setAutoScales:YES];
1042 [PDFSubview setAutoScales:NO];
1043 [PDFSubview setScaleFactor:scaleFactor];
1045 [PDFSubview setDisplayMode:[prefs PDFDisplayMode]];
1048 - (BOOL)_canLookUpInDictionary
1050 return [PDFSubview respondsToSelector:@selector(_searchInDictionary:)];
1053 - (NSClipView *)_clipViewForPDFDocumentView
1055 NSClipView *clipView = (NSClipView *)[[PDFSubview documentView] _web_superviewOfClass:[NSClipView class]];
1060 - (NSEvent *)_fakeKeyEventWithFunctionKey:(unichar)functionKey
1062 // FIXME 4400480: when PDFView implements the standard scrolling selectors that this
1063 // method is used to mimic, we can eliminate this method and call them directly.
1064 NSString *keyAsString = [NSString stringWithCharacters:&functionKey length:1];
1065 return [NSEvent keyEventWithType:NSKeyDown
1066 location:NSZeroPoint
1071 characters:keyAsString
1072 charactersIgnoringModifiers:keyAsString
1077 - (void)_lookUpInDictionaryFromMenu:(id)sender
1079 // This method is used by WebKit's context menu item. Here we map to the method that
1080 // PDFView uses. Since the PDFView method isn't API, and isn't available on all versions
1081 // of PDFKit, we use performSelector after a respondsToSelector check, rather than calling it directly.
1082 if ([self _canLookUpInDictionary])
1083 [PDFSubview performSelector:@selector(_searchInDictionary:) withObject:sender];
1086 - (NSMutableArray *)_menuItemsFromPDFKitForEvent:(NSEvent *)theEvent
1088 NSMutableArray *copiedItems = [NSMutableArray array];
1089 NSDictionary *actionsToTags = [[NSDictionary alloc] initWithObjectsAndKeys:
1090 [NSNumber numberWithInt:WebMenuItemPDFActualSize], NSStringFromSelector(@selector(_setActualSize:)),
1091 [NSNumber numberWithInt:WebMenuItemPDFZoomIn], NSStringFromSelector(@selector(zoomIn:)),
1092 [NSNumber numberWithInt:WebMenuItemPDFZoomOut], NSStringFromSelector(@selector(zoomOut:)),
1093 [NSNumber numberWithInt:WebMenuItemPDFAutoSize], NSStringFromSelector(@selector(_setAutoSize:)),
1094 [NSNumber numberWithInt:WebMenuItemPDFSinglePage], NSStringFromSelector(@selector(_setSinglePage:)),
1095 [NSNumber numberWithInt:WebMenuItemPDFSinglePageScrolling], NSStringFromSelector(@selector(_setSinglePageScrolling:)),
1096 [NSNumber numberWithInt:WebMenuItemPDFFacingPages], NSStringFromSelector(@selector(_setDoublePage:)),
1097 [NSNumber numberWithInt:WebMenuItemPDFFacingPagesScrolling], NSStringFromSelector(@selector(_setDoublePageScrolling:)),
1098 [NSNumber numberWithInt:WebMenuItemPDFContinuous], NSStringFromSelector(@selector(_toggleContinuous:)),
1099 [NSNumber numberWithInt:WebMenuItemPDFNextPage], NSStringFromSelector(@selector(goToNextPage:)),
1100 [NSNumber numberWithInt:WebMenuItemPDFPreviousPage], NSStringFromSelector(@selector(goToPreviousPage:)),
1103 // Leave these menu items out, since WebKit inserts equivalent ones. Note that we leave out PDFKit's "Look Up in Dictionary"
1104 // item here because WebKit already includes an item with the same title and purpose. We map WebKit's to PDFKit's
1105 // "Look Up in Dictionary" via the implementation of -[WebPDFView _lookUpInDictionaryFromMenu:].
1106 NSSet *unwantedActions = [[NSSet alloc] initWithObjects:
1107 NSStringFromSelector(@selector(_searchInSpotlight:)),
1108 NSStringFromSelector(@selector(_searchInGoogle:)),
1109 NSStringFromSelector(@selector(_searchInDictionary:)),
1110 NSStringFromSelector(@selector(copy:)),
1113 NSEnumerator *e = [[[PDFSubview menuForEvent:theEvent] itemArray] objectEnumerator];
1115 while ((item = [e nextObject]) != nil) {
1117 NSString *actionString = NSStringFromSelector([item action]);
1119 if ([unwantedActions containsObject:actionString])
1122 // Copy items since a menu item can be in only one menu at a time, and we don't
1123 // want to modify the original menu supplied by PDFKit.
1124 NSMenuItem *itemCopy = [item copy];
1125 [copiedItems addObject:itemCopy];
1127 // Include all of PDFKit's separators for now. At the end we'll remove any ones that were made
1128 // useless by removing PDFKit's menu items.
1129 if ([itemCopy isSeparatorItem])
1132 NSNumber *tagNumber = [actionsToTags objectForKey:actionString];
1135 if (tagNumber != nil)
1136 tag = [tagNumber intValue];
1138 // This should happen only if PDFKit updates behind WebKit's back. It's non-ideal because clients that only include tags
1139 // that they recognize (like Safari) won't get these PDFKit additions until WebKit is updated to match.
1140 tag = WebMenuItemTagOther;
1141 LOG_ERROR("no WebKit menu item tag found for PDF context menu item action \"%@\", using WebMenuItemTagOther", actionString);
1144 if ([itemCopy tag] == 0) {
1145 [itemCopy setTag:tag];
1146 if ([itemCopy target] == PDFSubview) {
1147 // Note that updating the defaults is cheap because it catches redundant settings, so installing
1148 // the proxy for actions that don't impact the defaults is OK
1149 [itemCopy setTarget:PDFSubviewProxy];
1152 LOG_ERROR("PDF context menu item %@ came with tag %d, so no WebKit tag was applied. This could mean that the item doesn't appear in clients such as Safari.", [itemCopy title], [itemCopy tag]);
1155 [actionsToTags release];
1156 [unwantedActions release];
1158 // Since we might have removed elements supplied by PDFKit, and we want to minimize our hardwired
1159 // knowledge of the order and arrangement of PDFKit's menu items, we need to remove any bogus
1160 // separators that were left behind.
1161 [copiedItems _webkit_removeUselessMenuItemSeparators];
1166 - (PDFSelection *)_nextMatchFor:(NSString *)string direction:(BOOL)forward caseSensitive:(BOOL)caseFlag wrap:(BOOL)wrapFlag fromSelection:(PDFSelection *)initialSelection startInSelection:(BOOL)startInSelection
1168 if (![string length])
1173 options |= NSBackwardsSearch;
1176 options |= NSCaseInsensitiveSearch;
1178 PDFDocument *document = [PDFSubview document];
1180 PDFSelection *selectionForInitialSearch = [initialSelection copy];
1181 if (startInSelection) {
1182 // Initially we want to include the selected text in the search. PDFDocument's API always searches from just
1183 // past the passed-in selection, so we need to pass a selection that's modified appropriately.
1184 // FIXME 4182863: Ideally we'd use a zero-length selection at the edge of the current selection, but zero-length
1185 // selections don't work in PDFDocument. So instead we make a one-length selection just before or after the
1186 // current selection, which works for our purposes even when the current selection is at an edge of the
1188 int initialSelectionLength = [[initialSelection string] length];
1190 [selectionForInitialSearch extendSelectionAtStart:1];
1191 [selectionForInitialSearch extendSelectionAtEnd:-initialSelectionLength];
1193 [selectionForInitialSearch extendSelectionAtEnd:1];
1194 [selectionForInitialSearch extendSelectionAtStart:-initialSelectionLength];
1197 PDFSelection *foundSelection = [document findString:string fromSelection:selectionForInitialSearch withOptions:options];
1198 [selectionForInitialSearch release];
1200 // If we first searched in the selection, and we found the selection, search again from just past the selection
1201 if (startInSelection && _PDFSelectionsAreEqual(foundSelection, initialSelection))
1202 foundSelection = [document findString:string fromSelection:initialSelection withOptions:options];
1204 if (!foundSelection && wrapFlag)
1205 foundSelection = [document findString:string fromSelection:nil withOptions:options];
1207 return foundSelection;
1210 - (void)_openWithFinder:(id)sender
1212 // We don't want to write the file until we have a document to write (see 4892525).
1213 if (![PDFSubview document]) {
1218 NSString *opath = [self _path];
1222 // Create a PDF file with the minimal permissions (only accessible to the current user, see 4145714)
1223 NSNumber *permissions = [[NSNumber alloc] initWithInt:S_IRUSR];
1224 NSDictionary *fileAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:permissions, NSFilePosixPermissions, nil];
1225 [permissions release];
1227 [[NSFileManager defaultManager] createFileAtPath:opath contents:[dataSource data] attributes:fileAttributes];
1229 [fileAttributes release];
1233 if (![[NSWorkspace sharedWorkspace] openFile:opath]) {
1234 // NSWorkspace couldn't open file. Do we need an alert
1235 // here? We ignore the error elsewhere.
1242 // Generate path once.
1246 NSString *filename = [[dataSource response] suggestedFilename];
1247 NSFileManager *manager = [NSFileManager defaultManager];
1248 NSString *temporaryPDFDirectoryPath = [self _temporaryPDFDirectoryPath];
1250 if (!temporaryPDFDirectoryPath) {
1251 // This should never happen; if it does we'll fail silently on non-debug builds.
1252 ASSERT_NOT_REACHED();
1256 path = [temporaryPDFDirectoryPath stringByAppendingPathComponent:filename];
1257 if ([manager fileExistsAtPath:path]) {
1258 NSString *pathTemplatePrefix = [temporaryPDFDirectoryPath stringByAppendingPathComponent:@"XXXXXX-"];
1259 NSString *pathTemplate = [pathTemplatePrefix stringByAppendingString:filename];
1260 // fileSystemRepresentation returns a const char *; copy it into a char * so we can modify it safely
1261 char *cPath = strdup([pathTemplate fileSystemRepresentation]);
1262 int fd = mkstemps(cPath, strlen(cPath) - strlen([pathTemplatePrefix fileSystemRepresentation]) + 1);
1264 // Couldn't create a temporary file! Should never happen; if it does we'll fail silently on non-debug builds.
1265 ASSERT_NOT_REACHED();
1269 path = [manager stringWithFileSystemRepresentation:cPath length:strlen(cPath)];
1279 - (void)_PDFDocumentViewMightHaveScrolled:(NSNotification *)notification
1281 NSClipView *clipView = [self _clipViewForPDFDocumentView];
1282 ASSERT([notification object] == clipView);
1284 NSPoint scrollPosition = [clipView bounds].origin;
1285 if (NSEqualPoints(scrollPosition, lastScrollPosition))
1288 lastScrollPosition = scrollPosition;
1289 WebView *webView = [self _webView];
1290 [[webView _UIDelegateForwarder] webView:webView didScrollDocumentInFrameView:[[dataSource webFrame] frameView]];
1293 - (PDFView *)_PDFSubview
1298 - (BOOL)_pointIsInSelection:(NSPoint)point
1300 PDFPage *page = [PDFSubview pageForPoint:point nearest:NO];
1304 NSRect selectionRect = [PDFSubview convertRect:[[PDFSubview currentSelection] boundsForPage:page] fromPage:page];
1306 return NSPointInRect(point, selectionRect);
1309 - (void)_scaleOrDisplayModeOrPageChanged:(NSNotification *)notification
1311 ASSERT([notification object] == PDFSubview);
1312 if (!_ignoreScaleAndDisplayModeAndPageNotifications) {
1313 [self _updatePreferencesSoon];
1314 // Notify UI delegate that the entire page has been redrawn, since (unlike for WebHTMLView)
1315 // we can't hook into the drawing mechanism itself. This fixes 5337529.
1316 WebView *webView = [self _webView];
1317 [[webView _UIDelegateForwarder] webView:webView didDrawRect:[webView bounds]];
1321 - (NSAttributedString *)_scaledAttributedString:(NSAttributedString *)unscaledAttributedString
1323 if (!unscaledAttributedString)
1326 float scaleFactor = [PDFSubview scaleFactor];
1327 if (scaleFactor == 1.0)
1328 return unscaledAttributedString;
1330 NSMutableAttributedString *result = [[unscaledAttributedString mutableCopy] autorelease];
1331 unsigned int length = [result length];
1332 NSRange effectiveRange = NSMakeRange(0,0);
1334 [result beginEditing];
1335 while (NSMaxRange(effectiveRange) < length) {
1336 NSFont *unscaledFont = [result attribute:NSFontAttributeName atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
1338 if (!unscaledFont) {
1339 // FIXME: We can't scale the font if we don't know what it is. We should always know what it is,
1340 // but sometimes don't due to PDFKit issue 5089411. When that's addressed, we can remove this
1342 LOG_ERROR("no font attribute found in range %@ for attributed string \"%@\" on page %@ (see radar 5089411)", NSStringFromRange(effectiveRange), result, [[dataSource request] URL]);
1346 NSFont *scaledFont = [NSFont fontWithName:[unscaledFont fontName] size:[unscaledFont pointSize]*scaleFactor];
1347 [result addAttribute:NSFontAttributeName value:scaledFont range:effectiveRange];
1349 [result endEditing];
1354 - (void)_setTextMatches:(NSArray *)array
1357 [textMatches release];
1358 textMatches = array;
1361 - (NSString *)_temporaryPDFDirectoryPath
1363 // Returns nil if the temporary PDF directory didn't exist and couldn't be created
1365 static NSString *_temporaryPDFDirectoryPath = nil;
1367 if (!_temporaryPDFDirectoryPath) {
1368 NSString *temporaryDirectoryTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"WebKitPDFs-XXXXXX"];
1369 char *cTemplate = strdup([temporaryDirectoryTemplate fileSystemRepresentation]);
1371 if (!mkdtemp(cTemplate)) {
1372 // This should never happen; if it does we'll fail silently on non-debug builds.
1373 ASSERT_NOT_REACHED();
1375 // cTemplate has now been modified to be the just-created directory name. This directory has 700 permissions,
1376 // so only the current user can add to it or view its contents.
1377 _temporaryPDFDirectoryPath = [[[NSFileManager defaultManager] stringWithFileSystemRepresentation:cTemplate length:strlen(cTemplate)] retain];
1383 return _temporaryPDFDirectoryPath;
1386 - (void)_trackFirstResponder
1388 ASSERT([self window]);
1390 id newFirstResponder = [[self window] firstResponder];
1391 if (newFirstResponder == trackedFirstResponder)
1394 // This next clause is the entire purpose of _trackFirstResponder. In other WebDocument
1395 // view classes this is done in a resignFirstResponder override, but in this case the
1396 // first responder view is a PDFKit class that we can't subclass.
1397 if (trackedFirstResponder == [PDFSubview documentView] && ![[dataSource _webView] maintainsInactiveSelection])
1401 [trackedFirstResponder release];
1402 trackedFirstResponder = [newFirstResponder retain];
1405 - (void)_updatePreferences:(WebPreferences *)prefs
1407 float scaleFactor = [PDFSubview autoScales] ? 0.0f : [PDFSubview scaleFactor];
1408 [prefs setPDFScaleFactor:scaleFactor];
1409 [prefs setPDFDisplayMode:[PDFSubview displayMode]];
1410 _willUpdatePreferencesSoon = NO;
1415 - (void)_updatePreferencesSoon
1417 // Consolidate calls; due to the PDFPrefUpdatingProxy method, this can be called multiple times with a single user action
1418 // such as showing the context menu.
1419 if (_willUpdatePreferencesSoon)
1422 WebPreferences *prefs = [[dataSource _webView] preferences];
1426 [self performSelector:@selector(_updatePreferences:) withObject:prefs afterDelay:0];
1427 _willUpdatePreferencesSoon = YES;
1430 - (NSSet *)_visiblePDFPages
1432 // Returns the set of pages that are at least partly visible, used to avoid processing non-visible pages
1433 PDFDocument *pdfDocument = [PDFSubview document];
1437 NSRect pdfViewBounds = [PDFSubview bounds];
1438 PDFPage *topLeftPage = [PDFSubview pageForPoint:NSMakePoint(NSMinX(pdfViewBounds), NSMaxY(pdfViewBounds)) nearest:YES];
1439 PDFPage *bottomRightPage = [PDFSubview pageForPoint:NSMakePoint(NSMaxX(pdfViewBounds), NSMinY(pdfViewBounds)) nearest:YES];
1441 // only page-free documents should return nil for either of these two since we passed YES for nearest:
1443 ASSERT(!bottomRightPage);
1447 NSUInteger firstVisiblePageIndex = [pdfDocument indexForPage:topLeftPage];
1448 NSUInteger lastVisiblePageIndex = [pdfDocument indexForPage:bottomRightPage];
1450 if (firstVisiblePageIndex > lastVisiblePageIndex) {
1451 NSUInteger swap = firstVisiblePageIndex;
1452 firstVisiblePageIndex = lastVisiblePageIndex;
1453 lastVisiblePageIndex = swap;
1456 NSMutableSet *result = [NSMutableSet set];
1457 NSUInteger pageIndex;
1458 for (pageIndex = firstVisiblePageIndex; pageIndex <= lastVisiblePageIndex; ++pageIndex)
1459 [result addObject:[pdfDocument pageAtIndex:pageIndex]];
1466 @implementation PDFPrefUpdatingProxy
1468 - (id)initWithView:(WebPDFView *)aView
1470 // No [super init], since we inherit from NSProxy
1475 - (void)forwardInvocation:(NSInvocation *)invocation
1477 [invocation invokeWithTarget:[view _PDFSubview]];
1478 [view _updatePreferencesSoon];
1481 - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
1483 return [[view _PDFSubview] methodSignatureForSelector:sel];