0fb8047e46d6a4a8fe7c15e711439627921430f9
[WebKit-https.git] / Source / WebKit / win / WebView.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "WebView.h"
28
29 #include "CFDictionaryPropertyBag.h"
30 #include "DOMCoreClasses.h"
31 #include "FullscreenVideoController.h"
32 #include "MarshallingHelpers.h"
33 #include "SoftLinking.h"
34 #include "WebBackForwardList.h"
35 #include "WebChromeClient.h"
36 #include "WebContextMenuClient.h"
37 #include "WebCoreTextRenderer.h"
38 #include "WebDatabaseManager.h"
39 #include "WebDocumentLoader.h"
40 #include "WebDownload.h"
41 #include "WebDragClient.h"
42 #include "WebEditorClient.h"
43 #include "WebElementPropertyBag.h"
44 #include "WebFrame.h"
45 #include "WebGeolocationClient.h"
46 #include "WebGeolocationPosition.h"
47 #include "WebIconDatabase.h"
48 #include "WebInspector.h"
49 #include "WebInspectorClient.h"
50 #include "WebKit.h"
51 #include "WebKitDLL.h"
52 #include "WebKitLogging.h"
53 #include "WebKitStatisticsPrivate.h"
54 #include "WebKitSystemBits.h"
55 #include "WebMutableURLRequest.h"
56 #include "WebNotificationCenter.h"
57 #include "WebPlatformStrategies.h"
58 #include "WebPluginHalterClient.h"
59 #include "WebPreferences.h"
60 #include "WebScriptWorld.h"
61 #include "WindowsTouch.h"
62 #include "resource.h"
63 #include <JavaScriptCore/APICast.h>
64 #include <JavaScriptCore/InitializeThreading.h>
65 #include <JavaScriptCore/JSLock.h>
66 #include <JavaScriptCore/JSValue.h>
67 #include <WebCore/AbstractDatabase.h>
68 #include <WebCore/AXObjectCache.h>
69 #include <WebCore/ApplicationCacheStorage.h>
70 #include <WebCore/BString.h>
71 #include <WebCore/BackForwardListImpl.h>
72 #include <WebCore/BitmapInfo.h>
73 #include <WebCore/MemoryCache.h>
74 #include <WebCore/Chrome.h>
75 #include <WebCore/ContextMenu.h>
76 #include <WebCore/ContextMenuController.h>
77 #include <WebCore/Cursor.h>
78 #include <WebCore/Document.h>
79 #include <WebCore/DocumentMarkerController.h>
80 #include <WebCore/DragController.h>
81 #include <WebCore/DragData.h>
82 #include <WebCore/Editor.h>
83 #include <WebCore/EventHandler.h>
84 #include <WebCore/EventNames.h>
85 #include <WebCore/FileSystem.h>
86 #include <WebCore/FloatQuad.h>
87 #include <WebCore/FocusController.h>
88 #include <WebCore/FrameLoader.h>
89 #include <WebCore/FrameTree.h>
90 #include <WebCore/FrameView.h>
91 #include <WebCore/FrameWin.h>
92 #include <WebCore/GDIObjectCounter.h>
93 #include <WebCore/GraphicsContext.h>
94 #include <WebCore/HTMLMediaElement.h>
95 #include <WebCore/HTMLNames.h>
96 #include <WebCore/HistoryItem.h>
97 #include <WebCore/HitTestRequest.h>
98 #include <WebCore/HitTestResult.h>
99 #include <WebCore/IntRect.h>
100 #include <WebCore/JSElement.h>
101 #include <WebCore/KeyboardEvent.h>
102 #include <WebCore/Logging.h>
103 #include <WebCore/MIMETypeRegistry.h>
104 #include <WebCore/Page.h>
105 #include <WebCore/PageCache.h>
106 #include <WebCore/PageGroup.h>
107 #include <WebCore/PlatformKeyboardEvent.h>
108 #include <WebCore/PlatformMouseEvent.h>
109 #include <WebCore/PlatformWheelEvent.h>
110 #include <WebCore/PluginData.h>
111 #include <WebCore/PluginDatabase.h>
112 #include <WebCore/PluginView.h>
113 #include <WebCore/PopupMenu.h>
114 #include <WebCore/PopupMenuWin.h>
115 #include <WebCore/ProgressTracker.h>
116 #include <WebCore/RenderLayer.h>
117 #include <WebCore/RenderTheme.h>
118 #include <WebCore/RenderTreeAsText.h>
119 #include <WebCore/RenderView.h>
120 #include <WebCore/RenderWidget.h>
121 #include <WebCore/ResourceHandle.h>
122 #include <WebCore/ResourceHandleClient.h>
123 #include <WebCore/SchemeRegistry.h>
124 #include <WebCore/ScriptValue.h>
125 #include <WebCore/Scrollbar.h>
126 #include <WebCore/ScrollbarTheme.h>
127 #include <WebCore/SecurityOrigin.h>
128 #include <WebCore/SelectionController.h>
129 #include <WebCore/Settings.h>
130 #include <WebCore/SimpleFontData.h>
131 #include <WebCore/TypingCommand.h>
132 #include <WebCore/WindowMessageBroadcaster.h>
133 #include <wtf/Threading.h>
134
135 #if ENABLE(CLIENT_BASED_GEOLOCATION)
136 #include <WebCore/GeolocationController.h>
137 #include <WebCore/GeolocationError.h>
138 #endif
139
140 #if PLATFORM(CG)
141 #include <CoreGraphics/CGContext.h>
142 #endif
143
144 #if USE(CF)
145 #include <CoreFoundation/CoreFoundation.h>
146 #endif
147
148 #if USE(CFNETWORK)
149 #include <CFNetwork/CFURLCachePriv.h>
150 #include <CFNetwork/CFURLProtocolPriv.h>
151 #include <WebCore/CookieStorageCFNet.h>
152 #include <WebKitSystemInterface/WebKitSystemInterface.h> 
153 #endif
154
155 #if USE(ACCELERATED_COMPOSITING)
156 #include <WebCore/CACFLayerTreeHost.h>
157 #include <WebCore/PlatformCALayer.h>
158 #endif
159
160 #include <ShlObj.h>
161 #include <comutil.h>
162 #include <dimm.h>
163 #include <oleacc.h>
164 #include <tchar.h>
165 #include <windowsx.h>
166 #include <wtf/HashSet.h>
167 #include <wtf/text/CString.h>
168 #include <wtf/text/StringConcatenate.h>
169
170 // Soft link functions for gestures and panning feedback
171 SOFT_LINK_LIBRARY(USER32);
172 SOFT_LINK_OPTIONAL(USER32, GetGestureInfo, BOOL, WINAPI, (HGESTUREINFO, PGESTUREINFO));
173 SOFT_LINK_OPTIONAL(USER32, SetGestureConfig, BOOL, WINAPI, (HWND, DWORD, UINT, PGESTURECONFIG, UINT));
174 SOFT_LINK_OPTIONAL(USER32, CloseGestureInfoHandle, BOOL, WINAPI, (HGESTUREINFO));
175 SOFT_LINK_LIBRARY(Uxtheme);
176 SOFT_LINK_OPTIONAL(Uxtheme, BeginPanningFeedback, BOOL, WINAPI, (HWND));
177 SOFT_LINK_OPTIONAL(Uxtheme, EndPanningFeedback, BOOL, WINAPI, (HWND, BOOL));
178 SOFT_LINK_OPTIONAL(Uxtheme, UpdatePanningFeedback, BOOL, WINAPI, (HWND, LONG, LONG, BOOL));
179
180 using namespace WebCore;
181 using namespace std;
182 using JSC::JSLock;
183
184 static HMODULE accessibilityLib;
185 static HashSet<WebView*> pendingDeleteBackingStoreSet;
186
187 static String osVersion();
188 static String webKitVersion();
189
190 WebView* kit(Page* page)
191 {
192     return page ? static_cast<WebChromeClient*>(page->chrome()->client())->webView() : 0;
193 }
194
195 class PreferencesChangedOrRemovedObserver : public IWebNotificationObserver {
196 public:
197     static PreferencesChangedOrRemovedObserver* sharedInstance();
198
199 private:
200     PreferencesChangedOrRemovedObserver() {}
201     ~PreferencesChangedOrRemovedObserver() {}
202
203     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void**) { return E_FAIL; }
204     virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 0; }
205     virtual ULONG STDMETHODCALLTYPE Release(void) { return 0; }
206
207 public:
208     // IWebNotificationObserver
209     virtual HRESULT STDMETHODCALLTYPE onNotify( 
210         /* [in] */ IWebNotification* notification);
211
212 private:
213     HRESULT notifyPreferencesChanged(WebCacheModel);
214     HRESULT notifyPreferencesRemoved(WebCacheModel);
215 };
216
217 PreferencesChangedOrRemovedObserver* PreferencesChangedOrRemovedObserver::sharedInstance()
218 {
219     static PreferencesChangedOrRemovedObserver* shared = new PreferencesChangedOrRemovedObserver;
220     return shared;
221 }
222
223 HRESULT PreferencesChangedOrRemovedObserver::onNotify(IWebNotification* notification)
224 {
225     HRESULT hr = S_OK;
226
227     COMPtr<IUnknown> unkPrefs;
228     hr = notification->getObject(&unkPrefs);
229     if (FAILED(hr))
230         return hr;
231
232     COMPtr<IWebPreferences> preferences(Query, unkPrefs);
233     if (!preferences)
234         return E_NOINTERFACE;
235
236     WebCacheModel cacheModel;
237     hr = preferences->cacheModel(&cacheModel);
238     if (FAILED(hr))
239         return hr;
240
241     BSTR nameBSTR;
242     hr = notification->name(&nameBSTR);
243     if (FAILED(hr))
244         return hr;
245     BString name;
246     name.adoptBSTR(nameBSTR);
247
248     if (wcscmp(name, WebPreferences::webPreferencesChangedNotification()) == 0)
249         return notifyPreferencesChanged(cacheModel);
250
251     if (wcscmp(name, WebPreferences::webPreferencesRemovedNotification()) == 0)
252         return notifyPreferencesRemoved(cacheModel);
253
254     ASSERT_NOT_REACHED();
255     return E_FAIL;
256 }
257
258 HRESULT PreferencesChangedOrRemovedObserver::notifyPreferencesChanged(WebCacheModel cacheModel)
259 {
260     HRESULT hr = S_OK;
261
262     if (!WebView::didSetCacheModel() || cacheModel > WebView::cacheModel())
263         WebView::setCacheModel(cacheModel);
264     else if (cacheModel < WebView::cacheModel()) {
265         WebCacheModel sharedPreferencesCacheModel;
266         hr = WebPreferences::sharedStandardPreferences()->cacheModel(&sharedPreferencesCacheModel);
267         if (FAILED(hr))
268             return hr;
269         WebView::setCacheModel(max(sharedPreferencesCacheModel, WebView::maxCacheModelInAnyInstance()));
270     }
271
272     return hr;
273 }
274
275 HRESULT PreferencesChangedOrRemovedObserver::notifyPreferencesRemoved(WebCacheModel cacheModel)
276 {
277     HRESULT hr = S_OK;
278
279     if (cacheModel == WebView::cacheModel()) {
280         WebCacheModel sharedPreferencesCacheModel;
281         hr = WebPreferences::sharedStandardPreferences()->cacheModel(&sharedPreferencesCacheModel);
282         if (FAILED(hr))
283             return hr;
284         WebView::setCacheModel(max(sharedPreferencesCacheModel, WebView::maxCacheModelInAnyInstance()));
285     }
286
287     return hr;
288 }
289
290
291 const LPCWSTR kWebViewWindowClassName = L"WebViewWindowClass";
292
293 const int WM_XP_THEMECHANGED = 0x031A;
294 const int WM_VISTA_MOUSEHWHEEL = 0x020E;
295
296 static const int maxToolTipWidth = 250;
297
298 static const int delayBeforeDeletingBackingStoreMsec = 5000;
299
300 static ATOM registerWebView();
301
302 static void initializeStaticObservers();
303
304 static HRESULT updateSharedSettingsFromPreferencesIfNeeded(IWebPreferences*);
305
306 HRESULT createMatchEnumerator(Vector<IntRect>* rects, IEnumTextMatches** matches);
307
308 static bool continuousSpellCheckingEnabled;
309 static bool grammarCheckingEnabled;
310
311 static bool s_didSetCacheModel;
312 static WebCacheModel s_cacheModel = WebCacheModelDocumentViewer;
313
314 enum {
315     UpdateActiveStateTimer = 1,
316     DeleteBackingStoreTimer = 2,
317 };
318
319 // WebView ----------------------------------------------------------------
320
321 bool WebView::s_allowSiteSpecificHacks = false;
322
323 WebView::WebView()
324     : m_refCount(0)
325 #if !ASSERT_DISABLED
326     , m_deletionHasBegun(false)
327 #endif
328     , m_hostWindow(0)
329     , m_viewWindow(0)
330     , m_mainFrame(0)
331     , m_page(0)
332     , m_hasCustomDropTarget(false)
333     , m_useBackForwardList(true)
334     , m_userAgentOverridden(false)
335     , m_zoomMultiplier(1.0f)
336     , m_zoomsTextOnly(false)
337     , m_mouseActivated(false)
338     , m_dragData(0)
339     , m_currentCharacterCode(0)
340     , m_isBeingDestroyed(false)
341     , m_paintCount(0)
342     , m_hasSpellCheckerDocumentTag(false)
343     , m_smartInsertDeleteEnabled(false)
344     , m_didClose(false)
345     , m_inIMEComposition(0)
346     , m_toolTipHwnd(0)
347     , m_closeWindowTimer(0)
348     , m_topLevelParent(0)
349     , m_deleteBackingStoreTimerActive(false)
350     , m_transparent(false)
351     , m_selectTrailingWhitespaceEnabled(false)
352     , m_lastPanX(0)
353     , m_lastPanY(0)
354     , m_xOverpan(0)
355     , m_yOverpan(0)
356 #if USE(ACCELERATED_COMPOSITING)
357     , m_isAcceleratedCompositing(false)
358 #endif
359     , m_nextDisplayIsSynchronous(false)
360     , m_lastSetCursor(0)
361 {
362     JSC::initializeThreading();
363     WTF::initializeMainThread();
364
365     m_backingStoreSize.cx = m_backingStoreSize.cy = 0;
366
367     CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, IID_IDropTargetHelper,(void**)&m_dropTargetHelper);
368
369     initializeStaticObservers();
370
371     WebPreferences* sharedPreferences = WebPreferences::sharedStandardPreferences();
372     BOOL enabled;
373     if (SUCCEEDED(sharedPreferences->continuousSpellCheckingEnabled(&enabled)))
374         continuousSpellCheckingEnabled = !!enabled;
375     if (SUCCEEDED(sharedPreferences->grammarCheckingEnabled(&enabled)))
376         grammarCheckingEnabled = !!enabled;
377
378     WebViewCount++;
379     gClassCount++;
380     gClassNameCount.add("WebView");
381 }
382
383 WebView::~WebView()
384 {
385     deleteBackingStore();
386
387     // the tooltip window needs to be explicitly destroyed since it isn't a WS_CHILD
388     if (::IsWindow(m_toolTipHwnd))
389         ::DestroyWindow(m_toolTipHwnd);
390
391     ASSERT(!m_page);
392     ASSERT(!m_preferences);
393     ASSERT(!m_viewWindow);
394
395 #if USE(ACCELERATED_COMPOSITING)
396     ASSERT(!m_layerTreeHost);
397     ASSERT(!m_backingLayer);
398 #endif
399
400     WebViewCount--;
401     gClassCount--;
402     gClassNameCount.remove("WebView");
403 }
404
405 WebView* WebView::createInstance()
406 {
407     WebView* instance = new WebView();
408     instance->AddRef();
409     return instance;
410 }
411
412 void initializeStaticObservers()
413 {
414     static bool initialized;
415     if (initialized)
416         return;
417     initialized = true;
418
419     IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
420     notifyCenter->addObserver(PreferencesChangedOrRemovedObserver::sharedInstance(), WebPreferences::webPreferencesChangedNotification(), 0);
421     notifyCenter->addObserver(PreferencesChangedOrRemovedObserver::sharedInstance(), WebPreferences::webPreferencesRemovedNotification(), 0);
422 }
423
424 static HashSet<WebView*>& allWebViewsSet()
425 {
426     static HashSet<WebView*> allWebViewsSet;
427     return allWebViewsSet;
428 }
429
430 void WebView::addToAllWebViewsSet()
431 {
432     allWebViewsSet().add(this);
433 }
434
435 void WebView::removeFromAllWebViewsSet()
436 {
437     allWebViewsSet().remove(this);
438 }
439
440 void WebView::setCacheModel(WebCacheModel cacheModel)
441 {
442 #if USE(CFNETWORK)
443     if (s_didSetCacheModel && cacheModel == s_cacheModel)
444         return;
445
446     RetainPtr<CFURLCacheRef> cfurlCache(AdoptCF, CFURLCacheCopySharedURLCache());
447     RetainPtr<CFStringRef> cfurlCacheDirectory(AdoptCF, wkCopyFoundationCacheDirectory());
448     if (!cfurlCacheDirectory)
449         cfurlCacheDirectory.adoptCF(WebCore::localUserSpecificStorageDirectory().createCFString());
450
451     // As a fudge factor, use 1000 instead of 1024, in case the reported byte 
452     // count doesn't align exactly to a megabyte boundary.
453     unsigned long long memSize = WebMemorySize() / 1024 / 1000;
454     unsigned long long diskFreeSize = WebVolumeFreeSize(cfurlCacheDirectory.get()) / 1024 / 1000;
455
456     unsigned cacheTotalCapacity = 0;
457     unsigned cacheMinDeadCapacity = 0;
458     unsigned cacheMaxDeadCapacity = 0;
459     double deadDecodedDataDeletionInterval = 0;
460
461     unsigned pageCacheCapacity = 0;
462
463     CFIndex cfurlCacheMemoryCapacity = 0;
464     CFIndex cfurlCacheDiskCapacity = 0;
465
466     switch (cacheModel) {
467     case WebCacheModelDocumentViewer: {
468         // Page cache capacity (in pages)
469         pageCacheCapacity = 0;
470
471         // Object cache capacities (in bytes)
472         if (memSize >= 2048)
473             cacheTotalCapacity = 96 * 1024 * 1024;
474         else if (memSize >= 1536)
475             cacheTotalCapacity = 64 * 1024 * 1024;
476         else if (memSize >= 1024)
477             cacheTotalCapacity = 32 * 1024 * 1024;
478         else if (memSize >= 512)
479             cacheTotalCapacity = 16 * 1024 * 1024; 
480
481         cacheMinDeadCapacity = 0;
482         cacheMaxDeadCapacity = 0;
483
484         // Foundation memory cache capacity (in bytes)
485         cfurlCacheMemoryCapacity = 0;
486
487         // Foundation disk cache capacity (in bytes)
488         cfurlCacheDiskCapacity = CFURLCacheDiskCapacity(cfurlCache.get());
489
490         break;
491     }
492     case WebCacheModelDocumentBrowser: {
493         // Page cache capacity (in pages)
494         if (memSize >= 1024)
495             pageCacheCapacity = 3;
496         else if (memSize >= 512)
497             pageCacheCapacity = 2;
498         else if (memSize >= 256)
499             pageCacheCapacity = 1;
500         else
501             pageCacheCapacity = 0;
502
503         // Object cache capacities (in bytes)
504         if (memSize >= 2048)
505             cacheTotalCapacity = 96 * 1024 * 1024;
506         else if (memSize >= 1536)
507             cacheTotalCapacity = 64 * 1024 * 1024;
508         else if (memSize >= 1024)
509             cacheTotalCapacity = 32 * 1024 * 1024;
510         else if (memSize >= 512)
511             cacheTotalCapacity = 16 * 1024 * 1024; 
512
513         cacheMinDeadCapacity = cacheTotalCapacity / 8;
514         cacheMaxDeadCapacity = cacheTotalCapacity / 4;
515
516         // Foundation memory cache capacity (in bytes)
517         if (memSize >= 2048)
518             cfurlCacheMemoryCapacity = 4 * 1024 * 1024;
519         else if (memSize >= 1024)
520             cfurlCacheMemoryCapacity = 2 * 1024 * 1024;
521         else if (memSize >= 512)
522             cfurlCacheMemoryCapacity = 1 * 1024 * 1024;
523         else
524             cfurlCacheMemoryCapacity =      512 * 1024; 
525
526         // Foundation disk cache capacity (in bytes)
527         if (diskFreeSize >= 16384)
528             cfurlCacheDiskCapacity = 50 * 1024 * 1024;
529         else if (diskFreeSize >= 8192)
530             cfurlCacheDiskCapacity = 40 * 1024 * 1024;
531         else if (diskFreeSize >= 4096)
532             cfurlCacheDiskCapacity = 30 * 1024 * 1024;
533         else
534             cfurlCacheDiskCapacity = 20 * 1024 * 1024;
535
536         break;
537     }
538     case WebCacheModelPrimaryWebBrowser: {
539         // Page cache capacity (in pages)
540         // (Research indicates that value / page drops substantially after 3 pages.)
541         if (memSize >= 2048)
542             pageCacheCapacity = 5;
543         else if (memSize >= 1024)
544             pageCacheCapacity = 4;
545         else if (memSize >= 512)
546             pageCacheCapacity = 3;
547         else if (memSize >= 256)
548             pageCacheCapacity = 2;
549         else
550             pageCacheCapacity = 1;
551
552         // Object cache capacities (in bytes)
553         // (Testing indicates that value / MB depends heavily on content and
554         // browsing pattern. Even growth above 128MB can have substantial 
555         // value / MB for some content / browsing patterns.)
556         if (memSize >= 2048)
557             cacheTotalCapacity = 128 * 1024 * 1024;
558         else if (memSize >= 1536)
559             cacheTotalCapacity = 96 * 1024 * 1024;
560         else if (memSize >= 1024)
561             cacheTotalCapacity = 64 * 1024 * 1024;
562         else if (memSize >= 512)
563             cacheTotalCapacity = 32 * 1024 * 1024; 
564
565         cacheMinDeadCapacity = cacheTotalCapacity / 4;
566         cacheMaxDeadCapacity = cacheTotalCapacity / 2;
567
568         // This code is here to avoid a PLT regression. We can remove it if we
569         // can prove that the overall system gain would justify the regression.
570         cacheMaxDeadCapacity = max(24u, cacheMaxDeadCapacity);
571
572         deadDecodedDataDeletionInterval = 60;
573
574         // Foundation memory cache capacity (in bytes)
575         // (These values are small because WebCore does most caching itself.)
576         if (memSize >= 1024)
577             cfurlCacheMemoryCapacity = 4 * 1024 * 1024;
578         else if (memSize >= 512)
579             cfurlCacheMemoryCapacity = 2 * 1024 * 1024;
580         else if (memSize >= 256)
581             cfurlCacheMemoryCapacity = 1 * 1024 * 1024;
582         else
583             cfurlCacheMemoryCapacity =      512 * 1024; 
584
585         // Foundation disk cache capacity (in bytes)
586         if (diskFreeSize >= 16384)
587             cfurlCacheDiskCapacity = 175 * 1024 * 1024;
588         else if (diskFreeSize >= 8192)
589             cfurlCacheDiskCapacity = 150 * 1024 * 1024;
590         else if (diskFreeSize >= 4096)
591             cfurlCacheDiskCapacity = 125 * 1024 * 1024;
592         else if (diskFreeSize >= 2048)
593             cfurlCacheDiskCapacity = 100 * 1024 * 1024;
594         else if (diskFreeSize >= 1024)
595             cfurlCacheDiskCapacity = 75 * 1024 * 1024;
596         else
597             cfurlCacheDiskCapacity = 50 * 1024 * 1024;
598
599         break;
600     }
601     default:
602         ASSERT_NOT_REACHED();
603     }
604
605     // Don't shrink a big disk cache, since that would cause churn.
606     cfurlCacheDiskCapacity = max(cfurlCacheDiskCapacity, CFURLCacheDiskCapacity(cfurlCache.get()));
607
608     memoryCache()->setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
609     memoryCache()->setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
610     pageCache()->setCapacity(pageCacheCapacity);
611
612     CFURLCacheSetMemoryCapacity(cfurlCache.get(), cfurlCacheMemoryCapacity);
613     CFURLCacheSetDiskCapacity(cfurlCache.get(), cfurlCacheDiskCapacity);
614
615     s_didSetCacheModel = true;
616     s_cacheModel = cacheModel;
617     return;
618 #endif
619 }
620
621 WebCacheModel WebView::cacheModel()
622 {
623     return s_cacheModel;
624 }
625
626 bool WebView::didSetCacheModel()
627 {
628     return s_didSetCacheModel;
629 }
630
631 WebCacheModel WebView::maxCacheModelInAnyInstance()
632 {
633     WebCacheModel cacheModel = WebCacheModelDocumentViewer;
634
635     HashSet<WebView*>::iterator end = allWebViewsSet().end();
636     for (HashSet<WebView*>::iterator it = allWebViewsSet().begin(); it != end; ++it) {
637         COMPtr<IWebPreferences> pref;
638         if (FAILED((*it)->preferences(&pref)))
639             continue;
640         WebCacheModel prefCacheModel = WebCacheModelDocumentViewer;
641         if (FAILED(pref->cacheModel(&prefCacheModel)))
642             continue;
643
644         cacheModel = max(cacheModel, prefCacheModel);
645     }
646
647     return cacheModel;
648 }
649
650 HRESULT STDMETHODCALLTYPE WebView::close()
651 {
652     if (m_didClose)
653         return S_OK;
654
655     m_didClose = true;
656
657 #if USE(ACCELERATED_COMPOSITING)
658     setAcceleratedCompositing(false);
659 #endif
660
661     WebNotificationCenter::defaultCenterInternal()->postNotificationName(_bstr_t(WebViewWillCloseNotification).GetBSTR(), static_cast<IWebView*>(this), 0);
662
663     if (m_uiDelegatePrivate)
664         m_uiDelegatePrivate->webViewClosing(this);
665
666     removeFromAllWebViewsSet();
667
668     if (m_page) {
669         if (Frame* frame = m_page->mainFrame())
670             frame->loader()->detachFromParent();
671     }
672
673     if (m_mouseOutTracker) {
674         m_mouseOutTracker->dwFlags = TME_CANCEL;
675         ::TrackMouseEvent(m_mouseOutTracker.get());
676         m_mouseOutTracker.set(0);
677     }
678     
679     revokeDragDrop();
680
681     if (m_viewWindow) {
682         // We can't check IsWindow(m_viewWindow) here, because that will return true even while
683         // we're already handling WM_DESTROY. So we check !isBeingDestroyed() instead.
684         if (!isBeingDestroyed())
685             DestroyWindow(m_viewWindow);
686         // Either we just destroyed m_viewWindow, or it's in the process of being destroyed. Either
687         // way, we clear it out to make sure we don't try to use it later.
688         m_viewWindow = 0;
689     }
690
691     setHostWindow(0);
692
693     setDownloadDelegate(0);
694     setEditingDelegate(0);
695     setFrameLoadDelegate(0);
696     setFrameLoadDelegatePrivate(0);
697     setHistoryDelegate(0);
698     setPolicyDelegate(0);
699     setResourceLoadDelegate(0);
700     setUIDelegate(0);
701     setFormDelegate(0);
702     setPluginHalterDelegate(0);
703
704     if (m_webInspector)
705         m_webInspector->webViewClosed();
706
707     delete m_page;
708     m_page = 0;
709
710     registerForIconNotification(false);
711     IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
712     notifyCenter->removeObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
713
714     if (COMPtr<WebPreferences> preferences = m_preferences) {
715         BSTR identifier = 0;
716         preferences->identifier(&identifier);
717
718         m_preferences = 0;
719         preferences->didRemoveFromWebView();
720         // Make sure we release the reference, since WebPreferences::removeReferenceForIdentifier will check for last reference to WebPreferences
721         preferences = 0;
722         if (identifier) {
723             WebPreferences::removeReferenceForIdentifier(identifier);
724             SysFreeString(identifier);
725         }
726     }
727
728     deleteBackingStore();
729     return S_OK;
730 }
731
732 void WebView::repaint(const WebCore::IntRect& windowRect, bool contentChanged, bool immediate, bool repaintContentOnly)
733 {
734 #if USE(ACCELERATED_COMPOSITING)
735     if (isAcceleratedCompositing()) {
736         // The contentChanged, immediate, and repaintContentOnly parameters are all based on a non-
737         // compositing painting/scrolling model.
738         addToDirtyRegion(windowRect);
739         return;
740     }
741 #endif
742
743     if (!repaintContentOnly) {
744         RECT rect = windowRect;
745         ::InvalidateRect(m_viewWindow, &rect, false);
746     }
747     if (contentChanged)
748         addToDirtyRegion(windowRect);
749     if (immediate) {
750         if (repaintContentOnly)
751             updateBackingStore(core(topLevelFrame())->view());
752         else
753             ::UpdateWindow(m_viewWindow);
754     }
755 }
756
757 void WebView::deleteBackingStore()
758 {
759     pendingDeleteBackingStoreSet.remove(this);
760
761     if (m_deleteBackingStoreTimerActive) {
762         KillTimer(m_viewWindow, DeleteBackingStoreTimer);
763         m_deleteBackingStoreTimerActive = false;
764     }
765     m_backingStoreBitmap.clear();
766     m_backingStoreDirtyRegion.clear();
767     m_backingStoreSize.cx = m_backingStoreSize.cy = 0;
768 }
769
770 bool WebView::ensureBackingStore()
771 {
772     RECT windowRect;
773     ::GetClientRect(m_viewWindow, &windowRect);
774     LONG width = windowRect.right - windowRect.left;
775     LONG height = windowRect.bottom - windowRect.top;
776     if (width > 0 && height > 0 && (width != m_backingStoreSize.cx || height != m_backingStoreSize.cy)) {
777         deleteBackingStore();
778
779         m_backingStoreSize.cx = width;
780         m_backingStoreSize.cy = height;
781         BitmapInfo bitmapInfo = BitmapInfo::createBottomUp(IntSize(m_backingStoreSize));
782
783         void* pixels = NULL;
784         m_backingStoreBitmap = RefCountedHBITMAP::create(::CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0));
785         return true;
786     }
787
788     return false;
789 }
790
791 void WebView::addToDirtyRegion(const IntRect& dirtyRect)
792 {
793     // FIXME: We want an assert here saying that the dirtyRect is inside the clienRect,
794     // but it was being hit during our layout tests, and is being investigated in
795     // http://webkit.org/b/29350.
796
797 #if USE(ACCELERATED_COMPOSITING)
798     if (isAcceleratedCompositing()) {
799         m_backingLayer->setNeedsDisplayInRect(dirtyRect);
800         return;
801     }
802 #endif
803
804     HRGN newRegion = ::CreateRectRgn(dirtyRect.x(), dirtyRect.y(),
805                                      dirtyRect.maxX(), dirtyRect.maxY());
806     addToDirtyRegion(newRegion);
807 }
808
809 void WebView::addToDirtyRegion(HRGN newRegion)
810 {
811 #if USE(ACCELERATED_COMPOSITING)
812     ASSERT(!isAcceleratedCompositing());
813 #endif
814
815     LOCAL_GDI_COUNTER(0, __FUNCTION__);
816
817     if (m_backingStoreDirtyRegion) {
818         HRGN combinedRegion = ::CreateRectRgn(0,0,0,0);
819         ::CombineRgn(combinedRegion, m_backingStoreDirtyRegion->handle(), newRegion, RGN_OR);
820         ::DeleteObject(newRegion);
821         m_backingStoreDirtyRegion = RefCountedHRGN::create(combinedRegion);
822     } else
823         m_backingStoreDirtyRegion = RefCountedHRGN::create(newRegion);
824
825     if (m_uiDelegatePrivate)
826         m_uiDelegatePrivate->webViewDidInvalidate(this);
827 }
828
829 void WebView::scrollBackingStore(FrameView* frameView, int dx, int dy, const IntRect& scrollViewRect, const IntRect& clipRect)
830 {
831 #if USE(ACCELERATED_COMPOSITING)
832     if (isAcceleratedCompositing()) {
833         // FIXME: We should be doing something smarter here, like moving tiles around and painting
834         // any newly-exposed tiles. <http://webkit.org/b/52714>
835         m_backingLayer->setNeedsDisplayInRect(scrollViewRect);
836         return;
837     }
838 #endif
839
840     LOCAL_GDI_COUNTER(0, __FUNCTION__);
841
842     // If there's no backing store we don't need to update it
843     if (!m_backingStoreBitmap) {
844         if (m_uiDelegatePrivate)
845             m_uiDelegatePrivate->webViewScrolled(this);
846
847         return;
848     }
849
850     // Make a region to hold the invalidated scroll area.
851     HRGN updateRegion = ::CreateRectRgn(0, 0, 0, 0);
852
853     // Collect our device context info and select the bitmap to scroll.
854     HDC windowDC = ::GetDC(m_viewWindow);
855     HDC bitmapDC = ::CreateCompatibleDC(windowDC);
856     HGDIOBJ oldBitmap = ::SelectObject(bitmapDC, m_backingStoreBitmap->handle());
857     
858     // Scroll the bitmap.
859     RECT scrollRectWin(scrollViewRect);
860     RECT clipRectWin(clipRect);
861     ::ScrollDC(bitmapDC, dx, dy, &scrollRectWin, &clipRectWin, updateRegion, 0);
862     RECT regionBox;
863     ::GetRgnBox(updateRegion, &regionBox);
864
865     // Flush.
866     GdiFlush();
867
868     // Add the dirty region to the backing store's dirty region.
869     addToDirtyRegion(updateRegion);
870
871     if (m_uiDelegatePrivate)
872         m_uiDelegatePrivate->webViewScrolled(this);
873
874     // Update the backing store.
875     updateBackingStore(frameView, bitmapDC, false);
876
877     // Clean up.
878     ::SelectObject(bitmapDC, oldBitmap);
879     ::DeleteDC(bitmapDC);
880     ::ReleaseDC(m_viewWindow, windowDC);
881 }
882
883 void WebView::sizeChanged(const IntSize& newSize)
884 {
885     deleteBackingStore();
886
887     if (Frame* coreFrame = core(topLevelFrame()))
888         coreFrame->view()->resize(newSize);
889
890 #if USE(ACCELERATED_COMPOSITING)
891     if (m_layerTreeHost)
892         m_layerTreeHost->resize();
893     if (m_backingLayer) {
894         m_backingLayer->setSize(newSize);
895         m_backingLayer->setNeedsDisplay();
896     }
897 #endif
898 }
899
900 // This emulates the Mac smarts for painting rects intelligently.  This is very
901 // important for us, since we double buffer based off dirty rects.
902 static void getUpdateRects(HRGN region, const IntRect& dirtyRect, Vector<IntRect>& rects)
903 {
904     ASSERT_ARG(region, region);
905
906     const int cRectThreshold = 10;
907     const float cWastedSpaceThreshold = 0.75f;
908
909     rects.clear();
910
911     DWORD regionDataSize = GetRegionData(region, sizeof(RGNDATA), NULL);
912     if (!regionDataSize) {
913         rects.append(dirtyRect);
914         return;
915     }
916
917     Vector<unsigned char> buffer(regionDataSize);
918     RGNDATA* regionData = reinterpret_cast<RGNDATA*>(buffer.data());
919     GetRegionData(region, regionDataSize, regionData);
920     if (regionData->rdh.nCount > cRectThreshold) {
921         rects.append(dirtyRect);
922         return;
923     }
924
925     double singlePixels = 0.0;
926     unsigned i;
927     RECT* rect;
928     for (i = 0, rect = reinterpret_cast<RECT*>(regionData->Buffer); i < regionData->rdh.nCount; i++, rect++)
929         singlePixels += (rect->right - rect->left) * (rect->bottom - rect->top);
930
931     double unionPixels = dirtyRect.width() * dirtyRect.height();
932     double wastedSpace = 1.0 - (singlePixels / unionPixels);
933     if (wastedSpace <= cWastedSpaceThreshold) {
934         rects.append(dirtyRect);
935         return;
936     }
937
938     for (i = 0, rect = reinterpret_cast<RECT*>(regionData->Buffer); i < regionData->rdh.nCount; i++, rect++)
939         rects.append(*rect);
940 }
941
942 void WebView::updateBackingStore(FrameView* frameView, HDC dc, bool backingStoreCompletelyDirty, WindowsToPaint windowsToPaint)
943 {
944 #if USE(ACCELERATED_COMPOSITING)
945     ASSERT(!isAcceleratedCompositing());
946 #endif
947
948     LOCAL_GDI_COUNTER(0, __FUNCTION__);
949
950     HDC windowDC = 0;
951     HDC bitmapDC = dc;
952     if (!dc) {
953         windowDC = ::GetDC(m_viewWindow);
954         bitmapDC = ::CreateCompatibleDC(windowDC);
955         ::SelectObject(bitmapDC, m_backingStoreBitmap->handle());
956     }
957
958     if (m_backingStoreBitmap && (m_backingStoreDirtyRegion || backingStoreCompletelyDirty)) {
959         // Do a layout first so that everything we render to the backing store is always current.
960         if (Frame* coreFrame = core(m_mainFrame))
961             if (FrameView* view = coreFrame->view())
962                 view->updateLayoutAndStyleIfNeededRecursive();
963
964         Vector<IntRect> paintRects;
965         if (!backingStoreCompletelyDirty && m_backingStoreDirtyRegion) {
966             RECT regionBox;
967             ::GetRgnBox(m_backingStoreDirtyRegion->handle(), &regionBox);
968             getUpdateRects(m_backingStoreDirtyRegion->handle(), regionBox, paintRects);
969         } else {
970             RECT clientRect;
971             ::GetClientRect(m_viewWindow, &clientRect);
972             paintRects.append(clientRect);
973         }
974
975         for (unsigned i = 0; i < paintRects.size(); ++i)
976             paintIntoBackingStore(frameView, bitmapDC, paintRects[i], windowsToPaint);
977
978         if (m_uiDelegatePrivate)
979             m_uiDelegatePrivate->webViewPainted(this);
980
981         m_backingStoreDirtyRegion.clear();
982     }
983
984     if (!dc) {
985         ::DeleteDC(bitmapDC);
986         ::ReleaseDC(m_viewWindow, windowDC);
987     }
988
989     GdiFlush();
990 }
991
992 void WebView::paint(HDC dc, LPARAM options)
993 {
994     LOCAL_GDI_COUNTER(0, __FUNCTION__);
995
996 #if USE(ACCELERATED_COMPOSITING)
997     if (isAcceleratedCompositing()) {
998         m_layerTreeHost->flushPendingLayerChangesNow();
999         // Flushing might have taken us out of compositing mode.
1000         if (isAcceleratedCompositing()) {
1001             // FIXME: We need to paint into dc (if provided). <http://webkit.org/b/52578>
1002             m_layerTreeHost->paint();
1003             ::ValidateRect(m_viewWindow, 0);
1004             return;
1005         }
1006     }
1007 #endif
1008
1009     Frame* coreFrame = core(m_mainFrame);
1010     if (!coreFrame)
1011         return;
1012     FrameView* frameView = coreFrame->view();
1013
1014     RECT rcPaint;
1015     HDC hdc;
1016     OwnPtr<HRGN> region;
1017     int regionType = NULLREGION;
1018     PAINTSTRUCT ps;
1019     WindowsToPaint windowsToPaint;
1020     if (!dc) {
1021         region.set(CreateRectRgn(0,0,0,0));
1022         regionType = GetUpdateRgn(m_viewWindow, region.get(), false);
1023         hdc = BeginPaint(m_viewWindow, &ps);
1024         rcPaint = ps.rcPaint;
1025         // We're painting to the screen, and our child windows can handle
1026         // painting themselves to the screen.
1027         windowsToPaint = PaintWebViewOnly;
1028     } else {
1029         hdc = dc;
1030         ::GetClientRect(m_viewWindow, &rcPaint);
1031         if (options & PRF_ERASEBKGND)
1032             ::FillRect(hdc, &rcPaint, (HBRUSH)GetStockObject(WHITE_BRUSH));
1033         // Since we aren't painting to the screen, we want to paint all our
1034         // children into the HDC.
1035         windowsToPaint = PaintWebViewAndChildren;
1036     }
1037
1038     bool backingStoreCompletelyDirty = ensureBackingStore();
1039     if (!m_backingStoreBitmap) {
1040         if (!dc)
1041             EndPaint(m_viewWindow, &ps);
1042         return;
1043     }
1044
1045     m_paintCount++;
1046
1047     HDC bitmapDC = ::CreateCompatibleDC(hdc);
1048     HGDIOBJ oldBitmap = ::SelectObject(bitmapDC, m_backingStoreBitmap->handle());
1049
1050     // Update our backing store if needed.
1051     updateBackingStore(frameView, bitmapDC, backingStoreCompletelyDirty, windowsToPaint);
1052
1053     // Now we blit the updated backing store
1054     IntRect windowDirtyRect = rcPaint;
1055     
1056     // Apply the same heuristic for this update region too.
1057     Vector<IntRect> blitRects;
1058     if (region && regionType == COMPLEXREGION)
1059         getUpdateRects(region.get(), windowDirtyRect, blitRects);
1060     else
1061         blitRects.append(windowDirtyRect);
1062
1063     for (unsigned i = 0; i < blitRects.size(); ++i)
1064         paintIntoWindow(bitmapDC, hdc, blitRects[i]);
1065
1066     ::SelectObject(bitmapDC, oldBitmap);
1067     ::DeleteDC(bitmapDC);
1068
1069     if (!dc)
1070         EndPaint(m_viewWindow, &ps);
1071
1072     m_paintCount--;
1073
1074     if (active())
1075         cancelDeleteBackingStoreSoon();
1076     else
1077         deleteBackingStoreSoon();
1078 }
1079
1080 void WebView::paintIntoBackingStore(FrameView* frameView, HDC bitmapDC, const IntRect& dirtyRect, WindowsToPaint windowsToPaint)
1081 {
1082 #if USE(ACCELERATED_COMPOSITING)
1083     ASSERT(!isAcceleratedCompositing());
1084 #endif
1085
1086     LOCAL_GDI_COUNTER(0, __FUNCTION__);
1087
1088     // FIXME: We want an assert here saying that the dirtyRect is inside the clienRect,
1089     // but it was being hit during our layout tests, and is being investigated in
1090     // http://webkit.org/b/29350.
1091
1092     RECT rect = dirtyRect;
1093
1094 #if FLASH_BACKING_STORE_REDRAW
1095     HDC dc = ::GetDC(m_viewWindow);
1096     OwnPtr<HBRUSH> yellowBrush(CreateSolidBrush(RGB(255, 255, 0)));
1097     FillRect(dc, &rect, yellowBrush.get());
1098     GdiFlush();
1099     Sleep(50);
1100     paintIntoWindow(bitmapDC, dc, dirtyRect);
1101     ::ReleaseDC(m_viewWindow, dc);
1102 #endif
1103
1104     GraphicsContext gc(bitmapDC, m_transparent);
1105     gc.setShouldIncludeChildWindows(windowsToPaint == PaintWebViewAndChildren);
1106     gc.save();
1107     if (m_transparent)
1108         gc.clearRect(dirtyRect);
1109     else
1110         FillRect(bitmapDC, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
1111
1112     COMPtr<IWebUIDelegatePrivate2> uiPrivate(Query, m_uiDelegate);
1113     if (uiPrivate)
1114         uiPrivate->drawBackground(this, reinterpret_cast<OLE_HANDLE>(bitmapDC), &rect);
1115
1116     if (frameView && frameView->frame() && frameView->frame()->contentRenderer()) {
1117         gc.clip(dirtyRect);
1118         frameView->paint(&gc, dirtyRect);
1119     }
1120     gc.restore();
1121 }
1122
1123 void WebView::paintIntoWindow(HDC bitmapDC, HDC windowDC, const IntRect& dirtyRect)
1124 {
1125 #if USE(ACCELERATED_COMPOSITING)
1126     ASSERT(!isAcceleratedCompositing());
1127 #endif
1128
1129     LOCAL_GDI_COUNTER(0, __FUNCTION__);
1130 #if FLASH_WINDOW_REDRAW
1131     OwnPtr<HBRUSH> greenBrush = CreateSolidBrush(RGB(0, 255, 0));
1132     RECT rect = dirtyRect;
1133     FillRect(windowDC, &rect, greenBrush.get());
1134     GdiFlush();
1135     Sleep(50);
1136 #endif
1137
1138     // Blit the dirty rect from the backing store into the same position
1139     // in the destination DC.
1140     BitBlt(windowDC, dirtyRect.x(), dirtyRect.y(), dirtyRect.width(), dirtyRect.height(), bitmapDC,
1141            dirtyRect.x(), dirtyRect.y(), SRCCOPY);
1142 }
1143
1144 void WebView::frameRect(RECT* rect)
1145 {
1146     ::GetWindowRect(m_viewWindow, rect);
1147 }
1148
1149 class WindowCloseTimer : public WebCore::SuspendableTimer {
1150 public:
1151     static WindowCloseTimer* create(WebView*);
1152
1153 private:
1154     WindowCloseTimer(ScriptExecutionContext*, WebView*);
1155     virtual void contextDestroyed();
1156     virtual void fired();
1157
1158     WebView* m_webView;
1159 };
1160
1161 WindowCloseTimer* WindowCloseTimer::create(WebView* webView)
1162 {
1163     ASSERT_ARG(webView, webView);
1164     Frame* frame = core(webView->topLevelFrame());
1165     ASSERT(frame);
1166     if (!frame)
1167         return 0;
1168
1169     Document* document = frame->document();
1170     ASSERT(document);
1171     if (!document)
1172         return 0;
1173
1174     return new WindowCloseTimer(document, webView);
1175 }
1176
1177 WindowCloseTimer::WindowCloseTimer(ScriptExecutionContext* context, WebView* webView)
1178     : SuspendableTimer(context)
1179     , m_webView(webView)
1180 {
1181     ASSERT_ARG(context, context);
1182     ASSERT_ARG(webView, webView);
1183 }
1184
1185 void WindowCloseTimer::contextDestroyed()
1186 {
1187     SuspendableTimer::contextDestroyed();
1188     delete this;
1189 }
1190
1191 void WindowCloseTimer::fired()
1192 {
1193     m_webView->closeWindowTimerFired();
1194 }
1195
1196 void WebView::closeWindowSoon()
1197 {
1198     if (m_closeWindowTimer)
1199         return;
1200
1201     m_closeWindowTimer = WindowCloseTimer::create(this);
1202     if (!m_closeWindowTimer)
1203         return;
1204     m_closeWindowTimer->startOneShot(0);
1205
1206     AddRef();
1207 }
1208
1209 void WebView::closeWindowTimerFired()
1210 {
1211     closeWindow();
1212     Release();
1213 }
1214
1215 void WebView::closeWindow()
1216 {
1217     if (m_hasSpellCheckerDocumentTag) {
1218         if (m_editingDelegate)
1219             m_editingDelegate->closeSpellDocument(this);
1220         m_hasSpellCheckerDocumentTag = false;
1221     }
1222
1223     COMPtr<IWebUIDelegate> ui;
1224     if (SUCCEEDED(uiDelegate(&ui)))
1225         ui->webViewClose(this);
1226 }
1227
1228 bool WebView::canHandleRequest(const WebCore::ResourceRequest& request)
1229 {
1230     // On the mac there's an about url protocol implementation but CFNetwork doesn't have that.
1231     if (equalIgnoringCase(String(request.url().protocol()), "about"))
1232         return true;
1233
1234 #if USE(CFNETWORK)
1235     if (CFURLProtocolCanHandleRequest(request.cfURLRequest()))
1236         return true;
1237
1238     // FIXME: Mac WebKit calls _representationExistsForURLScheme here
1239     return false;
1240 #else
1241     return true;
1242 #endif
1243 }
1244
1245 String WebView::standardUserAgentWithApplicationName(const String& applicationName)
1246 {
1247     if (applicationName.isEmpty())
1248         return makeString("Mozilla/5.0 (", osVersion(), ") AppleWebKit/", webKitVersion(), " (KHTML, like Gecko)");
1249     return makeString("Mozilla/5.0 (", osVersion(), ") AppleWebKit/", webKitVersion(), " (KHTML, like Gecko) ", applicationName);
1250 }
1251
1252 Page* WebView::page()
1253 {
1254     return m_page;
1255 }
1256
1257 bool WebView::handleContextMenuEvent(WPARAM wParam, LPARAM lParam)
1258 {
1259     // Translate the screen coordinates into window coordinates
1260     POINT coords = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
1261     if (coords.x == -1 || coords.y == -1) {
1262         // The contextMenuController() holds onto the last context menu that was popped up on the
1263         // page until a new one is created. We need to clear this menu before propagating the event
1264         // through the DOM so that we can detect if we create a new menu for this event, since we
1265         // won't create a new menu if the DOM swallows the event and the defaultEventHandler does
1266         // not run.
1267         m_page->contextMenuController()->clearContextMenu();
1268
1269         Frame* focusedFrame = m_page->focusController()->focusedOrMainFrame();
1270         return focusedFrame->eventHandler()->sendContextMenuEventForKey();
1271
1272     } else {
1273         if (!::ScreenToClient(m_viewWindow, &coords))
1274             return false;
1275     }
1276
1277     lParam = MAKELPARAM(coords.x, coords.y);
1278
1279     m_page->contextMenuController()->clearContextMenu();
1280
1281     IntPoint documentPoint(m_page->mainFrame()->view()->windowToContents(coords));
1282     HitTestResult result = m_page->mainFrame()->eventHandler()->hitTestResultAtPoint(documentPoint, false);
1283     Frame* targetFrame = result.innerNonSharedNode() ? result.innerNonSharedNode()->document()->frame() : m_page->focusController()->focusedOrMainFrame();
1284
1285     targetFrame->view()->setCursor(pointerCursor());
1286     PlatformMouseEvent mouseEvent(m_viewWindow, WM_RBUTTONUP, wParam, lParam);
1287     bool handledEvent = targetFrame->eventHandler()->sendContextMenuEvent(mouseEvent);
1288     if (!handledEvent)
1289         return false;
1290
1291     ContextMenuController* contextMenuController = m_page->contextMenuController();
1292
1293     // Show the menu
1294     ContextMenu* coreMenu = contextMenuController->contextMenu();
1295     if (!coreMenu)
1296         return false;
1297
1298     Node* node = contextMenuController->hitTestResult().innerNonSharedNode();
1299     if (!node)
1300         return false;
1301
1302     Frame* frame = node->document()->frame();
1303     if (!frame)
1304         return false;
1305
1306     FrameView* view = frame->view();
1307     if (!view)
1308         return false;
1309
1310     POINT point(view->contentsToWindow(contextMenuController->hitTestResult().point()));
1311
1312     // Translate the point to screen coordinates
1313     if (!::ClientToScreen(m_viewWindow, &point))
1314         return false;
1315
1316     BOOL hasCustomMenus = false;
1317     if (m_uiDelegate)
1318         m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1319
1320     if (hasCustomMenus)
1321         m_uiDelegate->trackCustomPopupMenu((IWebView*)this, (OLE_HANDLE)(ULONG64)coreMenu->nativeMenu(), &point);
1322     else {
1323         // Surprisingly, TPM_RIGHTBUTTON means that items are selectable with either the right OR left mouse button
1324         UINT flags = TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_VERPOSANIMATION | TPM_HORIZONTAL
1325             | TPM_LEFTALIGN | TPM_HORPOSANIMATION;
1326         ::TrackPopupMenuEx(coreMenu->nativeMenu(), flags, point.x, point.y, m_viewWindow, 0);
1327     }
1328
1329     return true;
1330 }
1331
1332 bool WebView::onMeasureItem(WPARAM /*wParam*/, LPARAM lParam)
1333 {
1334     if (!m_uiDelegate)
1335         return false;
1336
1337     BOOL hasCustomMenus = false;
1338     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1339     if (!hasCustomMenus)
1340         return false;
1341
1342     m_uiDelegate->measureCustomMenuItem((IWebView*)this, (void*)lParam);
1343     return true;
1344 }
1345
1346 bool WebView::onDrawItem(WPARAM /*wParam*/, LPARAM lParam)
1347 {
1348     if (!m_uiDelegate)
1349         return false;
1350
1351     BOOL hasCustomMenus = false;
1352     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1353     if (!hasCustomMenus)
1354         return false;
1355
1356     m_uiDelegate->drawCustomMenuItem((IWebView*)this, (void*)lParam);
1357     return true;
1358 }
1359
1360 bool WebView::onInitMenuPopup(WPARAM wParam, LPARAM /*lParam*/)
1361 {
1362     if (!m_uiDelegate)
1363         return false;
1364
1365     HMENU menu = (HMENU)wParam;
1366     if (!menu)
1367         return false;
1368
1369     BOOL hasCustomMenus = false;
1370     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1371     if (!hasCustomMenus)
1372         return false;
1373
1374     m_uiDelegate->addCustomMenuDrawingData((IWebView*)this, (OLE_HANDLE)(ULONG64)menu);
1375     return true;
1376 }
1377
1378 bool WebView::onUninitMenuPopup(WPARAM wParam, LPARAM /*lParam*/)
1379 {
1380     if (!m_uiDelegate)
1381         return false;
1382
1383     HMENU menu = (HMENU)wParam;
1384     if (!menu)
1385         return false;
1386
1387     BOOL hasCustomMenus = false;
1388     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1389     if (!hasCustomMenus)
1390         return false;
1391
1392     m_uiDelegate->cleanUpCustomMenuDrawingData((IWebView*)this, (OLE_HANDLE)(ULONG64)menu);
1393     return true;
1394 }
1395
1396 void WebView::performContextMenuAction(WPARAM wParam, LPARAM lParam, bool byPosition)
1397 {
1398     ContextMenu* menu = m_page->contextMenuController()->contextMenu();
1399     ASSERT(menu);
1400
1401     ContextMenuItem* item = byPosition ? menu->itemAtIndex((unsigned)wParam) : menu->itemWithAction((ContextMenuAction)wParam);
1402     if (!item)
1403         return;
1404     m_page->contextMenuController()->contextMenuItemSelected(item);
1405 }
1406
1407 bool WebView::handleMouseEvent(UINT message, WPARAM wParam, LPARAM lParam)
1408 {
1409     static LONG globalClickCount;
1410     static IntPoint globalPrevPoint;
1411     static MouseButton globalPrevButton;
1412     static LONG globalPrevMouseDownTime;
1413
1414     if (message == WM_CANCELMODE) {
1415         m_page->mainFrame()->eventHandler()->lostMouseCapture();
1416         return true;
1417     }
1418
1419     // Create our event.
1420     // On WM_MOUSELEAVE we need to create a mouseout event, so we force the position
1421     // of the event to be at (MINSHORT, MINSHORT).
1422     LPARAM position = (message == WM_MOUSELEAVE) ? ((MINSHORT << 16) | MINSHORT) : lParam;
1423     PlatformMouseEvent mouseEvent(m_viewWindow, message, wParam, position, m_mouseActivated);
1424
1425     setMouseActivated(false);
1426
1427     bool insideThreshold = abs(globalPrevPoint.x() - mouseEvent.pos().x()) < ::GetSystemMetrics(SM_CXDOUBLECLK) &&
1428                            abs(globalPrevPoint.y() - mouseEvent.pos().y()) < ::GetSystemMetrics(SM_CYDOUBLECLK);
1429     LONG messageTime = ::GetMessageTime();
1430
1431     bool handled = false;
1432
1433     if (message == WM_LBUTTONDOWN || message == WM_MBUTTONDOWN || message == WM_RBUTTONDOWN) {
1434         // FIXME: I'm not sure if this is the "right" way to do this
1435         // but without this call, we never become focused since we don't allow
1436         // the default handling of mouse events.
1437         SetFocus(m_viewWindow);
1438
1439         // Always start capturing events when the mouse goes down in our HWND.
1440         ::SetCapture(m_viewWindow);
1441
1442         if (((messageTime - globalPrevMouseDownTime) < (LONG)::GetDoubleClickTime()) && 
1443             insideThreshold &&
1444             mouseEvent.button() == globalPrevButton)
1445             globalClickCount++;
1446         else
1447             // Reset the click count.
1448             globalClickCount = 1;
1449         globalPrevMouseDownTime = messageTime;
1450         globalPrevButton = mouseEvent.button();
1451         globalPrevPoint = mouseEvent.pos();
1452         
1453         mouseEvent.setClickCount(globalClickCount);
1454         handled = m_page->mainFrame()->eventHandler()->handleMousePressEvent(mouseEvent);
1455     } else if (message == WM_LBUTTONDBLCLK || message == WM_MBUTTONDBLCLK || message == WM_RBUTTONDBLCLK) {
1456         globalClickCount++;
1457         mouseEvent.setClickCount(globalClickCount);
1458         handled = m_page->mainFrame()->eventHandler()->handleMousePressEvent(mouseEvent);
1459     } else if (message == WM_LBUTTONUP || message == WM_MBUTTONUP || message == WM_RBUTTONUP) {
1460         // Record the global position and the button of the up.
1461         globalPrevButton = mouseEvent.button();
1462         globalPrevPoint = mouseEvent.pos();
1463         mouseEvent.setClickCount(globalClickCount);
1464         m_page->mainFrame()->eventHandler()->handleMouseReleaseEvent(mouseEvent);
1465         ::ReleaseCapture();
1466     } else if (message == WM_MOUSELEAVE && m_mouseOutTracker) {
1467         // Once WM_MOUSELEAVE is fired windows clears this tracker
1468         // so there is no need to disable it ourselves.
1469         m_mouseOutTracker.set(0);
1470         m_page->mainFrame()->eventHandler()->mouseMoved(mouseEvent);
1471         handled = true;
1472     } else if (message == WM_MOUSEMOVE) {
1473         if (!insideThreshold)
1474             globalClickCount = 0;
1475         mouseEvent.setClickCount(globalClickCount);
1476         handled = m_page->mainFrame()->eventHandler()->mouseMoved(mouseEvent);
1477         if (!m_mouseOutTracker) {
1478             m_mouseOutTracker.set(new TRACKMOUSEEVENT);
1479             m_mouseOutTracker->cbSize = sizeof(TRACKMOUSEEVENT);
1480             m_mouseOutTracker->dwFlags = TME_LEAVE;
1481             m_mouseOutTracker->hwndTrack = m_viewWindow;
1482             ::TrackMouseEvent(m_mouseOutTracker.get());
1483         }
1484     }
1485     return handled;
1486 }
1487
1488 bool WebView::gestureNotify(WPARAM wParam, LPARAM lParam)
1489 {
1490     GESTURENOTIFYSTRUCT* gn = reinterpret_cast<GESTURENOTIFYSTRUCT*>(lParam);
1491
1492     Frame* coreFrame = core(m_mainFrame);
1493     if (!coreFrame)
1494         return false;
1495
1496     ScrollView* view = coreFrame->view();
1497     if (!view)
1498         return false;
1499
1500     // If we don't have this function, we shouldn't be receiving this message
1501     ASSERT(SetGestureConfigPtr());
1502
1503     bool hitScrollbar = false;
1504     POINT gestureBeginPoint = {gn->ptsLocation.x, gn->ptsLocation.y};
1505     HitTestRequest request(HitTestRequest::ReadOnly);
1506     for (Frame* childFrame = m_page->mainFrame(); childFrame; childFrame = EventHandler::subframeForTargetNode(m_gestureTargetNode.get())) {
1507         FrameView* frameView = childFrame->view();
1508         if (!frameView)
1509             break;
1510         RenderView* renderView = childFrame->document()->renderView();
1511         if (!renderView)
1512             break;
1513         RenderLayer* layer = renderView->layer();
1514         if (!layer)
1515             break;
1516
1517         HitTestResult result(frameView->screenToContents(gestureBeginPoint));
1518         layer->hitTest(request, result);
1519         m_gestureTargetNode = result.innerNode();
1520
1521         if (!hitScrollbar)
1522             hitScrollbar = result.scrollbar();
1523     }
1524
1525     if (!hitScrollbar) {
1526         // The hit testing above won't detect if we've hit the main frame's vertical scrollbar. Check that manually now.
1527         RECT webViewRect;
1528         GetWindowRect(m_viewWindow, &webViewRect);
1529         hitScrollbar = view->verticalScrollbar() && (gestureBeginPoint.x > (webViewRect.right - view->verticalScrollbar()->theme()->scrollbarThickness()));  
1530     }
1531
1532     bool canBeScrolled = false;
1533     if (m_gestureTargetNode) {
1534         for (RenderObject* renderer = m_gestureTargetNode->renderer(); renderer; renderer = renderer->parent()) {
1535             if (renderer->isBox() && toRenderBox(renderer)->canBeScrolledAndHasScrollableArea()) {
1536                 canBeScrolled = true;
1537                 break;
1538             }
1539         }
1540     }
1541
1542     // We always allow two-fingered panning with inertia and a gutter (which limits movement to one
1543     // direction in most cases).
1544     DWORD dwPanWant = GC_PAN | GC_PAN_WITH_INERTIA | GC_PAN_WITH_GUTTER;
1545     // We never allow single-fingered horizontal panning. That gesture is reserved for creating text
1546     // selections. This matches IE.
1547     DWORD dwPanBlock = GC_PAN_WITH_SINGLE_FINGER_HORIZONTALLY;
1548
1549     if (hitScrollbar || !canBeScrolled) {
1550         // The part of the page under the gesture can't be scrolled, or the gesture is on a scrollbar.
1551         // Disallow single-fingered vertical panning in this case, too, so we'll fall back to the default
1552         // behavior (which allows the scrollbar thumb to be dragged, text selections to be made, etc.).
1553         dwPanBlock |= GC_PAN_WITH_SINGLE_FINGER_VERTICALLY;
1554     } else {
1555         // The part of the page the gesture is under can be scrolled, and we're not under a scrollbar.
1556         // Allow single-fingered vertical panning in this case, so the user will be able to pan the page
1557         // with one or two fingers.
1558         dwPanWant |= GC_PAN_WITH_SINGLE_FINGER_VERTICALLY;
1559     }
1560
1561     GESTURECONFIG gc = { GID_PAN, dwPanWant, dwPanBlock };
1562     return SetGestureConfigPtr()(m_viewWindow, 0, 1, &gc, sizeof(GESTURECONFIG));
1563 }
1564
1565 bool WebView::gesture(WPARAM wParam, LPARAM lParam) 
1566 {
1567     // We want to bail out if we don't have either of these functions
1568     if (!GetGestureInfoPtr() || !CloseGestureInfoHandlePtr())
1569         return false;
1570
1571     HGESTUREINFO gestureHandle = reinterpret_cast<HGESTUREINFO>(lParam);
1572     
1573     GESTUREINFO gi = {0};
1574     gi.cbSize = sizeof(GESTUREINFO);
1575
1576     if (!GetGestureInfoPtr()(gestureHandle, reinterpret_cast<PGESTUREINFO>(&gi)))
1577         return false;
1578
1579     switch (gi.dwID) {
1580     case GID_BEGIN:
1581         m_lastPanX = gi.ptsLocation.x;
1582         m_lastPanY = gi.ptsLocation.y;
1583
1584         break;
1585     case GID_END:
1586         m_gestureTargetNode = 0;
1587         break;
1588     case GID_PAN: {
1589         // Where are the fingers currently?
1590         long currentX = gi.ptsLocation.x;
1591         long currentY = gi.ptsLocation.y;
1592         // How far did we pan in each direction?
1593         long deltaX = currentX - m_lastPanX;
1594         long deltaY = currentY - m_lastPanY;
1595         // Calculate the overpan for window bounce
1596         m_yOverpan -= m_lastPanY - currentY;
1597         m_xOverpan -= m_lastPanX - currentX;
1598         // Update our class variables with updated values
1599         m_lastPanX = currentX;
1600         m_lastPanY = currentY;
1601
1602         Frame* coreFrame = core(m_mainFrame);
1603         if (!coreFrame) {
1604             CloseGestureInfoHandlePtr()(gestureHandle);
1605             return false;
1606         }
1607
1608         if (!m_gestureTargetNode || !m_gestureTargetNode->renderer())
1609             return false;
1610
1611         // We negate here since panning up moves the content up, but moves the scrollbar down.
1612         m_gestureTargetNode->renderer()->enclosingLayer()->scrollByRecursively(-deltaX, -deltaY);
1613            
1614         if (!(UpdatePanningFeedbackPtr() && BeginPanningFeedbackPtr() && EndPanningFeedbackPtr())) {
1615             CloseGestureInfoHandlePtr()(gestureHandle);
1616             return true;
1617         }
1618
1619         if (gi.dwFlags & GF_BEGIN) {
1620             BeginPanningFeedbackPtr()(m_viewWindow);
1621             m_yOverpan = 0;
1622         } else if (gi.dwFlags & GF_END) {
1623             EndPanningFeedbackPtr()(m_viewWindow, true);
1624             m_yOverpan = 0;
1625         }
1626
1627         ScrollView* view = coreFrame->view();
1628         if (!view) {
1629             CloseGestureInfoHandlePtr()(gestureHandle);
1630             return true;
1631         }
1632         Scrollbar* vertScrollbar = view->verticalScrollbar();
1633         if (!vertScrollbar) {
1634             CloseGestureInfoHandlePtr()(gestureHandle);
1635             return true;
1636         }
1637
1638         // FIXME: Support Horizontal Window Bounce. <https://webkit.org/b/28500>.
1639         // FIXME: If the user starts panning down after a window bounce has started, the window doesn't bounce back 
1640         // until they release their finger. <https://webkit.org/b/28501>.
1641         if (vertScrollbar->currentPos() == 0)
1642             UpdatePanningFeedbackPtr()(m_viewWindow, 0, m_yOverpan, gi.dwFlags & GF_INERTIA);
1643         else if (vertScrollbar->currentPos() >= vertScrollbar->maximum())
1644             UpdatePanningFeedbackPtr()(m_viewWindow, 0, m_yOverpan, gi.dwFlags & GF_INERTIA);
1645
1646         CloseGestureInfoHandlePtr()(gestureHandle);
1647         return true;
1648     }
1649     default:
1650         break;
1651     }
1652
1653     // If we get to this point, the gesture has not been handled. We forward
1654     // the call to DefWindowProc by returning false, and we don't need to 
1655     // to call CloseGestureInfoHandle. 
1656     // http://msdn.microsoft.com/en-us/library/dd353228(VS.85).aspx
1657     return false;
1658 }
1659
1660 bool WebView::mouseWheel(WPARAM wParam, LPARAM lParam, bool isMouseHWheel)
1661 {
1662     // Ctrl+Mouse wheel doesn't ever go into WebCore.  It is used to
1663     // zoom instead (Mac zooms the whole Desktop, but Windows browsers trigger their
1664     // own local zoom modes for Ctrl+wheel).
1665     if (wParam & MK_CONTROL) {
1666         short delta = short(HIWORD(wParam));
1667         if (delta < 0)
1668             makeTextSmaller(0);
1669         else
1670             makeTextLarger(0);
1671         return true;
1672     }
1673     
1674     // FIXME: This doesn't fix https://bugs.webkit.org/show_bug.cgi?id=28217. This only fixes https://bugs.webkit.org/show_bug.cgi?id=28203.
1675     HWND focusedWindow = GetFocus();
1676     if (focusedWindow && focusedWindow != m_viewWindow) {
1677         // Our focus is on a different hwnd, see if it's a PopupMenu and if so, set the focus back on us (which will hide the popup).
1678         TCHAR className[256];
1679
1680         // Make sure truncation won't affect the comparison.
1681         ASSERT(WTF_ARRAY_LENGTH(className) > _tcslen(PopupMenuWin::popupClassName()));
1682
1683         if (GetClassName(focusedWindow, className, WTF_ARRAY_LENGTH(className)) && !_tcscmp(className, PopupMenuWin::popupClassName())) {
1684             // We don't let the WebView scroll here for two reasons - 1) To match Firefox behavior, 2) If we do scroll, we lose the
1685             // focus ring around the select menu.
1686             SetFocus(m_viewWindow);
1687             return true;
1688         }
1689     }
1690
1691     PlatformWheelEvent wheelEvent(m_viewWindow, wParam, lParam, isMouseHWheel);
1692     Frame* coreFrame = core(m_mainFrame);
1693     if (!coreFrame)
1694         return false;
1695
1696     return coreFrame->eventHandler()->handleWheelEvent(wheelEvent);
1697 }
1698
1699 bool WebView::verticalScroll(WPARAM wParam, LPARAM /*lParam*/)
1700 {
1701     ScrollDirection direction;
1702     ScrollGranularity granularity;
1703     switch (LOWORD(wParam)) {
1704     case SB_LINEDOWN:
1705         granularity = ScrollByLine;
1706         direction = ScrollDown;
1707         break;
1708     case SB_LINEUP:
1709         granularity = ScrollByLine;
1710         direction = ScrollUp;
1711         break;
1712     case SB_PAGEDOWN:
1713         granularity = ScrollByDocument;
1714         direction = ScrollDown;
1715         break;
1716     case SB_PAGEUP:
1717         granularity = ScrollByDocument;
1718         direction = ScrollUp;
1719         break;
1720     default:
1721         return false;
1722         break;
1723     }
1724     
1725     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1726     return frame->eventHandler()->scrollRecursively(direction, granularity);
1727 }
1728
1729 bool WebView::horizontalScroll(WPARAM wParam, LPARAM /*lParam*/)
1730 {
1731     ScrollDirection direction;
1732     ScrollGranularity granularity;
1733     switch (LOWORD(wParam)) {
1734     case SB_LINELEFT:
1735         granularity = ScrollByLine;
1736         direction = ScrollLeft;
1737         break;
1738     case SB_LINERIGHT:
1739         granularity = ScrollByLine;
1740         direction = ScrollRight;
1741         break;
1742     case SB_PAGELEFT:
1743         granularity = ScrollByDocument;
1744         direction = ScrollLeft;
1745         break;
1746     case SB_PAGERIGHT:
1747         granularity = ScrollByDocument;
1748         direction = ScrollRight;
1749         break;
1750     default:
1751         return false;
1752     }
1753
1754     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1755     return frame->eventHandler()->scrollRecursively(direction, granularity);
1756 }
1757
1758
1759 bool WebView::execCommand(WPARAM wParam, LPARAM /*lParam*/)
1760 {
1761     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1762     switch (LOWORD(wParam)) {
1763         case SelectAll:
1764             return frame->editor()->command("SelectAll").execute();
1765         case Undo:
1766             return frame->editor()->command("Undo").execute();
1767         case Redo:
1768             return frame->editor()->command("Redo").execute();
1769     }
1770     return false;
1771 }
1772
1773 bool WebView::keyUp(WPARAM virtualKeyCode, LPARAM keyData, bool systemKeyDown)
1774 {
1775     PlatformKeyboardEvent keyEvent(m_viewWindow, virtualKeyCode, keyData, PlatformKeyboardEvent::KeyUp, systemKeyDown);
1776
1777     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1778     m_currentCharacterCode = 0;
1779
1780     return frame->eventHandler()->keyEvent(keyEvent);
1781 }
1782
1783 static const unsigned CtrlKey = 1 << 0;
1784 static const unsigned AltKey = 1 << 1;
1785 static const unsigned ShiftKey = 1 << 2;
1786
1787
1788 struct KeyDownEntry {
1789     unsigned virtualKey;
1790     unsigned modifiers;
1791     const char* name;
1792 };
1793
1794 struct KeyPressEntry {
1795     unsigned charCode;
1796     unsigned modifiers;
1797     const char* name;
1798 };
1799
1800 static const KeyDownEntry keyDownEntries[] = {
1801     { VK_LEFT,   0,                  "MoveLeft"                                    },
1802     { VK_LEFT,   ShiftKey,           "MoveLeftAndModifySelection"                  },
1803     { VK_LEFT,   CtrlKey,            "MoveWordLeft"                                },
1804     { VK_LEFT,   CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection"              },
1805     { VK_RIGHT,  0,                  "MoveRight"                                   },
1806     { VK_RIGHT,  ShiftKey,           "MoveRightAndModifySelection"                 },
1807     { VK_RIGHT,  CtrlKey,            "MoveWordRight"                               },
1808     { VK_RIGHT,  CtrlKey | ShiftKey, "MoveWordRightAndModifySelection"             },
1809     { VK_UP,     0,                  "MoveUp"                                      },
1810     { VK_UP,     ShiftKey,           "MoveUpAndModifySelection"                    },
1811     { VK_PRIOR,  ShiftKey,           "MovePageUpAndModifySelection"                },
1812     { VK_DOWN,   0,                  "MoveDown"                                    },
1813     { VK_DOWN,   ShiftKey,           "MoveDownAndModifySelection"                  },
1814     { VK_NEXT,   ShiftKey,           "MovePageDownAndModifySelection"              },
1815     { VK_PRIOR,  0,                  "MovePageUp"                                  },
1816     { VK_NEXT,   0,                  "MovePageDown"                                },
1817     { VK_HOME,   0,                  "MoveToBeginningOfLine"                       },
1818     { VK_HOME,   ShiftKey,           "MoveToBeginningOfLineAndModifySelection"     },
1819     { VK_HOME,   CtrlKey,            "MoveToBeginningOfDocument"                   },
1820     { VK_HOME,   CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
1821
1822     { VK_END,    0,                  "MoveToEndOfLine"                             },
1823     { VK_END,    ShiftKey,           "MoveToEndOfLineAndModifySelection"           },
1824     { VK_END,    CtrlKey,            "MoveToEndOfDocument"                         },
1825     { VK_END,    CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection"       },
1826
1827     { VK_BACK,   0,                  "DeleteBackward"                              },
1828     { VK_BACK,   ShiftKey,           "DeleteBackward"                              },
1829     { VK_DELETE, 0,                  "DeleteForward"                               },
1830     { VK_BACK,   CtrlKey,            "DeleteWordBackward"                          },
1831     { VK_DELETE, CtrlKey,            "DeleteWordForward"                           },
1832     
1833     { 'B',       CtrlKey,            "ToggleBold"                                  },
1834     { 'I',       CtrlKey,            "ToggleItalic"                                },
1835
1836     { VK_ESCAPE, 0,                  "Cancel"                                      },
1837     { VK_OEM_PERIOD, CtrlKey,        "Cancel"                                      },
1838     { VK_TAB,    0,                  "InsertTab"                                   },
1839     { VK_TAB,    ShiftKey,           "InsertBacktab"                               },
1840     { VK_RETURN, 0,                  "InsertNewline"                               },
1841     { VK_RETURN, CtrlKey,            "InsertNewline"                               },
1842     { VK_RETURN, AltKey,             "InsertNewline"                               },
1843     { VK_RETURN, ShiftKey,           "InsertNewline"                               },
1844     { VK_RETURN, AltKey | ShiftKey,  "InsertNewline"                               },
1845
1846     // It's not quite clear whether clipboard shortcuts and Undo/Redo should be handled
1847     // in the application or in WebKit. We chose WebKit.
1848     { 'C',       CtrlKey,            "Copy"                                        },
1849     { 'V',       CtrlKey,            "Paste"                                       },
1850     { 'X',       CtrlKey,            "Cut"                                         },
1851     { 'A',       CtrlKey,            "SelectAll"                                   },
1852     { VK_INSERT, CtrlKey,            "Copy"                                        },
1853     { VK_DELETE, ShiftKey,           "Cut"                                         },
1854     { VK_INSERT, ShiftKey,           "Paste"                                       },
1855     { 'Z',       CtrlKey,            "Undo"                                        },
1856     { 'Z',       CtrlKey | ShiftKey, "Redo"                                        },
1857 };
1858
1859 static const KeyPressEntry keyPressEntries[] = {
1860     { '\t',   0,                  "InsertTab"                                   },
1861     { '\t',   ShiftKey,           "InsertBacktab"                               },
1862     { '\r',   0,                  "InsertNewline"                               },
1863     { '\r',   CtrlKey,            "InsertNewline"                               },
1864     { '\r',   AltKey,             "InsertNewline"                               },
1865     { '\r',   ShiftKey,           "InsertNewline"                               },
1866     { '\r',   AltKey | ShiftKey,  "InsertNewline"                               },
1867 };
1868
1869 const char* WebView::interpretKeyEvent(const KeyboardEvent* evt)
1870 {
1871     ASSERT(evt->type() == eventNames().keydownEvent || evt->type() == eventNames().keypressEvent);
1872
1873     static HashMap<int, const char*>* keyDownCommandsMap = 0;
1874     static HashMap<int, const char*>* keyPressCommandsMap = 0;
1875
1876     if (!keyDownCommandsMap) {
1877         keyDownCommandsMap = new HashMap<int, const char*>;
1878         keyPressCommandsMap = new HashMap<int, const char*>;
1879
1880         for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyDownEntries); ++i)
1881             keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
1882
1883         for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyPressEntries); ++i)
1884             keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
1885     }
1886
1887     unsigned modifiers = 0;
1888     if (evt->shiftKey())
1889         modifiers |= ShiftKey;
1890     if (evt->altKey())
1891         modifiers |= AltKey;
1892     if (evt->ctrlKey())
1893         modifiers |= CtrlKey;
1894
1895     if (evt->type() == eventNames().keydownEvent) {
1896         int mapKey = modifiers << 16 | evt->keyCode();
1897         return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
1898     }
1899
1900     int mapKey = modifiers << 16 | evt->charCode();
1901     return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
1902 }
1903
1904 bool WebView::handleEditingKeyboardEvent(KeyboardEvent* evt)
1905 {
1906     Node* node = evt->target()->toNode();
1907     ASSERT(node);
1908     Frame* frame = node->document()->frame();
1909     ASSERT(frame);
1910
1911     const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
1912     if (!keyEvent || keyEvent->isSystemKey())  // do not treat this as text input if it's a system key event
1913         return false;
1914
1915     Editor::Command command = frame->editor()->command(interpretKeyEvent(evt));
1916
1917     if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
1918         // WebKit doesn't have enough information about mode to decide how commands that just insert text if executed via Editor should be treated,
1919         // so we leave it upon WebCore to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
1920         // (e.g. Tab that inserts a Tab character, or Enter).
1921         return !command.isTextInsertion() && command.execute(evt);
1922     }
1923
1924      if (command.execute(evt))
1925         return true;
1926
1927     // Don't insert null or control characters as they can result in unexpected behaviour
1928     if (evt->charCode() < ' ')
1929         return false;
1930
1931     return frame->editor()->insertText(evt->keyEvent()->text(), evt);
1932 }
1933
1934 bool WebView::keyDown(WPARAM virtualKeyCode, LPARAM keyData, bool systemKeyDown)
1935 {
1936     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1937     if (virtualKeyCode == VK_CAPITAL)
1938         frame->eventHandler()->capsLockStateMayHaveChanged();
1939
1940     PlatformKeyboardEvent keyEvent(m_viewWindow, virtualKeyCode, keyData, PlatformKeyboardEvent::RawKeyDown, systemKeyDown);
1941     bool handled = frame->eventHandler()->keyEvent(keyEvent);
1942
1943     // These events cannot be canceled, and we have no default handling for them.
1944     // FIXME: match IE list more closely, see <http://msdn2.microsoft.com/en-us/library/ms536938.aspx>.
1945     if (systemKeyDown && virtualKeyCode != VK_RETURN)
1946         return false;
1947
1948     if (handled) {
1949         // FIXME: remove WM_UNICHAR, too
1950         MSG msg;
1951         // WM_SYSCHAR events should not be removed, because access keys are implemented in WebCore in WM_SYSCHAR handler.
1952         if (!systemKeyDown)
1953             ::PeekMessage(&msg, m_viewWindow, WM_CHAR, WM_CHAR, PM_REMOVE);
1954         return true;
1955     }
1956
1957     // We need to handle back/forward using either Backspace(+Shift) or Ctrl+Left/Right Arrow keys.
1958     if ((virtualKeyCode == VK_BACK && keyEvent.shiftKey()) || (virtualKeyCode == VK_RIGHT && keyEvent.ctrlKey()))
1959         return m_page->goForward();
1960     if (virtualKeyCode == VK_BACK || (virtualKeyCode == VK_LEFT && keyEvent.ctrlKey()))
1961         return m_page->goBack();
1962
1963     // Need to scroll the page if the arrow keys, pgup/dn, or home/end are hit.
1964     ScrollDirection direction;
1965     ScrollGranularity granularity;
1966     switch (virtualKeyCode) {
1967         case VK_LEFT:
1968             granularity = ScrollByLine;
1969             direction = ScrollLeft;
1970             break;
1971         case VK_RIGHT:
1972             granularity = ScrollByLine;
1973             direction = ScrollRight;
1974             break;
1975         case VK_UP:
1976             granularity = ScrollByLine;
1977             direction = ScrollUp;
1978             break;
1979         case VK_DOWN:
1980             granularity = ScrollByLine;
1981             direction = ScrollDown;
1982             break;
1983         case VK_HOME:
1984             granularity = ScrollByDocument;
1985             direction = ScrollUp;
1986             break;
1987         case VK_END:
1988             granularity = ScrollByDocument;
1989             direction = ScrollDown;
1990             break;
1991         case VK_PRIOR:
1992             granularity = ScrollByPage;
1993             direction = ScrollUp;
1994             break;
1995         case VK_NEXT:
1996             granularity = ScrollByPage;
1997             direction = ScrollDown;
1998             break;
1999         default:
2000             return false;
2001     }
2002
2003     return frame->eventHandler()->scrollRecursively(direction, granularity);
2004 }
2005
2006 bool WebView::keyPress(WPARAM charCode, LPARAM keyData, bool systemKeyDown)
2007 {
2008     Frame* frame = m_page->focusController()->focusedOrMainFrame();
2009
2010     PlatformKeyboardEvent keyEvent(m_viewWindow, charCode, keyData, PlatformKeyboardEvent::Char, systemKeyDown);
2011     // IE does not dispatch keypress event for WM_SYSCHAR.
2012     if (systemKeyDown)
2013         return frame->eventHandler()->handleAccessKey(keyEvent);
2014     return frame->eventHandler()->keyEvent(keyEvent);
2015 }
2016
2017 bool WebView::registerWebViewWindowClass()
2018 {
2019     static bool haveRegisteredWindowClass = false;
2020     if (haveRegisteredWindowClass)
2021         return true;
2022
2023     haveRegisteredWindowClass = true;
2024
2025     WNDCLASSEX wcex;
2026
2027     wcex.cbSize = sizeof(WNDCLASSEX);
2028
2029     wcex.style          = CS_DBLCLKS;
2030     wcex.lpfnWndProc    = WebViewWndProc;
2031     wcex.cbClsExtra     = 0;
2032     wcex.cbWndExtra     = 4; // 4 bytes for the IWebView pointer
2033     wcex.hInstance      = gInstance;
2034     wcex.hIcon          = 0;
2035     wcex.hCursor        = ::LoadCursor(0, IDC_ARROW);
2036     wcex.hbrBackground  = 0;
2037     wcex.lpszMenuName   = 0;
2038     wcex.lpszClassName  = kWebViewWindowClassName;
2039     wcex.hIconSm        = 0;
2040
2041     return !!RegisterClassEx(&wcex);
2042 }
2043
2044 static HWND findTopLevelParent(HWND window)
2045 {
2046     if (!window)
2047         return 0;
2048
2049     HWND current = window;
2050     for (HWND parent = GetParent(current); current; current = parent, parent = GetParent(parent))
2051         if (!parent || !(GetWindowLongPtr(current, GWL_STYLE) & (WS_POPUP | WS_CHILD)))
2052             return current;
2053     ASSERT_NOT_REACHED();
2054     return 0;
2055 }
2056
2057 LRESULT CALLBACK WebView::WebViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2058 {
2059     LRESULT lResult = 0;
2060     LONG_PTR longPtr = GetWindowLongPtr(hWnd, 0);
2061     WebView* webView = reinterpret_cast<WebView*>(longPtr);
2062     WebFrame* mainFrameImpl = webView ? webView->topLevelFrame() : 0;
2063     if (!mainFrameImpl || webView->isBeingDestroyed())
2064         return DefWindowProc(hWnd, message, wParam, lParam);
2065
2066     // hold a ref, since the WebView could go away in an event handler.
2067     COMPtr<WebView> protector(webView);
2068     ASSERT(webView);
2069
2070     // Windows Media Player has a modal message loop that will deliver messages
2071     // to us at inappropriate times and we will crash if we handle them when
2072     // they are delivered. We repost paint messages so that we eventually get
2073     // a chance to paint once the modal loop has exited, but other messages
2074     // aren't safe to repost, so we just drop them.
2075     if (PluginView::isCallingPlugin()) {
2076         if (message == WM_PAINT)
2077             PostMessage(hWnd, message, wParam, lParam);
2078         return 0;
2079     }
2080
2081     bool handled = true;
2082
2083     switch (message) {
2084         case WM_PAINT: {
2085             webView->paint(0, 0);
2086             break;
2087         }
2088         case WM_PRINTCLIENT:
2089             webView->paint((HDC)wParam, lParam);
2090             break;
2091         case WM_DESTROY:
2092             webView->setIsBeingDestroyed();
2093             webView->close();
2094             break;
2095         case WM_GESTURENOTIFY:
2096             handled = webView->gestureNotify(wParam, lParam);
2097             break;
2098         case WM_GESTURE:
2099             handled = webView->gesture(wParam, lParam);
2100             break;
2101         case WM_MOUSEMOVE:
2102         case WM_LBUTTONDOWN:
2103         case WM_MBUTTONDOWN:
2104         case WM_RBUTTONDOWN:
2105         case WM_LBUTTONDBLCLK:
2106         case WM_MBUTTONDBLCLK:
2107         case WM_RBUTTONDBLCLK:
2108         case WM_LBUTTONUP:
2109         case WM_MBUTTONUP:
2110         case WM_RBUTTONUP:
2111         case WM_MOUSELEAVE:
2112         case WM_CANCELMODE:
2113             if (Frame* coreFrame = core(mainFrameImpl))
2114                 if (coreFrame->view()->didFirstLayout())
2115                     handled = webView->handleMouseEvent(message, wParam, lParam);
2116             break;
2117         case WM_MOUSEWHEEL:
2118         case WM_VISTA_MOUSEHWHEEL:
2119             if (Frame* coreFrame = core(mainFrameImpl))
2120                 if (coreFrame->view()->didFirstLayout())
2121                     handled = webView->mouseWheel(wParam, lParam, message == WM_VISTA_MOUSEHWHEEL);
2122             break;
2123         case WM_SYSKEYDOWN:
2124             handled = webView->keyDown(wParam, lParam, true);
2125             break;
2126         case WM_KEYDOWN:
2127             handled = webView->keyDown(wParam, lParam);
2128             break;
2129         case WM_SYSKEYUP:
2130             handled = webView->keyUp(wParam, lParam, true);
2131             break;
2132         case WM_KEYUP:
2133             handled = webView->keyUp(wParam, lParam);
2134             break;
2135         case WM_SYSCHAR:
2136             handled = webView->keyPress(wParam, lParam, true);
2137             break;
2138         case WM_CHAR:
2139             handled = webView->keyPress(wParam, lParam);
2140             break;
2141         // FIXME: We need to check WM_UNICHAR to support supplementary characters (that don't fit in 16 bits).
2142         case WM_SIZE:
2143             if (lParam != 0)
2144                 webView->sizeChanged(IntSize(LOWORD(lParam), HIWORD(lParam)));
2145             break;
2146         case WM_SHOWWINDOW:
2147             lResult = DefWindowProc(hWnd, message, wParam, lParam);
2148             if (wParam == 0) {
2149                 // The window is being hidden (e.g., because we switched tabs).
2150                 // Null out our backing store.
2151                 webView->deleteBackingStore();
2152             }
2153             break;
2154         case WM_SETFOCUS: {
2155             COMPtr<IWebUIDelegate> uiDelegate;
2156             COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2157             if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2158                 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate)
2159                 uiDelegatePrivate->webViewReceivedFocus(webView);
2160
2161             FocusController* focusController = webView->page()->focusController();
2162             if (Frame* frame = focusController->focusedFrame()) {
2163                 // Send focus events unless the previously focused window is a
2164                 // child of ours (for example a plugin).
2165                 if (!IsChild(hWnd, reinterpret_cast<HWND>(wParam)))
2166                     focusController->setFocused(true);
2167             } else
2168                 focusController->setFocused(true);
2169             break;
2170         }
2171         case WM_KILLFOCUS: {
2172             COMPtr<IWebUIDelegate> uiDelegate;
2173             COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2174             HWND newFocusWnd = reinterpret_cast<HWND>(wParam);
2175             if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2176                 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate)
2177                 uiDelegatePrivate->webViewLostFocus(webView, (OLE_HANDLE)(ULONG64)newFocusWnd);
2178
2179             FocusController* focusController = webView->page()->focusController();
2180             Frame* frame = focusController->focusedOrMainFrame();
2181             webView->resetIME(frame);
2182             // Send blur events unless we're losing focus to a child of ours.
2183             if (!IsChild(hWnd, newFocusWnd))
2184                 focusController->setFocused(false);
2185
2186             // If we are pan-scrolling when we lose focus, stop the pan scrolling.
2187             frame->eventHandler()->stopAutoscrollTimer();
2188
2189             break;
2190         }
2191         case WM_WINDOWPOSCHANGED:
2192             if (reinterpret_cast<WINDOWPOS*>(lParam)->flags & SWP_SHOWWINDOW)
2193                 webView->updateActiveStateSoon();
2194             handled = false;
2195             break;
2196         case WM_CUT:
2197             webView->cut(0);
2198             break;
2199         case WM_COPY:
2200             webView->copy(0);
2201             break;
2202         case WM_PASTE:
2203             webView->paste(0);
2204             break;
2205         case WM_CLEAR:
2206             webView->delete_(0);
2207             break;
2208         case WM_COMMAND:
2209             if (HIWORD(wParam))
2210                 handled = webView->execCommand(wParam, lParam);
2211             else // If the high word of wParam is 0, the message is from a menu
2212                 webView->performContextMenuAction(wParam, lParam, false);
2213             break;
2214         case WM_MENUCOMMAND:
2215             webView->performContextMenuAction(wParam, lParam, true);
2216             break;
2217         case WM_CONTEXTMENU:
2218             handled = webView->handleContextMenuEvent(wParam, lParam);
2219             break;
2220         case WM_INITMENUPOPUP:
2221             handled = webView->onInitMenuPopup(wParam, lParam);
2222             break;
2223         case WM_MEASUREITEM:
2224             handled = webView->onMeasureItem(wParam, lParam);
2225             break;
2226         case WM_DRAWITEM:
2227             handled = webView->onDrawItem(wParam, lParam);
2228             break;
2229         case WM_UNINITMENUPOPUP:
2230             handled = webView->onUninitMenuPopup(wParam, lParam);
2231             break;
2232         case WM_XP_THEMECHANGED:
2233             if (Frame* coreFrame = core(mainFrameImpl)) {
2234                 webView->deleteBackingStore();
2235                 coreFrame->page()->theme()->themeChanged();
2236                 ScrollbarTheme::nativeTheme()->themeChanged();
2237                 RECT windowRect;
2238                 ::GetClientRect(hWnd, &windowRect);
2239                 ::InvalidateRect(hWnd, &windowRect, false);
2240 #if USE(ACCELERATED_COMPOSITING)
2241                 if (webView->isAcceleratedCompositing())
2242                     webView->m_backingLayer->setNeedsDisplay();
2243 #endif
2244            }
2245             break;
2246         case WM_MOUSEACTIVATE:
2247             webView->setMouseActivated(true);
2248             handled = false;
2249             break;
2250         case WM_GETDLGCODE: {
2251             COMPtr<IWebUIDelegate> uiDelegate;
2252             COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2253             LONG_PTR dlgCode = 0;
2254             UINT keyCode = 0;
2255             if (lParam) {
2256                 LPMSG lpMsg = (LPMSG)lParam;
2257                 if (lpMsg->message == WM_KEYDOWN)
2258                     keyCode = (UINT) lpMsg->wParam;
2259             }
2260             if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2261                 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate
2262                 && SUCCEEDED(uiDelegatePrivate->webViewGetDlgCode(webView, keyCode, &dlgCode)))
2263                 return dlgCode;
2264             handled = false;
2265             break;
2266         }
2267         case WM_GETOBJECT:
2268             handled = webView->onGetObject(wParam, lParam, lResult);
2269             break;
2270         case WM_IME_STARTCOMPOSITION:
2271             handled = webView->onIMEStartComposition();
2272             break;
2273         case WM_IME_REQUEST:
2274             lResult = webView->onIMERequest(wParam, lParam);
2275             break;
2276         case WM_IME_COMPOSITION:
2277             handled = webView->onIMEComposition(lParam);
2278             break;
2279         case WM_IME_ENDCOMPOSITION:
2280             handled = webView->onIMEEndComposition();
2281             break;
2282         case WM_IME_CHAR:
2283             handled = webView->onIMEChar(wParam, lParam);
2284             break;
2285         case WM_IME_NOTIFY:
2286             handled = webView->onIMENotify(wParam, lParam, &lResult);
2287             break;
2288         case WM_IME_SELECT:
2289             handled = webView->onIMESelect(wParam, lParam);
2290             break;
2291         case WM_IME_SETCONTEXT:
2292             handled = webView->onIMESetContext(wParam, lParam);
2293             break;
2294         case WM_TIMER:
2295             switch (wParam) {
2296                 case UpdateActiveStateTimer:
2297                     KillTimer(hWnd, UpdateActiveStateTimer);
2298                     webView->updateActiveState();
2299                     break;
2300                 case DeleteBackingStoreTimer:
2301                     webView->deleteBackingStore();
2302                     break;
2303             }
2304             break;
2305         case WM_SETCURSOR:
2306             handled = ::SetCursor(webView->m_lastSetCursor);
2307             break;
2308         case WM_VSCROLL:
2309             handled = webView->verticalScroll(wParam, lParam);
2310             break;
2311         case WM_HSCROLL:
2312             handled = webView->horizontalScroll(wParam, lParam);
2313             break;
2314         default:
2315             handled = false;
2316             break;
2317     }
2318
2319     if (!handled)
2320         lResult = DefWindowProc(hWnd, message, wParam, lParam);
2321     
2322     // Let the client know whether we consider this message handled.
2323     return (message == WM_KEYDOWN || message == WM_SYSKEYDOWN || message == WM_KEYUP || message == WM_SYSKEYUP) ? !handled : lResult;
2324 }
2325
2326 bool WebView::developerExtrasEnabled() const
2327 {
2328     if (m_preferences->developerExtrasDisabledByOverride())
2329         return false;
2330
2331 #ifdef NDEBUG
2332     BOOL enabled;
2333     return SUCCEEDED(m_preferences->developerExtrasEnabled(&enabled)) && enabled;
2334 #else
2335     return true;
2336 #endif
2337 }
2338
2339 static String osVersion()
2340 {
2341     String osVersion;
2342     OSVERSIONINFO versionInfo = {0};
2343     versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
2344     GetVersionEx(&versionInfo);
2345
2346     if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
2347         if (versionInfo.dwMajorVersion == 4) {
2348             if (versionInfo.dwMinorVersion == 0)
2349                 osVersion = "Windows 95";
2350             else if (versionInfo.dwMinorVersion == 10)
2351                 osVersion = "Windows 98";
2352             else if (versionInfo.dwMinorVersion == 90)
2353                 osVersion = "Windows 98; Win 9x 4.90";
2354         }
2355     } else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
2356         osVersion = makeString("Windows NT ", String::number(versionInfo.dwMajorVersion), '.', String::number(versionInfo.dwMinorVersion));
2357
2358     if (!osVersion.length())
2359         osVersion = makeString("Windows ", String::number(versionInfo.dwMajorVersion), '.', String::number(versionInfo.dwMinorVersion));
2360     return osVersion;
2361 }
2362
2363 static String webKitVersion()
2364 {
2365     LPWSTR buildNumberStringPtr;
2366     if (!::LoadStringW(gInstance, BUILD_NUMBER, reinterpret_cast<LPWSTR>(&buildNumberStringPtr), 0) || !buildNumberStringPtr)
2367         return "534+";
2368
2369     return buildNumberStringPtr;
2370 }
2371
2372 const String& WebView::userAgentForKURL(const KURL&)
2373 {
2374     if (m_userAgentOverridden)
2375         return m_userAgentCustom;
2376
2377     if (!m_userAgentStandard.length())
2378         m_userAgentStandard = WebView::standardUserAgentWithApplicationName(m_applicationName);
2379     return m_userAgentStandard;
2380 }
2381
2382 // IUnknown -------------------------------------------------------------------
2383
2384 HRESULT STDMETHODCALLTYPE WebView::QueryInterface(REFIID riid, void** ppvObject)
2385 {
2386     *ppvObject = 0;
2387     if (IsEqualGUID(riid, CLSID_WebView))
2388         *ppvObject = this;
2389     else if (IsEqualGUID(riid, IID_IUnknown))
2390         *ppvObject = static_cast<IWebView*>(this);
2391     else if (IsEqualGUID(riid, IID_IWebView))
2392         *ppvObject = static_cast<IWebView*>(this);
2393     else if (IsEqualGUID(riid, IID_IWebViewPrivate))
2394         *ppvObject = static_cast<IWebViewPrivate*>(this);
2395     else if (IsEqualGUID(riid, IID_IWebIBActions))
2396         *ppvObject = static_cast<IWebIBActions*>(this);
2397     else if (IsEqualGUID(riid, IID_IWebViewCSS))
2398         *ppvObject = static_cast<IWebViewCSS*>(this);
2399     else if (IsEqualGUID(riid, IID_IWebViewEditing))
2400         *ppvObject = static_cast<IWebViewEditing*>(this);
2401     else if (IsEqualGUID(riid, IID_IWebViewUndoableEditing))
2402         *ppvObject = static_cast<IWebViewUndoableEditing*>(this);
2403     else if (IsEqualGUID(riid, IID_IWebViewEditingActions))
2404         *ppvObject = static_cast<IWebViewEditingActions*>(this);
2405     else if (IsEqualGUID(riid, IID_IWebNotificationObserver))
2406         *ppvObject = static_cast<IWebNotificationObserver*>(this);
2407     else if (IsEqualGUID(riid, IID_IDropTarget))
2408         *ppvObject = static_cast<IDropTarget*>(this);
2409     else
2410         return E_NOINTERFACE;
2411
2412     AddRef();
2413     return S_OK;
2414 }
2415
2416 ULONG STDMETHODCALLTYPE WebView::AddRef(void)
2417 {
2418     ASSERT(!m_deletionHasBegun);
2419     return ++m_refCount;
2420 }
2421
2422 ULONG STDMETHODCALLTYPE WebView::Release(void)
2423 {
2424     ASSERT(!m_deletionHasBegun);
2425
2426     if (m_refCount == 1) {
2427         // Call close() now so that clients don't have to. (It's harmless to call close() multiple
2428         // times.) We do this here instead of in our destructor because close() can cause AddRef()
2429         // and Release() to be called, and if that happened in our destructor we would be destroyed
2430         // more than once.
2431         close();
2432     }
2433
2434     ULONG newRef = --m_refCount;
2435     if (!newRef) {
2436 #if !ASSERT_DISABLED
2437         m_deletionHasBegun = true;
2438 #endif
2439         delete(this);
2440     }
2441
2442     return newRef;
2443 }
2444
2445 // IWebView --------------------------------------------------------------------
2446
2447 HRESULT STDMETHODCALLTYPE WebView::canShowMIMEType( 
2448     /* [in] */ BSTR mimeType,
2449     /* [retval][out] */ BOOL* canShow)
2450 {
2451     String mimeTypeStr(mimeType, SysStringLen(mimeType));
2452
2453     if (!canShow)
2454         return E_POINTER;
2455
2456     *canShow = MIMETypeRegistry::isSupportedImageMIMEType(mimeTypeStr) ||
2457         MIMETypeRegistry::isSupportedNonImageMIMEType(mimeTypeStr) ||
2458         (m_page && m_page->pluginData() && m_page->pluginData()->supportsMimeType(mimeTypeStr)) ||
2459         shouldUseEmbeddedView(mimeTypeStr);
2460     
2461     return S_OK;
2462 }
2463
2464 HRESULT STDMETHODCALLTYPE WebView::canShowMIMETypeAsHTML( 
2465     /* [in] */ BSTR /*mimeType*/,
2466     /* [retval][out] */ BOOL* canShow)
2467 {
2468     // FIXME
2469     *canShow = TRUE;
2470     return S_OK;
2471 }
2472
2473 HRESULT STDMETHODCALLTYPE WebView::MIMETypesShownAsHTML( 
2474     /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
2475 {
2476     ASSERT_NOT_REACHED();
2477     return E_NOTIMPL;
2478 }
2479
2480 HRESULT STDMETHODCALLTYPE WebView::setMIMETypesShownAsHTML( 
2481         /* [size_is][in] */ BSTR* /*mimeTypes*/,
2482         /* [in] */ int /*cMimeTypes*/)
2483 {
2484     ASSERT_NOT_REACHED();
2485     return E_NOTIMPL;
2486 }
2487
2488 HRESULT STDMETHODCALLTYPE WebView::URLFromPasteboard( 
2489     /* [in] */ IDataObject* /*pasteboard*/,
2490     /* [retval][out] */ BSTR* /*url*/)
2491 {
2492     ASSERT_NOT_REACHED();
2493     return E_NOTIMPL;
2494 }
2495
2496 HRESULT STDMETHODCALLTYPE WebView::URLTitleFromPasteboard( 
2497     /* [in] */ IDataObject* /*pasteboard*/,
2498     /* [retval][out] */ BSTR* /*urlTitle*/)
2499 {
2500     ASSERT_NOT_REACHED();
2501     return E_NOTIMPL;
2502 }
2503
2504 static void WebKitSetApplicationCachePathIfNecessary()
2505 {
2506     static bool initialized = false;
2507     if (initialized)
2508         return;
2509
2510     String path = localUserSpecificStorageDirectory();
2511     if (!path.isNull())
2512         cacheStorage().setCacheDirectory(path);
2513
2514     initialized = true;
2515 }
2516
2517 bool WebView::shouldInitializeTrackPointHack()
2518 {
2519     static bool shouldCreateScrollbars;
2520     static bool hasRunTrackPointCheck;
2521
2522     if (hasRunTrackPointCheck)
2523         return shouldCreateScrollbars;
2524
2525     hasRunTrackPointCheck = true;
2526     const TCHAR trackPointKeys[][50] = { TEXT("Software\\Lenovo\\TrackPoint"),
2527         TEXT("Software\\Lenovo\\UltraNav"),
2528         TEXT("Software\\Alps\\Apoint\\TrackPoint"),
2529         TEXT("Software\\Synaptics\\SynTPEnh\\UltraNavUSB"),
2530         TEXT("Software\\Synaptics\\SynTPEnh\\UltraNavPS2") };
2531
2532     for (int i = 0; i < 5; ++i) {
2533         HKEY trackPointKey;
2534         int readKeyResult = ::RegOpenKeyEx(HKEY_CURRENT_USER, trackPointKeys[i], 0, KEY_READ, &trackPointKey);
2535         ::RegCloseKey(trackPointKey);
2536         if (readKeyResult == ERROR_SUCCESS) {
2537             shouldCreateScrollbars = true;
2538             return shouldCreateScrollbars;
2539         }
2540     }
2541
2542     return shouldCreateScrollbars;
2543 }
2544     
2545 HRESULT STDMETHODCALLTYPE WebView::initWithFrame( 
2546     /* [in] */ RECT frame,
2547     /* [in] */ BSTR frameName,
2548     /* [in] */ BSTR groupName)
2549 {
2550     HRESULT hr = S_OK;
2551
2552     if (m_viewWindow)
2553         return E_FAIL;
2554
2555     registerWebViewWindowClass();
2556
2557     m_viewWindow = CreateWindowEx(0, kWebViewWindowClassName, 0, WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
2558         frame.left, frame.top, frame.right - frame.left, frame.bottom - frame.top, m_hostWindow ? m_hostWindow : HWND_MESSAGE, 0, gInstance, 0);
2559     ASSERT(::IsWindow(m_viewWindow));
2560
2561     if (shouldInitializeTrackPointHack()) {
2562         // If we detected a registry key belonging to a TrackPoint driver, then create fake trackpoint
2563         // scrollbars, so the WebView will receive WM_VSCROLL and WM_HSCROLL messages. We create one
2564         // vertical scrollbar and one horizontal to allow for receiving both types of messages.
2565         ::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTHSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_HORZ, 0, 0, 0, 0, m_viewWindow, 0, gInstance, 0);
2566         ::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTVSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_VERT, 0, 0, 0, 0, m_viewWindow, 0, gInstance, 0);
2567     }
2568
2569     hr = registerDragDrop();
2570     if (FAILED(hr))
2571         return hr;
2572
2573     WebPreferences* sharedPreferences = WebPreferences::sharedStandardPreferences();
2574     sharedPreferences->willAddToWebView();
2575     m_preferences = sharedPreferences;
2576
2577     static bool didOneTimeInitialization;
2578     if (!didOneTimeInitialization) {
2579         InitializeLoggingChannelsIfNecessary();
2580 #if ENABLE(DATABASE)
2581         WebKitInitializeWebDatabasesIfNecessary();
2582 #endif
2583         WebKitSetApplicationCachePathIfNecessary();
2584         WebPlatformStrategies::initialize();
2585         Settings::setDefaultMinDOMTimerInterval(0.004);
2586
2587         didOneTimeInitialization = true;
2588      }
2589
2590 #if USE(SAFARI_THEME)
2591     BOOL shouldPaintNativeControls;
2592     if (SUCCEEDED(m_preferences->shouldPaintNativeControls(&shouldPaintNativeControls)))
2593         Settings::setShouldPaintNativeControls(shouldPaintNativeControls);
2594 #endif
2595
2596     BOOL useHighResolutionTimer;
2597     if (SUCCEEDED(m_preferences->shouldUseHighResolutionTimers(&useHighResolutionTimer)))
2598         Settings::setShouldUseHighResolutionTimers(useHighResolutionTimer);
2599
2600     Page::PageClients pageClients;
2601     pageClients.chromeClient = new WebChromeClient(this);
2602     pageClients.contextMenuClient = new WebContextMenuClient(this);
2603     pageClients.editorClient = new WebEditorClient(this);
2604     pageClients.dragClient = new WebDragClient(this);
2605     pageClients.inspectorClient = new WebInspectorClient(this);
2606     pageClients.pluginHalterClient = new WebPluginHalterClient(this);
2607 #if ENABLE(CLIENT_BASED_GEOLOCATION)
2608     pageClients.geolocationClient = new WebGeolocationClient(this);
2609 #endif
2610     m_page = new Page(pageClients);
2611
2612     BSTR localStoragePath;
2613     if (SUCCEEDED(m_preferences->localStorageDatabasePath(&localStoragePath))) {
2614         m_page->settings()->setLocalStorageDatabasePath(String(localStoragePath, SysStringLen(localStoragePath)));
2615         SysFreeString(localStoragePath);
2616     }
2617
2618     if (m_uiDelegate) {
2619         BSTR path;
2620         if (SUCCEEDED(m_uiDelegate->ftpDirectoryTemplatePath(this, &path))) {
2621             m_page->settings()->setFTPDirectoryTemplatePath(String(path, SysStringLen(path)));
2622             SysFreeString(path);
2623         }
2624     }
2625
2626     WebFrame* webFrame = WebFrame::createInstance();
2627     RefPtr<Frame> coreFrame = webFrame->init(this, m_page, 0);
2628     m_mainFrame = webFrame;
2629     webFrame->Release(); // The WebFrame is owned by the Frame, so release our reference to it.
2630
2631     coreFrame->tree()->setName(String(frameName, SysStringLen(frameName)));
2632     coreFrame->init();
2633     setGroupName(groupName);
2634
2635     addToAllWebViewsSet();
2636
2637     #pragma warning(suppress: 4244)
2638     SetWindowLongPtr(m_viewWindow, 0, (LONG_PTR)this);
2639     ShowWindow(m_viewWindow, SW_SHOW);
2640
2641     initializeToolTipWindow();
2642     windowAncestryDidChange();
2643
2644     IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
2645     notifyCenter->addObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
2646     m_preferences->postPreferencesChangesNotification();
2647
2648     setSmartInsertDeleteEnabled(TRUE);
2649     return hr;
2650 }
2651
2652 static bool initCommonControls()
2653 {
2654     static bool haveInitialized = false;
2655     if (haveInitialized)
2656         return true;
2657
2658     INITCOMMONCONTROLSEX init;
2659     init.dwSize = sizeof(init);
2660     init.dwICC = ICC_TREEVIEW_CLASSES;
2661     haveInitialized = !!::InitCommonControlsEx(&init);
2662     return haveInitialized;
2663 }
2664
2665 void WebView::initializeToolTipWindow()
2666 {
2667     if (!initCommonControls())
2668         return;
2669
2670     m_toolTipHwnd = CreateWindowEx(WS_EX_TRANSPARENT, TOOLTIPS_CLASS, 0, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
2671                                    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2672                                    m_viewWindow, 0, 0, 0);
2673     if (!m_toolTipHwnd)
2674         return;
2675
2676     TOOLINFO info = {0};
2677     info.cbSize = sizeof(info);
2678     info.uFlags = TTF_IDISHWND | TTF_SUBCLASS ;
2679     info.uId = reinterpret_cast<UINT_PTR>(m_viewWindow);
2680
2681     ::SendMessage(m_toolTipHwnd, TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&info));
2682     ::SendMessage(m_toolTipHwnd, TTM_SETMAXTIPWIDTH, 0, maxToolTipWidth);
2683
2684     ::SetWindowPos(m_toolTipHwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
2685 }
2686
2687 void WebView::setToolTip(const String& toolTip)
2688 {
2689     if (!m_toolTipHwnd)
2690         return;
2691
2692     if (toolTip == m_toolTip)
2693         return;
2694
2695     m_toolTip = toolTip;
2696
2697     if (!m_toolTip.isEmpty()) {
2698         TOOLINFO info = {0};
2699         info.cbSize = sizeof(info);
2700         info.uFlags = TTF_IDISHWND;
2701         info.uId = reinterpret_cast<UINT_PTR>(m_viewWindow);
2702         info.lpszText = const_cast<UChar*>(m_toolTip.charactersWithNullTermination());
2703         ::SendMessage(m_toolTipHwnd, TTM_UPDATETIPTEXT, 0, reinterpret_cast<LPARAM>(&info));
2704     }
2705
2706     ::SendMessage(m_toolTipHwnd, TTM_ACTIVATE, !m_toolTip.isEmpty(), 0);
2707 }
2708
2709 HRESULT WebView::notifyDidAddIcon(IWebNotification* notification)
2710 {
2711     COMPtr<IPropertyBag> propertyBag;
2712     HRESULT hr = notification->userInfo(&propertyBag);
2713     if (FAILED(hr))
2714         return hr;
2715     if (!propertyBag)
2716         return E_FAIL;
2717
2718     COMPtr<CFDictionaryPropertyBag> dictionaryPropertyBag;
2719     hr = propertyBag->QueryInterface(&dictionaryPropertyBag);
2720     if (FAILED(hr))
2721         return hr;
2722
2723     CFDictionaryRef dictionary = dictionaryPropertyBag->dictionary();
2724     if (!dictionary)
2725         return E_FAIL;
2726
2727     CFTypeRef value = CFDictionaryGetValue(dictionary, WebIconDatabase::iconDatabaseNotificationUserInfoURLKey());
2728     if (!value)
2729         return E_FAIL;
2730     if (CFGetTypeID(value) != CFStringGetTypeID())
2731         return E_FAIL;
2732
2733     String mainFrameURL;
2734     if (m_mainFrame)
2735         mainFrameURL = m_mainFrame->url().string();
2736
2737     if (!mainFrameURL.isEmpty() && mainFrameURL == String((CFStringRef)value))
2738         dispatchDidReceiveIconFromWebFrame(m_mainFrame);
2739
2740     return hr;
2741 }
2742
2743 void WebView::registerForIconNotification(bool listen)
2744 {
2745     IWebNotificationCenter* nc = WebNotificationCenter::defaultCenterInternal();
2746     if (listen)
2747         nc->addObserver(this, WebIconDatabase::iconDatabaseDidAddIconNotification(), 0);
2748     else
2749         nc->removeObserver(this, WebIconDatabase::iconDatabaseDidAddIconNotification(), 0);
2750 }
2751
2752 void WebView::dispatchDidReceiveIconFromWebFrame(WebFrame* frame)
2753 {
2754     registerForIconNotification(false);
2755
2756     if (m_frameLoadDelegate)
2757         // FIXME: <rdar://problem/5491010> - Pass in the right HBITMAP. 
2758         m_frameLoadDelegate->didReceiveIcon(this, 0, frame);
2759 }
2760
2761 HRESULT STDMETHODCALLTYPE WebView::setUIDelegate( 
2762     /* [in] */ IWebUIDelegate* d)
2763 {
2764     m_uiDelegate = d;
2765
2766     if (m_uiDelegatePrivate)
2767         m_uiDelegatePrivate = 0;
2768
2769     if (d) {
2770         if (FAILED(d->QueryInterface(IID_IWebUIDelegatePrivate, (void**)&m_uiDelegatePrivate)))
2771             m_uiDelegatePrivate = 0;
2772     }
2773
2774     return S_OK;
2775 }
2776
2777 HRESULT STDMETHODCALLTYPE WebView::uiDelegate( 
2778     /* [out][retval] */ IWebUIDelegate** d)
2779 {
2780     if (!m_uiDelegate)
2781         return E_FAIL;
2782
2783     return m_uiDelegate.copyRefTo(d);
2784 }
2785
2786 HRESULT STDMETHODCALLTYPE WebView::setResourceLoadDelegate( 
2787     /* [in] */ IWebResourceLoadDelegate* d)
2788 {
2789     m_resourceLoadDelegate = d;
2790     return S_OK;
2791 }
2792
2793 HRESULT STDMETHODCALLTYPE WebView::resourceLoadDelegate( 
2794     /* [out][retval] */ IWebResourceLoadDelegate** d)
2795 {
2796     if (!m_resourceLoadDelegate)
2797         return E_FAIL;
2798
2799     return m_resourceLoadDelegate.copyRefTo(d);
2800 }
2801
2802 HRESULT STDMETHODCALLTYPE WebView::setDownloadDelegate( 
2803     /* [in] */ IWebDownloadDelegate* d)
2804 {
2805     m_downloadDelegate = d;
2806     return S_OK;
2807 }
2808
2809 HRESULT STDMETHODCALLTYPE WebView::downloadDelegate( 
2810     /* [out][retval] */ IWebDownloadDelegate** d)
2811 {
2812     if (!m_downloadDelegate)
2813         return E_FAIL;
2814
2815     return m_downloadDelegate.copyRefTo(d);
2816 }
2817
2818 HRESULT STDMETHODCALLTYPE WebView::setFrameLoadDelegate( 
2819     /* [in] */ IWebFrameLoadDelegate* d)
2820 {
2821     m_frameLoadDelegate = d;
2822     return S_OK;
2823 }
2824
2825 HRESULT STDMETHODCALLTYPE WebView::frameLoadDelegate( 
2826     /* [out][retval] */ IWebFrameLoadDelegate** d)
2827 {
2828     if (!m_frameLoadDelegate)
2829         return E_FAIL;
2830
2831     return m_frameLoadDelegate.copyRefTo(d);
2832 }
2833
2834 HRESULT STDMETHODCALLTYPE WebView::setPolicyDelegate( 
2835     /* [in] */ IWebPolicyDelegate* d)
2836 {
2837     m_policyDelegate = d;
2838     return S_OK;
2839 }
2840
2841 HRESULT STDMETHODCALLTYPE WebView::policyDelegate( 
2842     /* [out][retval] */ IWebPolicyDelegate** d)
2843 {
2844     if (!m_policyDelegate)
2845         return E_FAIL;
2846     return m_policyDelegate.copyRefTo(d);
2847 }
2848
2849 HRESULT STDMETHODCALLTYPE WebView::mainFrame( 
2850     /* [out][retval] */ IWebFrame** frame)
2851 {
2852     if (!frame) {
2853         ASSERT_NOT_REACHED();
2854         return E_POINTER;
2855     }
2856
2857     *frame = m_mainFrame;
2858     if (!m_mainFrame)
2859         return E_FAIL;
2860
2861     m_mainFrame->AddRef();
2862     return S_OK;
2863 }
2864
2865 HRESULT STDMETHODCALLTYPE WebView::focusedFrame( 
2866     /* [out][retval] */ IWebFrame** frame)
2867 {
2868     if (!frame) {
2869         ASSERT_NOT_REACHED();
2870         return E_POINTER;
2871     }
2872
2873     *frame = 0;
2874     Frame* f = m_page->focusController()->focusedFrame();
2875     if (!f)
2876         return E_FAIL;
2877
2878     WebFrame* webFrame = kit(f);
2879     if (!webFrame)
2880         return E_FAIL;
2881
2882     return webFrame->QueryInterface(IID_IWebFrame, (void**) frame);
2883 }
2884
2885 HRESULT STDMETHODCALLTYPE WebView::backForwardList( 
2886     /* [out][retval] */ IWebBackForwardList** list)
2887 {
2888     if (!m_useBackForwardList)
2889         return E_FAIL;
2890  
2891     *list = WebBackForwardList::createInstance(static_cast<WebCore::BackForwardListImpl*>(m_page->backForwardList()));
2892
2893     return S_OK;
2894 }
2895
2896 HRESULT STDMETHODCALLTYPE WebView::setMaintainsBackForwardList( 
2897     /* [in] */ BOOL flag)
2898 {
2899     m_useBackForwardList = !!flag;
2900     return S_OK;
2901 }
2902
2903 HRESULT STDMETHODCALLTYPE WebView::goBack( 
2904     /* [retval][out] */ BOOL* succeeded)
2905 {
2906     *succeeded = m_page->goBack();
2907     return S_OK;
2908 }
2909
2910 HRESULT STDMETHODCALLTYPE WebView::goForward( 
2911     /* [retval][out] */ BOOL* succeeded)
2912 {
2913     *succeeded = m_page->goForward();
2914     return S_OK;
2915 }
2916
2917 HRESULT STDMETHODCALLTYPE WebView::goToBackForwardItem( 
2918     /* [in] */ IWebHistoryItem* item,
2919     /* [retval][out] */ BOOL* succeeded)
2920 {
2921     *succeeded = FALSE;
2922
2923     COMPtr<WebHistoryItem> webHistoryItem;
2924     HRESULT hr = item->QueryInterface(&webHistoryItem);
2925     if (FAILED(hr))
2926         return hr;
2927
2928     m_page->goToItem(webHistoryItem->historyItem(), FrameLoadTypeIndexedBackForward);
2929     *succeeded = TRUE;
2930
2931     return S_OK;
2932 }
2933
2934 HRESULT STDMETHODCALLTYPE WebView::setTextSizeMultiplier( 
2935     /* [in] */ float multiplier)
2936 {
2937     if (!m_mainFrame)
2938         return E_FAIL;
2939     setZoomMultiplier(multiplier, true);
2940     return S_OK;
2941 }
2942
2943 HRESULT STDMETHODCALLTYPE WebView::setPageSizeMultiplier( 
2944     /* [in] */ float multiplier)
2945 {
2946     if (!m_mainFrame)
2947         return E_FAIL;
2948     setZoomMultiplier(multiplier, false);
2949     return S_OK;
2950 }
2951
2952 void WebView::setZoomMultiplier(float multiplier, bool isTextOnly)
2953 {
2954     m_zoomMultiplier = multiplier;
2955     m_zoomsTextOnly = isTextOnly;
2956
2957     if (Frame* coreFrame = core(m_mainFrame)) {
2958         if (m_zoomsTextOnly)
2959             coreFrame->setPageAndTextZoomFactors(1, multiplier);
2960         else
2961             coreFrame->setPageAndTextZoomFactors(multiplier, 1);
2962     }
2963 }
2964
2965 HRESULT STDMETHODCALLTYPE WebView::textSizeMultiplier( 
2966     /* [retval][out] */ float* multiplier)
2967 {
2968     *multiplier = zoomMultiplier(true);
2969     return S_OK;
2970 }
2971
2972 HRESULT STDMETHODCALLTYPE WebView::pageSizeMultiplier( 
2973     /* [retval][out] */ float* multiplier)
2974 {
2975     *multiplier = zoomMultiplier(false);
2976     return S_OK;
2977 }
2978
2979 float WebView::zoomMultiplier(bool isTextOnly)
2980 {
2981     if (isTextOnly != m_zoomsTextOnly)
2982         return 1.0f;
2983     return m_zoomMultiplier;
2984 }
2985
2986 HRESULT STDMETHODCALLTYPE WebView::setApplicationNameForUserAgent( 
2987     /* [in] */ BSTR applicationName)
2988 {
2989     m_applicationName = String(applicationName, SysStringLen(applicationName));
2990     m_userAgentStandard = String();
2991     return S_OK;
2992 }
2993
2994 HRESULT STDMETHODCALLTYPE WebView::applicationNameForUserAgent( 
2995     /* [retval][out] */ BSTR* applicationName)
2996 {
2997     *applicationName = SysAllocStringLen(m_applicationName.characters(), m_applicationName.length());
2998     if (!*applicationName && m_applicationName.length())
2999         return E_OUTOFMEMORY;
3000     return S_OK;
3001 }
3002
3003 HRESULT STDMETHODCALLTYPE WebView::setCustomUserAgent( 
3004     /* [in] */ BSTR userAgentString)
3005 {
3006     m_userAgentOverridden = userAgentString;
3007     m_userAgentCustom = String(userAgentString, SysStringLen(userAgentString));
3008     return S_OK;
3009 }
3010
3011 HRESULT STDMETHODCALLTYPE WebView::customUserAgent( 
3012     /* [retval][out] */ BSTR* userAgentString)
3013 {
3014     *userAgentString = 0;
3015     if (!m_userAgentOverridden)
3016         return S_OK;
3017     *userAgentString = SysAllocStringLen(m_userAgentCustom.characters(), m_userAgentCustom.length());
3018     if (!*userAgentString && m_userAgentCustom.length())
3019         return E_OUTOFMEMORY;
3020     return S_OK;
3021 }
3022
3023 HRESULT STDMETHODCALLTYPE WebView::userAgentForURL( 
3024     /* [in] */ BSTR url,
3025     /* [retval][out] */ BSTR* userAgent)
3026 {
3027     String userAgentString = userAgentForKURL(MarshallingHelpers::BSTRToKURL(url));
3028     *userAgent = SysAllocStringLen(userAgentString.characters(), userAgentString.length());
3029     if (!*userAgent && userAgentString.length())
3030         return E_OUTOFMEMORY;
3031     return S_OK;
3032 }
3033
3034 HRESULT STDMETHODCALLTYPE WebView::supportsTextEncoding( 
3035     /* [retval][out] */ BOOL* supports)
3036 {
3037     *supports = TRUE;
3038     return S_OK;
3039 }
3040
3041 HRESULT STDMETHODCALLTYPE WebView::setCustomTextEncodingName( 
3042     /* [in] */ BSTR encodingName)
3043 {
3044     if (!m_mainFrame)
3045         return E_FAIL;
3046
3047     HRESULT hr;
3048     BSTR oldEncoding;
3049     hr = customTextEncodingName(&oldEncoding);
3050     if (FAILED(hr))
3051         return hr;
3052
3053     if (oldEncoding != encodingName && (!oldEncoding || !encodingName || _tcscmp(oldEncoding, encodingName))) {
3054         if (Frame* coreFrame = core(m_mainFrame))
3055             coreFrame->loader()->reloadWithOverrideEncoding(String(encodingName, SysStringLen(encodingName)));
3056     }
3057
3058     return S_OK;
3059 }
3060
3061 HRESULT STDMETHODCALLTYPE WebView::customTextEncodingName( 
3062     /* [retval][out] */ BSTR* encodingName)
3063 {
3064     HRESULT hr = S_OK;
3065     COMPtr<IWebDataSource> dataSource;
3066     COMPtr<WebDataSource> dataSourceImpl;
3067     *encodingName = 0;
3068
3069     if (!m_mainFrame)
3070         return E_FAIL;
3071
3072     if (FAILED(m_mainFrame->provisionalDataSource(&dataSource)) || !dataSource) {
3073         hr = m_mainFrame->dataSource(&dataSource);
3074         if (FAILED(hr) || !dataSource)
3075             return hr;
3076     }
3077
3078     hr = dataSource->QueryInterface(IID_WebDataSource, (void**)&dataSourceImpl);
3079     if (FAILED(hr))
3080         return hr;
3081
3082     BString str = dataSourceImpl->documentLoader()->overrideEncoding();
3083     if (FAILED(hr))
3084         return hr;
3085
3086     if (!*encodingName)
3087         *encodingName = SysAllocStringLen(m_overrideEncoding.characters(), m_overrideEncoding.length());
3088
3089     if (!*encodingName && m_overrideEncoding.length())
3090         return E_OUTOFMEMORY;
3091
3092     return S_OK;
3093 }
3094
3095 HRESULT STDMETHODCALLTYPE WebView::setMediaStyle( 
3096     /* [in] */ BSTR /*media*/)
3097 {
3098     ASSERT_NOT_REACHED();
3099     return E_NOTIMPL;
3100 }
3101
3102 HRESULT STDMETHODCALLTYPE WebView::mediaStyle( 
3103     /* [retval][out] */ BSTR* /*media*/)
3104 {
3105     ASSERT_NOT_REACHED();
3106     return E_NOTIMPL;
3107 }
3108
3109 HRESULT STDMETHODCALLTYPE WebView::stringByEvaluatingJavaScriptFromString( 
3110     /* [in] */ BSTR script, // assumes input does not have "JavaScript" at the begining.
3111     /* [retval][out] */ BSTR* result)
3112 {
3113     if (!result) {
3114         ASSERT_NOT_REACHED();
3115         return E_POINTER;
3116     }
3117
3118     *result = 0;
3119
3120     Frame* coreFrame = core(m_mainFrame);
3121     if (!coreFrame)
3122         return E_FAIL;
3123
3124     JSC::JSValue scriptExecutionResult = coreFrame->script()->executeScript(WTF::String(script), true).jsValue();
3125     if (!scriptExecutionResult)
3126         return E_FAIL;
3127     else if (scriptExecutionResult.isString()) {
3128         JSLock lock(JSC::SilenceAssertionsOnly);
3129         JSC::ExecState* exec = coreFrame->script()->globalObject(mainThreadNormalWorld())->globalExec();
3130         *result = BString(ustringToString(scriptExecutionResult.getString(exec)));
3131     }
3132
3133     return S_OK;
3134 }
3135
3136 HRESULT STDMETHODCALLTYPE WebView::windowScriptObject( 
3137     /* [retval][out] */ IWebScriptObject** /*webScriptObject*/)
3138 {
3139     ASSERT_NOT_REACHED();
3140     return E_NOTIMPL;
3141 }
3142
3143 HRESULT STDMETHODCALLTYPE WebView::setPreferences( 
3144     /* [in] */ IWebPreferences* prefs)
3145 {
3146     if (!prefs)
3147         prefs = WebPreferences::sharedStandardPreferences();
3148
3149     if (m_preferences == prefs)
3150         return S_OK;
3151
3152     COMPtr<WebPreferences> webPrefs(Query, prefs);
3153     if (!webPrefs)
3154         return E_NOINTERFACE;
3155     webPrefs->willAddToWebView();
3156
3157     COMPtr<WebPreferences> oldPrefs = m_preferences;
3158
3159     IWebNotificationCenter* nc = WebNotificationCenter::defaultCenterInternal();
3160     nc->removeObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
3161
3162     BSTR identifier = 0;
3163     oldPrefs->identifier(&identifier);
3164     oldPrefs->didRemoveFromWebView();
3165     oldPrefs = 0; // Make sure we release the reference, since WebPreferences::removeReferenceForIdentifier will check for last reference to WebPreferences
3166
3167     m_preferences = webPrefs;
3168
3169     if (identifier) {
3170         WebPreferences::removeReferenceForIdentifier(identifier);
3171         SysFreeString(identifier);
3172     }
3173
3174     nc->addObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
3175
3176     m_preferences->postPreferencesChangesNotification();
3177
3178     return S_OK;
3179 }
3180
3181 HRESULT STDMETHODCALLTYPE WebView::preferences( 
3182     /* [retval][out] */ IWebPreferences** prefs)
3183 {
3184     if (!prefs)
3185         return E_POINTER;
3186     *prefs = m_preferences.get();
3187     if (m_preferences)
3188         m_preferences->AddRef();
3189     return S_OK;
3190 }
3191
3192 HRESULT STDMETHODCALLTYPE WebView::setPreferencesIdentifier( 
3193     /* [in] */ BSTR /*anIdentifier*/)
3194 {
3195     ASSERT_NOT_REACHED();
3196     return E_NOTIMPL;
3197 }
3198
3199 HRESULT STDMETHODCALLTYPE WebView::preferencesIdentifier( 
3200     /* [retval][out] */ BSTR* /*anIdentifier*/)
3201 {
3202     ASSERT_NOT_REACHED();
3203     return E_NOTIMPL;
3204 }
3205
3206 static void systemParameterChanged(WPARAM parameter)
3207 {
3208 #if PLATFORM(CG)
3209     if (parameter == SPI_SETFONTSMOOTHING || parameter == SPI_SETFONTSMOOTHINGTYPE || parameter == SPI_SETFONTSMOOTHINGCONTRAST || parameter == SPI_SETFONTSMOOTHINGORIENTATION)
3210         wkSystemFontSmoothingChanged();
3211 #endif
3212 }
3213
3214 void WebView::windowReceivedMessage(HWND, UINT message, WPARAM wParam, LPARAM)
3215 {
3216     switch (message) {
3217     case WM_NCACTIVATE:
3218         updateActiveStateSoon();
3219         if (!wParam)
3220             deleteBackingStoreSoon();
3221         break;
3222     case WM_SETTINGCHANGE:
3223         systemParameterChanged(wParam);
3224         break;
3225     }
3226 }
3227
3228 void WebView::updateActiveStateSoon() const
3229 {
3230     // This function is called while processing the WM_NCACTIVATE message.
3231     // While processing WM_NCACTIVATE when we are being deactivated, GetActiveWindow() will
3232     // still return our window. If we were to call updateActiveState() in that case, we would
3233     // wrongly think that we are still the active window. To work around this, we update our
3234     // active state after a 0-delay timer fires, at which point GetActiveWindow() will return
3235     // the newly-activated window.
3236
3237     SetTimer(m_viewWindow, UpdateActiveStateTimer, 0, 0);
3238 }
3239
3240 void WebView::deleteBackingStoreSoon()
3241 {
3242     if (pendingDeleteBackingStoreSet.size() > 2) {
3243         Vector<WebView*> views;
3244         HashSet<WebView*>::iterator end = pendingDeleteBackingStoreSet.end();
3245         for (HashSet<WebView*>::iterator it = pendingDeleteBackingStoreSet.begin(); it != end; ++it)
3246             views.append(*it);
3247         for (int i = 0; i < views.size(); ++i)
3248             views[i]->deleteBackingStore();
3249         ASSERT(pendingDeleteBackingStoreSet.isEmpty());
3250     }
3251
3252     pendingDeleteBackingStoreSet.add(this);
3253     m_deleteBackingStoreTimerActive = true;
3254     SetTimer(m_viewWindow, DeleteBackingStoreTimer, delayBeforeDeletingBackingStoreMsec, 0);
3255 }
3256
3257 void WebView::cancelDeleteBackingStoreSoon()
3258 {
3259     if (!m_deleteBackingStoreTimerActive)
3260         return;
3261     pendingDeleteBackingStoreSet.remove(this);
3262     m_deleteBackingStoreTimerActive = false;
3263     KillTimer(m_viewWindow, DeleteBackingStoreTimer);
3264 }
3265
3266 HRESULT STDMETHODCALLTYPE WebView::setHostWindow( 
3267     /* [in] */ OLE_HANDLE oleWindow)
3268 {
3269     HWND window = (HWND)(ULONG64)oleWindow;
3270     if (m_viewWindow) {
3271         if (window)
3272             SetParent(m_viewWindow, window);
3273         else if (!isBeingDestroyed()) {
3274             // Turn the WebView into a message-only window so it will no longer be a child of the
3275             // old host window and will be hidden from screen. We only do this when
3276             // isBeingDestroyed() is false because doing this while handling WM_DESTROY can leave
3277             // m_viewWindow in a weird state (see <http://webkit.org/b/29337>).
3278             SetParent(m_viewWindow, HWND_MESSAGE);
3279         }
3280     }
3281
3282     m_hostWindow = window;
3283
3284     windowAncestryDidChange();
3285
3286     return S_OK;
3287 }
3288
3289 HRESULT STDMETHODCALLTYPE WebView::hostWindow( 
3290     /* [retval][out] */ OLE_HANDLE* window)
3291 {
3292     *window = (OLE_HANDLE)(ULONG64)m_hostWindow;
3293     return S_OK;
3294 }
3295
3296
3297 static Frame *incrementFrame(Frame *curr, bool forward, bool wrapFlag)
3298 {
3299     return forward
3300         ? curr->tree()->traverseNextWithWrap(wrapFlag)
3301         : curr->tree()->traversePreviousWithWrap(wrapFlag);
3302 }
3303
3304 HRESULT STDMETHODCALLTYPE WebView::searchFor( 
3305     /* [in] */ BSTR str,
3306     /* [in] */ BOOL forward,
3307     /* [in] */ BOOL caseFlag,
3308     /* [in] */ BOOL wrapFlag,
3309     /* [retval][out] */ BOOL* found)
3310 {
3311     if (!found)
3312         return E_INVALIDARG;
3313     
3314     if (!m_page || !m_page->mainFrame())
3315         return E_UNEXPECTED;
3316
3317     if (!str || !SysStringLen(str))
3318         return E_INVALIDARG;
3319
3320     *found = m_page->findString(String(str, SysStringLen(str)), caseFlag ? TextCaseSensitive : TextCaseInsensitive, forward ? FindDirectionForward : FindDirectionBackward, wrapFlag);
3321     return S_OK;
3322 }
3323
3324 bool WebView::active()
3325 {
3326     HWND activeWindow = GetActiveWindow();
3327     return (activeWindow && m_topLevelParent == findTopLevelParent(activeWindow));
3328 }
3329
3330 void WebView::updateActiveState()
3331 {
3332     m_page->focusController()->setActive(active());
3333 }
3334
3335 HRESULT STDMETHODCALLTYPE WebView::updateFocusedAndActiveState()
3336 {
3337     updateActiveState();
3338
3339     bool active = m_page->focusController()->isActive();
3340     Frame* mainFrame = m_page->mainFrame();
3341     Frame* focusedFrame = m_page->focusController()->focusedOrMainFrame();
3342     mainFrame->selection()->setFocused(active && mainFrame == focusedFrame);
3343
3344     return S_OK;
3345 }
3346
3347 HRESULT STDMETHODCALLTYPE WebView::executeCoreCommandByName(BSTR bName, BSTR bValue)
3348 {
3349     String name(bName, SysStringLen(bName));
3350     String value(bValue, SysStringLen(bValue));
3351
3352     m_page->focusController()->focusedOrMainFrame()->editor()->command(name).execute(value);
3353
3354     return S_OK;
3355 }
3356
3357 HRESULT STDMETHODCALLTYPE WebView::clearMainFrameName()
3358 {
3359     m_page->mainFrame()->tree()->clearName();
3360
3361     return S_OK;
3362 }
3363
3364 HRESULT STDMETHODCALLTYPE WebView::markAllMatchesForText(
3365     BSTR str, BOOL caseSensitive, BOOL highlight, UINT limit, UINT* matches)
3366 {
3367     if (!matches)
3368         return E_INVALIDARG;
3369
3370     if (!m_page || !m_page->mainFrame())
3371         return E_UNEXPECTED;
3372
3373     if (!str || !SysStringLen(str))
3374         return E_INVALIDARG;
3375
3376     *matches = m_page->markAllMatchesForText(String(str, SysStringLen(str)), caseSensitive ? TextCaseSensitive : TextCaseInsensitive, highlight, limit);
3377     return S_OK;
3378 }
3379
3380 HRESULT STDMETHODCALLTYPE WebView::unmarkAllTextMatches()
3381 {
3382     if (!m_page || !m_page->mainFrame())
3383         return E_UNEXPECTED;
3384
3385     m_page->unmarkAllTextMatches();
3386     return S_OK;
3387 }
3388
3389 HRESULT STDMETHODCALLTYPE WebView::rectsForTextMatches(
3390     IEnumTextMatches** pmatches)
3391 {
3392     Vector<IntRect> allRects;
3393     WebCore::Frame* frame = m_page->mainFrame();
3394     do {
3395         if (Document* document = frame->document()) {
3396             IntRect visibleRect = frame->view()->visibleContentRect();
3397             Vector<IntRect> frameRects = document->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
3398             IntPoint frameOffset(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
3399             frameOffset = frame->view()->convertToContainingWindow(frameOffset);
3400
3401             Vector<IntRect>::iterator end = frameRects.end();
3402             for (Vector<IntRect>::iterator it = frameRects.begin(); it < end; it++) {
3403                 it->intersect(visibleRect);
3404                 it->move(frameOffset.x(), frameOffset.y());
3405                 allRects.append(*it);
3406             }
3407         }
3408         frame = incrementFrame(frame, true, false);
3409     } while (frame);
3410
3411     return createMatchEnumerator(&allRects, pmatches);
3412 }
3413
3414 HRESULT STDMETHODCALLTYPE WebView::generateSelectionImage(BOOL forceWhiteText, OLE_HANDLE* hBitmap)
3415 {
3416     *hBitmap = 0;
3417
3418     WebCore::Frame* frame = m_page->focusController()->focusedOrMainFrame();
3419
3420     if (frame) {
3421         HBITMAP bitmap = imageFromSelection(frame, forceWhiteText ? TRUE : FALSE);
3422         *hBitmap = (OLE_HANDLE)(ULONG64)bitmap;
3423     }
3424
3425     return S_OK;
3426 }
3427
3428 HRESULT STDMETHODCALLTYPE WebView::selectionRect(RECT* rc)
3429 {
3430     WebCore::Frame* frame = m_page->focusController()->focusedOrMainFrame();
3431
3432     if (frame) {
3433         IntRect ir = enclosingIntRect(frame->selection()->bounds());
3434         ir = frame->view()->convertToContainingWindow(ir);
3435         ir.move(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
3436         rc->left = ir.x();
3437         rc->top = ir.y();
3438         rc->bottom = rc->top + ir.height();
3439         rc->right = rc->left + ir.width();
3440     }
3441
3442     return S_OK;
3443 }
3444
3445 HRESULT STDMETHODCALLTYPE WebView::registerViewClass( 
3446     /* [in] */ IWebDocumentView* /*view*/,
3447     /* [in] */ IWebDocumentRepresentation* /*representation*/,
3448     /* [in] */ BSTR /*forMIMEType*/)
3449 {
3450     ASSERT_NOT_REACHED();
3451     return E_NOTIMPL;
3452 }
3453
3454 HRESULT STDMETHODCALLTYPE WebView::setGroupName( 
3455         /* [in] */ BSTR groupName)
3456 {
3457     if (!m_page)
3458         return S_OK;
3459     m_page->setGroupName(String(groupName, SysStringLen(groupName)));
3460     return S_OK;
3461 }
3462     
3463 HRESULT STDMETHODCALLTYPE WebView::groupName( 
3464         /* [retval][out] */ BSTR* groupName)
3465 {
3466     *groupName = 0;
3467     if (!m_page)
3468         return S_OK;
3469     String groupNameString = m_page->groupName();
3470     *groupName = SysAllocStringLen(groupNameString.characters(), groupNameString.length());
3471     if (!*groupName && groupNameString.length())
3472         return E_OUTOFMEMORY;
3473     return S_OK;
3474 }
3475     
3476 HRESULT STDMETHODCALLTYPE WebView::estimatedProgress( 
3477         /* [retval][out] */ double* estimatedProgress)
3478 {
3479     *estimatedProgress = m_page->progress()->estimatedProgress();
3480     return S_OK;
3481 }
3482     
3483 HRESULT STDMETHODCALLTYPE WebView::isLoading( 
3484         /* [retval][out] */ BOOL* isLoading)
3485 {
3486     COMPtr<IWebDataSource> dataSource;
3487     COMPtr<IWebDataSource> provisionalDataSource;
3488
3489     if (!isLoading)
3490         return E_POINTER;
3491
3492     *isLoading = FALSE;
3493
3494     if (!m_mainFrame)
3495         return E_FAIL;
3496
3497     if (SUCCEEDED(m_mainFrame->dataSource(&dataSource)))
3498         dataSource->isLoading(isLoading);
3499
3500     if (*isLoading)
3501         return S_OK;
3502
3503     if (SUCCEEDED(m_mainFrame->provisionalDataSource(&provisionalDataSource)))
3504         provisionalDataSource->isLoading(isLoading);
3505     return S_OK;
3506 }
3507     
3508 HRESULT STDMETHODCALLTYPE WebView::elementAtPoint( 
3509         /* [in] */ LPPOINT point,
3510         /* [retval][out] */ IPropertyBag** elementDictionary)
3511 {
3512     if (!elementDictionary) {
3513         ASSERT_NOT_REACHED();
3514         return E_POINTER;
3515     }
3516
3517     *elementDictionary = 0;
3518
3519     Frame* frame = core(m_mainFrame);
3520     if (!frame)
3521         return E_FAIL;
3522
3523     IntPoint webCorePoint = IntPoint(point->x, point->y);
3524     HitTestResult result = HitTestResult(webCorePoint);
3525     if (frame->contentRenderer())
3526         result = frame->eventHandler()->hitTestResultAtPoint(webCorePoint, false);
3527     *elementDictionary = WebElementPropertyBag::createInstance(result);
3528     return S_OK;
3529 }
3530     
3531 HRESULT STDMETHODCALLTYPE WebView::pasteboardTypesForSelection( 
3532     /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
3533 {
3534     ASSERT_NOT_REACHED();
3535     return E_NOTIMPL;
3536 }
3537     
3538 HRESULT STDMETHODCALLTYPE WebView::writeSelectionWithPasteboardTypes( 
3539         /* [size_is][in] */ BSTR* /*types*/,
3540         /* [in] */ int /*cTypes*/,
3541         /* [in] */ IDataObject* /*pasteboard*/)
3542 {
3543     ASSERT_NOT_REACHED();
3544     return E_NOTIMPL;
3545 }
3546     
3547 HRESULT STDMETHODCALLTYPE WebView::pasteboardTypesForElement( 
3548     /* [in] */ IPropertyBag* /*elementDictionary*/,
3549     /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
3550 {
3551     ASSERT_NOT_REACHED();
3552     return E_NOTIMPL;
3553 }
3554     
3555 HRESULT STDMETHODCALLTYPE WebView::writeElement( 
3556         /* [in] */ IPropertyBag* /*elementDictionary*/,
3557         /* [size_is][in] */ BSTR* /*withPasteboardTypes*/,
3558         /* [in] */ int /*cWithPasteboardTypes*/,
3559         /* [in] */ IDataObject* /*pasteboard*/)
3560 {
3561     ASSERT_NOT_REACHED();
3562     return E_NOTIMPL;
3563 }
3564     
3565 HRESULT STDMETHODCALLTYPE WebView::selectedText(
3566         /* [out, retval] */ BSTR* text)
3567 {
3568     if (!text) {
3569         ASSERT_NOT_REACHED();
3570         return E_POINTER;
3571     }
3572
3573     *text = 0;
3574
3575     Frame* focusedFrame = (m_page && m_page->focusController()) ? m_page->focusController()->focusedOrMainFrame() : 0;
3576     if (!focusedFrame)
3577         return E_FAIL;
3578
3579     String frameSelectedText = focusedFrame->editor()->selectedText();
3580     *text = SysAllocStringLen(frameSelectedText.characters(), frameSelectedText.length());
3581     if (!*text && frameSelectedText.length())
3582         return E_OUTOFMEMORY;
3583     return S_OK;
3584 }
3585
3586 HRESULT STDMETHODCALLTYPE WebView::centerSelectionInVisibleArea(
3587         /* [in] */ IUnknown* /* sender */)
3588 {
3589     Frame* coreFrame = core(m_mainFrame);
3590     if (!coreFrame)
3591         return E_FAIL;
3592
3593     coreFrame->selection()->revealSelection(ScrollAlignment::alignCenterAlways);
3594     return S_OK;
3595 }
3596
3597
3598 HRESULT STDMETHODCALLTYPE WebView::moveDragCaretToPoint( 
3599         /* [in] */ LPPOINT /*point*/)
3600 {
3601     ASSERT_NOT_REACHED();
3602     return E_NOTIMPL;
3603 }
3604     
3605 HRESULT STDMETHODCALLTYPE WebView::removeDragCaret( void)
3606 {
3607     ASSERT_NOT_REACHED();
3608     return E_NOTIMPL;
3609 }
3610     
3611 HRESULT STDMETHODCALLTYPE WebView::setDrawsBackground( 
3612         /* [in] */ BOOL /*drawsBackground*/)
3613 {
3614     ASSERT_NOT_REACHED();
3615     return E_NOTIMPL;
3616 }
3617     
3618 HRESULT STDMETHODCALLTYPE WebView::drawsBackground( 
3619         /* [retval][out] */ BOOL* /*drawsBackground*/)
3620 {
3621     ASSERT_NOT_REACHED();
3622     return E_NOTIMPL;
3623 }
3624     
3625 HRESULT STDMETHODCALLTYPE WebView::setMainFrameURL( 
3626         /* [in] */ BSTR /*urlString*/)
3627 {
3628     ASSERT_NOT_REACHED();
3629     return E_NOTIMPL;
3630 }
3631     
3632 HRESULT STDMETHODCALLTYPE WebView::mainFrameURL( 
3633         /* [retval][out] */ BSTR* urlString)
3634 {
3635     if (!urlString)
3636         return E_POINTER;
3637
3638     if (!m_mainFrame)
3639         return E_FAIL;
3640
3641     COMPtr<IWebDataSource> dataSource;
3642
3643     if (FAILED(m_mainFrame->provisionalDataSource(&dataSource))) {
3644         if (FAILED(m_mainFrame->dataSource(&dataSource)))
3645             return E_FAIL;
3646     }
3647
3648     if (!dataSource) {
3649         *urlString = 0;
3650         return S_OK;
3651     }
3652     
3653     COMPtr<IWebMutableURLRequest> request;
3654     if (FAILED(dataSource->request(&request)) || !request)
3655         return E_FAIL;
3656
3657     if (FAILED(request->URL(urlString)))
3658         return E_FAIL;
3659
3660     return S_OK;
3661 }
3662     
3663 HRESULT STDMETHODCALLTYPE WebView::mainFrameDocument( 
3664         /* [retval][out] */ IDOMDocument** document)
3665 {
3666     if (document)
3667         *document = 0;
3668     if (!m_mainFrame)
3669         return E_FAIL;
3670     return m_mainFrame->DOMDocument(document);
3671 }
3672     
3673 HRESULT STDMETHODCALLTYPE WebView::mainFrameTitle( 
3674         /* [retval][out] */ BSTR* /*title*/)
3675 {
3676     ASSERT_NOT_REACHED();
3677     return E_NOTIMPL;
3678 }
3679     
3680 HRESULT STDMETHODCALLTYPE WebView::mainFrameIcon( 
3681         /* [retval][out] */ OLE_HANDLE* /*hBitmap*/)
3682 {
3683     ASSERT_NOT_REACHED();
3684     return E_NOTIMPL;
3685 }
3686
3687 HRESULT STDMETHODCALLTYPE WebView::registerURLSchemeAsLocal( 
3688         /* [in] */ BSTR scheme)
3689 {
3690     if (!scheme)
3691         return E_POINTER;
3692
3693     SchemeRegistry::registerURLSchemeAsLocal(String(scheme, ::SysStringLen(scheme)));
3694
3695     return S_OK;
3696 }
3697
3698 // IWebIBActions ---------------------------------------------------------------
3699
3700 HRESULT STDMETHODCALLTYPE WebView::takeStringURLFrom( 
3701         /* [in] */ IUnknown* /*sender*/)
3702 {
3703     ASSERT_NOT_REACHED();
3704     return E_NOTIMPL;
3705 }
3706     
3707 HRESULT STDMETHODCALLTYPE WebView::stopLoading( 
3708         /* [in] */ IUnknown* /*sender*/)
3709 {
3710     if (!m_mainFrame)
3711         return E_FAIL;
3712
3713     return m_mainFrame->stopLoading();
3714 }
3715     
3716 HRESULT STDMETHODCALLTYPE WebView::reload( 
3717         /* [in] */ IUnknown* /*sender*/)
3718 {
3719     if (!m_mainFrame)
3720         return E_FAIL;
3721
3722     return m_mainFrame->reload();
3723 }
3724     
3725 HRESULT STDMETHODCALLTYPE WebView::canGoBack( 
3726         /* [in] */ IUnknown* /*sender*/,
3727         /* [retval][out] */ BOOL* result)
3728 {
3729     *result = !!(m_page->backForwardList()->backItem() && !m_page->defersLoading());
3730     return S_OK;
3731 }
3732     
3733 HRESULT STDMETHODCALLTYPE WebView::goBack( 
3734         /* [in] */ IUnknown* /*sender*/)
3735 {
3736     ASSERT_NOT_REACHED();
3737     return E_NOTIMPL;
3738 }
3739     
3740 HRESULT STDMETHODCALLTYPE WebView::canGoForward( 
3741         /* [in] */ IUnknown* /*sender*/,
3742         /* [retval][out] */ BOOL* result)
3743 {
3744     *result = !!(m_page->backForwardList()->forwardItem() && !m_page->defersLoading());
3745     return S_OK;
3746 }
3747     
3748 HRESULT STDMETHODCALLTYPE WebView::goForward( 
3749         /* [in] */ IUnknown* /*sender*/)
3750 {
3751     ASSERT_NOT_REACHED();
3752     return E_NOTIMPL;
3753 }
3754
3755 // FIXME: This code should move into WebCore so it can be shared by all the WebKits.
3756 #define MinimumZoomMultiplier   0.5f
3757 #define MaximumZoomMultiplier   3.0f
3758 #define ZoomMultiplierRatio     1.2f
3759
3760 HRESULT STDMETHODCALLTYPE WebView::canMakeTextLarger( 
3761         /* [in] */ IUnknown* /*sender*/,
3762         /* [retval][out] */ BOOL* result)
3763 {
3764     bool canGrowMore = canZoomIn(m_zoomsTextOnly);
3765     *result = canGrowMore ? TRUE : FALSE;
3766     return S_OK;
3767 }
3768
3769 HRESULT STDMETHODCALLTYPE WebView::canZoomPageIn( 
3770         /* [in] */ IUnknown* /*sender*/,
3771         /* [retval][out] */ BOOL* result)
3772 {
3773     bool canGrowMore = canZoomIn(false);
3774     *result = canGrowMore ? TRUE : FALSE;
3775     return S_OK;
3776 }
3777
3778 bool WebView::canZoomIn(bool isTextOnly)
3779 {
3780     return zoomMultiplier(isTextOnly) * ZoomMultiplierRatio < MaximumZoomMultiplier;
3781 }
3782     
3783 HRESULT STDMETHODCALLTYPE WebView::makeTextLarger( 
3784         /* [in] */ IUnknown* /*sender*/)
3785 {
3786     return zoomIn(m_zoomsTextOnly);
3787 }
3788
3789 HRESULT STDMETHODCALLTYPE WebView::zoomPageIn( 
3790         /* [in] */ IUnknown* /*sender*/)
3791 {
3792     return zoomIn(false);
3793 }
3794
3795 HRESULT WebView::zoomIn(bool isTextOnly)
3796 {
3797     if (!canZoomIn(isTextOnly))
3798         return E_FAIL;
3799     setZoomMultiplier(zoomMultiplier(isTextOnly) * ZoomMultiplierRatio, isTextOnly);
3800     return S_OK;
3801 }
3802
3803 HRESULT STDMETHODCALLTYPE WebView::canMakeTextSmaller( 
3804         /* [in] */ IUnknown* /*sender*/,
3805         /* [retval][out] */ BOOL* result)
3806 {
3807     bool canShrinkMore = canZoomOut(m_zoomsTextOnly);
3808     *result = canShrinkMore ? TRUE : FALSE;
3809     return S_OK;
3810 }
3811
3812 HRESULT STDMETHODCALLTYPE WebView::canZoomPageOut( 
3813         /* [in] */ IUnknown* /*sender*/,
3814         /* [retval][out] */ BOOL* result)
3815 {
3816     bool canShrinkMore = canZoomOut(false);
3817     *result = canShrinkMore ? TRUE : FALSE;
3818     return S_OK;
3819 }
3820
3821 bool WebView::canZoomOut(bool isTextOnly)
3822 {
3823     return zoomMultiplier(isTextOnly) / ZoomMultiplierRatio > MinimumZoomMultiplier;
3824 }
3825
3826 HRESULT STDMETHODCALLTYPE WebView::makeTextSmaller( 
3827         /* [in] */ IUnknown* /*sender*/)
3828 {
3829     return zoomOut(m_zoomsTextOnly);
3830 }
3831
3832 HRESULT STDMETHODCALLTYPE WebView::zoomPageOut( 
3833         /* [in] */ IUnknown* /*sender*/)
3834 {
3835     return zoomOut(false);
3836 }
3837
3838 HRESULT WebView::zoomOut(bool isTextOnly)
3839 {
3840     if (!canZoomOut(isTextOnly))
3841         return E_FAIL;
3842     setZoomMultiplier(zoomMultiplier(isTextOnly) / ZoomMultiplierRatio, isTextOnly);
3843     return S_OK;
3844 }
3845
3846 HRESULT STDMETHODCALLTYPE WebView::canMakeTextStandardSize( 
3847     /* [in] */ IUnknown* /*sender*/,
3848     /* [retval][out] */ BOOL* result)
3849 {
3850     // Since we always reset text zoom and page zoom together, this should continue to return an answer about text zoom even if its not enabled.
3851     bool notAlreadyStandard = canResetZoom(true);
3852     *result = notAlreadyStandard ? TRUE : FALSE;
3853     return S_OK;
3854 }
3855
3856 HRESULT STDMETHODCALLTYPE WebView::canResetPageZoom( 
3857     /* [in] */ IUnknown* /*sender*/,
3858     /* [retval][out] */ BOOL* result)
3859 {
3860     bool notAlreadyStandard = canResetZoom(false);
3861     *result = notAlreadyStandard ? TRUE : FALSE;
3862     return S_OK;
3863 }
3864
3865 bool WebView::canResetZoom(bool isTextOnly)
3866 {
3867     return zoomMultiplier(isTextOnly) != 1.0f;
3868 }
3869
3870 HRESULT STDMETHODCALLTYPE WebView::makeTextStandardSize( 
3871     /* [in] */ IUnknown* /*sender*/)
3872 {
3873     return resetZoom(true);
3874 }
3875
3876 HRESULT STDMETHODCALLTYPE WebView::resetPageZoom( 
3877     /* [in] */ IUnknown* /*sender*/)
3878 {
3879     return resetZoom(false);
3880 }
3881
3882 HRESULT WebView::resetZoom(bool isTextOnly)
3883 {
3884     if (!canResetZoom(isTextOnly))
3885         return E_FAIL;
3886     setZoomMultiplier(1.0f, isTextOnly);
3887     return S_OK;
3888 }
3889
3890 HRESULT STDMETHODCALLTYPE WebView::toggleContinuousSpellChecking( 
3891     /* [in] */ IUnknown* /*sender*/)
3892 {
3893     HRESULT hr;
3894     BOOL enabled;
3895     if (FAILED(hr = isContinuousSpellCheckingEnabled(&enabled)))
3896         return hr;
3897     return setContinuousSpellCheckingEnabled(enabled ? FALSE : TRUE);
3898 }
3899
3900 HRESULT STDMETHODCALLTYPE WebView::toggleSmartInsertDelete( 
3901     /* [in] */ IUnknown* /*sender*/)
3902 {
3903     BOOL enabled = FALSE;
3904     HRESULT hr = smartInsertDeleteEnabled(&enabled);
3905     if (FAILED(hr))
3906         return hr;
3907
3908     return setSmartInsertDeleteEnabled(enabled ? FALSE : TRUE);
3909 }
3910
3911 HRESULT STDMETHODCALLTYPE WebView::toggleGrammarChecking( 
3912     /* [in] */ IUnknown* /*sender*/)
3913 {
3914     BOOL enabled;
3915     HRESULT hr = isGrammarCheckingEnabled(&enabled);
3916     if (FAILED(hr))
3917         return hr;
3918
3919     return setGrammarCheckingEnabled(enabled ? FALSE : TRUE);
3920 }
3921
3922 HRESULT STDMETHODCALLTYPE WebView::reloadFromOrigin( 
3923         /* [in] */ IUnknown* /*sender*/)
3924 {
3925     if (!m_mainFrame)
3926         return E_FAIL;
3927
3928     return m_mainFrame->reloadFromOrigin();
3929 }
3930
3931 // IWebViewCSS -----------------------------------------------------------------
3932
3933 HRESULT STDMETHODCALLTYPE WebView::computedStyleForElement( 
3934         /* [in] */ IDOMElement* /*element*/,
3935         /* [in] */ BSTR /*pseudoElement*/,
3936         /* [retval][out] */ IDOMCSSStyleDeclaration** /*style*/)
3937 {
3938     ASSERT_NOT_REACHED();
3939     return E_NOTIMPL;
3940 }
3941
3942 // IWebViewEditing -------------------------------------------------------------
3943
3944 HRESULT STDMETHODCALLTYPE WebView::editableDOMRangeForPoint( 
3945         /* [in] */ LPPOINT /*point*/,
3946         /* [retval][out] */ IDOMRange** /*range*/)
3947 {
3948     ASSERT_NOT_REACHED();
3949     return E_NOTIMPL;
3950 }
3951     
3952 HRESULT STDMETHODCALLTYPE WebView::setSelectedDOMRange( 
3953         /* [in] */ IDOMRange* /*range*/,
3954         /* [in] */ WebSelectionAffinity /*affinity*/)
3955 {
3956     ASSERT_NOT_REACHED();
3957     return E_NOTIMPL;
3958 }
3959     
3960 HRESULT STDMETHODCALLTYPE WebView::selectedDOMRange( 
3961         /* [retval][out] */ IDOMRange** /*range*/)
3962 {
3963     ASSERT_NOT_REACHED();
3964     return E_NOTIMPL;
3965 }
3966     
3967 HRESULT STDMETHODCALLTYPE WebView::selectionAffinity( 
3968         /* [retval][out][retval][out] */ WebSelectionAffinity* /*affinity*/)
3969 {
3970     ASSERT_NOT_REACHED();
3971     return E_NOTIMPL;
3972 }
3973     
3974 HRESULT STDMETHODCALLTYPE WebView::setEditable( 
3975         /* [in] */ BOOL /*flag*/)
3976 {
3977     ASSERT_NOT_REACHED();
3978     return E_NOTIMPL;
3979 }
3980     
3981 HRESULT STDMETHODCALLTYPE WebView::isEditable( 
3982         /* [retval][out] */ BOOL* /*isEditable*/)
3983 {
3984     ASSERT_NOT_REACHED();
3985     return E_NOTIMPL;
3986 }
3987     
3988 HRESULT STDMETHODCALLTYPE WebView::setTypingStyle( 
3989         /* [in] */ IDOMCSSStyleDeclaration* /*style*/)
3990 {
3991     ASSERT_NOT_REACHED();
3992     return E_NOTIMPL;
3993 }
3994     
3995 HRESULT STDMETHODCALLTYPE WebView::typingStyle( 
3996         /* [retval][out] */ IDOMCSSStyleDeclaration** /*style*/)
3997 {
3998     ASSERT_NOT_REACHED();
3999     return E_NOTIMPL;
4000 }
4001     
4002 HRESULT STDMETHODCALLTYPE WebView::setSmartInsertDeleteEnabled( 
4003         /* [in] */ BOOL flag)
4004 {
4005     m_smartInsertDeleteEnabled = !!flag;
4006     if (m_smartInsertDeleteEnabled)
4007         setSelectTrailingWhitespaceEnabled(false);
4008     return S_OK;
4009 }
4010     
4011 HRESULT STDMETHODCALLTYPE WebView::smartInsertDeleteEnabled( 
4012         /* [retval][out] */ BOOL* enabled)
4013 {
4014     *enabled = m_smartInsertDeleteEnabled ? TRUE : FALSE;
4015     return S_OK;
4016 }
4017  
4018 HRESULT STDMETHODCALLTYPE WebView::setSelectTrailingWhitespaceEnabled( 
4019         /* [in] */ BOOL flag)
4020 {
4021     m_selectTrailingWhitespaceEnabled = !!flag;
4022     if (m_selectTrailingWhitespaceEnabled)
4023         setSmartInsertDeleteEnabled(false);
4024     return S_OK;
4025 }
4026     
4027 HRESULT STDMETHODCALLTYPE WebView::isSelectTrailingWhitespaceEnabled( 
4028         /* [retval][out] */ BOOL* enabled)
4029 {
4030     *enabled = m_selectTrailingWhitespaceEnabled ? TRUE : FALSE;
4031     return S_OK;
4032 }
4033
4034 HRESULT STDMETHODCALLTYPE WebView::setContinuousSpellCheckingEnabled( 
4035         /* [in] */ BOOL flag)
4036 {
4037     if (continuousSpellCheckingEnabled != !!flag) {
4038         continuousSpellCheckingEnabled = !!flag;
4039         COMPtr<IWebPreferences> prefs;
4040         if (SUCCEEDED(preferences(&prefs)))
4041             prefs->setContinuousSpellCheckingEnabled(flag);
4042     }
4043     
4044     BOOL spellCheckingEnabled;
4045     if (SUCCEEDED(isContinuousSpellCheckingEnabled(&spellCheckingEnabled)) && spellCheckingEnabled)
4046         preflightSpellChecker();
4047     else
4048         m_mainFrame->unmarkAllMisspellings();
4049
4050     return S_OK;
4051 }
4052     
4053 HRESULT STDMETHODCALLTYPE WebView::isContinuousSpellCheckingEnabled( 
4054         /* [retval][out] */ BOOL* enabled)
4055 {
4056     *enabled = (continuousSpellCheckingEnabled && continuousCheckingAllowed()) ? TRUE : FALSE;
4057     return S_OK;
4058 }
4059     
4060 HRESULT STDMETHODCALLTYPE WebView::spellCheckerDocumentTag( 
4061         /* [retval][out] */ int* tag)
4062 {
4063     // we just use this as a flag to indicate that we've spell checked the document
4064     // and need to close the spell checker out when the view closes.
4065     *tag = 0;
4066     m_hasSpellCheckerDocumentTag = true;
4067     return S_OK;
4068 }
4069
4070 static COMPtr<IWebEditingDelegate> spellingDelegateForTimer;
4071
4072 static void preflightSpellCheckerNow()
4073 {
4074     spellingDelegateForTimer->preflightChosenSpellServer();
4075     spellingDelegateForTimer = 0;
4076 }
4077
4078 static void CALLBACK preflightSpellCheckerTimerCallback(HWND, UINT, UINT_PTR id, DWORD)
4079 {
4080     ::KillTimer(0, id);
4081     preflightSpellCheckerNow();
4082 }
4083
4084 void WebView::preflightSpellChecker()
4085 {
4086     // As AppKit does, we wish to delay tickling the shared spellchecker into existence on application launch.
4087     if (!m_editingDelegate)
4088         return;
4089
4090     BOOL exists;
4091     spellingDelegateForTimer = m_editingDelegate;
4092     if (SUCCEEDED(m_editingDelegate->sharedSpellCheckerExists(&exists)) && exists)
4093         preflightSpellCheckerNow();
4094     else
4095         ::SetTimer(0, 0, 2000, preflightSpellCheckerTimerCallback);
4096 }
4097
4098 bool WebView::continuousCheckingAllowed()
4099 {
4100     static bool allowContinuousSpellChecking = true;
4101     static bool readAllowContinuousSpellCheckingDefault = false;
4102     if (!readAllowContinuousSpellCheckingDefault) {
4103         COMPtr<IWebPreferences> prefs;
4104         if (SUCCEEDED(preferences(&prefs))) {
4105             BOOL allowed;
4106             prefs->allowContinuousSpellChecking(&allowed);
4107             allowContinuousSpellChecking = !!allowed;
4108         }
4109         readAllowContinuousSpellCheckingDefault = true;
4110     }
4111     return allowContinuousSpellChecking;
4112 }
4113
4114 HRESULT STDMETHODCALLTYPE WebView::undoManager( 
4115         /* [retval][out] */ IWebUndoManager** /*manager*/)
4116 {
4117     ASSERT_NOT_REACHED();
4118     return E_NOTIMPL;
4119 }
4120     
4121 HRESULT STDMETHODCALLTYPE WebView::setEditingDelegate( 
4122         /* [in] */ IWebEditingDelegate* d)
4123 {
4124     m_editingDelegate = d;
4125     return S_OK;
4126 }
4127     
4128 HRESULT STDMETHODCALLTYPE WebView::editingDelegate( 
4129         /* [retval][out] */ IWebEditingDelegate** d)
4130 {
4131     if (!d) {
4132         ASSERT_NOT_REACHED();
4133         return E_POINTER;
4134     }
4135
4136     *d = m_editingDelegate.get();
4137     if (!*d)
4138         return E_FAIL;
4139
4140     (*d)->AddRef();
4141     return S_OK;
4142 }
4143     
4144 HRESULT STDMETHODCALLTYPE WebView::styleDeclarationWithText( 
4145         /* [in] */ BSTR /*text*/,
4146         /* [retval][out] */ IDOMCSSStyleDeclaration** /*style*/)
4147 {
4148     ASSERT_NOT_REACHED();
4149     return E_NOTIMPL;
4150 }
4151     
4152 HRESULT STDMETHODCALLTYPE WebView::hasSelectedRange( 
4153         /* [retval][out] */ BOOL* hasSelectedRange)
4154 {
4155     *hasSelectedRange = m_page->mainFrame()->selection()->isRange();
4156     return S_OK;
4157 }
4158     
4159 HRESULT STDMETHODCALLTYPE WebView::cutEnabled( 
4160         /* [retval][out] */ BOOL* enabled)
4161 {
4162     Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4163     *enabled = editor->canCut() || editor->canDHTMLCut();
4164     return S_OK;
4165 }
4166     
4167 HRESULT STDMETHODCALLTYPE WebView::copyEnabled( 
4168         /* [retval][out] */ BOOL* enabled)
4169 {
4170     Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4171     *enabled = editor->canCopy() || editor->canDHTMLCopy();
4172     return S_OK;
4173 }
4174     
4175 HRESULT STDMETHODCALLTYPE WebView::pasteEnabled( 
4176         /* [retval][out] */ BOOL* enabled)
4177 {
4178     Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4179     *enabled = editor->canPaste() || editor->canDHTMLPaste();
4180     return S_OK;
4181 }
4182     
4183 HRESULT STDMETHODCALLTYPE WebView::deleteEnabled( 
4184         /* [retval][out] */ BOOL* enabled)
4185 {
4186     *enabled = m_page->focusController()->focusedOrMainFrame()->editor()->canDelete();
4187     return S_OK;
4188 }
4189     
4190 HRESULT STDMETHODCALLTYPE WebView::editingEnabled( 
4191         /* [retval][out] */ BOOL* enabled)
4192 {
4193     *enabled = m_page->focusController()->focusedOrMainFrame()->editor()->canEdit();
4194     return S_OK;
4195 }
4196
4197 HRESULT STDMETHODCALLTYPE WebView::isGrammarCheckingEnabled( 
4198     /* [retval][out] */ BOOL* enabled)
4199 {
4200     *enabled = grammarCheckingEnabled ? TRUE : FALSE;
4201     return S_OK;
4202 }
4203
4204 HRESULT STDMETHODCALLTYPE WebView::setGrammarCheckingEnabled( 
4205     BOOL enabled)
4206 {
4207     if (!m_editingDelegate) {
4208         LOG_ERROR("No NSSpellChecker");
4209         return E_FAIL;
4210     }
4211
4212     if (grammarCheckingEnabled == !!enabled)
4213         return S_OK;
4214     
4215     grammarCheckingEnabled = !!enabled;
4216     COMPtr<IWebPreferences> prefs;
4217     if (SUCCEEDED(preferences(&prefs)))
4218         prefs->setGrammarCheckingEnabled(enabled);
4219     
4220     m_editingDelegate->updateGrammar();
4221
4222     // We call _preflightSpellChecker when turning continuous spell checking on, but we don't need to do that here
4223     // because grammar checking only occurs on code paths that already preflight spell checking appropriately.
4224     
4225     BOOL grammarEnabled;
4226     if (SUCCEEDED(isGrammarCheckingEnabled(&grammarEnabled)) && !grammarEnabled)
4227         m_mainFrame->unmarkAllBadGrammar();
4228
4229     return S_OK;
4230 }
4231
4232 // IWebViewUndoableEditing -----------------------------------------------------
4233
4234 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithNode( 
4235         /* [in] */ IDOMNode* /*node*/)
4236 {
4237     ASSERT_NOT_REACHED();
4238     return E_NOTIMPL;
4239 }
4240     
4241 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithText( 
4242         /* [in] */ BSTR text)
4243 {
4244     String textString(text, ::SysStringLen(text));
4245     Position start = m_page->mainFrame()->selection()->selection().start();
4246     m_page->focusController()->focusedOrMainFrame()->editor()->insertText(textString, 0);
4247     m_page->mainFrame()->selection()->setBase(start);
4248     return S_OK;
4249 }
4250     
4251 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithMarkupString( 
4252         /* [in] */ BSTR /*markupString*/)
4253 {
4254     ASSERT_NOT_REACHED();
4255     return E_NOTIMPL;
4256 }
4257     
4258 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithArchive( 
4259         /* [in] */ IWebArchive* /*archive*/)
4260 {
4261     ASSERT_NOT_REACHED();
4262     return E_NOTIMPL;
4263 }
4264     
4265 HRESULT STDMETHODCALLTYPE WebView::deleteSelection( void)
4266 {
4267     Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4268     editor->deleteSelectionWithSmartDelete(editor->canSmartCopyOrDelete());
4269     return S_OK;
4270 }
4271
4272 HRESULT STDMETHODCALLTYPE WebView::clearSelection( void)
4273 {
4274     m_page->focusController()->focusedOrMainFrame()->selection()->clear();
4275     return S_OK;
4276 }
4277     
4278 HRESULT STDMETHODCALLTYPE WebView::applyStyle( 
4279         /* [in] */ IDOMCSSStyleDeclaration* /*style*/)
4280 {
4281     ASSERT_NOT_REACHED();
4282     return E_NOTIMPL;
4283 }
4284
4285 // IWebViewEditingActions ------------------------------------------------------
4286
4287 HRESULT STDMETHODCALLTYPE WebView::copy( 
4288         /* [in] */ IUnknown* /*sender*/)
4289 {
4290     m_page->focusController()->focusedOrMainFrame()->editor()->command("Copy").execute();
4291     return S_OK;
4292 }
4293
4294 HRESULT STDMETHODCALLTYPE WebView::cut( 
4295         /* [in] */ IUnknown* /*sender*/)
4296 {
4297     m_page->focusController()->focusedOrMainFrame()->editor()->command("Cut").execute();
4298     return S_OK;
4299 }
4300
4301 HRESULT STDMETHODCALLTYPE WebView::paste( 
4302         /* [in] */ IUnknown* /*sender*/)
4303 {
4304     m_page->focusController()->focusedOrMainFrame()->editor()->command("Paste").execute();
4305     return S_OK;
4306 }
4307
4308 HRESULT STDMETHODCALLTYPE WebView::copyURL( 
4309         /* [in] */ BSTR url)
4310 {
4311     m_page->focusController()->focusedOrMainFrame()->editor()->copyURL(MarshallingHelpers::BSTRToKURL(url), "");
4312     return S_OK;
4313 }
4314
4315
4316 HRESULT STDMETHODCALLTYPE WebView::copyFont( 
4317         /* [in] */ IUnknown* /*sender*/)
4318 {
4319     ASSERT_NOT_REACHED();
4320     return E_NOTIMPL;
4321 }
4322     
4323 HRESULT STDMETHODCALLTYPE WebView::pasteFont( 
4324         /* [in] */ IUnknown* /*sender*/)