cbb8536eca457d9665700ab665d4c8ce93d12872
[WebKit-https.git] / WebKit / WebView / WebView.mm
1 /*
2  * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
3  * Copyright (C) 2006 David Smith (catfish.man@gmail.com)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #import "WebViewInternal.h"
31
32 #import "DOMRangeInternal.h"
33 #import "WebBackForwardList.h"
34 #import "WebBackForwardListInternal.h"
35 #import "WebBaseNetscapePluginView.h"
36 #import "WebChromeClient.h"
37 #import "WebContextMenuClient.h"
38 #import "WebDOMOperationsPrivate.h"
39 #import "WebDashboardRegion.h"
40 #import "WebDataSourceInternal.h"
41 #import "WebDefaultEditingDelegate.h"
42 #import "WebDefaultFrameLoadDelegate.h"
43 #import "WebDefaultPolicyDelegate.h"
44 #import "WebDefaultResourceLoadDelegate.h"
45 #import "WebDefaultScriptDebugDelegate.h"
46 #import "WebDefaultUIDelegate.h"
47 #import "WebDocument.h"
48 #import "WebDocumentInternal.h"
49 #import "WebDownload.h"
50 #import "WebDownloadInternal.h"
51 #import "WebDragClient.h"
52 #import "WebDynamicScrollBarsView.h"
53 #import "WebEditingDelegate.h"
54 #import "WebEditorClient.h"
55 #import "WebFormDelegatePrivate.h"
56 #import "WebFrameBridge.h"
57 #import "WebFrameInternal.h"
58 #import "WebFrameViewInternal.h"
59 #import "WebHTMLRepresentation.h"
60 #import "WebHTMLViewInternal.h"
61 #import "WebHistoryItemInternal.h"
62 #import "WebIconDatabase.h"
63 #import "WebInspector.h"
64 #import "WebKitErrors.h"
65 #import "WebKitLogging.h"
66 #import "WebKitNSStringExtras.h"
67 #import "WebKitStatisticsPrivate.h"
68 #import "WebKitVersionChecks.h"
69 #import "WebLocalizableStrings.h"
70 #import "WebNSDataExtras.h"
71 #import "WebNSDataExtrasPrivate.h"
72 #import "WebNSDictionaryExtras.h"
73 #import "WebNSEventExtras.h"
74 #import "WebNSObjectExtras.h"
75 #import "WebNSPasteboardExtras.h"
76 #import "WebNSPrintOperationExtras.h"
77 #import "WebNSURLExtras.h"
78 #import "WebNSURLRequestExtras.h"
79 #import "WebNSUserDefaultsExtras.h"
80 #import "WebNSViewExtras.h"
81 #import "WebPasteboardHelper.h"
82 #import "WebPDFView.h"
83 #import "WebPluginDatabase.h"
84 #import "WebPolicyDelegate.h"
85 #import "WebPreferenceKeysPrivate.h"
86 #import "WebPreferencesPrivate.h"
87 #import "WebResourceLoadDelegate.h"
88 #import "WebScriptDebugDelegatePrivate.h"
89 #import "WebScriptDebugServerPrivate.h"
90 #import "WebUIDelegate.h"
91 #import "WebUIDelegatePrivate.h"
92 #import <CoreFoundation/CFSet.h>
93 #import <Foundation/NSURLConnection.h>
94 #import <JavaScriptCore/Assertions.h>
95 #import <WebCore/Document.h>
96 #import <WebCore/DocumentLoader.h>
97 #import <WebCore/DragController.h>
98 #import <WebCore/DragData.h>
99 #import <WebCore/Editor.h>
100 #import <WebCore/ExceptionHandlers.h>
101 #import <WebCore/Frame.h>
102 #import <WebCore/FrameLoader.h>
103 #import <WebCore/FrameTree.h>
104 #import <WebCore/HTMLNames.h>
105 #import <WebCore/HistoryItem.h>
106 #import <WebCore/Logging.h>
107 #import <WebCore/Page.h>
108 #import <WebCore/PlatformMouseEvent.h>
109 #import <WebCore/ProgressTracker.h>
110 #import <WebCore/SelectionController.h>
111 #import <WebCore/Settings.h>
112 #import <WebCore/TextResourceDecoder.h>
113 #import <WebCore/WebCoreFrameBridge.h>
114 #import <WebCore/WebCoreObjCExtras.h>
115 #import <WebCore/WebCoreTextRenderer.h>
116 #import <WebCore/WebCoreView.h>
117 #import <WebKit/DOM.h>
118 #import <WebKit/DOMExtensions.h>
119 #import <WebKit/DOMPrivate.h>
120 #import <WebKitSystemInterface.h>
121 #import <objc/objc-runtime.h>
122 #import <wtf/RefPtr.h>
123 #import <wtf/HashTraits.h>
124
125 using namespace WebCore;
126
127 #if defined(__ppc__) || defined(__ppc64__)
128 #define PROCESSOR "PPC"
129 #elif defined(__i386__) || defined(__x86_64__)
130 #define PROCESSOR "Intel"
131 #else
132 #error Unknown architecture
133 #endif
134
135 #define FOR_EACH_RESPONDER_SELECTOR(macro) \
136 macro(alignCenter) \
137 macro(alignJustified) \
138 macro(alignLeft) \
139 macro(alignRight) \
140 macro(capitalizeWord) \
141 macro(centerSelectionInVisibleArea) \
142 macro(changeAttributes) \
143 macro(changeColor) \
144 macro(changeDocumentBackgroundColor) \
145 macro(changeFont) \
146 macro(checkSpelling) \
147 macro(complete) \
148 macro(copy) \
149 macro(copyFont) \
150 macro(cut) \
151 macro(delete) \
152 macro(deleteBackward) \
153 macro(deleteBackwardByDecomposingPreviousCharacter) \
154 macro(deleteForward) \
155 macro(deleteToBeginningOfLine) \
156 macro(deleteToBeginningOfParagraph) \
157 macro(deleteToEndOfLine) \
158 macro(deleteToEndOfParagraph) \
159 macro(deleteWordBackward) \
160 macro(deleteWordForward) \
161 macro(ignoreSpelling) \
162 macro(indent) \
163 macro(insertBacktab) \
164 macro(insertNewline) \
165 macro(insertNewlineIgnoringFieldEditor) \
166 macro(insertParagraphSeparator) \
167 macro(insertTab) \
168 macro(insertTabIgnoringFieldEditor) \
169 macro(lowercaseWord) \
170 macro(moveBackward) \
171 macro(moveBackwardAndModifySelection) \
172 macro(moveDown) \
173 macro(moveDownAndModifySelection) \
174 macro(moveForward) \
175 macro(moveForwardAndModifySelection) \
176 macro(moveLeft) \
177 macro(moveLeftAndModifySelection) \
178 macro(moveRight) \
179 macro(moveRightAndModifySelection) \
180 macro(moveToBeginningOfDocument) \
181 macro(moveToBeginningOfDocumentAndModifySelection) \
182 macro(moveToBeginningOfSentence) \
183 macro(moveToBeginningOfSentenceAndModifySelection) \
184 macro(moveToBeginningOfLine) \
185 macro(moveToBeginningOfLineAndModifySelection) \
186 macro(moveToBeginningOfParagraph) \
187 macro(moveToBeginningOfParagraphAndModifySelection) \
188 macro(moveToEndOfDocument) \
189 macro(moveToEndOfDocumentAndModifySelection) \
190 macro(moveToEndOfLine) \
191 macro(moveToEndOfLineAndModifySelection) \
192 macro(moveToEndOfParagraph) \
193 macro(moveToEndOfParagraphAndModifySelection) \
194 macro(moveToEndOfSentence) \
195 macro(moveToEndOfSentenceAndModifySelection) \
196 macro(moveUp) \
197 macro(moveUpAndModifySelection) \
198 macro(moveWordBackward) \
199 macro(moveWordBackwardAndModifySelection) \
200 macro(moveWordForward) \
201 macro(moveWordForwardAndModifySelection) \
202 macro(moveWordLeft) \
203 macro(moveWordLeftAndModifySelection) \
204 macro(moveWordRight) \
205 macro(moveWordRightAndModifySelection) \
206 macro(outdent) \
207 macro(pageDown) \
208 macro(pageUp) \
209 macro(paste) \
210 macro(pasteAsPlainText) \
211 macro(pasteAsRichText) \
212 macro(pasteFont) \
213 macro(performFindPanelAction) \
214 macro(scrollLineDown) \
215 macro(scrollLineUp) \
216 macro(scrollPageDown) \
217 macro(scrollPageUp) \
218 macro(scrollToBeginningOfDocument) \
219 macro(scrollToEndOfDocument) \
220 macro(selectAll) \
221 macro(selectWord) \
222 macro(selectSentence) \
223 macro(selectLine) \
224 macro(selectParagraph) \
225 macro(showGuessPanel) \
226 macro(startSpeaking) \
227 macro(stopSpeaking) \
228 macro(subscript) \
229 macro(superscript) \
230 macro(underline) \
231 macro(unscript) \
232 macro(uppercaseWord) \
233 macro(yank) \
234 macro(yankAndSelect) \
235
236 #define WebKitOriginalTopPrintingMarginKey @"WebKitOriginalTopMargin"
237 #define WebKitOriginalBottomPrintingMarginKey @"WebKitOriginalBottomMargin"
238
239 static BOOL applicationIsTerminating;
240 static int pluginDatabaseClientCount = 0;
241
242 @interface NSSpellChecker (AppKitSecretsIKnow)
243 - (void)_preflightChosenSpellServer;
244 @end
245
246 @interface NSView (AppKitSecretsIKnow)
247 - (NSView *)_hitTest:(NSPoint *)aPoint dragTypes:(NSSet *)types;
248 - (void)_autoscrollForDraggingInfo:(id)dragInfo timeDelta:(NSTimeInterval)repeatDelta;
249 - (BOOL)_shouldAutoscrollForDraggingInfo:(id)dragInfo;
250 @end
251
252 @interface WebViewPrivate : NSObject
253 {
254 @public
255     Page* page;
256     
257     id UIDelegate;
258     id UIDelegateForwarder;
259     id resourceProgressDelegate;
260     id resourceProgressDelegateForwarder;
261     id downloadDelegate;
262     id policyDelegate;
263     id policyDelegateForwarder;
264     id frameLoadDelegate;
265     id frameLoadDelegateForwarder;
266     id <WebFormDelegate> formDelegate;
267     id editingDelegate;
268     id editingDelegateForwarder;
269     id scriptDebugDelegate;
270     id scriptDebugDelegateForwarder;
271     
272     BOOL useBackForwardList;
273     BOOL allowsUndo;
274         
275     float textSizeMultiplier;
276
277     NSString *applicationNameForUserAgent;
278     String* userAgent;
279     BOOL userAgentOverridden;
280     
281     WebPreferences *preferences;
282     BOOL useSiteSpecificSpoofing;
283         
284     BOOL lastElementWasNonNil;
285
286     NSWindow *hostWindow;
287
288     int programmaticFocusCount;
289     
290     WebResourceDelegateImplementationCache resourceLoadDelegateImplementations;
291
292     void *observationInfo;
293     
294     BOOL closed;
295     BOOL shouldCloseWithWindow;
296     BOOL mainFrameDocumentReady;
297     BOOL drawsBackground;
298     BOOL editable;
299     BOOL tabKeyCyclesThroughElementsChanged;
300     BOOL becomingFirstResponder;
301     BOOL hoverFeedbackSuspended;
302
303     NSColor *backgroundColor;
304
305     NSString *mediaStyle;
306     
307     BOOL hasSpellCheckerDocumentTag;
308     WebNSInteger spellCheckerDocumentTag;
309
310     BOOL smartInsertDeleteEnabled;
311         
312     BOOL dashboardBehaviorAlwaysSendMouseEventsToAllWindows;
313     BOOL dashboardBehaviorAlwaysSendActiveNullEventsToPlugIns;
314     BOOL dashboardBehaviorAlwaysAcceptsFirstMouse;
315     BOOL dashboardBehaviorAllowWheelScrolling;
316     
317     BOOL selectWordBeforeMenuEvent;
318     
319     // WebKit has both a global plug-in database and a separate, per WebView plug-in database. Dashboard uses the per WebView database.
320     WebPluginDatabase *pluginDatabase;
321     
322     HashMap<unsigned long, RetainPtr<id> >* identifierMap;
323 }
324 @end
325
326 @interface WebView (WebFileInternal)
327 - (WebFrame *)_selectedOrMainFrame;
328 - (WebFrameBridge *)_bridgeForSelectedOrMainFrame;
329 - (BOOL)_isLoading;
330 - (WebFrameView *)_frameViewAtWindowPoint:(NSPoint)point;
331 - (WebFrameBridge *)_bridgeAtPoint:(NSPoint)point;
332 - (WebFrame *)_focusedFrame;
333 + (void)_preflightSpellChecker;
334 - (BOOL)_continuousCheckingAllowed;
335 - (NSResponder *)_responderForResponderOperations;
336 - (BOOL)_performTextSizingSelector:(SEL)sel withObject:(id)arg onTrackingDocs:(BOOL)doTrackingViews selForNonTrackingDocs:(SEL)testSel newScaleFactor:(float)newScaleFactor;
337 - (void)_notifyTextSizeMultiplierChanged;
338 @end
339
340 NSString *WebElementDOMNodeKey =            @"WebElementDOMNode";
341 NSString *WebElementFrameKey =              @"WebElementFrame";
342 NSString *WebElementImageKey =              @"WebElementImage";
343 NSString *WebElementImageAltStringKey =     @"WebElementImageAltString";
344 NSString *WebElementImageRectKey =          @"WebElementImageRect";
345 NSString *WebElementImageURLKey =           @"WebElementImageURL";
346 NSString *WebElementIsSelectedKey =         @"WebElementIsSelected";
347 NSString *WebElementLinkLabelKey =          @"WebElementLinkLabel";
348 NSString *WebElementLinkTargetFrameKey =    @"WebElementTargetFrame";
349 NSString *WebElementLinkTitleKey =          @"WebElementLinkTitle";
350 NSString *WebElementLinkURLKey =            @"WebElementLinkURL";
351 NSString *WebElementSpellingToolTipKey =    @"WebElementSpellingToolTip";
352 NSString *WebElementTitleKey =              @"WebElementTitle";
353 NSString *WebElementLinkIsLiveKey =         @"WebElementLinkIsLive";
354 NSString *WebElementIsContentEditableKey =  @"WebElementIsContentEditableKey";
355
356 NSString *WebViewProgressStartedNotification =          @"WebProgressStartedNotification";
357 NSString *WebViewProgressEstimateChangedNotification =  @"WebProgressEstimateChangedNotification";
358 NSString *WebViewProgressFinishedNotification =         @"WebProgressFinishedNotification";
359
360 NSString * const WebViewDidBeginEditingNotification =         @"WebViewDidBeginEditingNotification";
361 NSString * const WebViewDidChangeNotification =               @"WebViewDidChangeNotification";
362 NSString * const WebViewDidEndEditingNotification =           @"WebViewDidEndEditingNotification";
363 NSString * const WebViewDidChangeTypingStyleNotification =    @"WebViewDidChangeTypingStyleNotification";
364 NSString * const WebViewDidChangeSelectionNotification =      @"WebViewDidChangeSelectionNotification";
365
366 enum { WebViewVersion = 3 };
367
368 #define timedLayoutSize 4096
369
370 static NSMutableSet *schemesWithRepresentationsSet;
371
372 NSString *_WebCanGoBackKey =            @"canGoBack";
373 NSString *_WebCanGoForwardKey =         @"canGoForward";
374 NSString *_WebEstimatedProgressKey =    @"estimatedProgress";
375 NSString *_WebIsLoadingKey =            @"isLoading";
376 NSString *_WebMainFrameIconKey =        @"mainFrameIcon";
377 NSString *_WebMainFrameTitleKey =       @"mainFrameTitle";
378 NSString *_WebMainFrameURLKey =         @"mainFrameURL";
379 NSString *_WebMainFrameDocumentKey =    @"mainFrameDocument";
380
381 @interface WebProgressItem : NSObject
382 {
383 @public
384     long long bytesReceived;
385     long long estimatedLength;
386 }
387 @end
388
389 @implementation WebProgressItem
390 @end
391
392 static BOOL continuousSpellCheckingEnabled;
393 #ifndef BUILDING_ON_TIGER
394 static BOOL grammarCheckingEnabled;
395 #endif
396
397 @implementation WebViewPrivate
398
399 #ifndef BUILDING_ON_TIGER
400 + (void)initialize
401 {
402     WebCoreObjCFinalizeOnMainThread(self);
403 }
404 #endif
405
406 - init 
407 {
408     self = [super init];
409     if (!self)
410         return nil;
411     allowsUndo = YES;
412     textSizeMultiplier = 1;
413     dashboardBehaviorAllowWheelScrolling = YES;
414     shouldCloseWithWindow = objc_collecting_enabled();
415     continuousSpellCheckingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:WebContinuousSpellCheckingEnabled];
416
417 #ifndef BUILDING_ON_TIGER
418     grammarCheckingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:WebGrammarCheckingEnabled];
419 #endif
420     userAgent = new String;
421     
422     identifierMap = new HashMap<unsigned long, RetainPtr<id> >();
423     pluginDatabaseClientCount++;
424
425     return self;
426 }
427
428 - (void)dealloc
429 {
430     ASSERT(!page);
431
432     delete userAgent;
433     delete identifierMap;
434     
435     [applicationNameForUserAgent release];
436     [backgroundColor release];
437     
438     [preferences release];
439     [hostWindow release];
440     
441     [policyDelegateForwarder release];
442     [resourceProgressDelegateForwarder release];
443     [UIDelegateForwarder release];
444     [frameLoadDelegateForwarder release];
445     [editingDelegateForwarder release];
446     [scriptDebugDelegateForwarder release];
447     
448     [mediaStyle release];
449     
450     [super dealloc];
451 }
452
453 - (void)finalize
454 {
455     ASSERT_MAIN_THREAD();
456     delete userAgent;
457     [super finalize];
458 }
459
460 @end
461
462 @implementation WebView (AllWebViews)
463
464 static CFSetCallBacks NonRetainingSetCallbacks = {
465     0,
466     NULL,
467     NULL,
468     CFCopyDescription,
469     CFEqual,
470     CFHash
471 };
472
473 static CFMutableSetRef allWebViewsSet;
474
475 + (void)_makeAllWebViewsPerformSelector:(SEL)selector
476 {
477     if (!allWebViewsSet)
478         return;
479
480     [(NSMutableSet *)allWebViewsSet makeObjectsPerformSelector:selector];
481 }
482
483 - (void)_removeFromAllWebViewsSet
484 {
485     if (allWebViewsSet)
486         CFSetRemoveValue(allWebViewsSet, self);
487 }
488
489 - (void)_addToAllWebViewsSet
490 {
491     if (!allWebViewsSet)
492         allWebViewsSet = CFSetCreateMutable(NULL, 0, &NonRetainingSetCallbacks);
493
494     CFSetSetValue(allWebViewsSet, self);
495 }
496
497 @end
498
499 @implementation WebView (WebPrivate)
500
501 #ifdef DEBUG_WIDGET_DRAWING
502 static bool debugWidget = true;
503 - (void)drawRect:(NSRect)rect
504 {
505     [[NSColor blueColor] set];
506     NSRectFill (rect);
507     
508     NSRect htmlViewRect = [[[[self mainFrame] frameView] documentView] frame];
509
510     if (debugWidget) {
511         while (debugWidget) {
512             sleep (1);
513         }
514     }
515
516     NSLog (@"%s:   rect:  (%0.f,%0.f) %0.f %0.f, htmlViewRect:  (%0.f,%0.f) %0.f %0.f\n", 
517         __PRETTY_FUNCTION__, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height,
518         htmlViewRect.origin.x, htmlViewRect.origin.y, htmlViewRect.size.width, htmlViewRect.size.height
519     );
520
521     [super drawRect:rect];
522 }
523 #endif
524
525 + (BOOL)_developerExtrasEnabled
526 {
527 #ifdef NDEBUG
528     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
529     BOOL enableDebugger = [defaults boolForKey:@"WebKitDeveloperExtras"];
530     if (!enableDebugger)
531         enableDebugger = [defaults boolForKey:@"IncludeDebugMenu"];
532     return enableDebugger;
533 #else
534     return YES; // always enable in debug builds
535 #endif
536 }
537
538 + (BOOL)_scriptDebuggerEnabled
539 {
540 #ifdef NDEBUG
541     return [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitScriptDebuggerEnabled"];
542 #else
543     return YES; // always enable in debug builds
544 #endif
545 }
546
547 + (NSArray *)_supportedMIMETypes
548 {
549     // Load the plug-in DB allowing plug-ins to install types.
550     [WebPluginDatabase sharedDatabase];
551     return [[WebFrameView _viewTypesAllowImageTypeOmission:NO] allKeys];
552 }
553
554 + (NSArray *)_supportedFileExtensions
555 {
556     NSMutableSet *extensions = [[NSMutableSet alloc] init];
557     NSArray *MIMETypes = [self _supportedMIMETypes];
558     NSEnumerator *enumerator = [MIMETypes objectEnumerator];
559     NSString *MIMEType;
560     while ((MIMEType = [enumerator nextObject]) != nil) {
561         NSArray *extensionsForType = WKGetExtensionsForMIMEType(MIMEType);
562         if (extensionsForType) {
563             [extensions addObjectsFromArray:extensionsForType];
564         }
565     }
566     NSArray *uniqueExtensions = [extensions allObjects];
567     [extensions release];
568     return uniqueExtensions;
569 }
570
571 + (BOOL)_viewClass:(Class *)vClass andRepresentationClass:(Class *)rClass forMIMEType:(NSString *)MIMEType;
572 {
573     MIMEType = [MIMEType lowercaseString];
574     Class viewClass = [[WebFrameView _viewTypesAllowImageTypeOmission:YES] _webkit_objectForMIMEType:MIMEType];
575     Class repClass = [[WebDataSource _repTypesAllowImageTypeOmission:YES] _webkit_objectForMIMEType:MIMEType];
576     
577     if (!viewClass || !repClass || [[WebPDFView supportedMIMETypes] containsObject:MIMEType]) {
578         // Our optimization to avoid loading the plug-in DB and image types for the HTML case failed.
579         // Load the plug-in DB allowing plug-ins to install types.
580         [WebPluginDatabase sharedDatabase];
581             
582         // Load the image types and get the view class and rep class. This should be the fullest picture of all handled types.
583         viewClass = [[WebFrameView _viewTypesAllowImageTypeOmission:NO] _webkit_objectForMIMEType:MIMEType];
584         repClass = [[WebDataSource _repTypesAllowImageTypeOmission:NO] _webkit_objectForMIMEType:MIMEType];
585     }
586     
587     if (viewClass && repClass) {
588         // Special-case WebHTMLView for text types that shouldn't be shown.
589         if (viewClass == [WebHTMLView class] &&
590             repClass == [WebHTMLRepresentation class] &&
591             [[WebHTMLView unsupportedTextMIMETypes] containsObject:MIMEType]) {
592             return NO;
593         }
594         if (vClass)
595             *vClass = viewClass;
596         if (rClass)
597             *rClass = repClass;
598         return YES;
599     }
600     
601     return NO;
602 }
603
604 - (BOOL)_viewClass:(Class *)vClass andRepresentationClass:(Class *)rClass forMIMEType:(NSString *)MIMEType;
605 {
606     if ([[self class] _viewClass:vClass andRepresentationClass:rClass forMIMEType:MIMEType])
607         return YES;
608
609     if (_private->pluginDatabase) {
610         WebBasePluginPackage *pluginPackage = [_private->pluginDatabase pluginForMIMEType:MIMEType];
611         if (pluginPackage) {
612             if (vClass)
613                 *vClass = [WebHTMLView class];
614             if (rClass)
615                 *rClass = [WebHTMLRepresentation class];
616             return YES;
617         }
618     }
619     
620     return NO;
621 }
622
623 + (void)_setAlwaysUseATSU:(BOOL)f
624 {
625     WebCoreSetAlwaysUseATSU(f);
626 }
627
628 + (BOOL)canShowFile:(NSString *)path
629 {
630     return [[self class] canShowMIMEType:[WebView _MIMETypeForFile:path]];
631 }
632
633 + (NSString *)suggestedFileExtensionForMIMEType:(NSString *)type
634 {
635     return WKGetPreferredExtensionForMIMEType(type);
636 }
637
638 - (void)_close
639 {
640     if (!_private || _private->closed)
641         return;
642     _private->closed = YES;
643
644     [self _removeFromAllWebViewsSet];
645
646     [self setGroupName:nil];
647     [self setHostWindow:nil];
648     [self setDownloadDelegate:nil];
649     [self setEditingDelegate:nil];
650     [self setFrameLoadDelegate:nil];
651     [self setPolicyDelegate:nil];
652     [self setResourceLoadDelegate:nil];
653     [self setScriptDebugDelegate:nil];
654     [self setUIDelegate:nil];
655
656     // To avoid leaks, call removeDragCaret in case it wasn't called after moveDragCaretToPoint.
657     [self removeDragCaret];
658
659     FrameLoader* mainFrameLoader = [[self mainFrame] _frameLoader];
660     if (mainFrameLoader)
661         mainFrameLoader->detachFromParent();
662     
663     // Deleteing the WebCore::Page will clear the page cache so we call destroy on 
664     // all the plug-ins in the page cache to break any retain cycles.
665     // See comment in HistoryItem::releaseAllPendingPageCaches() for more information.
666     delete _private->page;
667     _private->page = 0;
668
669     if (_private->hasSpellCheckerDocumentTag) {
670         [[NSSpellChecker sharedSpellChecker] closeSpellDocumentWithTag:_private->spellCheckerDocumentTag];
671         _private->hasSpellCheckerDocumentTag = NO;
672     }
673     
674     [[NSNotificationCenter defaultCenter] removeObserver:self];
675     [WebPreferences _removeReferenceForIdentifier: [self preferencesIdentifier]];
676     pluginDatabaseClientCount--;
677     
678     // Make sure to close both sets of plug-ins databases because plug-ins need an opportunity to clean up files, etc.
679     
680     // Unload the WebView local plug-in database. 
681     if (_private->pluginDatabase) {
682         [_private->pluginDatabase close];
683         [_private->pluginDatabase release];
684         _private->pluginDatabase = nil;
685     }
686     
687     // Keep the global plug-in database active until the app terminates to avoid having to reload plug-in bundles.
688     if (!pluginDatabaseClientCount && applicationIsTerminating)
689         [[WebPluginDatabase sharedDatabase] close];
690 }
691
692 + (NSString *)_MIMETypeForFile:(NSString *)path
693 {
694     NSString *extension = [path pathExtension];
695     NSString *MIMEType = nil;
696
697     // Get the MIME type from the extension.
698     if ([extension length] != 0) {
699         MIMEType = WKGetMIMETypeForExtension(extension);
700     }
701
702     // If we can't get a known MIME type from the extension, sniff.
703     if ([MIMEType length] == 0 || [MIMEType isEqualToString:@"application/octet-stream"]) {
704         NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
705         NSData *data = [handle readDataOfLength:WEB_GUESS_MIME_TYPE_PEEK_LENGTH];
706         [handle closeFile];
707         if ([data length] != 0) {
708             MIMEType = [data _webkit_guessedMIMEType];
709         }
710         if ([MIMEType length] == 0) {
711             MIMEType = @"application/octet-stream";
712         }
713     }
714
715     return MIMEType;
716 }
717
718 - (void)_downloadURL:(NSURL *)URL
719 {
720     ASSERT(URL);
721     
722     NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL];
723     [WebDownload _downloadWithRequest:request
724                              delegate:_private->downloadDelegate
725                             directory:nil];
726     [request release];
727 }
728
729 - (WebView *)_openNewWindowWithRequest:(NSURLRequest *)request
730 {
731     id wd = [self UIDelegate];
732     WebView *newWindowWebView = nil;
733     if ([wd respondsToSelector:@selector(webView:createWebViewWithRequest:)])
734         newWindowWebView = [wd webView:self createWebViewWithRequest:request];
735     else {
736         newWindowWebView = [[WebDefaultUIDelegate sharedUIDelegate] webView:self createWebViewWithRequest: request];
737     }
738
739     [[newWindowWebView _UIDelegateForwarder] webViewShow: newWindowWebView];
740
741     return newWindowWebView;
742 }
743
744 - (WebCore::Page*)page
745 {
746     return _private->page;
747 }
748
749 - (NSMenu *)_menuForElement:(NSDictionary *)element defaultItems:(NSArray *)items
750 {
751     NSArray *defaultMenuItems = [[WebDefaultUIDelegate sharedUIDelegate]
752           webView:self contextMenuItemsForElement:element defaultMenuItems:items];
753     NSArray *menuItems = defaultMenuItems;
754     NSMenu *menu = nil;
755     unsigned i;
756
757     if ([_private->UIDelegate respondsToSelector:@selector(webView:contextMenuItemsForElement:defaultMenuItems:)])
758         menuItems = [_private->UIDelegate webView:self contextMenuItemsForElement:element defaultMenuItems:defaultMenuItems];
759
760     if (menuItems && [menuItems count] > 0) {
761         menu = [[[NSMenu alloc] init] autorelease];
762
763         for (i=0; i<[menuItems count]; i++) {
764             [menu addItem:[menuItems objectAtIndex:i]];
765         }
766     }
767
768     return menu;
769 }
770
771 - (void)_mouseDidMoveOverElement:(NSDictionary *)dictionary modifierFlags:(WebNSUInteger)modifierFlags
772 {
773     // When the mouse isn't over this view at all, we'll get called with a dictionary of nil over
774     // and over again. So it's a good idea to catch that here and not send multiple calls to the delegate
775     // for that case.
776     
777     if (dictionary && _private->lastElementWasNonNil) {
778         [[self _UIDelegateForwarder] webView:self mouseDidMoveOverElement:dictionary modifierFlags:modifierFlags];
779     }
780     _private->lastElementWasNonNil = dictionary != nil;
781 }
782
783
784
785 - (void)_loadBackForwardListFromOtherView:(WebView *)otherView
786 {
787     if (!_private->page)
788         return;
789     
790     // It turns out the right combination of behavior is done with the back/forward load
791     // type.  (See behavior matrix at the top of WebFramePrivate.)  So we copy all the items
792     // in the back forward list, and go to the current one.
793
794     BackForwardList* backForwardList = _private->page->backForwardList();
795     ASSERT(!backForwardList->currentItem()); // destination list should be empty
796
797     BackForwardList* otherBackForwardList = otherView->_private->page->backForwardList();
798     if (!otherBackForwardList->currentItem())
799         return; // empty back forward list, bail
800     
801     HistoryItem* newItemToGoTo = 0;
802
803     int lastItemIndex = otherBackForwardList->forwardListCount();
804     for (int i = -otherBackForwardList->backListCount(); i <= lastItemIndex; ++i) {
805         if (i == 0) {
806             // If this item is showing , save away its current scroll and form state,
807             // since that might have changed since loading and it is normally not saved
808             // until we leave that page.
809             otherView->_private->page->mainFrame()->loader()->saveDocumentAndScrollState();
810         }
811         RefPtr<HistoryItem> newItem = otherBackForwardList->itemAtIndex(i)->copy();
812         if (i == 0) 
813             newItemToGoTo = newItem.get();
814         backForwardList->addItem(newItem.release());
815     }
816     
817     ASSERT(newItemToGoTo);
818     _private->page->goToItem(newItemToGoTo, FrameLoadTypeIndexedBackForward);
819 }
820
821 - (void)_setFormDelegate: (id<WebFormDelegate>)delegate
822 {
823     _private->formDelegate = delegate;
824 }
825
826 - (id<WebFormDelegate>)_formDelegate
827 {
828     return _private->formDelegate;
829 }
830
831 - (void)_updateWebCoreSettingsFromPreferences:(WebPreferences *)preferences
832 {
833     if (!_private->page)
834         return;
835     
836     Settings* settings = _private->page->settings();
837     
838     settings->setCursiveFontFamily([preferences cursiveFontFamily]);
839     settings->setDefaultFixedFontSize([preferences defaultFixedFontSize]);
840     settings->setDefaultFontSize([preferences defaultFontSize]);
841     settings->setDefaultTextEncodingName([preferences defaultTextEncodingName]);
842     settings->setFantasyFontFamily([preferences fantasyFontFamily]);
843     settings->setFixedFontFamily([preferences fixedFontFamily]);
844     settings->setJavaEnabled([preferences isJavaEnabled]);
845     settings->setJavaScriptEnabled([preferences isJavaScriptEnabled]);
846     settings->setJavaScriptCanOpenWindowsAutomatically([preferences javaScriptCanOpenWindowsAutomatically]);
847     settings->setMinimumFontSize([preferences minimumFontSize]);
848     settings->setMinimumLogicalFontSize([preferences minimumLogicalFontSize]);
849     settings->setPluginsEnabled([preferences arePlugInsEnabled]);
850     settings->setPrivateBrowsingEnabled([preferences privateBrowsingEnabled]);
851     settings->setSansSerifFontFamily([preferences sansSerifFontFamily]);
852     settings->setSerifFontFamily([preferences serifFontFamily]);
853     settings->setStandardFontFamily([preferences standardFontFamily]);
854     settings->setLoadsImagesAutomatically([preferences loadsImagesAutomatically]);
855     settings->setShouldPrintBackgrounds([preferences shouldPrintBackgrounds]);
856     settings->setTextAreasAreResizable([preferences textAreasAreResizable]);
857     settings->setEditableLinkBehavior(core([preferences editableLinkBehavior]));
858     if ([preferences userStyleSheetEnabled]) {
859         NSString* location = [[preferences userStyleSheetLocation] _web_originalDataAsString];
860         settings->setUserStyleSheetLocation([NSURL URLWithString:(location ? location : @"")]);
861     } else
862         settings->setUserStyleSheetLocation([NSURL URLWithString:@""]);
863     settings->setNeedsAcrobatFrameReloadingQuirk(!WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITHOUT_ACROBAT_QUIRK)
864         && [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.adobe.Acrobat.Pro"]);
865 }
866
867 - (void)_preferencesChangedNotification: (NSNotification *)notification
868 {
869     WebPreferences *preferences = (WebPreferences *)[notification object];
870     
871     ASSERT(preferences == [self preferences]);
872     if (!_private->userAgentOverridden)
873         *_private->userAgent = String();
874     // Cache this value so we don't have to read NSUserDefaults on each page load
875     _private->useSiteSpecificSpoofing = [preferences _useSiteSpecificSpoofing];
876     [self _updateWebCoreSettingsFromPreferences: preferences];
877 }
878
879 - _frameLoadDelegateForwarder
880 {
881     if (!_private->frameLoadDelegateForwarder)
882         _private->frameLoadDelegateForwarder = [[_WebSafeForwarder alloc] initWithTarget: [self frameLoadDelegate]  defaultTarget: [WebDefaultFrameLoadDelegate sharedFrameLoadDelegate] templateClass: [WebDefaultFrameLoadDelegate class]];
883     return _private->frameLoadDelegateForwarder;
884 }
885
886 - _resourceLoadDelegateForwarder
887 {
888     if (!_private->resourceProgressDelegateForwarder)
889         _private->resourceProgressDelegateForwarder = [[_WebSafeForwarder alloc] initWithTarget: [self resourceLoadDelegate] defaultTarget: [WebDefaultResourceLoadDelegate sharedResourceLoadDelegate] templateClass: [WebDefaultResourceLoadDelegate class]];
890     return _private->resourceProgressDelegateForwarder;
891 }
892
893 - (void)_cacheResourceLoadDelegateImplementations
894 {
895     WebResourceDelegateImplementationCache *cache = &_private->resourceLoadDelegateImplementations;
896     id delegate = [self resourceLoadDelegate];
897     Class delegateClass = [delegate class];
898
899     cache->delegateImplementsDidCancelAuthenticationChallenge = [delegate respondsToSelector:@selector(webView:resource:didCancelAuthenticationChallenge:fromDataSource:)];
900     cache->delegateImplementsDidReceiveAuthenticationChallenge = [delegate respondsToSelector:@selector(webView:resource:didReceiveAuthenticationChallenge:fromDataSource:)];
901     cache->delegateImplementsDidFinishLoadingFromDataSource = [delegate respondsToSelector:@selector(webView:resource:didFinishLoadingFromDataSource:)];
902     cache->delegateImplementsDidReceiveContentLength = [delegate respondsToSelector:@selector(webView:resource:didReceiveContentLength:fromDataSource:)];
903     cache->delegateImplementsDidReceiveResponse = [delegate respondsToSelector:@selector(webView:resource:didReceiveResponse:fromDataSource:)];
904     cache->delegateImplementsWillSendRequest = [delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)];
905     cache->delegateImplementsIdentifierForRequest = [delegate respondsToSelector:@selector(webView:identifierForInitialRequest:fromDataSource:)];
906     cache->delegateImplementsDidLoadResourceFromMemoryCache = [delegate respondsToSelector:@selector(webView:didLoadResourceFromMemoryCache:response:length:fromDataSource:)];
907
908 #if defined(OBJC_API_VERSION) && OBJC_API_VERSION > 0
909 #define GET_OBJC_METHOD_IMP(class,selector) class_getMethodImplementation(class, selector)
910 #else
911 #define GET_OBJC_METHOD_IMP(class,selector) class_getInstanceMethod(class, selector)->method_imp
912 #endif
913
914     if (cache->delegateImplementsDidCancelAuthenticationChallenge)
915         cache->didCancelAuthenticationChallengeFunc = (WebDidCancelAuthenticationChallengeFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:resource:didReceiveAuthenticationChallenge:fromDataSource:));
916     if (cache->delegateImplementsDidReceiveAuthenticationChallenge)
917         cache->didReceiveAuthenticationChallengeFunc = (WebDidReceiveAuthenticationChallengeFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:resource:didReceiveAuthenticationChallenge:fromDataSource:));
918     if (cache->delegateImplementsDidFinishLoadingFromDataSource)
919         cache->didFinishLoadingFromDataSourceFunc = (WebDidFinishLoadingFromDataSourceFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:resource:didFinishLoadingFromDataSource:));
920     if (cache->delegateImplementsDidReceiveContentLength)
921         cache->didReceiveContentLengthFunc = (WebDidReceiveContentLengthFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:resource:didReceiveContentLength:fromDataSource:));
922     if (cache->delegateImplementsDidReceiveResponse)
923         cache->didReceiveResponseFunc = (WebDidReceiveResponseFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:resource:didReceiveResponse:fromDataSource:));
924     if (cache->delegateImplementsWillSendRequest)
925         cache->willSendRequestFunc = (WebWillSendRequestFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:));
926     if (cache->delegateImplementsIdentifierForRequest)
927         cache->identifierForRequestFunc = (WebIdentifierForRequestFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:identifierForInitialRequest:fromDataSource:));
928     if (cache->delegateImplementsDidLoadResourceFromMemoryCache)
929         cache->didLoadResourceFromMemoryCacheFunc = (WebDidLoadResourceFromMemoryCacheFunc)GET_OBJC_METHOD_IMP(delegateClass, @selector(webView:didLoadResourceFromMemoryCache:response:length:fromDataSource:));
930
931 #undef GET_OBJC_METHOD_IMP
932 }
933
934 id WebViewGetResourceLoadDelegate(WebView *webView)
935 {
936     return webView->_private->resourceProgressDelegate;
937 }
938
939 WebResourceDelegateImplementationCache WebViewGetResourceLoadDelegateImplementations(WebView *webView)
940 {
941     return webView->_private->resourceLoadDelegateImplementations;
942 }
943
944 - _policyDelegateForwarder
945 {
946     if (!_private->policyDelegateForwarder)
947         _private->policyDelegateForwarder = [[_WebSafeForwarder alloc] initWithTarget: [self policyDelegate] defaultTarget: [WebDefaultPolicyDelegate sharedPolicyDelegate] templateClass: [WebDefaultPolicyDelegate class]];
948     return _private->policyDelegateForwarder;
949 }
950
951 - _UIDelegateForwarder
952 {
953     if (!_private->UIDelegateForwarder)
954         _private->UIDelegateForwarder = [[_WebSafeForwarder alloc] initWithTarget: [self UIDelegate] defaultTarget: [WebDefaultUIDelegate sharedUIDelegate] templateClass: [WebDefaultUIDelegate class]];
955     return _private->UIDelegateForwarder;
956 }
957
958 - _editingDelegateForwarder
959 {
960     // This can be called during window deallocation by QTMovieView in the QuickTime Cocoa Plug-in.
961     // Not sure if that is a bug or not.
962     if (!_private)
963         return nil;
964     if (!_private->editingDelegateForwarder)
965         _private->editingDelegateForwarder = [[_WebSafeForwarder alloc] initWithTarget: [self editingDelegate] defaultTarget: [WebDefaultEditingDelegate sharedEditingDelegate] templateClass: [WebDefaultEditingDelegate class]];
966     return _private->editingDelegateForwarder;
967 }
968
969 - _scriptDebugDelegateForwarder
970 {
971     if (!_private->scriptDebugDelegateForwarder)
972         _private->scriptDebugDelegateForwarder = [[_WebSafeForwarder alloc] initWithTarget: [self scriptDebugDelegate] defaultTarget: [WebDefaultScriptDebugDelegate sharedScriptDebugDelegate] templateClass: [WebDefaultScriptDebugDelegate class]];
973     return _private->scriptDebugDelegateForwarder;
974 }
975
976 - (void)_closeWindow
977 {
978     [[self _UIDelegateForwarder] webViewClose:self];
979 }
980
981 + (void)_unregisterViewClassAndRepresentationClassForMIMEType:(NSString *)MIMEType;
982 {
983     [[WebFrameView _viewTypesAllowImageTypeOmission:NO] removeObjectForKey:MIMEType];
984     [[WebDataSource _repTypesAllowImageTypeOmission:NO] removeObjectForKey:MIMEType];
985 }
986
987 + (void)_registerViewClass:(Class)viewClass representationClass:(Class)representationClass forURLScheme:(NSString *)URLScheme;
988 {
989     NSString *MIMEType = [self _generatedMIMETypeForURLScheme:URLScheme];
990     [self registerViewClass:viewClass representationClass:representationClass forMIMEType:MIMEType];
991
992     // This is used to make _representationExistsForURLScheme faster.
993     // Without this set, we'd have to create the MIME type each time.
994     if (schemesWithRepresentationsSet == nil) {
995         schemesWithRepresentationsSet = [[NSMutableSet alloc] init];
996     }
997     [schemesWithRepresentationsSet addObject:[[[URLScheme lowercaseString] copy] autorelease]];
998 }
999
1000 + (NSString *)_generatedMIMETypeForURLScheme:(NSString *)URLScheme
1001 {
1002     return [@"x-apple-web-kit/" stringByAppendingString:[URLScheme lowercaseString]];
1003 }
1004
1005 + (BOOL)_representationExistsForURLScheme:(NSString *)URLScheme
1006 {
1007     return [schemesWithRepresentationsSet containsObject:[URLScheme lowercaseString]];
1008 }
1009
1010 + (BOOL)_canHandleRequest:(NSURLRequest *)request
1011 {
1012     if ([NSURLConnection canHandleRequest:request]) {
1013         return YES;
1014     }
1015
1016     return [self _representationExistsForURLScheme:[[request URL] scheme]];
1017 }
1018
1019 + (NSString *)_decodeData:(NSData *)data
1020 {
1021     HTMLNames::init(); // this method is used for importing bookmarks at startup, so HTMLNames are likely to be uninitialized yet
1022     RefPtr<TextResourceDecoder> decoder = new TextResourceDecoder("text/html"); // bookmark files are HTML
1023     String result = decoder->decode(static_cast<const char*>([data bytes]), [data length]);
1024     result += decoder->flush();
1025     return result;
1026 }
1027
1028 - (void)_pushPerformingProgrammaticFocus
1029 {
1030     _private->programmaticFocusCount++;
1031 }
1032
1033 - (void)_popPerformingProgrammaticFocus
1034 {
1035     _private->programmaticFocusCount--;
1036 }
1037
1038 - (BOOL)_isPerformingProgrammaticFocus
1039 {
1040     return _private->programmaticFocusCount != 0;
1041 }
1042
1043 - (void)_didChangeValueForKey: (NSString *)key
1044 {
1045     LOG (Bindings, "calling didChangeValueForKey: %@", key);
1046     [self didChangeValueForKey: key];
1047 }
1048
1049 - (void)_willChangeValueForKey: (NSString *)key
1050 {
1051     LOG (Bindings, "calling willChangeValueForKey: %@", key);
1052     [self willChangeValueForKey: key];
1053 }
1054
1055 // Required to prevent automatic observer notifications.
1056 + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
1057     return NO;
1058 }
1059
1060 - (NSArray *)_declaredKeys {
1061     static NSArray *declaredKeys = nil;
1062     
1063     if (!declaredKeys) {
1064         declaredKeys = [[NSArray alloc] initWithObjects:_WebMainFrameURLKey, _WebIsLoadingKey, _WebEstimatedProgressKey, _WebCanGoBackKey, _WebCanGoForwardKey, _WebMainFrameTitleKey, _WebMainFrameIconKey, _WebMainFrameDocumentKey, nil];
1065     }
1066
1067     return declaredKeys;
1068 }
1069
1070 - (void)setObservationInfo:(void *)info
1071 {
1072     _private->observationInfo = info;
1073 }
1074
1075 - (void *)observationInfo
1076 {
1077     return _private->observationInfo;
1078 }
1079
1080 - (void)_willChangeBackForwardKeys
1081 {
1082     [self _willChangeValueForKey: _WebCanGoBackKey];
1083     [self _willChangeValueForKey: _WebCanGoForwardKey];
1084 }
1085
1086 - (void)_didChangeBackForwardKeys
1087 {
1088     [self _didChangeValueForKey: _WebCanGoBackKey];
1089     [self _didChangeValueForKey: _WebCanGoForwardKey];
1090 }
1091
1092 - (void)_didStartProvisionalLoadForFrame:(WebFrame *)frame
1093 {
1094     [self _willChangeBackForwardKeys];
1095     if (frame == [self mainFrame]){
1096         // Force an observer update by sending a will/did.
1097         [self _willChangeValueForKey: _WebIsLoadingKey];
1098         [self _didChangeValueForKey: _WebIsLoadingKey];
1099
1100         [self _willChangeValueForKey: _WebMainFrameURLKey];
1101     }
1102     [NSApp setWindowsNeedUpdate:YES];
1103 }
1104
1105 - (void)_didCommitLoadForFrame:(WebFrame *)frame
1106 {
1107     if (frame == [self mainFrame]){
1108         [self _didChangeValueForKey: _WebMainFrameURLKey];
1109     }
1110     [NSApp setWindowsNeedUpdate:YES];
1111 }
1112
1113 - (void)_didFinishLoadForFrame:(WebFrame *)frame
1114 {
1115     [self _didChangeBackForwardKeys];
1116     if (frame == [self mainFrame]){
1117         // Force an observer update by sending a will/did.
1118         [self _willChangeValueForKey: _WebIsLoadingKey];
1119         [self _didChangeValueForKey: _WebIsLoadingKey];
1120     }
1121     [NSApp setWindowsNeedUpdate:YES];
1122 }
1123
1124 - (void)_didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
1125 {
1126     [self _didChangeBackForwardKeys];
1127     if (frame == [self mainFrame]){
1128         // Force an observer update by sending a will/did.
1129         [self _willChangeValueForKey: _WebIsLoadingKey];
1130         [self _didChangeValueForKey: _WebIsLoadingKey];
1131     }
1132     [NSApp setWindowsNeedUpdate:YES];
1133 }
1134
1135 - (void)_didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
1136 {
1137     [self _didChangeBackForwardKeys];
1138     if (frame == [self mainFrame]){
1139         // Force an observer update by sending a will/did.
1140         [self _willChangeValueForKey: _WebIsLoadingKey];
1141         [self _didChangeValueForKey: _WebIsLoadingKey];
1142         
1143         [self _didChangeValueForKey: _WebMainFrameURLKey];
1144     }
1145     [NSApp setWindowsNeedUpdate:YES];
1146 }
1147
1148 - (void)_reloadForPluginChanges
1149 {
1150     [[self mainFrame] _reloadForPluginChanges];
1151 }
1152
1153 - (NSCachedURLResponse *)_cachedResponseForURL:(NSURL *)URL
1154 {
1155     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
1156     [request _web_setHTTPUserAgent:[self userAgentForURL:URL]];
1157     NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
1158     [request release];
1159     return cachedResponse;
1160 }
1161
1162 - (void)_writeImageForElement:(NSDictionary *)element withPasteboardTypes:(NSArray *)types toPasteboard:(NSPasteboard *)pasteboard
1163 {
1164     NSURL *linkURL = [element objectForKey:WebElementLinkURLKey];
1165     DOMElement *domElement = [element objectForKey:WebElementDOMNodeKey];
1166     [pasteboard _web_writeImage:(NSImage *)(domElement ? nil : [element objectForKey:WebElementImageKey])
1167                         element:domElement
1168                             URL:linkURL ? linkURL : (NSURL *)[element objectForKey:WebElementImageURLKey]
1169                           title:[element objectForKey:WebElementImageAltStringKey] 
1170                         archive:[[element objectForKey:WebElementDOMNodeKey] webArchive]
1171                           types:types];
1172 }
1173
1174 - (void)_writeLinkElement:(NSDictionary *)element withPasteboardTypes:(NSArray *)types toPasteboard:(NSPasteboard *)pasteboard
1175 {
1176     [pasteboard _web_writeURL:[element objectForKey:WebElementLinkURLKey]
1177                      andTitle:[element objectForKey:WebElementLinkLabelKey]
1178                         types:types];
1179 }
1180
1181 - (void)_setInitiatedDrag:(BOOL)initiatedDrag
1182 {
1183     [self page]->dragController()->setDidInitiateDrag(initiatedDrag);
1184 }
1185
1186 #define DASHBOARD_CONTROL_LABEL @"control"
1187
1188 - (void)_addScrollerDashboardRegions:(NSMutableDictionary *)regions from:(NSArray *)views
1189 {
1190     // Add scroller regions for NSScroller and KWQScrollBar
1191     int i, count = [views count];
1192     
1193     for (i = 0; i < count; i++) {
1194         NSView *aView = [views objectAtIndex:i];
1195         
1196         if ([aView isKindOfClass:[NSScroller class]] ||
1197             [aView isKindOfClass:NSClassFromString (@"KWQScrollBar")]) {
1198             NSRect bounds = [aView bounds];
1199             NSRect adjustedBounds;
1200             adjustedBounds.origin = [self convertPoint:bounds.origin fromView:aView];
1201             adjustedBounds.origin.y = [self bounds].size.height - adjustedBounds.origin.y;
1202             
1203             // AppKit has horrible hack of placing absent scrollers at -100,-100
1204             if (adjustedBounds.origin.y == -100)
1205                 continue;
1206             adjustedBounds.size = bounds.size;
1207             NSRect clip = [aView visibleRect];
1208             NSRect adjustedClip;
1209             adjustedClip.origin = [self convertPoint:clip.origin fromView:aView];
1210             adjustedClip.origin.y = [self bounds].size.height - adjustedClip.origin.y;
1211             adjustedClip.size = clip.size;
1212             WebDashboardRegion *aRegion = 
1213                         [[[WebDashboardRegion alloc] initWithRect:adjustedBounds 
1214                                     clip:adjustedClip type:WebDashboardRegionTypeScrollerRectangle] autorelease];
1215             NSMutableArray *scrollerRegions;
1216             scrollerRegions = [regions objectForKey:DASHBOARD_CONTROL_LABEL];
1217             if (!scrollerRegions) {
1218                 scrollerRegions = [NSMutableArray array];
1219                 [regions setObject:scrollerRegions forKey:DASHBOARD_CONTROL_LABEL];
1220             }
1221             [scrollerRegions addObject:aRegion];
1222         }
1223         [self _addScrollerDashboardRegions:regions from:[aView subviews]];
1224     }
1225 }
1226
1227 - (void)_addScrollerDashboardRegions:(NSMutableDictionary *)regions
1228 {
1229     [self _addScrollerDashboardRegions:regions from:[self subviews]];
1230 }
1231
1232 - (NSDictionary *)_dashboardRegions
1233 {
1234     // Only return regions from main frame.
1235     Frame* mainFrame = [[[self mainFrame] _bridge] _frame];
1236     if (!mainFrame)
1237         return nil;
1238     NSMutableDictionary *regions = mainFrame->dashboardRegionsDictionary();
1239     [self _addScrollerDashboardRegions:regions];
1240     return regions;
1241 }
1242
1243 - (void)_setDashboardBehavior:(WebDashboardBehavior)behavior to:(BOOL)flag
1244 {
1245     // FIXME: Remove this blanket assignment once Dashboard and Dashcode implement 
1246     // specific support for the backward compatibility mode flag.
1247     if (behavior == WebDashboardBehaviorAllowWheelScrolling && flag == NO && _private->page)
1248         _private->page->settings()->setUsesDashboardBackwardCompatibilityMode(true);
1249     
1250     switch (behavior) {
1251         case WebDashboardBehaviorAlwaysSendMouseEventsToAllWindows: {
1252             _private->dashboardBehaviorAlwaysSendMouseEventsToAllWindows = flag;
1253             break;
1254         }
1255         case WebDashboardBehaviorAlwaysSendActiveNullEventsToPlugIns: {
1256             _private->dashboardBehaviorAlwaysSendActiveNullEventsToPlugIns = flag;
1257             break;
1258         }
1259         case WebDashboardBehaviorAlwaysAcceptsFirstMouse: {
1260             _private->dashboardBehaviorAlwaysAcceptsFirstMouse = flag;
1261             break;
1262         }
1263         case WebDashboardBehaviorAllowWheelScrolling: {
1264             _private->dashboardBehaviorAllowWheelScrolling = flag;
1265             break;
1266         }
1267         case WebDashboardBehaviorUseBackwardCompatibilityMode: {
1268             if (_private->page)
1269                 _private->page->settings()->setUsesDashboardBackwardCompatibilityMode(flag);
1270             break;
1271         }
1272     }
1273 }
1274
1275 - (BOOL)_dashboardBehavior:(WebDashboardBehavior)behavior
1276 {
1277     switch (behavior) {
1278         case WebDashboardBehaviorAlwaysSendMouseEventsToAllWindows: {
1279             return _private->dashboardBehaviorAlwaysSendMouseEventsToAllWindows;
1280         }
1281         case WebDashboardBehaviorAlwaysSendActiveNullEventsToPlugIns: {
1282             return _private->dashboardBehaviorAlwaysSendActiveNullEventsToPlugIns;
1283         }
1284         case WebDashboardBehaviorAlwaysAcceptsFirstMouse: {
1285             return _private->dashboardBehaviorAlwaysAcceptsFirstMouse;
1286         }
1287         case WebDashboardBehaviorAllowWheelScrolling: {
1288             return _private->dashboardBehaviorAllowWheelScrolling;
1289         }
1290         case WebDashboardBehaviorUseBackwardCompatibilityMode: {
1291             return _private->page && _private->page->settings()->usesDashboardBackwardCompatibilityMode();
1292         }
1293     }
1294     return NO;
1295 }
1296
1297 - (void)handleAuthenticationForResource:(id)identifier challenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
1298 {
1299     [[WebDefaultResourceLoadDelegate sharedResourceLoadDelegate] webView:self resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:dataSource];
1300 }
1301
1302 + (void)_setShouldUseFontSmoothing:(BOOL)f
1303 {
1304     WebCoreSetShouldUseFontSmoothing(f);
1305 }
1306
1307 + (BOOL)_shouldUseFontSmoothing
1308 {
1309     return WebCoreShouldUseFontSmoothing();
1310 }
1311
1312 + (NSString *)_minimumRequiredSafariBuildNumber
1313 {
1314     return @"420+";
1315 }
1316
1317 - (void)setAlwaysShowVerticalScroller:(BOOL)flag
1318 {
1319     WebDynamicScrollBarsView *scrollview = (WebDynamicScrollBarsView *)[[[self mainFrame] frameView] _scrollView];
1320     if (flag) {
1321         [scrollview setVerticalScrollingMode:WebCoreScrollbarAlwaysOn];
1322         [scrollview setVerticalScrollingModeLocked:YES];
1323     } else {
1324         [scrollview setVerticalScrollingModeLocked:NO];
1325         [scrollview setVerticalScrollingMode:WebCoreScrollbarAuto];
1326     }
1327 }
1328
1329 - (BOOL)alwaysShowVerticalScroller
1330 {
1331     WebDynamicScrollBarsView *scrollview = (WebDynamicScrollBarsView *)[[[self mainFrame] frameView] _scrollView];
1332     return [scrollview verticalScrollingModeLocked] && [scrollview verticalScrollingMode] == WebCoreScrollbarAlwaysOn;
1333 }
1334
1335 - (void)setAlwaysShowHorizontalScroller:(BOOL)flag
1336 {
1337     WebDynamicScrollBarsView *scrollview = (WebDynamicScrollBarsView *)[[[self mainFrame] frameView] _scrollView];
1338     if (flag) {
1339         [scrollview setHorizontalScrollingMode:WebCoreScrollbarAlwaysOn];
1340         [scrollview setHorizontalScrollingModeLocked:YES];
1341     } else {
1342         [scrollview setHorizontalScrollingModeLocked:NO];
1343         [scrollview setHorizontalScrollingMode:WebCoreScrollbarAuto];
1344     }
1345 }
1346
1347 - (void)setProhibitsMainFrameScrolling:(BOOL)prohibits
1348 {
1349     Frame* mainFrame = [[[self mainFrame] _bridge] _frame];
1350     if (mainFrame)
1351         mainFrame->setProhibitsScrolling(prohibits);
1352 }
1353
1354 - (BOOL)alwaysShowHorizontalScroller
1355 {
1356     WebDynamicScrollBarsView *scrollview = (WebDynamicScrollBarsView *)[[[self mainFrame] frameView] _scrollView];
1357     return [scrollview horizontalScrollingModeLocked] && [scrollview horizontalScrollingMode] == WebCoreScrollbarAlwaysOn;
1358 }
1359
1360 - (void)_setInViewSourceMode:(BOOL)flag
1361 {
1362     Frame* mainFrame = [[[self mainFrame] _bridge] _frame];
1363     if (mainFrame)
1364         mainFrame->setInViewSourceMode(flag);
1365 }
1366
1367 - (BOOL)_inViewSourceMode
1368 {
1369     Frame* mainFrame = [[[self mainFrame] _bridge] _frame];
1370     return mainFrame && mainFrame->inViewSourceMode();
1371 }
1372
1373 - (void)_setAdditionalWebPlugInPaths:(NSArray *)newPaths
1374 {
1375     if (!_private->pluginDatabase)
1376         _private->pluginDatabase = [[WebPluginDatabase alloc] init];
1377         
1378     [_private->pluginDatabase setPlugInPaths:newPaths];
1379     [_private->pluginDatabase refresh];
1380 }
1381
1382 - (void)_attachScriptDebuggerToAllFrames
1383 {
1384     for (Frame* frame = core([self mainFrame]); frame; frame = frame->tree()->traverseNext())
1385         [kit(frame) _attachScriptDebugger];
1386 }
1387
1388 - (void)_detachScriptDebuggerFromAllFrames
1389 {
1390     for (Frame* frame = core([self mainFrame]); frame; frame = frame->tree()->traverseNext())
1391         [kit(frame) _detachScriptDebugger];
1392 }
1393
1394 - (void)setBackgroundColor:(NSColor *)backgroundColor
1395 {
1396     if ([_private->backgroundColor isEqual:backgroundColor])
1397         return;
1398
1399     id old = _private->backgroundColor;
1400     _private->backgroundColor = [backgroundColor retain];
1401     [old release];
1402
1403     [[self mainFrame] _updateBackground];
1404 }
1405
1406 - (NSColor *)backgroundColor
1407 {
1408     return _private->backgroundColor;
1409 }
1410
1411 - (BOOL)defersCallbacks
1412 {
1413     if (!_private->page)
1414         return NO;
1415     return _private->page->defersLoading();
1416 }
1417
1418 - (void)setDefersCallbacks:(BOOL)defer
1419 {
1420     if (!_private->page)
1421         return;
1422     return _private->page->setDefersLoading(defer);
1423 }
1424
1425 @end
1426
1427 @implementation _WebSafeForwarder
1428
1429 - initWithTarget: t defaultTarget: dt templateClass: (Class)aClass
1430 {
1431     self = [super init];
1432     if (!self)
1433         return nil;
1434     
1435     target = t; // Non retained.
1436     defaultTarget = dt;
1437     templateClass = aClass;
1438     return self;
1439 }
1440
1441 // Used to send messages to delegates that implement informal protocols.
1442 + safeForwarderWithTarget: t defaultTarget: dt templateClass: (Class)aClass;
1443 {
1444     return [[[_WebSafeForwarder alloc] initWithTarget: t defaultTarget: dt templateClass: aClass] autorelease];
1445 }
1446
1447 #ifndef NDEBUG
1448 NSMutableDictionary *countInvocations;
1449 #endif
1450
1451 - (void)forwardInvocation:(NSInvocation *)anInvocation
1452 {
1453 #ifndef NDEBUG
1454     if (!countInvocations){
1455         countInvocations = [[NSMutableDictionary alloc] init];
1456     }
1457     NSNumber *count = [countInvocations objectForKey: NSStringFromSelector([anInvocation selector])];
1458     if (!count)
1459         count = [NSNumber numberWithInt: 1];
1460     else
1461         count = [NSNumber numberWithInt: [count intValue] + 1];
1462     [countInvocations setObject: count forKey: NSStringFromSelector([anInvocation selector])];
1463 #endif
1464     if ([target respondsToSelector: [anInvocation selector]])
1465         [anInvocation invokeWithTarget: target];
1466     else if ([defaultTarget respondsToSelector: [anInvocation selector]])
1467         [anInvocation invokeWithTarget: defaultTarget];
1468     // Do nothing quietly if method not implemented.
1469 }
1470
1471 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
1472 {
1473     return [templateClass instanceMethodSignatureForSelector: aSelector];
1474 }
1475
1476 @end
1477
1478 @implementation WebView
1479
1480 #ifdef REMOVE_SAFARI_DOM_TREE_DEBUG_ITEM
1481 // this prevents open source users from crashing when using the Show DOM Tree menu item in Safari
1482 // FIXME: remove this when it is no longer needed to prevent Safari from crashing
1483 + (void)initialize
1484 {
1485     static BOOL tooLate = NO;
1486     if (!tooLate) {
1487         if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.apple.Safari"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"IncludeDebugMenu"])
1488             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_finishedLaunching) name:NSApplicationDidFinishLaunchingNotification object:NSApp];
1489         
1490         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationWillTerminate) name:NSApplicationWillTerminateNotification object:NSApp];
1491         tooLate = YES;
1492     }
1493 }
1494
1495 + (void)_applicationWillTerminate
1496 {   
1497     applicationIsTerminating = YES;
1498     if (!pluginDatabaseClientCount)
1499         [[WebPluginDatabase sharedDatabase] close];
1500 }
1501
1502 + (void)_finishedLaunching
1503 {
1504     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_removeDOMTreeMenuItem:) name:NSMenuDidAddItemNotification object:[NSApp mainMenu]];
1505     [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidFinishLaunchingNotification object:NSApp];
1506 }
1507
1508 +(void)_removeDOMTreeMenuItem:(NSNotification *)notification
1509 {
1510     NSMenu *debugMenu = [[[[NSApp mainMenu] itemArray] lastObject] submenu];
1511     NSMenuItem *domTree = [debugMenu itemWithTitle:@"Show DOM Tree"];
1512     if (domTree)
1513         [debugMenu removeItem:domTree];
1514     [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMenuDidAddItemNotification object:[NSApp mainMenu]];
1515 }
1516 #endif
1517
1518 + (BOOL)canShowMIMEType:(NSString *)MIMEType
1519 {
1520     return [self _viewClass:nil andRepresentationClass:nil forMIMEType:MIMEType];
1521 }
1522
1523 - (WebBasePluginPackage *)_pluginForMIMEType:(NSString *)MIMEType
1524 {
1525     WebBasePluginPackage *pluginPackage = [[WebPluginDatabase sharedDatabase] pluginForMIMEType:MIMEType];
1526     if (pluginPackage)
1527         return pluginPackage;
1528     
1529     if (_private->pluginDatabase)
1530         return [_private->pluginDatabase pluginForMIMEType:MIMEType];
1531     
1532     return nil;
1533 }
1534
1535 - (WebBasePluginPackage *)_pluginForExtension:(NSString *)extension
1536 {
1537     WebBasePluginPackage *pluginPackage = [[WebPluginDatabase sharedDatabase] pluginForExtension:extension];
1538     if (pluginPackage)
1539         return pluginPackage;
1540     
1541     if (_private->pluginDatabase)
1542         return [_private->pluginDatabase pluginForExtension:extension];
1543     
1544     return nil;
1545 }
1546
1547 - (BOOL)_isMIMETypeRegisteredAsPlugin:(NSString *)MIMEType
1548 {
1549     if ([[WebPluginDatabase sharedDatabase] isMIMETypeRegistered:MIMEType])
1550         return YES;
1551         
1552     if (_private->pluginDatabase && [_private->pluginDatabase isMIMETypeRegistered:MIMEType])
1553         return YES;
1554     
1555     return NO;
1556 }
1557
1558 + (BOOL)canShowMIMETypeAsHTML:(NSString *)MIMEType
1559 {
1560     return [WebFrameView _canShowMIMETypeAsHTML:MIMEType];
1561 }
1562
1563 + (NSArray *)MIMETypesShownAsHTML
1564 {
1565     NSMutableDictionary *viewTypes = [WebFrameView _viewTypesAllowImageTypeOmission:YES];
1566     NSEnumerator *enumerator = [viewTypes keyEnumerator];
1567     id key;
1568     NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
1569     
1570     while ((key = [enumerator nextObject])) {
1571         if ([viewTypes objectForKey:key] == [WebHTMLView class])
1572             [array addObject:key];
1573     }
1574     
1575     return array;
1576 }
1577
1578 + (void)setMIMETypesShownAsHTML:(NSArray *)MIMETypes
1579 {
1580     NSMutableDictionary *viewTypes = [WebFrameView _viewTypesAllowImageTypeOmission:YES];
1581     NSEnumerator *enumerator = [viewTypes keyEnumerator];
1582     id key;
1583     while ((key = [enumerator nextObject])) {
1584         if ([viewTypes objectForKey:key] == [WebHTMLView class])
1585             [WebView _unregisterViewClassAndRepresentationClassForMIMEType:key];
1586     }
1587     
1588     int i, count = [MIMETypes count];
1589     for (i = 0; i < count; i++) {
1590         [WebView registerViewClass:[WebHTMLView class] 
1591                 representationClass:[WebHTMLRepresentation class] 
1592                 forMIMEType:[MIMETypes objectAtIndex:i]];
1593     }
1594 }
1595
1596 + (NSURL *)URLFromPasteboard:(NSPasteboard *)pasteboard
1597 {
1598     return [pasteboard _web_bestURL];
1599 }
1600
1601 + (NSString *)URLTitleFromPasteboard:(NSPasteboard *)pasteboard
1602 {
1603     return [pasteboard stringForType:WebURLNamePboardType];
1604 }
1605
1606 - (void)_registerDraggedTypes
1607 {
1608     NSArray *editableTypes = [WebHTMLView _insertablePasteboardTypes];
1609     NSArray *URLTypes = [NSPasteboard _web_dragTypesForURL];
1610     NSMutableSet *types = [[NSMutableSet alloc] initWithArray:editableTypes];
1611     [types addObjectsFromArray:URLTypes];
1612     [self registerForDraggedTypes:[types allObjects]];
1613     [types release];
1614 }
1615
1616 - (void)_commonInitializationWithFrameName:(NSString *)frameName groupName:(NSString *)groupName
1617 {
1618     _private->mainFrameDocumentReady = NO;
1619     _private->drawsBackground = YES;
1620     _private->smartInsertDeleteEnabled = YES;
1621     _private->backgroundColor = [[NSColor whiteColor] retain];
1622
1623     NSRect f = [self frame];
1624     WebFrameView *frameView = [[WebFrameView alloc] initWithFrame: NSMakeRect(0,0,f.size.width,f.size.height)];
1625     [frameView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
1626     [self addSubview:frameView];
1627     [frameView release];
1628
1629     WebKitInitializeLoggingChannelsIfNecessary();
1630     WebCore::InitializeLoggingChannelsIfNecessary();
1631     [WebBackForwardList setDefaultPageCacheSizeIfNecessary];
1632     [WebHistoryItem initWindowWatcherIfNecessary];
1633
1634     _private->page = new Page(new WebChromeClient(self), new WebContextMenuClient(self), new WebEditorClient(self), new WebDragClient(self));
1635     [[[WebFrameBridge alloc] initMainFrameWithPage:_private->page frameName:frameName frameView:frameView] release];
1636
1637     [self _addToAllWebViewsSet];
1638     [self setGroupName:groupName];
1639     
1640     // If there's already a next key view (e.g., from a nib), wire it up to our
1641     // contained frame view. In any case, wire our next key view up to the our
1642     // contained frame view. This works together with our becomeFirstResponder 
1643     // and setNextKeyView overrides.
1644     NSView *nextKeyView = [self nextKeyView];
1645     if (nextKeyView != nil && nextKeyView != frameView) {
1646         [frameView setNextKeyView:nextKeyView];
1647     }
1648     [super setNextKeyView:frameView];
1649
1650     ++WebViewCount;
1651
1652     [self _registerDraggedTypes];
1653
1654     // initialize WebScriptDebugServer here so listeners can register before any pages are loaded.
1655     if ([WebView _scriptDebuggerEnabled])
1656         [WebScriptDebugServer sharedScriptDebugServer];
1657
1658     // Update WebCore with preferences.  These values will either come from an archived WebPreferences,
1659     // or from the standard preferences, depending on whether this method was called from initWithCoder:
1660     // or initWithFrame, respectively.
1661     [self _updateWebCoreSettingsFromPreferences: [self preferences]];
1662     
1663     // Register to receive notifications whenever preference values change.
1664     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_preferencesChangedNotification:)
1665                                                  name:WebPreferencesChangedNotification object:[self preferences]];
1666
1667     if (WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITH_LOCAL_RESOURCE_SECURITY_RESTRICTION))
1668         FrameLoader::setRestrictAccessToLocal(true);
1669 }
1670
1671 - (id)initWithFrame:(NSRect)f
1672 {
1673     return [self initWithFrame:f frameName:nil groupName:nil];
1674 }
1675
1676 - (id)initWithFrame:(NSRect)f frameName:(NSString *)frameName groupName:(NSString *)groupName;
1677 {
1678     self = [super initWithFrame:f];
1679     if (!self)
1680         return nil;
1681
1682 #ifdef ENABLE_WEBKIT_UNSET_DYLD_FRAMEWORK_PATH
1683     // DYLD_FRAMEWORK_PATH is used so Safari will load the development version of WebKit, which
1684     // may not work with other WebKit applications.  Unsetting DYLD_FRAMEWORK_PATH removes the
1685     // need for Safari to unset it to prevent it from being passed to applications it launches.
1686     // Unsetting it when a WebView is first created is as good a place as any.
1687     // See <http://bugs.webkit.org/show_bug.cgi?id=4286> for more details.
1688     if (getenv("WEBKIT_UNSET_DYLD_FRAMEWORK_PATH")) {
1689         unsetenv("DYLD_FRAMEWORK_PATH");
1690         unsetenv("WEBKIT_UNSET_DYLD_FRAMEWORK_PATH");
1691     }
1692 #endif
1693
1694     _private = [[WebViewPrivate alloc] init];
1695     [self _commonInitializationWithFrameName:frameName groupName:groupName];
1696     [self setMaintainsBackForwardList: YES];
1697     return self;
1698 }
1699
1700 - (id)initWithCoder:(NSCoder *)decoder
1701 {
1702     WebView *result = nil;
1703
1704 NS_DURING
1705
1706     NSString *frameName;
1707     NSString *groupName;
1708     WebPreferences *preferences;
1709     BOOL useBackForwardList = NO;
1710     BOOL allowsUndo = YES;
1711     
1712     result = [super initWithCoder:decoder];
1713     result->_private = [[WebViewPrivate alloc] init];
1714
1715     // We don't want any of the archived subviews. The subviews will always
1716     // be created in _commonInitializationFrameName:groupName:.
1717     [[result subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
1718
1719     if ([decoder allowsKeyedCoding]) {
1720         frameName = [decoder decodeObjectForKey:@"FrameName"];
1721         groupName = [decoder decodeObjectForKey:@"GroupName"];
1722         preferences = [decoder decodeObjectForKey:@"Preferences"];
1723         useBackForwardList = [decoder decodeBoolForKey:@"UseBackForwardList"];
1724         if ([decoder containsValueForKey:@"AllowsUndo"])
1725             allowsUndo = [decoder decodeBoolForKey:@"AllowsUndo"];
1726     } else {
1727         int version;
1728         [decoder decodeValueOfObjCType:@encode(int) at:&version];
1729         frameName = [decoder decodeObject];
1730         groupName = [decoder decodeObject];
1731         preferences = [decoder decodeObject];
1732         if (version > 1)
1733             [decoder decodeValuesOfObjCTypes:"c", &useBackForwardList];
1734         if (version > 2)
1735             [decoder decodeValuesOfObjCTypes:"c", &allowsUndo];
1736     }
1737
1738     if (![frameName isKindOfClass:[NSString class]])
1739         frameName = nil;
1740     if (![groupName isKindOfClass:[NSString class]])
1741         groupName = nil;
1742     if (![preferences isKindOfClass:[WebPreferences class]])
1743         preferences = nil;
1744
1745     LOG(Encoding, "FrameName = %@, GroupName = %@, useBackForwardList = %d\n", frameName, groupName, (int)useBackForwardList);
1746     [result _commonInitializationWithFrameName:frameName groupName:groupName];
1747     result->_private->useBackForwardList = useBackForwardList;
1748     result->_private->allowsUndo = allowsUndo;
1749     if (preferences)
1750         [result setPreferences:preferences];
1751
1752 NS_HANDLER
1753
1754     result = nil;
1755     [self release];
1756
1757 NS_ENDHANDLER
1758
1759     return result;
1760 }
1761
1762 - (void)encodeWithCoder:(NSCoder *)encoder
1763 {
1764     [super encodeWithCoder:encoder];
1765
1766     if ([encoder allowsKeyedCoding]) {
1767         [encoder encodeObject:[[self mainFrame] name] forKey:@"FrameName"];
1768         [encoder encodeObject:[self groupName] forKey:@"GroupName"];
1769         [encoder encodeObject:[self preferences] forKey:@"Preferences"];
1770         [encoder encodeBool:_private->useBackForwardList forKey:@"UseBackForwardList"];
1771         [encoder encodeBool:_private->allowsUndo forKey:@"AllowsUndo"];
1772     } else {
1773         int version = WebViewVersion;
1774         [encoder encodeValueOfObjCType:@encode(int) at:&version];
1775         [encoder encodeObject:[[self mainFrame] name]];
1776         [encoder encodeObject:[self groupName]];
1777         [encoder encodeObject:[self preferences]];
1778         [encoder encodeValuesOfObjCTypes:"c", &_private->useBackForwardList];
1779         [encoder encodeValuesOfObjCTypes:"c", &_private->allowsUndo];
1780     }
1781
1782     LOG(Encoding, "FrameName = %@, GroupName = %@, useBackForwardList = %d\n", [[self mainFrame] name], [self groupName], (int)_private->useBackForwardList);
1783 }
1784
1785 - (void)dealloc
1786 {
1787     // call close to ensure we tear-down completly
1788     // this maintains our old behavior for existing applications
1789     [self _close];
1790
1791     --WebViewCount;
1792     
1793     [_private release];
1794     // [super dealloc] can end up dispatching against _private (3466082)
1795     _private = nil;
1796
1797     [super dealloc];
1798 }
1799
1800 - (void)finalize
1801 {
1802     ASSERT(_private->closed);
1803
1804     --WebViewCount;
1805
1806     [super finalize];
1807 }
1808
1809 - (void)close
1810 {
1811     [self _close];
1812 }
1813
1814 - (void)setShouldCloseWithWindow:(BOOL)close
1815 {
1816     _private->shouldCloseWithWindow = close;
1817 }
1818
1819 - (BOOL)shouldCloseWithWindow
1820 {
1821     return _private->shouldCloseWithWindow;
1822 }
1823
1824 - (void)viewWillMoveToWindow:(NSWindow *)window
1825 {
1826     // Don't do anything if we aren't initialized.  This happens when decoding a WebView.
1827     if (!_private)
1828         return;
1829     
1830     if ([self window])
1831         [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:[self window]];
1832
1833     if (window) {
1834         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowWillClose:) name:NSWindowWillCloseNotification object:window];
1835
1836         // Ensure that we will receive the events that WebHTMLView (at least) needs. It's expensive enough
1837         // that we don't want to call it over and over.
1838         [window setAcceptsMouseMovedEvents:YES];
1839         WKSetNSWindowShouldPostEventNotifications(window, YES);
1840     }
1841 }
1842
1843 - (void)_windowWillClose:(NSNotification *)notification
1844 {
1845     if ([self shouldCloseWithWindow] && ([self window] == [self hostWindow] || ([self window] && ![self hostWindow]) || (![self window] && [self hostWindow])))
1846         [self _close];
1847 }
1848
1849 - (void)setPreferences:(WebPreferences *)prefs
1850 {
1851     if (_private->preferences != prefs) {
1852         [[NSNotificationCenter defaultCenter] removeObserver:self name:WebPreferencesChangedNotification object:[self preferences]];
1853         [WebPreferences _removeReferenceForIdentifier:[_private->preferences identifier]];
1854         [_private->preferences release];
1855         _private->preferences = [prefs retain];
1856         // Cache this value so we don't have to read NSUserDefaults on each page load
1857         _private->useSiteSpecificSpoofing = [prefs _useSiteSpecificSpoofing];
1858         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_preferencesChangedNotification:)
1859             name:WebPreferencesChangedNotification object:[self preferences]];
1860         [[NSNotificationCenter defaultCenter]
1861             postNotificationName:WebPreferencesChangedNotification object:prefs userInfo:nil];
1862     }
1863 }
1864
1865 - (WebPreferences *)preferences
1866 {
1867     return _private->preferences ? _private->preferences : [WebPreferences standardPreferences];
1868 }
1869
1870 - (void)setPreferencesIdentifier:(NSString *)anIdentifier
1871 {
1872     if (!_private->closed && ![anIdentifier isEqual:[[self preferences] identifier]]) {
1873         WebPreferences *prefs = [[WebPreferences alloc] initWithIdentifier:anIdentifier];
1874         [self setPreferences:prefs];
1875         [prefs release];
1876     }
1877 }
1878
1879 - (NSString *)preferencesIdentifier
1880 {
1881     return [[self preferences] identifier];
1882 }
1883
1884
1885 - (void)setUIDelegate:delegate
1886 {
1887     _private->UIDelegate = delegate;
1888     [_private->UIDelegateForwarder release];
1889     _private->UIDelegateForwarder = nil;
1890 }
1891
1892 - UIDelegate
1893 {
1894     return _private->UIDelegate;
1895 }
1896
1897 - (void)setResourceLoadDelegate: delegate
1898 {
1899     _private->resourceProgressDelegate = delegate;
1900     [_private->resourceProgressDelegateForwarder release];
1901     _private->resourceProgressDelegateForwarder = nil;
1902     [self _cacheResourceLoadDelegateImplementations];
1903 }
1904
1905
1906 - resourceLoadDelegate
1907 {
1908     return _private->resourceProgressDelegate;
1909 }
1910
1911 - (void)setDownloadDelegate: delegate
1912 {
1913     _private->downloadDelegate = delegate;
1914 }
1915
1916
1917 - downloadDelegate
1918 {
1919     return _private->downloadDelegate;
1920 }
1921
1922 - (void)setPolicyDelegate:delegate
1923 {
1924     _private->policyDelegate = delegate;
1925     [_private->policyDelegateForwarder release];
1926     _private->policyDelegateForwarder = nil;
1927 }
1928
1929 - policyDelegate
1930 {
1931     return _private->policyDelegate;
1932 }
1933
1934 - (void)setFrameLoadDelegate:delegate
1935 {
1936     _private->frameLoadDelegate = delegate;
1937     [_private->frameLoadDelegateForwarder release];
1938     _private->frameLoadDelegateForwarder = nil;
1939 }
1940
1941 - frameLoadDelegate
1942 {
1943     return _private->frameLoadDelegate;
1944 }
1945
1946 - (WebFrame *)mainFrame
1947 {
1948     // This can be called in initialization, before _private has been set up (3465613)
1949     if (!_private)
1950         return nil;
1951     if (!_private->page)
1952         return nil;
1953     return kit(_private->page->mainFrame());
1954 }
1955
1956 - (WebFrame *)selectedFrame
1957 {
1958     // If the first responder is a view in our tree, we get the frame containing the first responder.
1959     // This is faster than searching the frame hierarchy, and will give us a result even in the case
1960     // where the focused frame doesn't actually contain a selection.
1961     WebFrame *focusedFrame = [self _focusedFrame];
1962     if (focusedFrame)
1963         return focusedFrame;
1964     
1965     // If the first responder is outside of our view tree, we search for a frame containing a selection.
1966     // There should be at most only one of these.
1967     return [[self mainFrame] _findFrameWithSelection];
1968 }
1969
1970 - (WebBackForwardList *)backForwardList
1971 {
1972     if (_private->useBackForwardList)
1973         return kit([self page]->backForwardList());
1974     return nil;
1975 }
1976
1977 - (void)setMaintainsBackForwardList: (BOOL)flag
1978 {
1979     _private->useBackForwardList = flag;
1980 }
1981
1982 - (BOOL)goBack
1983 {
1984     if (!_private->page)
1985         return NO;
1986     
1987     return _private->page->goBack();
1988 }
1989
1990 - (BOOL)goForward
1991 {
1992     if (!_private->page)
1993         return NO;
1994
1995     return _private->page->goForward();
1996 }
1997
1998 - (BOOL)goToBackForwardItem:(WebHistoryItem *)item
1999 {
2000     if (!_private->page)
2001         return NO;
2002
2003     _private->page->goToItem(core(item), FrameLoadTypeIndexedBackForward);
2004     return YES;
2005 }
2006
2007 - (void)setTextSizeMultiplier:(float)m
2008 {
2009     // NOTE: This has no visible effect when viewing a PDF (see <rdar://problem/4737380>)
2010     if (_private->textSizeMultiplier == m)
2011         return;
2012
2013     _private->textSizeMultiplier = m;
2014     [self _notifyTextSizeMultiplierChanged];
2015 }
2016
2017 - (float)textSizeMultiplier
2018 {
2019     return _private->textSizeMultiplier;
2020 }
2021
2022 - (void)setApplicationNameForUserAgent:(NSString *)applicationName
2023 {
2024     NSString *name = [applicationName copy];
2025     [_private->applicationNameForUserAgent release];
2026     _private->applicationNameForUserAgent = name;
2027     if (!_private->userAgentOverridden)
2028         *_private->userAgent = String();
2029 }
2030
2031 - (NSString *)applicationNameForUserAgent
2032 {
2033     return [[_private->applicationNameForUserAgent retain] autorelease];
2034 }
2035
2036 - (void)setCustomUserAgent:(NSString *)userAgentString
2037 {
2038     *_private->userAgent = userAgentString;
2039     _private->userAgentOverridden = userAgentString != nil;
2040 }
2041
2042 - (NSString *)customUserAgent
2043 {
2044     if (!_private->userAgentOverridden)
2045         return nil;
2046     return *_private->userAgent;
2047 }
2048
2049 - (void)setMediaStyle:(NSString *)mediaStyle
2050 {
2051     if (_private->mediaStyle != mediaStyle) {
2052         [_private->mediaStyle release];
2053         _private->mediaStyle = [mediaStyle copy];
2054     }
2055 }
2056
2057 - (NSString *)mediaStyle
2058 {
2059     return _private->mediaStyle;
2060 }
2061
2062 - (BOOL)supportsTextEncoding
2063 {
2064     id documentView = [[[self mainFrame] frameView] documentView];
2065     return [documentView conformsToProtocol:@protocol(WebDocumentText)]
2066         && [documentView supportsTextEncoding];
2067 }
2068
2069 - (void)setCustomTextEncodingName:(NSString *)encoding
2070 {
2071     NSString *oldEncoding = [self customTextEncodingName];
2072     if (encoding == oldEncoding || [encoding isEqualToString:oldEncoding])
2073         return;
2074     FrameLoader* mainFrameLoader = [[self mainFrame] _frameLoader];
2075     if (mainFrameLoader)
2076         mainFrameLoader->reloadAllowingStaleData(encoding);
2077 }
2078
2079 - (NSString *)_mainFrameOverrideEncoding
2080 {
2081     WebDataSource *dataSource = [[self mainFrame] provisionalDataSource];
2082     if (dataSource == nil)
2083         dataSource = [[self mainFrame] dataSource];
2084     if (dataSource == nil)
2085         return nil;
2086     return [dataSource _documentLoader]->overrideEncoding();
2087 }
2088
2089 - (NSString *)customTextEncodingName
2090 {
2091     return [self _mainFrameOverrideEncoding];
2092 }
2093
2094 - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
2095 {
2096     return [[[self mainFrame] _bridge] stringByEvaluatingJavaScriptFromString:script];
2097 }
2098
2099 - (WebScriptObject *)windowScriptObject
2100 {
2101     return core([self mainFrame])->windowScriptObject();
2102 }
2103
2104 // Get the appropriate user-agent string for a particular URL.
2105 - (NSString *)userAgentForURL:(NSURL *)url
2106 {
2107     return [self _userAgentForURL:KURL(url)];
2108 }
2109
2110 - (void)setHostWindow:(NSWindow *)hostWindow
2111 {
2112     if (!_private->closed && hostWindow != _private->hostWindow) {
2113         [[self mainFrame] _viewWillMoveToHostWindow:hostWindow];
2114         if (_private->hostWindow)
2115             [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:_private->hostWindow];
2116         if (hostWindow)
2117             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowWillClose:) name:NSWindowWillCloseNotification object:hostWindow];
2118         [_private->hostWindow release];
2119         _private->hostWindow = [hostWindow retain];
2120         [[self mainFrame] _viewDidMoveToHostWindow];
2121     }
2122 }
2123
2124 - (NSWindow *)hostWindow
2125 {
2126     return _private->hostWindow;
2127 }
2128
2129 - (NSView <WebDocumentView> *)documentViewAtWindowPoint:(NSPoint)point
2130 {
2131     return [[self _frameViewAtWindowPoint:point] documentView];
2132 }
2133
2134 - (NSDictionary *)_elementAtWindowPoint:(NSPoint)windowPoint
2135 {
2136     WebFrameView *frameView = [self _frameViewAtWindowPoint:windowPoint];
2137     if (!frameView)
2138         return nil;
2139     NSView <WebDocumentView> *documentView = [frameView documentView];
2140     if ([documentView conformsToProtocol:@protocol(WebDocumentElement)]) {
2141         NSPoint point = [documentView convertPoint:windowPoint fromView:nil];
2142         return [(NSView <WebDocumentElement> *)documentView elementAtPoint:point];
2143     }
2144     return [NSDictionary dictionaryWithObject:[frameView webFrame] forKey:WebElementFrameKey];
2145 }
2146
2147 - (NSDictionary *)elementAtPoint:(NSPoint)point
2148 {
2149     return [self _elementAtWindowPoint:[self convertPoint:point toView:nil]];
2150 }
2151
2152 // The following 2 internal NSView methods are called on the drag destination by make scrolling while dragging work.
2153 // Scrolling while dragging will only work if the drag destination is in a scroll view. The WebView is the drag destination. 
2154 // When dragging to a WebView, the document subview should scroll, but it doesn't because it is not the drag destination. 
2155 // Forward these calls to the document subview to make its scroll view scroll.
2156 - (void)_autoscrollForDraggingInfo:(id)draggingInfo timeDelta:(NSTimeInterval)repeatDelta
2157 {
2158     NSView <WebDocumentView> *documentView = [self documentViewAtWindowPoint:[draggingInfo draggingLocation]];
2159     [documentView _autoscrollForDraggingInfo:draggingInfo timeDelta:repeatDelta];
2160 }
2161
2162 - (BOOL)_shouldAutoscrollForDraggingInfo:(id)draggingInfo
2163 {
2164     NSView <WebDocumentView> *documentView = [self documentViewAtWindowPoint:[draggingInfo draggingLocation]];
2165     return [documentView _shouldAutoscrollForDraggingInfo:draggingInfo];
2166 }
2167
2168 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)draggingInfo
2169 {
2170     NSView <WebDocumentView>* view = [self documentViewAtWindowPoint:[draggingInfo draggingLocation]];
2171     WebPasteboardHelper helper([view isKindOfClass:[WebHTMLView class]] ? (WebHTMLView*)view : nil);
2172     IntPoint client([draggingInfo draggingLocation]);
2173     IntPoint global(globalPoint([draggingInfo draggingLocation], [self window]));
2174     DragData dragData(draggingInfo, client, global, (DragOperation)[draggingInfo draggingSourceOperationMask], &helper);
2175     return core(self)->dragController()->dragEntered(&dragData);
2176 }
2177
2178 - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)draggingInfo
2179 {
2180     NSView <WebDocumentView>* view = [self documentViewAtWindowPoint:[draggingInfo draggingLocation]];
2181     WebPasteboardHelper helper([view isKindOfClass:[WebHTMLView class]] ? (WebHTMLView*)view : nil);
2182     IntPoint client([draggingInfo draggingLocation]);
2183     IntPoint global(globalPoint([draggingInfo draggingLocation], [self window]));
2184     DragData dragData(draggingInfo, client, global, (DragOperation)[draggingInfo draggingSourceOperationMask], &helper);
2185     return core(self)->dragController()->dragUpdated(&dragData);
2186 }
2187
2188 - (void)draggingExited:(id <NSDraggingInfo>)draggingInfo
2189 {
2190     NSView <WebDocumentView>* view = [self documentViewAtWindowPoint:[draggingInfo draggingLocation]];
2191     WebPasteboardHelper helper([view isKindOfClass:[WebHTMLView class]] ? (WebHTMLView*)view : nil);
2192     IntPoint client([draggingInfo draggingLocation]);
2193     IntPoint global(globalPoint([draggingInfo draggingLocation], [self window]));
2194     DragData dragData(draggingInfo, client, global, (DragOperation)[draggingInfo draggingSourceOperationMask], &helper);
2195     core(self)->dragController()->dragExited(&dragData);
2196 }
2197
2198 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)draggingInfo
2199 {
2200     return YES;
2201 }
2202
2203 - (BOOL)performDragOperation:(id <NSDraggingInfo>)draggingInfo
2204 {
2205     NSView <WebDocumentView>* view = [self documentViewAtWindowPoint:[draggingInfo draggingLocation]];
2206     WebPasteboardHelper helper([view isKindOfClass:[WebHTMLView class]]? (WebHTMLView*)view : nil);
2207     IntPoint client([draggingInfo draggingLocation]);
2208     IntPoint global(globalPoint([draggingInfo draggingLocation], [self window]));
2209     DragData dragData(draggingInfo, client, global, (DragOperation)[draggingInfo draggingSourceOperationMask], &helper);
2210     return core(self)->dragController()->performDrag(&dragData);
2211 }
2212
2213 - (NSView *)_hitTest:(NSPoint *)aPoint dragTypes:(NSSet *)types
2214 {
2215     NSView *hitView = [super _hitTest:aPoint dragTypes:types];
2216     if (!hitView && [[self superview] mouse:*aPoint inRect:[self frame]]) {
2217         return self;
2218     } else {
2219         return hitView;
2220     }
2221 }
2222
2223 - (BOOL)acceptsFirstResponder
2224 {
2225     return [[[self mainFrame] frameView] acceptsFirstResponder];
2226 }
2227
2228 - (BOOL)becomeFirstResponder
2229 {
2230     if (_private->becomingFirstResponder) {
2231         // Fix for unrepro infinite recursion reported in radar 4448181. If we hit this assert on
2232         // a debug build, we should figure out what causes the problem and do a better fix.
2233         ASSERT_NOT_REACHED();
2234         return NO;
2235     }
2236     
2237     // This works together with setNextKeyView to splice the WebView into
2238     // the key loop similar to the way NSScrollView does this. Note that
2239     // WebFrameView has very similar code.
2240     NSWindow *window = [self window];
2241     WebFrameView *mainFrameView = [[self mainFrame] frameView];
2242     
2243     if ([window keyViewSelectionDirection] == NSSelectingPrevious) {
2244         NSView *previousValidKeyView = [self previousValidKeyView];
2245         if ((previousValidKeyView != self) && (previousValidKeyView != mainFrameView)) {
2246             _private->becomingFirstResponder = YES;
2247             [window makeFirstResponder:previousValidKeyView];
2248             _private->becomingFirstResponder = NO;
2249             return YES;
2250         } else {
2251             return NO;
2252         }
2253     }
2254     
2255     if ([mainFrameView acceptsFirstResponder]) {
2256         _private->becomingFirstResponder = YES;
2257         [window makeFirstResponder:mainFrameView];
2258         _private->becomingFirstResponder = NO;
2259         return YES;
2260     } 
2261     
2262     return NO;
2263 }
2264
2265 - (NSView *)_webcore_effectiveFirstResponder
2266 {
2267     WebFrameView *frameView = [[self mainFrame] frameView];
2268     return frameView ? [frameView _webcore_effectiveFirstResponder] : [super _webcore_effectiveFirstResponder];
2269 }
2270
2271 - (void)setNextKeyView:(NSView *)aView
2272 {
2273     // This works together with becomeFirstResponder to splice the WebView into
2274     // the key loop similar to the way NSScrollView does this. Note that
2275     // WebFrameView has very similar code.
2276     WebFrameView *mainFrameView = [[self mainFrame] frameView];
2277     if (mainFrameView != nil) {
2278         [mainFrameView setNextKeyView:aView];
2279     } else {
2280         [super setNextKeyView:aView];
2281     }
2282 }
2283
2284 static WebFrame *incrementFrame(WebFrame *curr, BOOL forward, BOOL wrapFlag)
2285 {
2286     Frame* coreFrame = core(curr);
2287     return kit(forward
2288         ? coreFrame->tree()->traverseNextWithWrap(wrapFlag)
2289         : coreFrame->tree()->traversePreviousWithWrap(wrapFlag));
2290 }
2291
2292 - (BOOL)searchFor:(NSString *)string direction:(BOOL)forward caseSensitive:(BOOL)caseFlag wrap:(BOOL)wrapFlag
2293 {
2294     return [self searchFor:string direction:forward caseSensitive:caseFlag wrap:wrapFlag startInSelection:NO];
2295 }
2296
2297 + (void)registerViewClass:(Class)viewClass representationClass:(Class)representationClass forMIMEType:(NSString *)MIMEType
2298 {
2299     [[WebFrameView _viewTypesAllowImageTypeOmission:YES] setObject:viewClass forKey:MIMEType];
2300     [[WebDataSource _repTypesAllowImageTypeOmission:YES] setObject:representationClass forKey:MIMEType];
2301 }
2302
2303 - (void)setGroupName:(NSString *)groupName
2304 {
2305     if (!_private->page)
2306         return;
2307     _private->page->setGroupName(groupName);
2308 }
2309
2310 - (NSString *)groupName
2311 {
2312     if (!_private->page)
2313         return nil;
2314     return _private->page->groupName();
2315 }
2316
2317 - (double)estimatedProgress
2318 {
2319     if (!_private->page)
2320         return 0.0;
2321
2322     return _private->page->progress()->estimatedProgress();
2323 }
2324
2325 - (NSArray *)pasteboardTypesForSelection
2326 {
2327     NSView <WebDocumentView> *documentView = [[[self _selectedOrMainFrame] frameView] documentView];
2328     if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)]) {
2329         return [(NSView <WebDocumentSelection> *)documentView pasteboardTypesForSelection];
2330     }
2331     return [NSArray array];
2332 }
2333
2334 - (void)writeSelectionWithPasteboardTypes:(NSArray *)types toPasteboard:(NSPasteboard *)pasteboard
2335 {
2336     WebFrame *frame = [self _selectedOrMainFrame];
2337     if (frame && [frame _hasSelection]) {
2338         NSView <WebDocumentView> *documentView = [[frame frameView] documentView];
2339         if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)])
2340             [(NSView <WebDocumentSelection> *)documentView writeSelectionWithPasteboardTypes:types toPasteboard:pasteboard];
2341     }
2342 }
2343
2344 - (NSArray *)pasteboardTypesForElement:(NSDictionary *)element
2345 {
2346     if ([element objectForKey:WebElementImageURLKey] != nil) {
2347         return [NSPasteboard _web_writableTypesForImageIncludingArchive:([element objectForKey:WebElementDOMNodeKey] != nil)];
2348     } else if ([element objectForKey:WebElementLinkURLKey] != nil) {
2349         return [NSPasteboard _web_writableTypesForURL];
2350     } else if ([[element objectForKey:WebElementIsSelectedKey] boolValue]) {
2351         return [self pasteboardTypesForSelection];
2352     }
2353     return [NSArray array];
2354 }
2355
2356 - (void)writeElement:(NSDictionary *)element withPasteboardTypes:(NSArray *)types toPasteboard:(NSPasteboard *)pasteboard
2357 {
2358     if ([element objectForKey:WebElementImageURLKey] != nil) {
2359         [self _writeImageForElement:element withPasteboardTypes:types toPasteboard:pasteboard];
2360     } else if ([element objectForKey:WebElementLinkURLKey] != nil) {
2361         [self _writeLinkElement:element withPasteboardTypes:types toPasteboard:pasteboard];
2362     } else if ([[element objectForKey:WebElementIsSelectedKey] boolValue]) {
2363         [self writeSelectionWithPasteboardTypes:types toPasteboard:pasteboard];
2364     }
2365 }
2366
2367 - (void)moveDragCaretToPoint:(NSPoint)point
2368 {
2369     [[[self mainFrame] _bridge] moveDragCaretToPoint:[self convertPoint:point toView:[[[self mainFrame] frameView] documentView]]];
2370 }
2371
2372 - (void)removeDragCaret
2373 {
2374     if (!_private->page)
2375         return;
2376     _private->page->dragCaretController()->clear();
2377 }
2378
2379 - (void)setMainFrameURL:(NSString *)URLString
2380 {
2381     [[self mainFrame] loadRequest: [NSURLRequest requestWithURL: [NSURL _web_URLWithDataAsString: URLString]]];
2382 }
2383
2384 - (NSString *)mainFrameURL
2385 {
2386     WebDataSource *ds;
2387     ds = [[self mainFrame] provisionalDataSource];
2388     if (!ds)
2389         ds = [[self mainFrame] dataSource];
2390     return [[[ds request] URL] _web_originalDataAsString];
2391 }
2392
2393 - (BOOL)isLoading
2394 {
2395     LOG (Bindings, "isLoading = %d", (int)[self _isLoading]);
2396     return [self _isLoading];
2397 }
2398
2399 - (NSString *)mainFrameTitle
2400 {
2401     NSString *mainFrameTitle = [[[self mainFrame] dataSource] pageTitle];
2402     return (mainFrameTitle != nil) ? mainFrameTitle : (NSString *)@"";
2403 }
2404
2405 - (NSImage *)mainFrameIcon
2406 {
2407     return [[WebIconDatabase sharedIconDatabase] iconForURL:[[[[self mainFrame] dataSource] _URL] _web_originalDataAsString] withSize:WebIconSmallSize];
2408 }
2409
2410 - (DOMDocument *)mainFrameDocument
2411 {
2412     // only return the actual value if the state we're in gives NSTreeController
2413     // enough time to release its observers on the old model
2414     if (_private->mainFrameDocumentReady)
2415         return [[self mainFrame] DOMDocument];
2416     return nil;
2417 }
2418
2419 - (void)setDrawsBackground:(BOOL)drawsBackground
2420 {
2421     if (_private->drawsBackground == drawsBackground)
2422         return;
2423     _private->drawsBackground = drawsBackground;
2424     [[self mainFrame] _updateBackground];
2425 }
2426
2427 - (BOOL)drawsBackground
2428 {
2429     return _private->drawsBackground;
2430 }
2431
2432 - (void)_inspectElement:(id)sender
2433 {
2434     NSDictionary *element = [sender representedObject];
2435     WebFrame *frame = [element objectForKey:WebElementFrameKey];
2436     DOMNode *node = [element objectForKey:WebElementDOMNodeKey];
2437     if (!node || !frame)
2438         return;
2439
2440     if ([node nodeType] != DOM_ELEMENT_NODE || [node nodeType] != DOM_DOCUMENT_NODE)
2441         node = [node parentNode];
2442
2443     WebInspector *inspector = [WebInspector sharedWebInspector];
2444     [inspector setWebFrame:frame];
2445     [inspector setFocusedDOMNode:node];
2446
2447     node = [node parentNode];
2448     node = [node parentNode];
2449     if (node) // set the root node to something relatively close to the focused node
2450         [inspector setRootDOMNode:node];
2451
2452     [inspector showWindow:nil];
2453 }
2454
2455 @end
2456
2457 @implementation WebView (WebIBActions)
2458
2459 - (IBAction)takeStringURLFrom: sender
2460 {
2461     NSString *URLString = [sender stringValue];
2462     
2463     [[self mainFrame] loadRequest: [NSURLRequest requestWithURL: [NSURL _web_URLWithDataAsString: URLString]]];
2464 }
2465
2466 - (BOOL)canGoBack
2467 {
2468     if (!_private->page)
2469         return NO;
2470
2471     return !!_private->page->backForwardList()->backItem();
2472 }
2473
2474 - (BOOL)canGoForward
2475 {
2476     if (!_private->page)
2477         return NO;
2478
2479     return !!_private->page->backForwardList()->forwardItem();
2480 }
2481
2482 - (IBAction)goBack:(id)sender
2483 {
2484     [self goBack];
2485 }
2486
2487 - (IBAction)goForward:(id)sender
2488 {
2489     [self goForward];
2490 }
2491
2492 - (IBAction)stopLoading:(id)sender
2493 {
2494     [[self mainFrame] stopLoading];
2495 }
2496
2497 - (IBAction)reload:(id)sender
2498 {
2499     [[self mainFrame] reload];
2500 }
2501
2502 #define MinimumTextSizeMultiplier       0.5f
2503 #define MaximumTextSizeMultiplier       3.0f
2504 #define TextSizeMultiplierRatio         1.2f
2505
2506 - (BOOL)canMakeTextSmaller
2507 {
2508     BOOL canShrinkMore = _private->textSizeMultiplier/TextSizeMultiplierRatio > MinimumTextSizeMultiplier;
2509     return [self _performTextSizingSelector:(SEL)0 withObject:nil onTrackingDocs:canShrinkMore selForNonTrackingDocs:@selector(_canMakeTextSmaller) newScaleFactor:0];
2510 }
2511
2512 - (BOOL)canMakeTextLarger
2513 {
2514     BOOL canGrowMore = _private->textSizeMultiplier*TextSizeMultiplierRatio < MaximumTextSizeMultiplier;
2515     return [self _performTextSizingSelector:(SEL)0 withObject:nil onTrackingDocs:canGrowMore selForNonTrackingDocs:@selector(_canMakeTextLarger) newScaleFactor:0];
2516 }
2517
2518 - (IBAction)makeTextSmaller:(id)sender
2519 {
2520     float newScale = _private->textSizeMultiplier / TextSizeMultiplierRatio;
2521     BOOL canShrinkMore = newScale > MinimumTextSizeMultiplier;
2522     [self _performTextSizingSelector:@selector(_makeTextSmaller:) withObject:sender onTrackingDocs:canShrinkMore selForNonTrackingDocs:@selector(_canMakeTextSmaller) newScaleFactor:newScale];
2523 }
2524
2525 - (IBAction)makeTextLarger:(id)sender
2526 {
2527     float newScale = _private->textSizeMultiplier*TextSizeMultiplierRatio;
2528     BOOL canGrowMore = newScale < MaximumTextSizeMultiplier;
2529     [self _performTextSizingSelector:@selector(_makeTextLarger:) withObject:sender onTrackingDocs:canGrowMore selForNonTrackingDocs:@selector(_canMakeTextLarger) newScaleFactor:newScale];
2530 }
2531
2532 - (IBAction)toggleSmartInsertDelete:(id)sender
2533 {
2534     [self setSmartInsertDeleteEnabled:![self smartInsertDeleteEnabled]];
2535 }
2536
2537 - (IBAction)toggleContinuousSpellChecking:(id)sender
2538 {
2539     [self setContinuousSpellCheckingEnabled:![self isContinuousSpellCheckingEnabled]];
2540 }
2541
2542 - (BOOL)_responderValidateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
2543 {
2544     id responder = [self _responderForResponderOperations];
2545     if (responder != self && [responder respondsToSelector:[item action]]) {
2546         if ([responder respondsToSelector:@selector(validateUserInterfaceItem:)]) {
2547             return [responder validateUserInterfaceItem:item];
2548         }
2549         return YES;
2550     }
2551     return NO;
2552 }
2553
2554 - (BOOL)canMakeTextStandardSize
2555 {
2556     BOOL notAlreadyStandard = _private->textSizeMultiplier != 1.0f;
2557     return [self _performTextSizingSelector:(SEL)0 withObject:nil onTrackingDocs:notAlreadyStandard selForNonTrackingDocs:@selector(_canMakeTextStandardSize) newScaleFactor:0.0f];
2558 }
2559
2560 - (IBAction)makeTextStandardSize:(id)sender
2561 {
2562     BOOL notAlreadyStandard = _private->textSizeMultiplier != 1.0f;
2563     [self _performTextSizingSelector:@selector(_makeTextStandardSize:) withObject:sender onTrackingDocs:notAlreadyStandard selForNonTrackingDocs:@selector(_canMakeTextStandardSize) newScaleFactor:1.0f];
2564 }
2565
2566 #define VALIDATE(name) \
2567     else if (action == @selector(name:)) { return [self _responderValidateUserInterfaceItem:item]; }
2568
2569 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
2570 {
2571     SEL action = [item action];
2572
2573     if (action == @selector(goBack:)) {
2574         return [self canGoBack];
2575     } else if (action == @selector(goForward:)) {
2576         return [self canGoForward];
2577     } else if (action == @selector(makeTextLarger:)) {
2578         return [self canMakeTextLarger];
2579     } else if (action == @selector(makeTextSmaller:)) {
2580         return [self canMakeTextSmaller];
2581     } else if (action == @selector(makeTextStandardSize:)) {
2582         return [self canMakeTextStandardSize];
2583     } else if (action == @selector(reload:)) {
2584         return [[self mainFrame] dataSource] != nil;
2585     } else if (action == @selector(stopLoading:)) {
2586         return [self _isLoading];
2587     } else if (action == @selector(toggleContinuousSpellChecking:)) {
2588         BOOL checkMark = NO;
2589         BOOL retVal = NO;
2590         if ([self _continuousCheckingAllowed]) {
2591             checkMark = [self isContinuousSpellCheckingEnabled];
2592             retVal = YES;
2593         }
2594         if ([(NSObject *)item isKindOfClass:[NSMenuItem class]]) {
2595             NSMenuItem *menuItem = (NSMenuItem *)item;
2596             [menuItem setState:checkMark ? NSOnState : NSOffState];
2597         }
2598         return retVal;
2599 #ifndef BUILDING_ON_TIGER
2600     } else if (action == @selector(toggleGrammarChecking:)) {
2601         BOOL checkMark = [self isGrammarCheckingEnabled];
2602         if ([(NSObject *)item isKindOfClass:[NSMenuItem class]]) {
2603             NSMenuItem *menuItem = (NSMenuItem *)item;
2604             [menuItem setState:checkMark ? NSOnState : NSOffState];
2605         }
2606         return YES;
2607 #endif
2608     }
2609     FOR_EACH_RESPONDER_SELECTOR(VALIDATE)
2610
2611     return YES;
2612 }
2613
2614 @end
2615
2616 @implementation WebView (WebPendingPublic)
2617
2618 - (BOOL)searchFor:(NSString *)string direction:(BOOL)forward caseSensitive:(BOOL)caseFlag wrap:(BOOL)wrapFlag startInSelection:(BOOL)startInSelection
2619 {
2620     if (_private->closed)
2621         return NO;
2622     
2623     // Get the frame holding the selection, or start with the main frame
2624     WebFrame *startFrame = [self _selectedOrMainFrame];
2625     
2626     // Search the first frame, then all the other frames, in order
2627     NSView <WebDocumentSearching> *startSearchView = nil;
2628     WebFrame *frame = startFrame;
2629     do {
2630         WebFrame *nextFrame = incrementFrame(frame, forward, wrapFlag);
2631         
2632         BOOL onlyOneFrame = (frame == nextFrame);
2633         ASSERT(!onlyOneFrame || frame == startFrame);
2634         
2635         id <WebDocumentView> view = [[frame frameView] documentView];
2636         if ([view conformsToProtocol:@protocol(WebDocumentSearching)]) {
2637             NSView <WebDocumentSearching> *searchView = (NSView <WebDocumentSearching> *)view;
2638             
2639             if (frame == startFrame)
2640                 startSearchView = searchView;
2641             
2642             BOOL foundString;
2643             // In some cases we have to search some content twice; see comment later in this method.
2644             // We can avoid ever doing this in the common one-frame case by passing YES for wrapFlag 
2645             // here, and then bailing out before we get to the code that would search again in the
2646             // same content.
2647             BOOL wrapOnThisPass = wrapFlag && onlyOneFrame;
2648             if ([searchView conformsToProtocol:@protocol(WebDocumentIncrementalSearching)])
2649                 foundString = [(NSView <WebDocumentIncrementalSearching> *)searchView searchFor:string direction:forward caseSensitive:caseFlag wrap:wrapOnThisPass startInSelection:startInSelection];
2650             else
2651                 foundString = [searchView searchFor:string direction:forward caseSensitive:caseFlag wrap:wrapOnThisPass];
2652             
2653             if (foundString) {
2654                 if (frame != startFrame)
2655                     [startFrame _clearSelection];
2656                 [[self window] makeFirstResponder:searchView];
2657                 return YES;
2658             }
2659             
2660             if (onlyOneFrame)
2661                 return NO;
2662         }
2663         frame = nextFrame;
2664     } while (frame && frame != startFrame);
2665     
2666     // If there are multiple frames and wrapFlag is true and we've visited each one without finding a result, we still need to search in the 
2667     // first-searched frame up to the selection. However, the API doesn't provide a way to search only up to a particular point. The only 
2668     // way to make sure the entire frame is searched is to pass YES for the wrapFlag. When there are no matches, this will search again
2669     // some content that we already searched on the first pass. In the worst case, we could search the entire contents of this frame twice.
2670     // To fix this, we'd need to add a mechanism to specify a range in which to search.
2671     if (wrapFlag && startSearchView) {
2672         BOOL foundString;
2673         if ([startSearchView conformsToProtocol:@protocol(WebDocumentIncrementalSearching)])
2674             foundString = [(NSView <WebDocumentIncrementalSearching> *)startSearchView searchFor:string direction:forward caseSensitive:caseFlag wrap:YES startInSelection:startInSelection];
2675         else
2676             foundString = [startSearchView searchFor:string direction:forward caseSensitive:caseFlag wrap:YES];
2677         if (foundString) {
2678             [[self window] makeFirstResponder:startSearchView];
2679             return YES;
2680         }
2681     }
2682     return NO;
2683 }
2684
2685 - (void)setHoverFeedbackSuspended:(BOOL)newValue
2686 {
2687     if (_private->hoverFeedbackSuspended == newValue)
2688         return;
2689     
2690     _private->hoverFeedbackSuspended = newValue;
2691     id <WebDocumentView> documentView = [[[self mainFrame] frameView] documentView];
2692     // FIXME: in a perfect world we'd do this in a general way that worked with any document view,
2693     // such as by calling a protocol method or using respondsToSelector or sending a notification.
2694     // But until there is any need for these more general solutions, we'll just hardwire it to work
2695     // with WebHTMLView.
2696     // Note that _hoverFeedbackSuspendedChanged needs to be called only on the main WebHTMLView, not
2697     // on each subframe separately.
2698     if ([documentView isKindOfClass:[WebHTMLView class]])
2699         [(WebHTMLView *)documentView _hoverFeedbackSuspendedChanged];
2700 }
2701
2702 - (BOOL)isHoverFeedbackSuspended
2703 {
2704     return _private->hoverFeedbackSuspended;
2705 }
2706
2707 - (void)setMainFrameDocumentReady:(BOOL)mainFrameDocumentReady
2708 {
2709     // by setting this to NO, calls to mainFrameDocument are forced to return nil
2710     // setting this to YES lets it return the actual DOMDocument value
2711     // we use this to tell NSTreeController to reset its observers and clear its state
2712     if (_private->mainFrameDocumentReady == mainFrameDocumentReady)
2713         return;
2714     [self _willChangeValueForKey:_WebMainFrameDocumentKey];
2715     _private->mainFrameDocumentReady = mainFrameDocumentReady;
2716     [self _didChangeValueForKey:_WebMainFrameDocumentKey];
2717     // this will cause observers to call mainFrameDocument where this flag will be checked
2718 }
2719
2720 // This method name is used by Mail on Tiger (but not post-Tiger), so we shouldn't delete it 
2721 // until the day comes when we're no longer supporting Mail on Tiger.
2722 - (WebFrame *)_frameForCurrentSelection
2723 {
2724     return [self _selectedOrMainFrame];
2725 }
2726
2727 - (void)setTabKeyCyclesThroughElements:(BOOL)cyclesElements
2728 {
2729     _private->tabKeyCyclesThroughElementsChanged = YES;
2730     if (_private->page)
2731         _private->page->setTabKeyCyclesThroughElements(cyclesElements);
2732 }
2733
2734 - (BOOL)tabKeyCyclesThroughElements
2735 {
2736     return _private->page && _private->page->tabKeyCyclesThroughElements();
2737 }
2738
2739 - (void)setScriptDebugDelegate:(id)delegate
2740 {
2741     _private->scriptDebugDelegate = delegate;
2742     [_private->scriptDebugDelegateForwarder release];
2743     _private->scriptDebugDelegateForwarder = nil;
2744     if (delegate)
2745         [self _attachScriptDebuggerToAllFrames];
2746     else
2747         [self _detachScriptDebuggerFromAllFrames];
2748 }
2749
2750 - (id)scriptDebugDelegate
2751 {
2752     return _private->scriptDebugDelegate;
2753 }
2754
2755 - (BOOL)shouldClose
2756 {
2757     Frame* coreFrame = core([self mainFrame]);
2758     if (!coreFrame)
2759         return YES;
2760     return coreFrame->shouldClose();
2761 }
2762
2763 - (NSAppleEventDescriptor *)aeDescByEvaluatingJavaScriptFromString:(NSString *)script
2764 {
2765     return [[[self mainFrame] _bridge] aeDescByEvaluatingJavaScriptFromString:script];
2766 }
2767
2768 - (unsigned)markAllMatchesForText:(NSString *)string caseSensitive:(BOOL)caseFlag highlight:(BOOL)highlight limit:(unsigned)limit
2769 {
2770     WebFrame *frame = [self mainFrame];
2771     unsigned matchCount = 0;
2772     do {
2773         id <WebDocumentView> view = [[frame frameView] documentView];
2774         // FIXME: introduce a protocol, or otherwise make this work with other types
2775         if ([view isKindOfClass:[WebHTMLView class]])
2776             [(WebHTMLView *)view setMarkedTextMatchesAreHighlighted:highlight];
2777         
2778         ASSERT(limit == 0 || matchCount < limit);
2779         matchCount += [(WebHTMLView *)view markAllMatchesForText:string caseSensitive:caseFlag limit:limit == 0 ? 0 : limit - matchCount];
2780
2781         // Stop looking if we've reached the limit. A limit of 0 means no limit.
2782         if (limit > 0 && matchCount >= limit)
2783             break;
2784         
2785         frame = incrementFrame(frame, YES, NO);
2786     } while (frame);
2787     
2788     return matchCount;
2789 }
2790
2791 - (void)unmarkAllTextMatches
2792 {
2793     WebFrame *frame = [self mainFrame];
2794     do {
2795         id <WebDocumentView> view = [[frame frameView] documentView];
2796         // FIXME: introduce a protocol, or otherwise make this work with other types
2797         if ([view isKindOfClass:[WebHTMLView class]])
2798             [(WebHTMLView *)view unmarkAllTextMatches];
2799         
2800         frame = incrementFrame(frame, YES, NO);
2801     } while (frame);
2802 }
2803
2804 - (NSArray *)rectsForTextMatches
2805 {
2806     NSMutableArray *result = [NSMutableArray array];
2807     WebFrame *frame = [self mainFrame];
2808     do {
2809         id <WebDocumentView> view = [[frame frameView] documentView];
2810         // FIXME: introduce a protocol, or otherwise make this work with other types
2811         if ([view isKindOfClass:[WebHTMLView class]]) {
2812             WebHTMLView *htmlView = (WebHTMLView *)view;
2813             NSArray *originalRects = [htmlView rectsForTextMatches];
2814             unsigned rectCount = [originalRects count];
2815             unsigned rectIndex;
2816             NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2817             for (rectIndex = 0; rectIndex < rectCount; ++rectIndex) {
2818                 NSRect r = [[originalRects objectAtIndex:rectIndex] rectValue];
2819                 if (NSIsEmptyRect(r))
2820                     continue;
2821                 
2822                 // Convert rect to our coordinate system
2823                 r = [htmlView convertRect:r toView:self];
2824                 [result addObject:[NSValue valueWithRect:r]];
2825                 if (rectIndex % 10 == 0)
2826                     [pool drain];
2827             }
2828             [pool release];
2829         }
2830         
2831         frame = incrementFrame(frame, YES, NO);
2832     } while (frame);
2833     
2834     return result;
2835 }
2836
2837 - (void)scrollDOMRangeToVisible:(DOMRange *)range
2838 {
2839     [[[range startContainer] _bridge] scrollDOMRangeToVisible:range];
2840 }
2841
2842 - (BOOL)allowsUndo
2843 {
2844     return _private->allowsUndo;
2845 }
2846
2847 - (void)setAllowsUndo:(BOOL)flag
2848 {
2849     _private->allowsUndo = flag;
2850 }
2851
2852 + (void)registerURLSchemeAsLocal:(NSString *)protocol
2853 {
2854     FrameLoader::registerURLSchemeAsLocal(protocol);
2855 }
2856
2857 @end
2858
2859 @implementation WebView (WebViewPrintingPrivate)
2860
2861 - (float)_headerHeight
2862 {
2863     if ([[self UIDelegate] respondsToSelector:@selector(webViewHeaderHeight:)]) {
2864         return [[self UIDelegate] webViewHeaderHeight:self];
2865     }
2866     
2867 #ifdef DEBUG_HEADER_AND_FOOTER
2868     return 25;
2869 #else
2870     return 0;
2871 #endif
2872 }
2873
2874 - (float)_footerHeight
2875 {
2876     if ([[self UIDelegate] respondsToSelector:@selector(webViewFooterHeight:)]) {
2877         return [[self UIDelegate] webViewFooterHeight:self];
2878     }
2879     
2880 #ifdef DEBUG_HEADER_AND_FOOTER
2881     return 50;
2882 #else
2883     return 0;
2884 #endif
2885 }
2886
2887 - (void)_drawHeaderInRect:(NSRect)rect
2888 {
2889 #ifdef DEBUG_HEADER_AND_FOOTER
2890     NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
2891     [currentContext saveGraphicsState];
2892     [[NSColor yellowColor] set];
2893     NSRectFill(rect);
2894     [currentContext restoreGraphicsState];
2895 #endif
2896     
2897     if ([[self UIDelegate] respondsToSelector:@selector(webView:drawHeaderInRect:)]) {
2898         NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
2899         [currentContext saveGraphicsState];
2900         NSRectClip(rect);
2901         [[self UIDelegate] webView:self drawHeaderInRect:rect]; 
2902         [currentContext restoreGraphicsState];
2903     }
2904 }
2905
2906 - (void)_drawFooterInRect:(NSRect)rect
2907 {
2908 #ifdef DEBUG_HEADER_AND_FOOTER
2909     NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
2910     [currentContext saveGraphicsState];
2911     [[NSColor cyanColor] set];
2912     NSRectFill(rect);
2913     [currentContext restoreGraphicsState];
2914 #endif
2915     
2916     if ([[self UIDelegate] respondsToSelector:@selector(webView:drawFooterInRect:)]) {
2917         NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
2918         [currentContext saveGraphicsState];
2919         NSRectClip(rect);
2920         [[self UIDelegate] webView:self drawFooterInRect:rect];
2921         [currentContext restoreGraphicsState];
2922     }
2923 }
2924
2925 - (void)_adjustPrintingMarginsForHeaderAndFooter
2926 {
2927     NSPrintOperation *op = [NSPrintOperation currentOperation];
2928     NSPrintInfo *info = [op printInfo];
2929     NSMutableDictionary *infoDictionary = [info dictionary];
2930     
2931     // We need to modify the top and bottom margins in the NSPrintInfo to account for the space needed by the
2932     // header and footer. Because this method can be called more than once on the same NSPrintInfo (see 5038087),
2933     // we stash away the unmodified top and bottom margins the first time this method is called, and we read from
2934     // those stashed-away values on subsequent calls.
2935     float originalTopMargin;
2936     float originalBottomMargin;
2937     NSNumber *originalTopMarginNumber = [infoDictionary objectForKey:WebKitOriginalTopPrintingMarginKey];
2938     if (!originalTopMarginNumber) {
2939         ASSERT(![infoDictionary objectForKey:WebKitOriginalBottomPrintingMarginKey]);
2940         originalTopMargin = [info topMargin];
2941         originalBottomMargin = [info bottomMargin];
2942         [infoDictionary setObject:[NSNumber numberWithFloat:originalTopMargin] forKey:WebKitOriginalTopPrintingMarginKey];
2943         [infoDictionary setObject:[NSNumber numberWithFloat:originalBottomMargin] forKey:WebKitOriginalBottomPrintingMarginKey];
2944     } else {
2945         ASSERT([originalTopMarginNumber isKindOfClass:[NSNumber class]]);
2946         ASSERT([[infoDictionary objectForKey:WebKitOriginalBottomPrintingMarginKey] isKindOfClass:[NSNumber class]]);
2947         originalTopMargin = [originalTopMarginNumber floatValue];
2948         originalBottomMargin = [[infoDictionary objectForKey:WebKitOriginalBottomPrintingMarginKey] floatValue];
2949     }
2950     
2951     float scale = [op _web_pageSetupScaleFactor];
2952     [info setTopMargin:originalTopMargin + [self _headerHeight] * scale];
2953     [info setBottomMargin:originalBottomMargin + [self _footerHeight] * scale];
2954 }
2955
2956 - (void)_drawHeaderAndFooter
2957 {
2958     // The header and footer rect height scales with the page, but the width is always
2959     // all the way across the printed page (inset by printing margins).
2960     NSPrintOperation *op = [NSPrintOperation currentOperation];
2961     float scale = [op _web_pageSetupScaleFactor];
2962     NSPrintInfo *printInfo = [op printInfo];
2963     NSSize paperSize = [printInfo paperSize];
2964     float headerFooterLeft = [printInfo leftMargin]/scale;
2965     float headerFooterWidth = (paperSize.width - ([printInfo leftMargin] + [printInfo rightMargin]))/scale;
2966     NSRect footerRect = NSMakeRect(headerFooterLeft, [printInfo bottomMargin]/scale - [self _footerHeight] , 
2967                                    headerFooterWidth, [self _footerHeight]);
2968     NSRect headerRect = NSMakeRect(headerFooterLeft, (paperSize.height - [printInfo topMargin])/scale, 
2969                                    headerFooterWidth, [self _headerHeight]);
2970     
2971     [self _drawHeaderInRect:headerRect];
2972     [self _drawFooterInRect:footerRect];
2973 }
2974 @end
2975
2976 @implementation WebView (WebDebugBinding)
2977
2978 - (void)addObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
2979 {
2980     LOG (Bindings, "addObserver:%p forKeyPath:%@ options:%x context:%p", anObserver, keyPath, options, context);
2981     [super addObserver:anObserver forKeyPath:keyPath options:options context:context];
2982 }
2983
2984 - (void)removeObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath
2985 {
2986     LOG (Bindings, "removeObserver:%p forKeyPath:%@", anObserver, keyPath);
2987     [super removeObserver:anObserver forKeyPath:keyPath];
2988 }
2989
2990 @end
2991
2992 //==========================================================================================
2993 // Editing
2994
2995 @implementation WebView (WebViewCSS)
2996
2997 - (DOMCSSStyleDeclaration *)computedStyleForElement:(DOMElement *)element pseudoElement:(NSString *)pseudoElement
2998 {
2999     // FIXME: is this the best level for this conversion?
3000     if (pseudoElement == nil) {
3001         pseudoElement = @"";
3002     }
3003     return [[element ownerDocument] getComputedStyle:element pseudoElement:pseudoElement];
3004 }
3005
3006 @end
3007
3008 @implementation WebView (WebViewEditing)
3009
3010 - (DOMRange *)editableDOMRangeForPoint:(NSPoint)point
3011 {
3012     WebFrameBridge *bridge = [self _bridgeAtPoint:point];
3013     return [bridge editableDOMRangeForPoint:[self convertPoint:point toView:[[[bridge webFrame] frameView] documentView]]];
3014 }
3015
3016 - (BOOL)_shouldChangeSelectedDOMRange:(DOMRange *)currentRange toDOMRange:(DOMRange *)proposedRange affinity:(NSSelectionAffinity)selectionAffinity stillSelecting:(BOOL)flag;
3017 {
3018     // FIXME: This quirk is needed due to <rdar://problem/4985321> - We can phase it out once Aperture can adopt the new behavior on their end
3019     if (!WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITHOUT_APERTURE_QUIRK) && [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.apple.Aperture"])
3020         return YES;
3021         
3022     return [[self _editingDelegateForwarder] webView:self shouldChangeSelectedDOMRange:currentRange toDOMRange:proposedRange affinity:selectionAffinity stillSelecting:flag];
3023 }
3024
3025 - (BOOL)maintainsInactiveSelection
3026 {
3027     return NO;
3028 }
3029
3030 - (void)setSelectedDOMRange:(DOMRange *)range affinity:(NSSelectionAffinity)selectionAffinity
3031 {
3032     Frame* coreFrame = core([self _selectedOrMainFrame]);
3033     if (!coreFrame)
3034         return;
3035
3036     if (range == nil)
3037         coreFrame->selectionController()->clear();
3038     else {
3039         // Derive the frame to use from the range passed in.
3040         // Using _bridgeForSelectedOrMainFrame could give us a different document than
3041         // the one the range uses.
3042         coreFrame = core([range startContainer])->document()->frame();
3043         if (!coreFrame)
3044             return;
3045
3046         ExceptionCode ec = 0;
3047         coreFrame->selectionController()->setSelectedRange([range _range], core(selectionAffinity), true, ec);
3048     }
3049 }
3050
3051 - (DOMRange *)selectedDOMRange
3052 {
3053     Frame* coreFrame = core([self _selectedOrMainFrame]);
3054     if (!coreFrame)
3055         return nil;
3056     return kit(coreFrame->selectionController()->toRange().get());
3057 }
3058
3059 - (NSSelectionAffinity)selectionAffinity
3060 {
3061     Frame* coreFrame = core([self _selectedOrMainFrame]);
3062     if (!coreFrame)
3063         return NSSelectionAffinityDownstream;
3064     return kit(coreFrame->selectionController()->affinity());
3065 }
3066
3067 - (void)setEditable:(BOOL)flag
3068 {
3069     if (_private->editable != flag) {
3070         _private->editable = flag;
3071         if (!_private->tabKeyCyclesThroughElementsChanged && _private->page)
3072             _private->page->setTabKeyCyclesThroughElements(!flag);
3073         Frame* mainFrame = [[[self mainFrame] _bridge] _frame];
3074         if (mainFrame) {
3075             if (flag) {
3076                 mainFrame->applyEditingStyleToBodyElement();
3077                 // If the WebView is made editable and the selection is empty, set it to something.
3078                 if (![self selectedDOMRange])
3079                     mainFrame->setSelectionFromNone();
3080             } else
3081                 mainFrame->removeEditingStyleFromBodyElement();
3082         }
3083     }
3084 }
3085
3086 - (BOOL)isEditable
3087 {
3088     return _private->editable;
3089 }
3090
3091 - (void)setTypingStyle:(DOMCSSStyleDeclaration *)style
3092 {
3093     // We don't know enough at thls level to pass in a relevant WebUndoAction; we'd have to
3094     // change the API to allow this.
3095     [[self _bridgeForSelectedOrMainFrame] setTypingStyle:style withUndoAction:EditActionUnspecified];
3096 }
3097
3098 - (DOMCSSStyleDeclaration *)typingStyle
3099 {
3100     return [[self _bridgeForSelectedOrMainFrame] typingStyle];
3101 }
3102
3103 - (void)setSmartInsertDeleteEnabled:(BOOL)flag
3104 {
3105     _private->smartInsertDeleteEnabled = flag;
3106 }
3107
3108 - (BOOL)smartInsertDeleteEnabled
3109 {
3110     return _private->smartInsertDeleteEnabled;
3111 }
3112
3113 - (void)setContinuousSpellCheckingEnabled:(BOOL)flag
3114 {
3115     if (continuousSpellCheckingEnabled != flag) {
3116         continuousSpellCheckingEnabled = flag;
3117         [[NSUserDefaults standardUserDefaults] setBool:continuousSpellCheckingEnabled forKey:WebContinuousSpellCheckingEnabled];
3118     }
3119     
3120     if ([self isContinuousSpellCheckingEnabled]) {
3121         [[self class] _preflightSpellChecker];
3122     } else {
3123         [[self mainFrame] _unmarkAllMisspellings];
3124     }
3125 }
3126
3127 - (BOOL)isContinuousSpellCheckingEnabled
3128 {
3129     return (continuousSpellCheckingEnabled && [self _continuousCheckingAllowed]);
3130 }
3131
3132 - (WebNSInteger)spellCheckerDocumentTag
3133 {
3134     if (!_private->hasSpellCheckerDocumentTag) {
3135         _private->spellCheckerDocumentTag = [NSSpellChecker uniqueSpellDocumentTag];
3136         _private->hasSpellCheckerDocumentTag = YES;
3137     }
3138     return _private->spellCheckerDocumentTag;
3139 }
3140
3141 - (NSUndoManager *)undoManager
3142 {
3143     if (!_private->allowsUndo)
3144         return nil;
3145
3146     NSUndoManager *undoManager = [[self _editingDelegateForwarder] undoManagerForWebView:self];
3147     if (undoManager)
3148         return undoManager;
3149
3150     return [super undoManager];
3151 }
3152
3153 - (void)registerForEditingDelegateNotification:(NSString *)name selector:(SEL)selector
3154 {
3155     NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
3156     if ([_private->editingDelegate respondsToSelector:selector])
3157         [defaultCenter addObserver:_private->editingDelegate selector:selector name:name object:self];
3158 }
3159
3160 - (void)setEditingDelegate:(id)delegate
3161 {
3162     if (_private->editingDelegate == delegate)
3163         return;
3164
3165     NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
3166
3167     // remove notifications from current delegate
3168     [defaultCenter removeObserver:_private->editingDelegate name:WebViewDidBeginEditingNotification object:self];
3169     [defaultCenter removeObserver:_private->editingDelegate name:WebViewDidChangeNotification object:self];
3170     [defaultCenter removeObserver:_private->editingDelegate name:WebViewDidEndEditingNotification object:self];
3171     [defaultCenter removeObserver:_private->editingDelegate name:WebViewDidChangeTypingStyleNotification object:self];
3172     [defaultCenter removeObserver:_private->editingDelegate name:WebViewDidChangeSelectionNotification object:self];
3173     
3174     _private->editingDelegate = delegate;
3175     [_private->editingDelegateForwarder release];
3176     _private->editingDelegateForwarder = nil;
3177     
3178     // add notifications for new delegate
3179     [self registerForEditingDelegateNotification:WebViewDidBeginEditingNotification selector:@selector(webViewDidBeginEditing:)];
3180     [self registerForEditingDelegateNotification:WebViewDidChangeNotification selector:@selector(webViewDidChange:)];
3181     [self registerForEditingDelegateNotification:WebViewDidEndEditingNotification selector:@selector(webViewDidEndEditing:)];
3182     [self registerForEditingDelegateNotification:WebViewDidChangeTypingStyleNotification selector:@selector(webViewDidChangeTypingStyle:)];
3183     [self registerForEditingDelegateNotification:WebViewDidChangeSelectionNotification selector:@selector(webViewDidChangeSelection:)];
3184 }
3185
3186 - (id)editingDelegate
3187 {
3188     return _private->editingDelegate;
3189 }
3190
3191 - (DOMCSSStyleDeclaration *)styleDeclarationWithText:(NSString *)text
3192 {
3193     // FIXME: Should this really be attached to the document with the current selection?
3194     DOMCSSStyleDeclaration *decl = [[[self _selectedOrMainFrame] DOMDocument] createCSSStyleDeclaration];
3195     [decl setCssText:text];
3196     return decl;
3197 }
3198
3199 @end
3200
3201 @implementation WebView (WebViewGrammarChecking)
3202
3203 // FIXME: This method should be merged into WebViewEditing when we're not in API freeze
3204 - (BOOL)isGrammarCheckingEnabled
3205 {
3206 #ifdef BUILDING_ON_TIGER
3207     return NO;
3208 #else
3209     return grammarCheckingEnabled;
3210 #endif
3211 }
3212
3213 #ifndef BUILDING_ON_TIGER
3214 // FIXME: This method should be merged into WebViewEditing when we're not in API freeze
3215 - (void)setGrammarCheckingEnabled:(BOOL)flag
3216 {
3217     if (grammarCheckingEnabled == flag)
3218         return;
3219     
3220     grammarCheckingEnabled = flag;
3221     [[NSUserDefaults standardUserDefaults] setBool:grammarCheckingEnabled forKey:WebGrammarCheckingEnabled];    
3222     
3223     // FIXME 4811447: workaround for lack of API
3224     NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
3225     if ([spellChecker respondsToSelector:@selector(_updateGrammar)])
3226         [spellChecker performSelector:@selector(_updateGrammar)];
3227     
3228     // We call _preflightSpellChecker when turning continuous spell checking on, but we don't need to do that here
3229     // because grammar checking only occurs on code paths that already preflight spell checking appropriately.
3230     
3231     if (![self isGrammarCheckingEnabled])
3232         [[self mainFrame] _unmarkAllBadGrammar];
3233 }
3234
3235 // FIXME: This method should be merged into WebIBActions when we're not in API freeze
3236 - (void)toggleGrammarChecking:(id)sender
3237 {
3238     [self setGrammarCheckingEnabled:![self isGrammarCheckingEnabled]];
3239 }
3240 #endif
3241
3242 @end
3243
3244 @implementation WebView (WebViewUndoableEditing)
3245
3246 - (void)replaceSelectionWithNode:(DOMNode *)node
3247 {
3248     [[self _bridgeForSelectedOrMainFrame] replaceSelectionWithNode:node selectReplacement:YES smartReplace:NO matchStyle:NO];
3249 }    
3250
3251 - (void)replaceSelectionWithText:(NSString *)text
3252 {
3253     [[self _bridgeForSelectedOrMainFrame] replaceSelectionWithText:text selectReplacement:YES smartReplace:NO];
3254 }
3255
3256 - (void)replaceSelectionWithMarkupString:(NSString *)markupString
3257 {
3258     [[self _bridgeForSelectedOrMainFrame] replaceSelectionWithMarkupString:markupString baseURLString:nil selectReplacement:YES smartReplace:NO];
3259 }
3260
3261 - (void)replaceSelectionWithArchive:(WebArchive *)archive
3262 {
3263     [[[[self _bridgeForSelectedOrMainFrame] webFrame] dataSource] _replaceSelectionWithArchive:archive selectReplacement:YES];
3264 }
3265
3266 - (void)deleteSelection
3267 {
3268     WebFrame *webFrame = [self _selectedOrMainFrame];
3269     Frame* coreFrame = core(webFrame);
3270     if (coreFrame)
3271         coreFrame->editor()->deleteSelectionWithSmartDelete([(WebHTMLView *)[[webFrame frameView] documentView] _canSmartCopyOrDelete]);
3272 }
3273     
3274 - (void)applyStyle:(DOMCSSStyleDeclaration *)style
3275 {
3276     // We don't know enough at thls level to pass in a relevant WebUndoAction; we'd have to
3277     // change the API to allow this.
3278     WebFrame *webFrame = [self _selectedOrMainFrame];
3279     Frame* coreFrame = core(webFrame);
3280     if (coreFrame)
3281         coreFrame->editor()->applyStyle(core(style));
3282 }
3283
3284 @end
3285
3286 @implementation WebView (WebViewEditingActions)
3287
3288 - (void)_performResponderOperation:(SEL)selector with:(id)parameter
3289 {
3290     static BOOL reentered = NO;
3291     if (reentered) {
3292         [[self nextResponder] tryToPerform:selector with:parameter];
3293         return;
3294     }
3295
3296     // There are two possibilities here.
3297     //
3298     // One is that WebView has been called in its role as part of the responder chain.
3299     // In that case, it's fine to call the first responder and end up calling down the
3300     // responder chain again. Later we will return here with reentered = YES and continue
3301     // past the WebView.
3302     //
3303     // The other is that we are being called directly, in which case we want to pass the
3304     // selector down to the view inside us that can handle it, and continue down the
3305     // responder chain as usual.
3306
3307     // Pass this selector down to the first responder.
3308     NSResponder *responder = [self _responderForResponderOperations];
3309     reentered = YES;
3310     [responder tryToPerform:selector with:parameter];
3311     reentered = NO;
3312 }
3313
3314 #define FORWARD(name) \
3315     - (void)name:(id)sender { [self _performResponderOperation:_cmd with:sender]; }
3316
3317 FOR_EACH_RESPONDER_SELECTOR(FORWARD)
3318
3319 - (void)insertText:(NSString *)text
3320 {
3321     [self _performResponderOperation:_cmd with:text];
3322 }
3323
3324 @end
3325
3326 @implementation WebView (WebViewEditingInMail)
3327
3328 - (void)_insertNewlineInQuotedContent;
3329 {
3330     [[self _bridgeForSelectedOrMainFrame] insertParagraphSeparatorInQuotedContent];
3331 }
3332
3333 - (BOOL)_selectWordBeforeMenuEvent
3334 {
3335     return _private->selectWordBeforeMenuEvent;
3336 }
3337
3338 - (void)_setSelectWordBeforeMenuEvent:(BOOL)flag
3339 {
3340     _private->selectWordBeforeMenuEvent = flag;
3341 }
3342
3343 - (void)_replaceSelectionWithNode:(DOMNode *)node matchStyle:(BOOL)matchStyle
3344 {
3345     [[self _bridgeForSelectedOrMainFrame] replaceSelectionWithNode:node selectReplacement:YES smartReplace:NO matchStyle:matchStyle];
3346 }
3347
3348 @end
3349
3350 static WebFrameView *containingFrameView(NSView *view)
3351 {
3352     while (view && ![view isKindOfClass:[WebFrameView class]])
3353         view = [view superview];
3354     return (WebFrameView *)view;    
3355 }
3356
3357 @implementation WebView (WebFileInternal)
3358
3359 - (WebFrame *)_focusedFrame
3360 {
3361     NSResponder *resp = [[self window] firstResponder];
3362     if (resp && [resp isKindOfClass:[NSView class]] && [(NSView *)resp isDescendantOf:[[self mainFrame] frameView]]) {
3363         WebFrameView *frameView = containingFrameView((NSView *)resp);
3364         ASSERT(frameView != nil);
3365         return [frameView webFrame];
3366     }
3367     
3368     return nil;
3369 }
3370
3371 - (WebFrame *)_selectedOrMainFrame
3372 {
3373     WebFrame *result = [self selectedFrame];
3374     if (result == nil)
3375         result = [self mainFrame];
3376     return result;
3377 }
3378
3379 - (WebFrameBridge *)_bridgeForSelectedOrMainFrame
3380 {
3381     return [[self _selectedOrMainFrame] _bridge];
3382 }
3383
3384 - (BOOL)_isLoading
3385 {
3386     WebFrame *mainFrame = [self mainFrame];
3387     return [[mainFrame dataSource] isLoading]
3388         || [[mainFrame provisionalDataSource] isLoading];
3389 }
3390
3391 - (WebFrameView *)_frameViewAtWindowPoint:(NSPoint)point
3392 {
3393     if (_private->closed)
3394         return nil;
3395     NSView *view = [self hitTest:[[self superview] convertPoint:point fromView:nil]];
3396     if (![view isDescendantOf:[[self mainFrame] frameView]])
3397         return nil;
3398     WebFrameView *frameView = containingFrameView(view);
3399     ASSERT(frameView);
3400     return frameView;
3401 }
3402
3403 - (WebFrameBridge *)_bridgeAtPoint:(NSPoint)point
3404 {
3405     return [[[self _frameViewAtWindowPoint:[self convertPoint:point toView:nil]] webFrame] _bridge];
3406 }
3407
3408 + (void)_preflightSpellCheckerNow:(id)sender
3409 {
3410     [[NSSpellChecker sharedSpellChecker] _preflightChosenSpellServer];
3411 }
3412
3413 + (void)_preflightSpellChecker
3414 {
3415     // As AppKit does, we wish to delay tickling the shared spellchecker into existence on application launch.
3416     if ([NSSpellChecker sharedSpellCheckerExists]) {
3417         [self _preflightSpellCheckerNow:self];
3418     } else {
3419         [self performSelector:@selector(_preflightSpellCheckerNow:) withObject:self afterDelay:2.0];
3420     }
3421 }
3422
3423 - (BOOL)_continuousCheckingAllowed
3424 {
3425     static BOOL allowContinuousSpellChecking = YES;
3426     static BOOL readAllowContinuousSpellCheckingDefault = NO;
3427     if (!readAllowContinuousSpellCheckingDefault) {
3428         if ([[NSUserDefaults standardUserDefaults] objectForKey:@"NSAllowContinuousSpellChecking"]) {
3429             allowContinuousSpellChecking = [[NSUserDefaults standardUserDefaults] boolForKey:@"NSAllowContinuousSpellChecking"];
3430         }
3431         readAllowContinuousSpellCheckingDefault = YES;
3432     }
3433     return allowContinuousSpellChecking;
3434 }
3435
3436 - (NSResponder *)_responderForResponderOperations
3437 {
3438     NSResponder *responder = [[self window] firstResponder];
3439     WebFrameView *mainFrameView = [[self mainFrame] frameView];
3440     
3441     // If the current responder is outside of the webview, use our main frameView or its
3442     // document view. We also do this for subviews of self that are siblings of the main
3443     // frameView since clients might insert non-webview-related views there (see 4552713).
3444     if (responder != self && ![mainFrameView _web_firstResponderIsSelfOrDescendantView]) {
3445         responder = [mainFrameView documentView];
3446         if (!responder)
3447             responder = mainFrameView;
3448     }
3449     return responder;
3450 }
3451
3452 - (void)_searchWithGoogleFromMenu:(id)sender
3453 {
3454     id documentView = [[[self selectedFrame] frameView] documentView];
3455     if (![documentView conformsToProtocol:@protocol(WebDocumentText)]) {
3456         return;
3457     }
3458     
3459     NSString *selectedString = [(id <WebDocumentText>)documentView selectedString];
3460     if ([selectedString length] == 0) {
3461         return;
3462     }
3463     
3464     NSPasteboard *pasteboard = [NSPasteboard pasteboardWithUniqueName];
3465     [pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
3466     NSMutableString *s = [selectedString mutableCopy];
3467     const unichar nonBreakingSpaceCharacter = 0xA0;
3468     NSString *nonBreakingSpaceString = [NSString stringWithCharacters:&nonBreakingSpaceCharacter length:1];
3469     [s replaceOccurrencesOfString:nonBreakingSpaceString withString:@" " options:0 range:NSMakeRange(0, [s length])];
3470     [pasteboard setString:s forType:NSStringPboardType];
3471     [s release];
3472     
3473     // FIXME: seems fragile to use the service by name, but this is what AppKit does
3474     NSPerformService(@"Search With Google", pasteboard);
3475 }
3476
3477 - (void)_searchWithSpotlightFromMenu:(id)sender
3478 {
3479     id documentView = [[[self selectedFrame] frameView] documentView];
3480     if (![documentView conformsToProtocol:@protocol(WebDocumentText)]) {
3481         return;
3482     }
3483     
3484     NSString *selectedString = [(id <WebDocumentText>)documentView selectedString];
3485     if ([selectedString length] == 0) {
3486         return;
3487     }
3488
3489     (void)HISearchWindowShow((CFStringRef)selectedString, kNilOptions);
3490 }
3491
3492 // Slightly funky method that lets us have one copy of the logic for finding docViews that can do
3493 // text sizing.  It returns whether it found any "suitable" doc views.  It sends sel to any suitable
3494 // doc views, or if sel==0 we do nothing to them.  For doc views that track our size factor, they are
3495 // suitable if doTrackingViews==YES (which in practice means that our size factor isn't at its max or
3496 // min).  For doc views that don't track it, we send them testSel to determine suitablility.  If we
3497 // do find any suitable tracking doc views and newScaleFactor!=0, we will set the common scale factor
3498 // to that new factor before we send sel to any of them. 
3499 - (BOOL)_performTextSizingSelector:(SEL)sel withObject:(id)arg onTrackingDocs:(BOOL)doTrackingViews selForNonTrackingDocs:(SEL)testSel newScaleFactor:(float)newScaleFactor
3500 {
3501     if ([[self mainFrame] dataSource] == nil)
3502         return NO;
3503     
3504     BOOL foundSome = NO;
3505     NSArray *docViews = [[self mainFrame] _documentViews];
3506     for (int i = [docViews count]-1; i >= 0; i--) {
3507         id docView = [docViews objectAtIndex:i];
3508         if ([docView conformsToProtocol:@protocol(_WebDocumentTextSizing)]) {
3509             id <_WebDocumentTextSizing> sizingDocView = (id <_WebDocumentTextSizing>)docView;
3510             BOOL isSuitable;
3511             if ([sizingDocView _tracksCommonSizeFactor]) {
3512                 isSuitable = doTrackingViews;
3513                 if (isSuitable && newScaleFactor != 0)
3514                     _private->textSizeMultiplier = newScaleFactor;
3515             } else {
3516                 // Incantation to perform a selector returning a BOOL.
3517                 isSuitable = ((BOOL(*)(id, SEL))objc_msgSend)(sizingDocView, testSel);
3518             }
3519             
3520             if (isSuitable) {
3521                 if (sel != 0) {
3522                     foundSome = YES;
3523                     [sizingDocView performSelector:sel withObject:arg];
3524                 } else {
3525                     // if we're just called for the benefit of the return value, we can return at first match
3526                     return YES;
3527                 }
3528             }
3529         }
3530     }
3531     
3532     return foundSome;
3533 }
3534
3535 - (void)_notifyTextSizeMultiplierChanged
3536 {
3537     if ([[self mainFrame] dataSource] == nil)
3538         return;
3539
3540     NSArray *docViews = [[self mainFrame] _documentViews];
3541     for (int i = [docViews count]-1; i >= 0; i--) {
3542         id docView = [docViews objectAtIndex:i];
3543         if ([docView conformsToProtocol:@protocol(_WebDocumentTextSizing)] == NO)
3544             continue;
3545
3546         id <_WebDocumentTextSizing> sizingDocView = (id <_WebDocumentTextSizing>)docView;
3547         if ([sizingDocView _tracksCommonSizeFactor])
3548             [sizingDocView _textSizeMultiplierChanged];
3549     }
3550
3551 }
3552
3553 @end
3554
3555 @implementation WebView (WebViewInternal)
3556
3557 - (NSString *)_userVisibleBundleVersionFromFullVersion:(NSString *)fullVersion
3558 {
3559     // If the version is 4 digits long or longer, then the first digit represents
3560     // the version of the OS. Our user agent string should not include this first digit,
3561     // so strip it off and report the rest as the version. <rdar://problem/4997547>
3562     NSRange nonDigitRange = [fullVersion rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
3563     if (nonDigitRange.location == NSNotFound && [fullVersion length] >= 4)
3564         return [fullVersion substringFromIndex:1];
3565     if (nonDigitRange.location != NSNotFound && nonDigitRange.location >= 4)
3566         return [fullVersion substringFromIndex:1];
3567     return fullVersion;
3568 }
3569
3570 - (NSString *)_userAgentWithApplicationName:(NSString *)applicationName andWebKitVersion:(NSString *)version
3571 {
3572     NSString *language = [NSUserDefaults _webkit_preferredLanguageCode];
3573     if ([applicationName length])
3574         return [NSString stringWithFormat:@"Mozilla/5.0 (Macintosh; U; " PROCESSOR " Mac OS X; %@) AppleWebKit/%@ (KHTML, like Gecko) %@", language, version, applicationName];
3575     return [NSString stringWithFormat:@"Mozilla/5.0 (Macintosh; U; " PROCESSOR " Mac OS X; %@) AppleWebKit/%@ (KHTML, like Gecko)", language, version];
3576 }
3577
3578 // Get the appropriate user-agent string for a particular URL.
3579 - (WebCore::String)_userAgentForURL:(const WebCore::KURL&)url
3580 {
3581     if (_private->useSiteSpecificSpoofing) {
3582         // FIXME: Make this a hash table lookup if more domains need spoofing.
3583         // FIXME: Remove yahoo.com once <rdar://problem/5057117> is fixed.
3584         if (url.host().endsWith("yahoo.com")) {
3585             static String yahooUserAgent([self _userAgentWithApplicationName:_private->applicationNameForUserAgent andWebKitVersion:@"422"]);
3586             return yahooUserAgent;
3587         }
3588         
3589         // FIXME: Remove flickr.com workaround once <rdar://problem/5084872> is fixed
3590         if (url.host().endsWith("flickr.com")) {
3591             // Safari 2.0.4's user agent string works here
3592             static String safari204UserAgent([self _userAgentWithApplicationName:@"Safari/419.3" andWebKitVersion:@"419"]);
3593             return safari204UserAgent;
3594         }
3595     }
3596     
3597     if (_private->userAgent->isNull()) {
3598         NSString *sourceVersion = [[NSBundle bundleForClass:[WebView class]] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
3599         sourceVersion = [self _userVisibleBundleVersionFromFullVersion:sourceVersion];
3600         *_private->userAgent = [self _userAgentWithApplicationName:_private->applicationNameForUserAgent andWebKitVersion:sourceVersion];
3601     }
3602
3603     return *_private->userAgent;
3604 }
3605
3606 - (void)_addObject:(id)object forIdentifier:(unsigned long)identifier
3607 {
3608     ASSERT(!_private->identifierMap->contains(identifier));
3609
3610     // If the identifier map is initially empty it means we're starting a load
3611     // of something. The semantic is that the web view should be around as long 
3612     // as something is loading. Because of that we retain the web view.
3613     if (_private->identifierMap->isEmpty())
3614         CFRetain(self);
3615     
3616     _private->identifierMap->set(identifier, object);
3617 }
3618
3619 - (id)_objectForIdentifier:(unsigned long)identifier
3620 {
3621     return _private->identifierMap->get(identifier).get();
3622 }
3623
3624 - (void)_removeObjectForIdentifier:(unsigned long)identifier
3625 {
3626     HashMap<unsigned long, RetainPtr<id> >::iterator it = _private->identifierMap->find(identifier);
3627     
3628     // FIXME: This is currently needed because of a bug that causes didFail to be sent twice 
3629     // sometimes, see <rdar://problem/5009627> for more information.
3630     if (it == _private->identifierMap->end())
3631         return;
3632     
3633     _private->identifierMap->remove(it);
3634     
3635     // If the identifier map is now empty it means we're no longer loading anything
3636     // and we should release the web view.
3637     if (_private->identifierMap->isEmpty())
3638         CFRelease(self);
3639 }
3640
3641 @end