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