Unify Windows version checks.
[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/SystemInfo.h>
132 #include <WebCore/TypingCommand.h>
133 #include <WebCore/WindowMessageBroadcaster.h>
134 #include <wtf/Threading.h>
135
136 #if ENABLE(CLIENT_BASED_GEOLOCATION)
137 #include <WebCore/GeolocationController.h>
138 #include <WebCore/GeolocationError.h>
139 #endif
140
141 #if PLATFORM(CG)
142 #include <CoreGraphics/CGContext.h>
143 #endif
144
145 #if USE(CF)
146 #include <CoreFoundation/CoreFoundation.h>
147 #endif
148
149 #if USE(CFNETWORK)
150 #include <CFNetwork/CFURLCachePriv.h>
151 #include <CFNetwork/CFURLProtocolPriv.h>
152 #include <WebCore/CookieStorageCFNet.h>
153 #include <WebKitSystemInterface/WebKitSystemInterface.h> 
154 #endif
155
156 #if USE(ACCELERATED_COMPOSITING)
157 #include <WebCore/CACFLayerTreeHost.h>
158 #include <WebCore/PlatformCALayer.h>
159 #endif
160
161 #include <ShlObj.h>
162 #include <comutil.h>
163 #include <dimm.h>
164 #include <oleacc.h>
165 #include <tchar.h>
166 #include <windowsx.h>
167 #include <wtf/HashSet.h>
168 #include <wtf/text/CString.h>
169 #include <wtf/text/StringConcatenate.h>
170
171 // Soft link functions for gestures and panning feedback
172 SOFT_LINK_LIBRARY(USER32);
173 SOFT_LINK_OPTIONAL(USER32, GetGestureInfo, BOOL, WINAPI, (HGESTUREINFO, PGESTUREINFO));
174 SOFT_LINK_OPTIONAL(USER32, SetGestureConfig, BOOL, WINAPI, (HWND, DWORD, UINT, PGESTURECONFIG, UINT));
175 SOFT_LINK_OPTIONAL(USER32, CloseGestureInfoHandle, BOOL, WINAPI, (HGESTUREINFO));
176 SOFT_LINK_LIBRARY(Uxtheme);
177 SOFT_LINK_OPTIONAL(Uxtheme, BeginPanningFeedback, BOOL, WINAPI, (HWND));
178 SOFT_LINK_OPTIONAL(Uxtheme, EndPanningFeedback, BOOL, WINAPI, (HWND, BOOL));
179 SOFT_LINK_OPTIONAL(Uxtheme, UpdatePanningFeedback, BOOL, WINAPI, (HWND, LONG, LONG, BOOL));
180
181 using namespace WebCore;
182 using namespace std;
183 using JSC::JSLock;
184
185 static HMODULE accessibilityLib;
186 static HashSet<WebView*> pendingDeleteBackingStoreSet;
187
188 static String webKitVersionString();
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     DEFINE_STATIC_LOCAL(String, osVersion, (windowsVersionForUAString()));
1248     DEFINE_STATIC_LOCAL(String, webKitVersion, (webKitVersionString()));
1249
1250     return makeString("Mozilla/5.0 (", osVersion, ") AppleWebKit/", webKitVersion, " (KHTML, like Gecko)", applicationName.isEmpty() ? "" : " ", applicationName);
1251 }
1252
1253 Page* WebView::page()
1254 {
1255     return m_page;
1256 }
1257
1258 bool WebView::handleContextMenuEvent(WPARAM wParam, LPARAM lParam)
1259 {
1260     // Translate the screen coordinates into window coordinates
1261     POINT coords = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
1262     if (coords.x == -1 || coords.y == -1) {
1263         // The contextMenuController() holds onto the last context menu that was popped up on the
1264         // page until a new one is created. We need to clear this menu before propagating the event
1265         // through the DOM so that we can detect if we create a new menu for this event, since we
1266         // won't create a new menu if the DOM swallows the event and the defaultEventHandler does
1267         // not run.
1268         m_page->contextMenuController()->clearContextMenu();
1269
1270         Frame* focusedFrame = m_page->focusController()->focusedOrMainFrame();
1271         return focusedFrame->eventHandler()->sendContextMenuEventForKey();
1272
1273     } else {
1274         if (!::ScreenToClient(m_viewWindow, &coords))
1275             return false;
1276     }
1277
1278     lParam = MAKELPARAM(coords.x, coords.y);
1279
1280     m_page->contextMenuController()->clearContextMenu();
1281
1282     IntPoint documentPoint(m_page->mainFrame()->view()->windowToContents(coords));
1283     HitTestResult result = m_page->mainFrame()->eventHandler()->hitTestResultAtPoint(documentPoint, false);
1284     Frame* targetFrame = result.innerNonSharedNode() ? result.innerNonSharedNode()->document()->frame() : m_page->focusController()->focusedOrMainFrame();
1285
1286     targetFrame->view()->setCursor(pointerCursor());
1287     PlatformMouseEvent mouseEvent(m_viewWindow, WM_RBUTTONUP, wParam, lParam);
1288     bool handledEvent = targetFrame->eventHandler()->sendContextMenuEvent(mouseEvent);
1289     if (!handledEvent)
1290         return false;
1291
1292     ContextMenuController* contextMenuController = m_page->contextMenuController();
1293
1294     // Show the menu
1295     ContextMenu* coreMenu = contextMenuController->contextMenu();
1296     if (!coreMenu)
1297         return false;
1298
1299     Node* node = contextMenuController->hitTestResult().innerNonSharedNode();
1300     if (!node)
1301         return false;
1302
1303     Frame* frame = node->document()->frame();
1304     if (!frame)
1305         return false;
1306
1307     FrameView* view = frame->view();
1308     if (!view)
1309         return false;
1310
1311     POINT point(view->contentsToWindow(contextMenuController->hitTestResult().point()));
1312
1313     // Translate the point to screen coordinates
1314     if (!::ClientToScreen(m_viewWindow, &point))
1315         return false;
1316
1317     BOOL hasCustomMenus = false;
1318     if (m_uiDelegate)
1319         m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1320
1321     if (hasCustomMenus)
1322         m_uiDelegate->trackCustomPopupMenu((IWebView*)this, (OLE_HANDLE)(ULONG64)coreMenu->nativeMenu(), &point);
1323     else {
1324         // Surprisingly, TPM_RIGHTBUTTON means that items are selectable with either the right OR left mouse button
1325         UINT flags = TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_VERPOSANIMATION | TPM_HORIZONTAL
1326             | TPM_LEFTALIGN | TPM_HORPOSANIMATION;
1327         ::TrackPopupMenuEx(coreMenu->nativeMenu(), flags, point.x, point.y, m_viewWindow, 0);
1328     }
1329
1330     return true;
1331 }
1332
1333 bool WebView::onMeasureItem(WPARAM /*wParam*/, LPARAM lParam)
1334 {
1335     if (!m_uiDelegate)
1336         return false;
1337
1338     BOOL hasCustomMenus = false;
1339     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1340     if (!hasCustomMenus)
1341         return false;
1342
1343     m_uiDelegate->measureCustomMenuItem((IWebView*)this, (void*)lParam);
1344     return true;
1345 }
1346
1347 bool WebView::onDrawItem(WPARAM /*wParam*/, LPARAM lParam)
1348 {
1349     if (!m_uiDelegate)
1350         return false;
1351
1352     BOOL hasCustomMenus = false;
1353     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1354     if (!hasCustomMenus)
1355         return false;
1356
1357     m_uiDelegate->drawCustomMenuItem((IWebView*)this, (void*)lParam);
1358     return true;
1359 }
1360
1361 bool WebView::onInitMenuPopup(WPARAM wParam, LPARAM /*lParam*/)
1362 {
1363     if (!m_uiDelegate)
1364         return false;
1365
1366     HMENU menu = (HMENU)wParam;
1367     if (!menu)
1368         return false;
1369
1370     BOOL hasCustomMenus = false;
1371     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1372     if (!hasCustomMenus)
1373         return false;
1374
1375     m_uiDelegate->addCustomMenuDrawingData((IWebView*)this, (OLE_HANDLE)(ULONG64)menu);
1376     return true;
1377 }
1378
1379 bool WebView::onUninitMenuPopup(WPARAM wParam, LPARAM /*lParam*/)
1380 {
1381     if (!m_uiDelegate)
1382         return false;
1383
1384     HMENU menu = (HMENU)wParam;
1385     if (!menu)
1386         return false;
1387
1388     BOOL hasCustomMenus = false;
1389     m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1390     if (!hasCustomMenus)
1391         return false;
1392
1393     m_uiDelegate->cleanUpCustomMenuDrawingData((IWebView*)this, (OLE_HANDLE)(ULONG64)menu);
1394     return true;
1395 }
1396
1397 void WebView::performContextMenuAction(WPARAM wParam, LPARAM lParam, bool byPosition)
1398 {
1399     ContextMenu* menu = m_page->contextMenuController()->contextMenu();
1400     ASSERT(menu);
1401
1402     ContextMenuItem* item = byPosition ? menu->itemAtIndex((unsigned)wParam) : menu->itemWithAction((ContextMenuAction)wParam);
1403     if (!item)
1404         return;
1405     m_page->contextMenuController()->contextMenuItemSelected(item);
1406 }
1407
1408 bool WebView::handleMouseEvent(UINT message, WPARAM wParam, LPARAM lParam)
1409 {
1410     static LONG globalClickCount;
1411     static IntPoint globalPrevPoint;
1412     static MouseButton globalPrevButton;
1413     static LONG globalPrevMouseDownTime;
1414
1415     if (message == WM_CANCELMODE) {
1416         m_page->mainFrame()->eventHandler()->lostMouseCapture();
1417         return true;
1418     }
1419
1420     // Create our event.
1421     // On WM_MOUSELEAVE we need to create a mouseout event, so we force the position
1422     // of the event to be at (MINSHORT, MINSHORT).
1423     LPARAM position = (message == WM_MOUSELEAVE) ? ((MINSHORT << 16) | MINSHORT) : lParam;
1424     PlatformMouseEvent mouseEvent(m_viewWindow, message, wParam, position, m_mouseActivated);
1425
1426     setMouseActivated(false);
1427
1428     bool insideThreshold = abs(globalPrevPoint.x() - mouseEvent.pos().x()) < ::GetSystemMetrics(SM_CXDOUBLECLK) &&
1429                            abs(globalPrevPoint.y() - mouseEvent.pos().y()) < ::GetSystemMetrics(SM_CYDOUBLECLK);
1430     LONG messageTime = ::GetMessageTime();
1431
1432     bool handled = false;
1433
1434     if (message == WM_LBUTTONDOWN || message == WM_MBUTTONDOWN || message == WM_RBUTTONDOWN) {
1435         // FIXME: I'm not sure if this is the "right" way to do this
1436         // but without this call, we never become focused since we don't allow
1437         // the default handling of mouse events.
1438         SetFocus(m_viewWindow);
1439
1440         // Always start capturing events when the mouse goes down in our HWND.
1441         ::SetCapture(m_viewWindow);
1442
1443         if (((messageTime - globalPrevMouseDownTime) < (LONG)::GetDoubleClickTime()) && 
1444             insideThreshold &&
1445             mouseEvent.button() == globalPrevButton)
1446             globalClickCount++;
1447         else
1448             // Reset the click count.
1449             globalClickCount = 1;
1450         globalPrevMouseDownTime = messageTime;
1451         globalPrevButton = mouseEvent.button();
1452         globalPrevPoint = mouseEvent.pos();
1453         
1454         mouseEvent.setClickCount(globalClickCount);
1455         handled = m_page->mainFrame()->eventHandler()->handleMousePressEvent(mouseEvent);
1456     } else if (message == WM_LBUTTONDBLCLK || message == WM_MBUTTONDBLCLK || message == WM_RBUTTONDBLCLK) {
1457         globalClickCount++;
1458         mouseEvent.setClickCount(globalClickCount);
1459         handled = m_page->mainFrame()->eventHandler()->handleMousePressEvent(mouseEvent);
1460     } else if (message == WM_LBUTTONUP || message == WM_MBUTTONUP || message == WM_RBUTTONUP) {
1461         // Record the global position and the button of the up.
1462         globalPrevButton = mouseEvent.button();
1463         globalPrevPoint = mouseEvent.pos();
1464         mouseEvent.setClickCount(globalClickCount);
1465         m_page->mainFrame()->eventHandler()->handleMouseReleaseEvent(mouseEvent);
1466         ::ReleaseCapture();
1467     } else if (message == WM_MOUSELEAVE && m_mouseOutTracker) {
1468         // Once WM_MOUSELEAVE is fired windows clears this tracker
1469         // so there is no need to disable it ourselves.
1470         m_mouseOutTracker.set(0);
1471         m_page->mainFrame()->eventHandler()->mouseMoved(mouseEvent);
1472         handled = true;
1473     } else if (message == WM_MOUSEMOVE) {
1474         if (!insideThreshold)
1475             globalClickCount = 0;
1476         mouseEvent.setClickCount(globalClickCount);
1477         handled = m_page->mainFrame()->eventHandler()->mouseMoved(mouseEvent);
1478         if (!m_mouseOutTracker) {
1479             m_mouseOutTracker.set(new TRACKMOUSEEVENT);
1480             m_mouseOutTracker->cbSize = sizeof(TRACKMOUSEEVENT);
1481             m_mouseOutTracker->dwFlags = TME_LEAVE;
1482             m_mouseOutTracker->hwndTrack = m_viewWindow;
1483             ::TrackMouseEvent(m_mouseOutTracker.get());
1484         }
1485     }
1486     return handled;
1487 }
1488
1489 bool WebView::gestureNotify(WPARAM wParam, LPARAM lParam)
1490 {
1491     GESTURENOTIFYSTRUCT* gn = reinterpret_cast<GESTURENOTIFYSTRUCT*>(lParam);
1492
1493     Frame* coreFrame = core(m_mainFrame);
1494     if (!coreFrame)
1495         return false;
1496
1497     ScrollView* view = coreFrame->view();
1498     if (!view)
1499         return false;
1500
1501     // If we don't have this function, we shouldn't be receiving this message
1502     ASSERT(SetGestureConfigPtr());
1503
1504     bool hitScrollbar = false;
1505     POINT gestureBeginPoint = {gn->ptsLocation.x, gn->ptsLocation.y};
1506     HitTestRequest request(HitTestRequest::ReadOnly);
1507     for (Frame* childFrame = m_page->mainFrame(); childFrame; childFrame = EventHandler::subframeForTargetNode(m_gestureTargetNode.get())) {
1508         FrameView* frameView = childFrame->view();
1509         if (!frameView)
1510             break;
1511         RenderView* renderView = childFrame->document()->renderView();
1512         if (!renderView)
1513             break;
1514         RenderLayer* layer = renderView->layer();
1515         if (!layer)
1516             break;
1517
1518         HitTestResult result(frameView->screenToContents(gestureBeginPoint));
1519         layer->hitTest(request, result);
1520         m_gestureTargetNode = result.innerNode();
1521
1522         if (!hitScrollbar)
1523             hitScrollbar = result.scrollbar();
1524     }
1525
1526     if (!hitScrollbar) {
1527         // The hit testing above won't detect if we've hit the main frame's vertical scrollbar. Check that manually now.
1528         RECT webViewRect;
1529         GetWindowRect(m_viewWindow, &webViewRect);
1530         hitScrollbar = view->verticalScrollbar() && (gestureBeginPoint.x > (webViewRect.right - view->verticalScrollbar()->theme()->scrollbarThickness()));  
1531     }
1532
1533     bool canBeScrolled = false;
1534     if (m_gestureTargetNode) {
1535         for (RenderObject* renderer = m_gestureTargetNode->renderer(); renderer; renderer = renderer->parent()) {
1536             if (renderer->isBox() && toRenderBox(renderer)->canBeScrolledAndHasScrollableArea()) {
1537                 canBeScrolled = true;
1538                 break;
1539             }
1540         }
1541     }
1542
1543     // We always allow two-fingered panning with inertia and a gutter (which limits movement to one
1544     // direction in most cases).
1545     DWORD dwPanWant = GC_PAN | GC_PAN_WITH_INERTIA | GC_PAN_WITH_GUTTER;
1546     // We never allow single-fingered horizontal panning. That gesture is reserved for creating text
1547     // selections. This matches IE.
1548     DWORD dwPanBlock = GC_PAN_WITH_SINGLE_FINGER_HORIZONTALLY;
1549
1550     if (hitScrollbar || !canBeScrolled) {
1551         // The part of the page under the gesture can't be scrolled, or the gesture is on a scrollbar.
1552         // Disallow single-fingered vertical panning in this case, too, so we'll fall back to the default
1553         // behavior (which allows the scrollbar thumb to be dragged, text selections to be made, etc.).
1554         dwPanBlock |= GC_PAN_WITH_SINGLE_FINGER_VERTICALLY;
1555     } else {
1556         // The part of the page the gesture is under can be scrolled, and we're not under a scrollbar.
1557         // Allow single-fingered vertical panning in this case, so the user will be able to pan the page
1558         // with one or two fingers.
1559         dwPanWant |= GC_PAN_WITH_SINGLE_FINGER_VERTICALLY;
1560     }
1561
1562     GESTURECONFIG gc = { GID_PAN, dwPanWant, dwPanBlock };
1563     return SetGestureConfigPtr()(m_viewWindow, 0, 1, &gc, sizeof(GESTURECONFIG));
1564 }
1565
1566 bool WebView::gesture(WPARAM wParam, LPARAM lParam) 
1567 {
1568     // We want to bail out if we don't have either of these functions
1569     if (!GetGestureInfoPtr() || !CloseGestureInfoHandlePtr())
1570         return false;
1571
1572     HGESTUREINFO gestureHandle = reinterpret_cast<HGESTUREINFO>(lParam);
1573     
1574     GESTUREINFO gi = {0};
1575     gi.cbSize = sizeof(GESTUREINFO);
1576
1577     if (!GetGestureInfoPtr()(gestureHandle, reinterpret_cast<PGESTUREINFO>(&gi)))
1578         return false;
1579
1580     switch (gi.dwID) {
1581     case GID_BEGIN:
1582         m_lastPanX = gi.ptsLocation.x;
1583         m_lastPanY = gi.ptsLocation.y;
1584
1585         break;
1586     case GID_END:
1587         m_gestureTargetNode = 0;
1588         break;
1589     case GID_PAN: {
1590         // Where are the fingers currently?
1591         long currentX = gi.ptsLocation.x;
1592         long currentY = gi.ptsLocation.y;
1593         // How far did we pan in each direction?
1594         long deltaX = currentX - m_lastPanX;
1595         long deltaY = currentY - m_lastPanY;
1596         // Calculate the overpan for window bounce
1597         m_yOverpan -= m_lastPanY - currentY;
1598         m_xOverpan -= m_lastPanX - currentX;
1599         // Update our class variables with updated values
1600         m_lastPanX = currentX;
1601         m_lastPanY = currentY;
1602
1603         Frame* coreFrame = core(m_mainFrame);
1604         if (!coreFrame) {
1605             CloseGestureInfoHandlePtr()(gestureHandle);
1606             return false;
1607         }
1608
1609         if (!m_gestureTargetNode || !m_gestureTargetNode->renderer())
1610             return false;
1611
1612         // We negate here since panning up moves the content up, but moves the scrollbar down.
1613         m_gestureTargetNode->renderer()->enclosingLayer()->scrollByRecursively(-deltaX, -deltaY);
1614            
1615         if (!(UpdatePanningFeedbackPtr() && BeginPanningFeedbackPtr() && EndPanningFeedbackPtr())) {
1616             CloseGestureInfoHandlePtr()(gestureHandle);
1617             return true;
1618         }
1619
1620         if (gi.dwFlags & GF_BEGIN) {
1621             BeginPanningFeedbackPtr()(m_viewWindow);
1622             m_yOverpan = 0;
1623         } else if (gi.dwFlags & GF_END) {
1624             EndPanningFeedbackPtr()(m_viewWindow, true);
1625             m_yOverpan = 0;
1626         }
1627
1628         ScrollView* view = coreFrame->view();
1629         if (!view) {
1630             CloseGestureInfoHandlePtr()(gestureHandle);
1631             return true;
1632         }
1633         Scrollbar* vertScrollbar = view->verticalScrollbar();
1634         if (!vertScrollbar) {
1635             CloseGestureInfoHandlePtr()(gestureHandle);
1636             return true;
1637         }
1638
1639         // FIXME: Support Horizontal Window Bounce. <https://webkit.org/b/28500>.
1640         // FIXME: If the user starts panning down after a window bounce has started, the window doesn't bounce back 
1641         // until they release their finger. <https://webkit.org/b/28501>.
1642         if (vertScrollbar->currentPos() == 0)
1643             UpdatePanningFeedbackPtr()(m_viewWindow, 0, m_yOverpan, gi.dwFlags & GF_INERTIA);
1644         else if (vertScrollbar->currentPos() >= vertScrollbar->maximum())
1645             UpdatePanningFeedbackPtr()(m_viewWindow, 0, m_yOverpan, gi.dwFlags & GF_INERTIA);
1646
1647         CloseGestureInfoHandlePtr()(gestureHandle);
1648         return true;
1649     }
1650     default:
1651         break;
1652     }
1653
1654     // If we get to this point, the gesture has not been handled. We forward
1655     // the call to DefWindowProc by returning false, and we don't need to 
1656     // to call CloseGestureInfoHandle. 
1657     // http://msdn.microsoft.com/en-us/library/dd353228(VS.85).aspx
1658     return false;
1659 }
1660
1661 bool WebView::mouseWheel(WPARAM wParam, LPARAM lParam, bool isMouseHWheel)
1662 {
1663     // Ctrl+Mouse wheel doesn't ever go into WebCore.  It is used to
1664     // zoom instead (Mac zooms the whole Desktop, but Windows browsers trigger their
1665     // own local zoom modes for Ctrl+wheel).
1666     if (wParam & MK_CONTROL) {
1667         short delta = short(HIWORD(wParam));
1668         if (delta < 0)
1669             makeTextSmaller(0);
1670         else
1671             makeTextLarger(0);
1672         return true;
1673     }
1674     
1675     // 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.
1676     HWND focusedWindow = GetFocus();
1677     if (focusedWindow && focusedWindow != m_viewWindow) {
1678         // 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).
1679         TCHAR className[256];
1680
1681         // Make sure truncation won't affect the comparison.
1682         ASSERT(WTF_ARRAY_LENGTH(className) > _tcslen(PopupMenuWin::popupClassName()));
1683
1684         if (GetClassName(focusedWindow, className, WTF_ARRAY_LENGTH(className)) && !_tcscmp(className, PopupMenuWin::popupClassName())) {
1685             // We don't let the WebView scroll here for two reasons - 1) To match Firefox behavior, 2) If we do scroll, we lose the
1686             // focus ring around the select menu.
1687             SetFocus(m_viewWindow);
1688             return true;
1689         }
1690     }
1691
1692     PlatformWheelEvent wheelEvent(m_viewWindow, wParam, lParam, isMouseHWheel);
1693     Frame* coreFrame = core(m_mainFrame);
1694     if (!coreFrame)
1695         return false;
1696
1697     return coreFrame->eventHandler()->handleWheelEvent(wheelEvent);
1698 }
1699
1700 bool WebView::verticalScroll(WPARAM wParam, LPARAM /*lParam*/)
1701 {
1702     ScrollDirection direction;
1703     ScrollGranularity granularity;
1704     switch (LOWORD(wParam)) {
1705     case SB_LINEDOWN:
1706         granularity = ScrollByLine;
1707         direction = ScrollDown;
1708         break;
1709     case SB_LINEUP:
1710         granularity = ScrollByLine;
1711         direction = ScrollUp;
1712         break;
1713     case SB_PAGEDOWN:
1714         granularity = ScrollByDocument;
1715         direction = ScrollDown;
1716         break;
1717     case SB_PAGEUP:
1718         granularity = ScrollByDocument;
1719         direction = ScrollUp;
1720         break;
1721     default:
1722         return false;
1723         break;
1724     }
1725     
1726     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1727     return frame->eventHandler()->scrollRecursively(direction, granularity);
1728 }
1729
1730 bool WebView::horizontalScroll(WPARAM wParam, LPARAM /*lParam*/)
1731 {
1732     ScrollDirection direction;
1733     ScrollGranularity granularity;
1734     switch (LOWORD(wParam)) {
1735     case SB_LINELEFT:
1736         granularity = ScrollByLine;
1737         direction = ScrollLeft;
1738         break;
1739     case SB_LINERIGHT:
1740         granularity = ScrollByLine;
1741         direction = ScrollRight;
1742         break;
1743     case SB_PAGELEFT:
1744         granularity = ScrollByDocument;
1745         direction = ScrollLeft;
1746         break;
1747     case SB_PAGERIGHT:
1748         granularity = ScrollByDocument;
1749         direction = ScrollRight;
1750         break;
1751     default:
1752         return false;
1753     }
1754
1755     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1756     return frame->eventHandler()->scrollRecursively(direction, granularity);
1757 }
1758
1759
1760 bool WebView::execCommand(WPARAM wParam, LPARAM /*lParam*/)
1761 {
1762     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1763     switch (LOWORD(wParam)) {
1764         case SelectAll:
1765             return frame->editor()->command("SelectAll").execute();
1766         case Undo:
1767             return frame->editor()->command("Undo").execute();
1768         case Redo:
1769             return frame->editor()->command("Redo").execute();
1770     }
1771     return false;
1772 }
1773
1774 bool WebView::keyUp(WPARAM virtualKeyCode, LPARAM keyData, bool systemKeyDown)
1775 {
1776     PlatformKeyboardEvent keyEvent(m_viewWindow, virtualKeyCode, keyData, PlatformKeyboardEvent::KeyUp, systemKeyDown);
1777
1778     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1779     m_currentCharacterCode = 0;
1780
1781     return frame->eventHandler()->keyEvent(keyEvent);
1782 }
1783
1784 static const unsigned CtrlKey = 1 << 0;
1785 static const unsigned AltKey = 1 << 1;
1786 static const unsigned ShiftKey = 1 << 2;
1787
1788
1789 struct KeyDownEntry {
1790     unsigned virtualKey;
1791     unsigned modifiers;
1792     const char* name;
1793 };
1794
1795 struct KeyPressEntry {
1796     unsigned charCode;
1797     unsigned modifiers;
1798     const char* name;
1799 };
1800
1801 static const KeyDownEntry keyDownEntries[] = {
1802     { VK_LEFT,   0,                  "MoveLeft"                                    },
1803     { VK_LEFT,   ShiftKey,           "MoveLeftAndModifySelection"                  },
1804     { VK_LEFT,   CtrlKey,            "MoveWordLeft"                                },
1805     { VK_LEFT,   CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection"              },
1806     { VK_RIGHT,  0,                  "MoveRight"                                   },
1807     { VK_RIGHT,  ShiftKey,           "MoveRightAndModifySelection"                 },
1808     { VK_RIGHT,  CtrlKey,            "MoveWordRight"                               },
1809     { VK_RIGHT,  CtrlKey | ShiftKey, "MoveWordRightAndModifySelection"             },
1810     { VK_UP,     0,                  "MoveUp"                                      },
1811     { VK_UP,     ShiftKey,           "MoveUpAndModifySelection"                    },
1812     { VK_PRIOR,  ShiftKey,           "MovePageUpAndModifySelection"                },
1813     { VK_DOWN,   0,                  "MoveDown"                                    },
1814     { VK_DOWN,   ShiftKey,           "MoveDownAndModifySelection"                  },
1815     { VK_NEXT,   ShiftKey,           "MovePageDownAndModifySelection"              },
1816     { VK_PRIOR,  0,                  "MovePageUp"                                  },
1817     { VK_NEXT,   0,                  "MovePageDown"                                },
1818     { VK_HOME,   0,                  "MoveToBeginningOfLine"                       },
1819     { VK_HOME,   ShiftKey,           "MoveToBeginningOfLineAndModifySelection"     },
1820     { VK_HOME,   CtrlKey,            "MoveToBeginningOfDocument"                   },
1821     { VK_HOME,   CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
1822
1823     { VK_END,    0,                  "MoveToEndOfLine"                             },
1824     { VK_END,    ShiftKey,           "MoveToEndOfLineAndModifySelection"           },
1825     { VK_END,    CtrlKey,            "MoveToEndOfDocument"                         },
1826     { VK_END,    CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection"       },
1827
1828     { VK_BACK,   0,                  "DeleteBackward"                              },
1829     { VK_BACK,   ShiftKey,           "DeleteBackward"                              },
1830     { VK_DELETE, 0,                  "DeleteForward"                               },
1831     { VK_BACK,   CtrlKey,            "DeleteWordBackward"                          },
1832     { VK_DELETE, CtrlKey,            "DeleteWordForward"                           },
1833     
1834     { 'B',       CtrlKey,            "ToggleBold"                                  },
1835     { 'I',       CtrlKey,            "ToggleItalic"                                },
1836
1837     { VK_ESCAPE, 0,                  "Cancel"                                      },
1838     { VK_OEM_PERIOD, CtrlKey,        "Cancel"                                      },
1839     { VK_TAB,    0,                  "InsertTab"                                   },
1840     { VK_TAB,    ShiftKey,           "InsertBacktab"                               },
1841     { VK_RETURN, 0,                  "InsertNewline"                               },
1842     { VK_RETURN, CtrlKey,            "InsertNewline"                               },
1843     { VK_RETURN, AltKey,             "InsertNewline"                               },
1844     { VK_RETURN, ShiftKey,           "InsertNewline"                               },
1845     { VK_RETURN, AltKey | ShiftKey,  "InsertNewline"                               },
1846
1847     // It's not quite clear whether clipboard shortcuts and Undo/Redo should be handled
1848     // in the application or in WebKit. We chose WebKit.
1849     { 'C',       CtrlKey,            "Copy"                                        },
1850     { 'V',       CtrlKey,            "Paste"                                       },
1851     { 'X',       CtrlKey,            "Cut"                                         },
1852     { 'A',       CtrlKey,            "SelectAll"                                   },
1853     { VK_INSERT, CtrlKey,            "Copy"                                        },
1854     { VK_DELETE, ShiftKey,           "Cut"                                         },
1855     { VK_INSERT, ShiftKey,           "Paste"                                       },
1856     { 'Z',       CtrlKey,            "Undo"                                        },
1857     { 'Z',       CtrlKey | ShiftKey, "Redo"                                        },
1858 };
1859
1860 static const KeyPressEntry keyPressEntries[] = {
1861     { '\t',   0,                  "InsertTab"                                   },
1862     { '\t',   ShiftKey,           "InsertBacktab"                               },
1863     { '\r',   0,                  "InsertNewline"                               },
1864     { '\r',   CtrlKey,            "InsertNewline"                               },
1865     { '\r',   AltKey,             "InsertNewline"                               },
1866     { '\r',   ShiftKey,           "InsertNewline"                               },
1867     { '\r',   AltKey | ShiftKey,  "InsertNewline"                               },
1868 };
1869
1870 const char* WebView::interpretKeyEvent(const KeyboardEvent* evt)
1871 {
1872     ASSERT(evt->type() == eventNames().keydownEvent || evt->type() == eventNames().keypressEvent);
1873
1874     static HashMap<int, const char*>* keyDownCommandsMap = 0;
1875     static HashMap<int, const char*>* keyPressCommandsMap = 0;
1876
1877     if (!keyDownCommandsMap) {
1878         keyDownCommandsMap = new HashMap<int, const char*>;
1879         keyPressCommandsMap = new HashMap<int, const char*>;
1880
1881         for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyDownEntries); ++i)
1882             keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
1883
1884         for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyPressEntries); ++i)
1885             keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
1886     }
1887
1888     unsigned modifiers = 0;
1889     if (evt->shiftKey())
1890         modifiers |= ShiftKey;
1891     if (evt->altKey())
1892         modifiers |= AltKey;
1893     if (evt->ctrlKey())
1894         modifiers |= CtrlKey;
1895
1896     if (evt->type() == eventNames().keydownEvent) {
1897         int mapKey = modifiers << 16 | evt->keyCode();
1898         return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
1899     }
1900
1901     int mapKey = modifiers << 16 | evt->charCode();
1902     return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
1903 }
1904
1905 bool WebView::handleEditingKeyboardEvent(KeyboardEvent* evt)
1906 {
1907     Node* node = evt->target()->toNode();
1908     ASSERT(node);
1909     Frame* frame = node->document()->frame();
1910     ASSERT(frame);
1911
1912     const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
1913     if (!keyEvent || keyEvent->isSystemKey())  // do not treat this as text input if it's a system key event
1914         return false;
1915
1916     Editor::Command command = frame->editor()->command(interpretKeyEvent(evt));
1917
1918     if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
1919         // WebKit doesn't have enough information about mode to decide how commands that just insert text if executed via Editor should be treated,
1920         // so we leave it upon WebCore to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
1921         // (e.g. Tab that inserts a Tab character, or Enter).
1922         return !command.isTextInsertion() && command.execute(evt);
1923     }
1924
1925      if (command.execute(evt))
1926         return true;
1927
1928     // Don't insert null or control characters as they can result in unexpected behaviour
1929     if (evt->charCode() < ' ')
1930         return false;
1931
1932     return frame->editor()->insertText(evt->keyEvent()->text(), evt);
1933 }
1934
1935 bool WebView::keyDown(WPARAM virtualKeyCode, LPARAM keyData, bool systemKeyDown)
1936 {
1937     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1938     if (virtualKeyCode == VK_CAPITAL)
1939         frame->eventHandler()->capsLockStateMayHaveChanged();
1940
1941     PlatformKeyboardEvent keyEvent(m_viewWindow, virtualKeyCode, keyData, PlatformKeyboardEvent::RawKeyDown, systemKeyDown);
1942     bool handled = frame->eventHandler()->keyEvent(keyEvent);
1943
1944     // These events cannot be canceled, and we have no default handling for them.
1945     // FIXME: match IE list more closely, see <http://msdn2.microsoft.com/en-us/library/ms536938.aspx>.
1946     if (systemKeyDown && virtualKeyCode != VK_RETURN)
1947         return false;
1948
1949     if (handled) {
1950         // FIXME: remove WM_UNICHAR, too
1951         MSG msg;
1952         // WM_SYSCHAR events should not be removed, because access keys are implemented in WebCore in WM_SYSCHAR handler.
1953         if (!systemKeyDown)
1954             ::PeekMessage(&msg, m_viewWindow, WM_CHAR, WM_CHAR, PM_REMOVE);
1955         return true;
1956     }
1957
1958     // We need to handle back/forward using either Backspace(+Shift) or Ctrl+Left/Right Arrow keys.
1959     if ((virtualKeyCode == VK_BACK && keyEvent.shiftKey()) || (virtualKeyCode == VK_RIGHT && keyEvent.ctrlKey()))
1960         return m_page->goForward();
1961     if (virtualKeyCode == VK_BACK || (virtualKeyCode == VK_LEFT && keyEvent.ctrlKey()))
1962         return m_page->goBack();
1963
1964     // Need to scroll the page if the arrow keys, pgup/dn, or home/end are hit.
1965     ScrollDirection direction;
1966     ScrollGranularity granularity;
1967     switch (virtualKeyCode) {
1968         case VK_LEFT:
1969             granularity = ScrollByLine;
1970             direction = ScrollLeft;
1971             break;
1972         case VK_RIGHT:
1973             granularity = ScrollByLine;
1974             direction = ScrollRight;
1975             break;
1976         case VK_UP:
1977             granularity = ScrollByLine;
1978             direction = ScrollUp;
1979             break;
1980         case VK_DOWN:
1981             granularity = ScrollByLine;
1982             direction = ScrollDown;
1983             break;
1984         case VK_HOME:
1985             granularity = ScrollByDocument;
1986             direction = ScrollUp;
1987             break;
1988         case VK_END:
1989             granularity = ScrollByDocument;
1990             direction = ScrollDown;
1991             break;
1992         case VK_PRIOR:
1993             granularity = ScrollByPage;
1994             direction = ScrollUp;
1995             break;
1996         case VK_NEXT:
1997             granularity = ScrollByPage;
1998             direction = ScrollDown;
1999             break;
2000         default:
2001             return false;
2002     }
2003
2004     return frame->eventHandler()->scrollRecursively(direction, granularity);
2005 }
2006
2007 bool WebView::keyPress(WPARAM charCode, LPARAM keyData, bool systemKeyDown)
2008 {
2009     Frame* frame = m_page->focusController()->focusedOrMainFrame();
2010
2011     PlatformKeyboardEvent keyEvent(m_viewWindow, charCode, keyData, PlatformKeyboardEvent::Char, systemKeyDown);
2012     // IE does not dispatch keypress event for WM_SYSCHAR.
2013     if (systemKeyDown)
2014         return frame->eventHandler()->handleAccessKey(keyEvent);
2015     return frame->eventHandler()->keyEvent(keyEvent);
2016 }
2017
2018 bool WebView::registerWebViewWindowClass()
2019 {
2020     static bool haveRegisteredWindowClass = false;
2021     if (haveRegisteredWindowClass)
2022         return true;
2023
2024     haveRegisteredWindowClass = true;
2025
2026     WNDCLASSEX wcex;
2027
2028     wcex.cbSize = sizeof(WNDCLASSEX);
2029
2030     wcex.style          = CS_DBLCLKS;
2031     wcex.lpfnWndProc    = WebViewWndProc;
2032     wcex.cbClsExtra     = 0;
2033     wcex.cbWndExtra     = 4; // 4 bytes for the IWebView pointer
2034     wcex.hInstance      = gInstance;
2035     wcex.hIcon          = 0;
2036     wcex.hCursor        = ::LoadCursor(0, IDC_ARROW);
2037     wcex.hbrBackground  = 0;
2038     wcex.lpszMenuName   = 0;
2039     wcex.lpszClassName  = kWebViewWindowClassName;
2040     wcex.hIconSm        = 0;
2041
2042     return !!RegisterClassEx(&wcex);
2043 }
2044
2045 static HWND findTopLevelParent(HWND window)
2046 {
2047     if (!window)
2048         return 0;
2049
2050     HWND current = window;
2051     for (HWND parent = GetParent(current); current; current = parent, parent = GetParent(parent))
2052         if (!parent || !(GetWindowLongPtr(current, GWL_STYLE) & (WS_POPUP | WS_CHILD)))
2053             return current;
2054     ASSERT_NOT_REACHED();
2055     return 0;
2056 }
2057
2058 LRESULT CALLBACK WebView::WebViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2059 {
2060     LRESULT lResult = 0;
2061     LONG_PTR longPtr = GetWindowLongPtr(hWnd, 0);
2062     WebView* webView = reinterpret_cast<WebView*>(longPtr);
2063     WebFrame* mainFrameImpl = webView ? webView->topLevelFrame() : 0;
2064     if (!mainFrameImpl || webView->isBeingDestroyed())
2065         return DefWindowProc(hWnd, message, wParam, lParam);
2066
2067     // hold a ref, since the WebView could go away in an event handler.
2068     COMPtr<WebView> protector(webView);
2069     ASSERT(webView);
2070
2071     // Windows Media Player has a modal message loop that will deliver messages
2072     // to us at inappropriate times and we will crash if we handle them when
2073     // they are delivered. We repost paint messages so that we eventually get
2074     // a chance to paint once the modal loop has exited, but other messages
2075     // aren't safe to repost, so we just drop them.
2076     if (PluginView::isCallingPlugin()) {
2077         if (message == WM_PAINT)
2078             PostMessage(hWnd, message, wParam, lParam);
2079         return 0;
2080     }
2081
2082     bool handled = true;
2083
2084     switch (message) {
2085         case WM_PAINT: {
2086             webView->paint(0, 0);
2087             break;
2088         }
2089         case WM_PRINTCLIENT:
2090             webView->paint((HDC)wParam, lParam);
2091             break;
2092         case WM_DESTROY:
2093             webView->setIsBeingDestroyed();
2094             webView->close();
2095             break;
2096         case WM_GESTURENOTIFY:
2097             handled = webView->gestureNotify(wParam, lParam);
2098             break;
2099         case WM_GESTURE:
2100             handled = webView->gesture(wParam, lParam);
2101             break;
2102         case WM_MOUSEMOVE:
2103         case WM_LBUTTONDOWN:
2104         case WM_MBUTTONDOWN:
2105         case WM_RBUTTONDOWN:
2106         case WM_LBUTTONDBLCLK:
2107         case WM_MBUTTONDBLCLK:
2108         case WM_RBUTTONDBLCLK:
2109         case WM_LBUTTONUP:
2110         case WM_MBUTTONUP:
2111         case WM_RBUTTONUP:
2112         case WM_MOUSELEAVE:
2113         case WM_CANCELMODE:
2114             if (Frame* coreFrame = core(mainFrameImpl))
2115                 if (coreFrame->view()->didFirstLayout())
2116                     handled = webView->handleMouseEvent(message, wParam, lParam);
2117             break;
2118         case WM_MOUSEWHEEL:
2119         case WM_VISTA_MOUSEHWHEEL:
2120             if (Frame* coreFrame = core(mainFrameImpl))
2121                 if (coreFrame->view()->didFirstLayout())
2122                     handled = webView->mouseWheel(wParam, lParam, message == WM_VISTA_MOUSEHWHEEL);
2123             break;
2124         case WM_SYSKEYDOWN:
2125             handled = webView->keyDown(wParam, lParam, true);
2126             break;
2127         case WM_KEYDOWN:
2128             handled = webView->keyDown(wParam, lParam);
2129             break;
2130         case WM_SYSKEYUP:
2131             handled = webView->keyUp(wParam, lParam, true);
2132             break;
2133         case WM_KEYUP:
2134             handled = webView->keyUp(wParam, lParam);
2135             break;
2136         case WM_SYSCHAR:
2137             handled = webView->keyPress(wParam, lParam, true);
2138             break;
2139         case WM_CHAR:
2140             handled = webView->keyPress(wParam, lParam);
2141             break;
2142         // FIXME: We need to check WM_UNICHAR to support supplementary characters (that don't fit in 16 bits).
2143         case WM_SIZE:
2144             if (lParam != 0)
2145                 webView->sizeChanged(IntSize(LOWORD(lParam), HIWORD(lParam)));
2146             break;
2147         case WM_SHOWWINDOW:
2148             lResult = DefWindowProc(hWnd, message, wParam, lParam);
2149             if (wParam == 0) {
2150                 // The window is being hidden (e.g., because we switched tabs).
2151                 // Null out our backing store.
2152                 webView->deleteBackingStore();
2153             }
2154             break;
2155         case WM_SETFOCUS: {
2156             COMPtr<IWebUIDelegate> uiDelegate;
2157             COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2158             if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2159                 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate)
2160                 uiDelegatePrivate->webViewReceivedFocus(webView);
2161
2162             FocusController* focusController = webView->page()->focusController();
2163             if (Frame* frame = focusController->focusedFrame()) {
2164                 // Send focus events unless the previously focused window is a
2165                 // child of ours (for example a plugin).
2166                 if (!IsChild(hWnd, reinterpret_cast<HWND>(wParam)))
2167                     focusController->setFocused(true);
2168             } else
2169                 focusController->setFocused(true);
2170             break;
2171         }
2172         case WM_KILLFOCUS: {
2173             COMPtr<IWebUIDelegate> uiDelegate;
2174             COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2175             HWND newFocusWnd = reinterpret_cast<HWND>(wParam);
2176             if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2177                 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate)
2178                 uiDelegatePrivate->webViewLostFocus(webView, (OLE_HANDLE)(ULONG64)newFocusWnd);
2179
2180             FocusController* focusController = webView->page()->focusController();
2181             Frame* frame = focusController->focusedOrMainFrame();
2182             webView->resetIME(frame);
2183             // Send blur events unless we're losing focus to a child of ours.
2184             if (!IsChild(hWnd, newFocusWnd))
2185                 focusController->setFocused(false);
2186
2187             // If we are pan-scrolling when we lose focus, stop the pan scrolling.
2188             frame->eventHandler()->stopAutoscrollTimer();
2189
2190             break;
2191         }
2192         case WM_WINDOWPOSCHANGED:
2193             if (reinterpret_cast<WINDOWPOS*>(lParam)->flags & SWP_SHOWWINDOW)
2194                 webView->updateActiveStateSoon();
2195             handled = false;
2196             break;
2197         case WM_CUT:
2198             webView->cut(0);
2199             break;
2200         case WM_COPY:
2201             webView->copy(0);
2202             break;
2203         case WM_PASTE:
2204             webView->paste(0);
2205             break;
2206         case WM_CLEAR:
2207             webView->delete_(0);
2208             break;
2209         case WM_COMMAND:
2210             if (HIWORD(wParam))
2211                 handled = webView->execCommand(wParam, lParam);
2212             else // If the high word of wParam is 0, the message is from a menu
2213                 webView->performContextMenuAction(wParam, lParam, false);
2214             break;
2215         case WM_MENUCOMMAND:
2216             webView->performContextMenuAction(wParam, lParam, true);
2217             break;
2218         case WM_CONTEXTMENU:
2219             handled = webView->handleContextMenuEvent(wParam, lParam);
2220             break;
2221         case WM_INITMENUPOPUP:
2222             handled = webView->onInitMenuPopup(wParam, lParam);
2223             break;
2224         case WM_MEASUREITEM:
2225             handled = webView->onMeasureItem(wParam, lParam);
2226             break;
2227         case WM_DRAWITEM:
2228             handled = webView->onDrawItem(wParam, lParam);
2229             break;
2230         case WM_UNINITMENUPOPUP:
2231             handled = webView->onUninitMenuPopup(wParam, lParam);
2232             break;
2233         case WM_XP_THEMECHANGED:
2234             if (Frame* coreFrame = core(mainFrameImpl)) {
2235                 webView->deleteBackingStore();
2236                 coreFrame->page()->theme()->themeChanged();
2237                 ScrollbarTheme::nativeTheme()->themeChanged();
2238                 RECT windowRect;
2239                 ::GetClientRect(hWnd, &windowRect);
2240                 ::InvalidateRect(hWnd, &windowRect, false);
2241 #if USE(ACCELERATED_COMPOSITING)
2242                 if (webView->isAcceleratedCompositing())
2243                     webView->m_backingLayer->setNeedsDisplay();
2244 #endif
2245            }
2246             break;
2247         case WM_MOUSEACTIVATE:
2248             webView->setMouseActivated(true);
2249             handled = false;
2250             break;
2251         case WM_GETDLGCODE: {
2252             COMPtr<IWebUIDelegate> uiDelegate;
2253             COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2254             LONG_PTR dlgCode = 0;
2255             UINT keyCode = 0;
2256             if (lParam) {
2257                 LPMSG lpMsg = (LPMSG)lParam;
2258                 if (lpMsg->message == WM_KEYDOWN)
2259                     keyCode = (UINT) lpMsg->wParam;
2260             }
2261             if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2262                 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate
2263                 && SUCCEEDED(uiDelegatePrivate->webViewGetDlgCode(webView, keyCode, &dlgCode)))
2264                 return dlgCode;
2265             handled = false;
2266             break;
2267         }
2268         case WM_GETOBJECT:
2269             handled = webView->onGetObject(wParam, lParam, lResult);
2270             break;
2271         case WM_IME_STARTCOMPOSITION:
2272             handled = webView->onIMEStartComposition();
2273             break;
2274         case WM_IME_REQUEST:
2275             lResult = webView->onIMERequest(wParam, lParam);
2276             break;
2277         case WM_IME_COMPOSITION:
2278             handled = webView->onIMEComposition(lParam);
2279             break;
2280         case WM_IME_ENDCOMPOSITION:
2281             handled = webView->onIMEEndComposition();
2282             break;
2283         case WM_IME_CHAR:
2284             handled = webView->onIMEChar(wParam, lParam);
2285             break;
2286         case WM_IME_NOTIFY:
2287             handled = webView->onIMENotify(wParam, lParam, &lResult);
2288             break;
2289         case WM_IME_SELECT:
2290             handled = webView->onIMESelect(wParam, lParam);
2291             break;
2292         case WM_IME_SETCONTEXT:
2293             handled = webView->onIMESetContext(wParam, lParam);
2294             break;
2295         case WM_TIMER:
2296             switch (wParam) {
2297                 case UpdateActiveStateTimer:
2298                     KillTimer(hWnd, UpdateActiveStateTimer);
2299                     webView->updateActiveState();
2300                     break;
2301                 case DeleteBackingStoreTimer:
2302                     webView->deleteBackingStore();
2303                     break;
2304             }
2305             break;
2306         case WM_SETCURSOR:
2307             handled = ::SetCursor(webView->m_lastSetCursor);
2308             break;
2309         case WM_VSCROLL:
2310             handled = webView->verticalScroll(wParam, lParam);
2311             break;
2312         case WM_HSCROLL:
2313             handled = webView->horizontalScroll(wParam, lParam);
2314             break;
2315         default:
2316             handled = false;
2317             break;
2318     }
2319
2320     if (!handled)
2321         lResult = DefWindowProc(hWnd, message, wParam, lParam);
2322     
2323     // Let the client know whether we consider this message handled.
2324     return (message == WM_KEYDOWN || message == WM_SYSKEYDOWN || message == WM_KEYUP || message == WM_SYSKEYUP) ? !handled : lResult;
2325 }
2326
2327 bool WebView::developerExtrasEnabled() const
2328 {
2329     if (m_preferences->developerExtrasDisabledByOverride())
2330         return false;
2331
2332 #ifdef NDEBUG
2333     BOOL enabled;
2334     return SUCCEEDED(m_preferences->developerExtrasEnabled(&enabled)) && enabled;
2335 #else
2336     return true;
2337 #endif
2338 }
2339
2340 static String webKitVersionString()
2341 {
2342     LPWSTR buildNumberStringPtr;
2343     if (!::LoadStringW(gInstance, BUILD_NUMBER, reinterpret_cast<LPWSTR>(&buildNumberStringPtr), 0) || !buildNumberStringPtr)
2344         return "534+";
2345
2346     return buildNumberStringPtr;
2347 }
2348
2349 const String& WebView::userAgentForKURL(const KURL&)
2350 {
2351     if (m_userAgentOverridden)
2352         return m_userAgentCustom;
2353
2354     if (!m_userAgentStandard.length())
2355         m_userAgentStandard = WebView::standardUserAgentWithApplicationName(m_applicationName);
2356     return m_userAgentStandard;
2357 }
2358
2359 // IUnknown -------------------------------------------------------------------
2360
2361 HRESULT STDMETHODCALLTYPE WebView::QueryInterface(REFIID riid, void** ppvObject)
2362 {
2363     *ppvObject = 0;
2364     if (IsEqualGUID(riid, CLSID_WebView))
2365         *ppvObject = this;
2366     else if (IsEqualGUID(riid, IID_IUnknown))
2367         *ppvObject = static_cast<IWebView*>(this);
2368     else if (IsEqualGUID(riid, IID_IWebView))
2369         *ppvObject = static_cast<IWebView*>(this);
2370     else if (IsEqualGUID(riid, IID_IWebViewPrivate))
2371         *ppvObject = static_cast<IWebViewPrivate*>(this);
2372     else if (IsEqualGUID(riid, IID_IWebIBActions))
2373         *ppvObject = static_cast<IWebIBActions*>(this);
2374     else if (IsEqualGUID(riid, IID_IWebViewCSS))
2375         *ppvObject = static_cast<IWebViewCSS*>(this);
2376     else if (IsEqualGUID(riid, IID_IWebViewEditing))
2377         *ppvObject = static_cast<IWebViewEditing*>(this);
2378     else if (IsEqualGUID(riid, IID_IWebViewUndoableEditing))
2379         *ppvObject = static_cast<IWebViewUndoableEditing*>(this);
2380     else if (IsEqualGUID(riid, IID_IWebViewEditingActions))
2381         *ppvObject = static_cast<IWebViewEditingActions*>(this);
2382     else if (IsEqualGUID(riid, IID_IWebNotificationObserver))
2383         *ppvObject = static_cast<IWebNotificationObserver*>(this);
2384     else if (IsEqualGUID(riid, IID_IDropTarget))
2385         *ppvObject = static_cast<IDropTarget*>(this);
2386     else
2387         return E_NOINTERFACE;
2388
2389     AddRef();
2390     return S_OK;
2391 }
2392
2393 ULONG STDMETHODCALLTYPE WebView::AddRef(void)
2394 {
2395     ASSERT(!m_deletionHasBegun);
2396     return ++m_refCount;
2397 }
2398
2399 ULONG STDMETHODCALLTYPE WebView::Release(void)
2400 {
2401     ASSERT(!m_deletionHasBegun);
2402
2403     if (m_refCount == 1) {
2404         // Call close() now so that clients don't have to. (It's harmless to call close() multiple
2405         // times.) We do this here instead of in our destructor because close() can cause AddRef()
2406         // and Release() to be called, and if that happened in our destructor we would be destroyed
2407         // more than once.
2408         close();
2409     }
2410
2411     ULONG newRef = --m_refCount;
2412     if (!newRef) {
2413 #if !ASSERT_DISABLED
2414         m_deletionHasBegun = true;
2415 #endif
2416         delete(this);
2417     }
2418
2419     return newRef;
2420 }
2421
2422 // IWebView --------------------------------------------------------------------
2423
2424 HRESULT STDMETHODCALLTYPE WebView::canShowMIMEType( 
2425     /* [in] */ BSTR mimeType,
2426     /* [retval][out] */ BOOL* canShow)
2427 {
2428     String mimeTypeStr(mimeType, SysStringLen(mimeType));
2429
2430     if (!canShow)
2431         return E_POINTER;
2432
2433     *canShow = MIMETypeRegistry::isSupportedImageMIMEType(mimeTypeStr) ||
2434         MIMETypeRegistry::isSupportedNonImageMIMEType(mimeTypeStr) ||
2435         (m_page && m_page->pluginData() && m_page->pluginData()->supportsMimeType(mimeTypeStr)) ||
2436         shouldUseEmbeddedView(mimeTypeStr);
2437     
2438     return S_OK;
2439 }
2440
2441 HRESULT STDMETHODCALLTYPE WebView::canShowMIMETypeAsHTML( 
2442     /* [in] */ BSTR /*mimeType*/,
2443     /* [retval][out] */ BOOL* canShow)
2444 {
2445     // FIXME
2446     *canShow = TRUE;
2447     return S_OK;
2448 }
2449
2450 HRESULT STDMETHODCALLTYPE WebView::MIMETypesShownAsHTML( 
2451     /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
2452 {
2453     ASSERT_NOT_REACHED();
2454     return E_NOTIMPL;
2455 }
2456
2457 HRESULT STDMETHODCALLTYPE WebView::setMIMETypesShownAsHTML( 
2458         /* [size_is][in] */ BSTR* /*mimeTypes*/,
2459         /* [in] */ int /*cMimeTypes*/)
2460 {
2461     ASSERT_NOT_REACHED();
2462     return E_NOTIMPL;
2463 }
2464
2465 HRESULT STDMETHODCALLTYPE WebView::URLFromPasteboard( 
2466     /* [in] */ IDataObject* /*pasteboard*/,
2467     /* [retval][out] */ BSTR* /*url*/)
2468 {
2469     ASSERT_NOT_REACHED();
2470     return E_NOTIMPL;
2471 }
2472
2473 HRESULT STDMETHODCALLTYPE WebView::URLTitleFromPasteboard( 
2474     /* [in] */ IDataObject* /*pasteboard*/,
2475     /* [retval][out] */ BSTR* /*urlTitle*/)
2476 {
2477     ASSERT_NOT_REACHED();
2478     return E_NOTIMPL;
2479 }
2480
2481 static void WebKitSetApplicationCachePathIfNecessary()
2482 {
2483     static bool initialized = false;
2484     if (initialized)
2485         return;
2486
2487     String path = localUserSpecificStorageDirectory();
2488     if (!path.isNull())
2489         cacheStorage().setCacheDirectory(path);
2490
2491     initialized = true;
2492 }
2493
2494 bool WebView::shouldInitializeTrackPointHack()
2495 {
2496     static bool shouldCreateScrollbars;
2497     static bool hasRunTrackPointCheck;
2498
2499     if (hasRunTrackPointCheck)
2500         return shouldCreateScrollbars;
2501
2502     hasRunTrackPointCheck = true;
2503     const TCHAR trackPointKeys[][50] = { TEXT("Software\\Lenovo\\TrackPoint"),
2504         TEXT("Software\\Lenovo\\UltraNav"),
2505         TEXT("Software\\Alps\\Apoint\\TrackPoint"),
2506         TEXT("Software\\Synaptics\\SynTPEnh\\UltraNavUSB"),
2507         TEXT("Software\\Synaptics\\SynTPEnh\\UltraNavPS2") };
2508
2509     for (int i = 0; i < 5; ++i) {
2510         HKEY trackPointKey;
2511         int readKeyResult = ::RegOpenKeyEx(HKEY_CURRENT_USER, trackPointKeys[i], 0, KEY_READ, &trackPointKey);
2512         ::RegCloseKey(trackPointKey);
2513         if (readKeyResult == ERROR_SUCCESS) {
2514             shouldCreateScrollbars = true;
2515             return shouldCreateScrollbars;
2516         }
2517     }
2518
2519     return shouldCreateScrollbars;
2520 }
2521     
2522 HRESULT STDMETHODCALLTYPE WebView::initWithFrame( 
2523     /* [in] */ RECT frame,
2524     /* [in] */ BSTR frameName,
2525     /* [in] */ BSTR groupName)
2526 {
2527     HRESULT hr = S_OK;
2528
2529     if (m_viewWindow)
2530         return E_FAIL;
2531
2532     registerWebViewWindowClass();
2533
2534     m_viewWindow = CreateWindowEx(0, kWebViewWindowClassName, 0, WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
2535         frame.left, frame.top, frame.right - frame.left, frame.bottom - frame.top, m_hostWindow ? m_hostWindow : HWND_MESSAGE, 0, gInstance, 0);
2536     ASSERT(::IsWindow(m_viewWindow));
2537
2538     if (shouldInitializeTrackPointHack()) {
2539         // If we detected a registry key belonging to a TrackPoint driver, then create fake trackpoint
2540         // scrollbars, so the WebView will receive WM_VSCROLL and WM_HSCROLL messages. We create one
2541         // vertical scrollbar and one horizontal to allow for receiving both types of messages.
2542         ::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTHSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_HORZ, 0, 0, 0, 0, m_viewWindow, 0, gInstance, 0);
2543         ::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTVSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_VERT, 0, 0, 0, 0, m_viewWindow, 0, gInstance, 0);
2544     }
2545
2546     hr = registerDragDrop();
2547     if (FAILED(hr))
2548         return hr;
2549
2550     WebPreferences* sharedPreferences = WebPreferences::sharedStandardPreferences();
2551     sharedPreferences->willAddToWebView();
2552     m_preferences = sharedPreferences;
2553
2554     static bool didOneTimeInitialization;
2555     if (!didOneTimeInitialization) {
2556         InitializeLoggingChannelsIfNecessary();
2557 #if ENABLE(DATABASE)
2558         WebKitInitializeWebDatabasesIfNecessary();
2559 #endif
2560         WebKitSetApplicationCachePathIfNecessary();
2561         WebPlatformStrategies::initialize();
2562         Settings::setDefaultMinDOMTimerInterval(0.004);
2563
2564         didOneTimeInitialization = true;
2565      }
2566
2567 #if USE(SAFARI_THEME)
2568     BOOL shouldPaintNativeControls;
2569     if (SUCCEEDED(m_preferences->shouldPaintNativeControls(&shouldPaintNativeControls)))
2570         Settings::setShouldPaintNativeControls(shouldPaintNativeControls);
2571 #endif
2572
2573     BOOL useHighResolutionTimer;
2574     if (SUCCEEDED(m_preferences->shouldUseHighResolutionTimers(&useHighResolutionTimer)))
2575         Settings::setShouldUseHighResolutionTimers(useHighResolutionTimer);
2576
2577     Page::PageClients pageClients;
2578     pageClients.chromeClient = new WebChromeClient(this);
2579     pageClients.contextMenuClient = new WebContextMenuClient(this);
2580     pageClients.editorClient = new WebEditorClient(this);
2581     pageClients.dragClient = new WebDragClient(this);
2582     pageClients.inspectorClient = new WebInspectorClient(this);
2583     pageClients.pluginHalterClient = new WebPluginHalterClient(this);
2584 #if ENABLE(CLIENT_BASED_GEOLOCATION)
2585     pageClients.geolocationClient = new WebGeolocationClient(this);
2586 #endif
2587     m_page = new Page(pageClients);
2588
2589     BSTR localStoragePath;
2590     if (SUCCEEDED(m_preferences->localStorageDatabasePath(&localStoragePath))) {
2591         m_page->settings()->setLocalStorageDatabasePath(String(localStoragePath, SysStringLen(localStoragePath)));
2592         SysFreeString(localStoragePath);
2593     }
2594
2595     if (m_uiDelegate) {
2596         BSTR path;
2597         if (SUCCEEDED(m_uiDelegate->ftpDirectoryTemplatePath(this, &path))) {
2598             m_page->settings()->setFTPDirectoryTemplatePath(String(path, SysStringLen(path)));
2599             SysFreeString(path);
2600         }
2601     }
2602
2603     WebFrame* webFrame = WebFrame::createInstance();
2604     RefPtr<Frame> coreFrame = webFrame->init(this, m_page, 0);
2605     m_mainFrame = webFrame;
2606     webFrame->Release(); // The WebFrame is owned by the Frame, so release our reference to it.
2607
2608     coreFrame->tree()->setName(String(frameName, SysStringLen(frameName)));
2609     coreFrame->init();
2610     setGroupName(groupName);
2611
2612     addToAllWebViewsSet();
2613
2614     #pragma warning(suppress: 4244)
2615     SetWindowLongPtr(m_viewWindow, 0, (LONG_PTR)this);
2616     ShowWindow(m_viewWindow, SW_SHOW);
2617
2618     initializeToolTipWindow();
2619     windowAncestryDidChange();
2620
2621     IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
2622     notifyCenter->addObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
2623     m_preferences->postPreferencesChangesNotification();
2624
2625     setSmartInsertDeleteEnabled(TRUE);
2626     return hr;
2627 }
2628
2629 static bool initCommonControls()
2630 {
2631     static bool haveInitialized = false;
2632     if (haveInitialized)
2633         return true;
2634
2635     INITCOMMONCONTROLSEX init;
2636     init.dwSize = sizeof(init);
2637     init.dwICC = ICC_TREEVIEW_CLASSES;
2638     haveInitialized = !!::InitCommonControlsEx(&init);
2639     return haveInitialized;
2640 }
2641
2642 void WebView::initializeToolTipWindow()
2643 {
2644     if (!initCommonControls())
2645         return;
2646
2647     m_toolTipHwnd = CreateWindowEx(WS_EX_TRANSPARENT, TOOLTIPS_CLASS, 0, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
2648                                    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2649                                    m_viewWindow, 0, 0, 0);
2650     if (!m_toolTipHwnd)
2651         return;
2652
2653     TOOLINFO info = {0};
2654     info.cbSize = sizeof(info);
2655     info.uFlags = TTF_IDISHWND | TTF_SUBCLASS ;
2656     info.uId = reinterpret_cast<UINT_PTR>(m_viewWindow);
2657
2658     ::SendMessage(m_toolTipHwnd, TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&info));
2659     ::SendMessage(m_toolTipHwnd, TTM_SETMAXTIPWIDTH, 0, maxToolTipWidth);
2660
2661     ::SetWindowPos(m_toolTipHwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
2662 }
2663
2664 void WebView::setToolTip(const String& toolTip)
2665 {
2666     if (!m_toolTipHwnd)
2667         return;
2668
2669     if (toolTip == m_toolTip)
2670         return;
2671
2672     m_toolTip = toolTip;
2673
2674     if (!m_toolTip.isEmpty()) {
2675         TOOLINFO info = {0};
2676         info.cbSize = sizeof(info);
2677         info.uFlags = TTF_IDISHWND;
2678         info.uId = reinterpret_cast<UINT_PTR>(m_viewWindow);
2679         info.lpszText = const_cast<UChar*>(m_toolTip.charactersWithNullTermination());
2680         ::SendMessage(m_toolTipHwnd, TTM_UPDATETIPTEXT, 0, reinterpret_cast<LPARAM>(&info));
2681     }
2682
2683     ::SendMessage(m_toolTipHwnd, TTM_ACTIVATE, !m_toolTip.isEmpty(), 0);
2684 }
2685
2686 HRESULT WebView::notifyDidAddIcon(IWebNotification* notification)
2687 {
2688     COMPtr<IPropertyBag> propertyBag;
2689     HRESULT hr = notification->userInfo(&propertyBag);
2690     if (FAILED(hr))
2691         return hr;
2692     if (!propertyBag)
2693         return E_FAIL;
2694
2695     COMPtr<CFDictionaryPropertyBag> dictionaryPropertyBag;
2696     hr = propertyBag->QueryInterface(&dictionaryPropertyBag);
2697     if (FAILED(hr))
2698         return hr;
2699
2700     CFDictionaryRef dictionary = dictionaryPropertyBag->dictionary();
2701     if (!dictionary)
2702         return E_FAIL;
2703
2704     CFTypeRef value = CFDictionaryGetValue(dictionary, WebIconDatabase::iconDatabaseNotificationUserInfoURLKey());
2705     if (!value)
2706         return E_FAIL;
2707     if (CFGetTypeID(value) != CFStringGetTypeID())
2708         return E_FAIL;
2709
2710     String mainFrameURL;
2711     if (m_mainFrame)
2712         mainFrameURL = m_mainFrame->url().string();
2713
2714     if (!mainFrameURL.isEmpty() && mainFrameURL == String((CFStringRef)value))
2715         dispatchDidReceiveIconFromWebFrame(m_mainFrame);
2716
2717     return hr;
2718 }
2719
2720 void WebView::registerForIconNotification(bool listen)
2721 {
2722     IWebNotificationCenter* nc = WebNotificationCenter::defaultCenterInternal();
2723     if (listen)
2724         nc->addObserver(this, WebIconDatabase::iconDatabaseDidAddIconNotification(), 0);
2725     else
2726         nc->removeObserver(this, WebIconDatabase::iconDatabaseDidAddIconNotification(), 0);
2727 }
2728
2729 void WebView::dispatchDidReceiveIconFromWebFrame(WebFrame* frame)
2730 {
2731     registerForIconNotification(false);
2732
2733     if (m_frameLoadDelegate)
2734         // FIXME: <rdar://problem/5491010> - Pass in the right HBITMAP. 
2735         m_frameLoadDelegate->didReceiveIcon(this, 0, frame);
2736 }
2737
2738 HRESULT STDMETHODCALLTYPE WebView::setUIDelegate( 
2739     /* [in] */ IWebUIDelegate* d)
2740 {
2741     m_uiDelegate = d;
2742
2743     if (m_uiDelegatePrivate)
2744         m_uiDelegatePrivate = 0;
2745
2746     if (d) {
2747         if (FAILED(d->QueryInterface(IID_IWebUIDelegatePrivate, (void**)&m_uiDelegatePrivate)))
2748             m_uiDelegatePrivate = 0;
2749     }
2750
2751     return S_OK;
2752 }
2753
2754 HRESULT STDMETHODCALLTYPE WebView::uiDelegate( 
2755     /* [out][retval] */ IWebUIDelegate** d)
2756 {
2757     if (!m_uiDelegate)
2758         return E_FAIL;
2759
2760     return m_uiDelegate.copyRefTo(d);
2761 }
2762
2763 HRESULT STDMETHODCALLTYPE WebView::setResourceLoadDelegate( 
2764     /* [in] */ IWebResourceLoadDelegate* d)
2765 {
2766     m_resourceLoadDelegate = d;
2767     return S_OK;
2768 }
2769
2770 HRESULT STDMETHODCALLTYPE WebView::resourceLoadDelegate( 
2771     /* [out][retval] */ IWebResourceLoadDelegate** d)
2772 {
2773     if (!m_resourceLoadDelegate)
2774         return E_FAIL;
2775
2776     return m_resourceLoadDelegate.copyRefTo(d);
2777 }
2778
2779 HRESULT STDMETHODCALLTYPE WebView::setDownloadDelegate( 
2780     /* [in] */ IWebDownloadDelegate* d)
2781 {
2782     m_downloadDelegate = d;
2783     return S_OK;
2784 }
2785
2786 HRESULT STDMETHODCALLTYPE WebView::downloadDelegate( 
2787     /* [out][retval] */ IWebDownloadDelegate** d)
2788 {
2789     if (!m_downloadDelegate)
2790         return E_FAIL;
2791
2792     return m_downloadDelegate.copyRefTo(d);
2793 }
2794
2795 HRESULT STDMETHODCALLTYPE WebView::setFrameLoadDelegate( 
2796     /* [in] */ IWebFrameLoadDelegate* d)
2797 {
2798     m_frameLoadDelegate = d;
2799     return S_OK;
2800 }
2801
2802 HRESULT STDMETHODCALLTYPE WebView::frameLoadDelegate( 
2803     /* [out][retval] */ IWebFrameLoadDelegate** d)
2804 {
2805     if (!m_frameLoadDelegate)
2806         return E_FAIL;
2807
2808     return m_frameLoadDelegate.copyRefTo(d);
2809 }
2810
2811 HRESULT STDMETHODCALLTYPE WebView::setPolicyDelegate( 
2812     /* [in] */ IWebPolicyDelegate* d)
2813 {
2814     m_policyDelegate = d;
2815     return S_OK;
2816 }
2817
2818 HRESULT STDMETHODCALLTYPE WebView::policyDelegate( 
2819     /* [out][retval] */ IWebPolicyDelegate** d)
2820 {
2821     if (!m_policyDelegate)
2822         return E_FAIL;
2823     return m_policyDelegate.copyRefTo(d);
2824 }
2825
2826 HRESULT STDMETHODCALLTYPE WebView::mainFrame( 
2827     /* [out][retval] */ IWebFrame** frame)
2828 {
2829     if (!frame) {
2830         ASSERT_NOT_REACHED();
2831         return E_POINTER;
2832     }
2833
2834     *frame = m_mainFrame;
2835     if (!m_mainFrame)
2836         return E_FAIL;
2837
2838     m_mainFrame->AddRef();
2839     return S_OK;
2840 }
2841
2842 HRESULT STDMETHODCALLTYPE WebView::focusedFrame( 
2843     /* [out][retval] */ IWebFrame** frame)
2844 {
2845     if (!frame) {
2846         ASSERT_NOT_REACHED();
2847         return E_POINTER;
2848     }
2849
2850     *frame = 0;
2851     Frame* f = m_page->focusController()->focusedFrame();
2852     if (!f)
2853         return E_FAIL;
2854
2855     WebFrame* webFrame = kit(f);
2856     if (!webFrame)
2857         return E_FAIL;
2858
2859     return webFrame->QueryInterface(IID_IWebFrame, (void**) frame);
2860 }
2861
2862 HRESULT STDMETHODCALLTYPE WebView::backForwardList( 
2863     /* [out][retval] */ IWebBackForwardList** list)
2864 {
2865     if (!m_useBackForwardList)
2866         return E_FAIL;
2867  
2868     *list = WebBackForwardList::createInstance(static_cast<WebCore::BackForwardListImpl*>(m_page->backForwardList()));
2869
2870     return S_OK;
2871 }
2872
2873 HRESULT STDMETHODCALLTYPE WebView::setMaintainsBackForwardList( 
2874     /* [in] */ BOOL flag)
2875 {
2876     m_useBackForwardList = !!flag;
2877     return S_OK;
2878 }
2879
2880 HRESULT STDMETHODCALLTYPE WebView::goBack( 
2881     /* [retval][out] */ BOOL* succeeded)
2882 {
2883     *succeeded = m_page->goBack();
2884     return S_OK;
2885 }
2886
2887 HRESULT STDMETHODCALLTYPE WebView::goForward( 
2888     /* [retval][out] */ BOOL* succeeded)
2889 {
2890     *succeeded = m_page->goForward();
2891     return S_OK;
2892 }
2893
2894 HRESULT STDMETHODCALLTYPE WebView::goToBackForwardItem( 
2895     /* [in] */ IWebHistoryItem* item,
2896     /* [retval][out] */ BOOL* succeeded)
2897 {
2898     *succeeded = FALSE;
2899
2900     COMPtr<WebHistoryItem> webHistoryItem;
2901     HRESULT hr = item->QueryInterface(&webHistoryItem);
2902     if (FAILED(hr))
2903         return hr;
2904
2905     m_page->goToItem(webHistoryItem->historyItem(), FrameLoadTypeIndexedBackForward);
2906     *succeeded = TRUE;
2907
2908     return S_OK;
2909 }
2910
2911 HRESULT STDMETHODCALLTYPE WebView::setTextSizeMultiplier( 
2912     /* [in] */ float multiplier)
2913 {
2914     if (!m_mainFrame)
2915         return E_FAIL;
2916     setZoomMultiplier(multiplier, true);
2917     return S_OK;
2918 }
2919
2920 HRESULT STDMETHODCALLTYPE WebView::setPageSizeMultiplier( 
2921     /* [in] */ float multiplier)
2922 {
2923     if (!m_mainFrame)
2924         return E_FAIL;
2925     setZoomMultiplier(multiplier, false);
2926     return S_OK;
2927 }
2928
2929 void WebView::setZoomMultiplier(float multiplier, bool isTextOnly)
2930 {
2931     m_zoomMultiplier = multiplier;
2932     m_zoomsTextOnly = isTextOnly;
2933
2934     if (Frame* coreFrame = core(m_mainFrame)) {
2935         if (m_zoomsTextOnly)
2936             coreFrame->setPageAndTextZoomFactors(1, multiplier);
2937         else
2938             coreFrame->setPageAndTextZoomFactors(multiplier, 1);
2939     }
2940 }
2941
2942 HRESULT STDMETHODCALLTYPE WebView::textSizeMultiplier( 
2943     /* [retval][out] */ float* multiplier)
2944 {
2945     *multiplier = zoomMultiplier(true);
2946     return S_OK;
2947 }
2948
2949 HRESULT STDMETHODCALLTYPE WebView::pageSizeMultiplier( 
2950     /* [retval][out] */ float* multiplier)
2951 {
2952     *multiplier = zoomMultiplier(false);
2953     return S_OK;
2954 }
2955
2956 float WebView::zoomMultiplier(bool isTextOnly)
2957 {
2958     if (isTextOnly != m_zoomsTextOnly)
2959         return 1.0f;
2960     return m_zoomMultiplier;
2961 }
2962
2963 HRESULT STDMETHODCALLTYPE WebView::setApplicationNameForUserAgent( 
2964     /* [in] */ BSTR applicationName)
2965 {
2966     m_applicationName = String(applicationName, SysStringLen(applicationName));
2967     m_userAgentStandard = String();
2968     return S_OK;
2969 }
2970
2971 HRESULT STDMETHODCALLTYPE WebView::applicationNameForUserAgent( 
2972     /* [retval][out] */ BSTR* applicationName)
2973 {
2974     *applicationName = SysAllocStringLen(m_applicationName.characters(), m_applicationName.length());
2975     if (!*applicationName && m_applicationName.length())
2976         return E_OUTOFMEMORY;
2977     return S_OK;
2978 }
2979
2980 HRESULT STDMETHODCALLTYPE WebView::setCustomUserAgent( 
2981     /* [in] */ BSTR userAgentString)
2982 {
2983     m_userAgentOverridden = userAgentString;
2984     m_userAgentCustom = String(userAgentString, SysStringLen(userAgentString));
2985     return S_OK;
2986 }
2987
2988 HRESULT STDMETHODCALLTYPE WebView::customUserAgent( 
2989     /* [retval][out] */ BSTR* userAgentString)
2990 {
2991     *userAgentString = 0;
2992     if (!m_userAgentOverridden)
2993         return S_OK;
2994     *userAgentString = SysAllocStringLen(m_userAgentCustom.characters(), m_userAgentCustom.length());
2995     if (!*userAgentString && m_userAgentCustom.length())
2996         return E_OUTOFMEMORY;
2997     return S_OK;
2998 }
2999
3000 HRESULT STDMETHODCALLTYPE WebView::userAgentForURL( 
3001     /* [in] */ BSTR url,
3002     /* [retval][out] */ BSTR* userAgent)
3003 {
3004     String userAgentString = userAgentForKURL(MarshallingHelpers::BSTRToKURL(url));
3005     *userAgent = SysAllocStringLen(userAgentString.characters(), userAgentString.length());
3006     if (!*userAgent && userAgentString.length())
3007         return E_OUTOFMEMORY;
3008     return S_OK;
3009 }
3010
3011 HRESULT STDMETHODCALLTYPE WebView::supportsTextEncoding( 
3012     /* [retval][out] */ BOOL* supports)
3013 {
3014     *supports = TRUE;
3015     return S_OK;
3016 }
3017
3018 HRESULT STDMETHODCALLTYPE WebView::setCustomTextEncodingName( 
3019     /* [in] */ BSTR encodingName)
3020 {
3021     if (!m_mainFrame)
3022         return E_FAIL;
3023
3024     HRESULT hr;
3025     BSTR oldEncoding;
3026     hr = customTextEncodingName(&oldEncoding);
3027     if (FAILED(hr))
3028         return hr;
3029
3030     if (oldEncoding != encodingName && (!oldEncoding || !encodingName || _tcscmp(oldEncoding, encodingName))) {
3031         if (Frame* coreFrame = core(m_mainFrame))
3032             coreFrame->loader()->reloadWithOverrideEncoding(String(encodingName, SysStringLen(encodingName)));
3033     }
3034
3035     return S_OK;
3036 }
3037
3038 HRESULT STDMETHODCALLTYPE WebView::customTextEncodingName( 
3039     /* [retval][out] */ BSTR* encodingName)
3040 {
3041     HRESULT hr = S_OK;
3042     COMPtr<IWebDataSource> dataSource;
3043     COMPtr<WebDataSource> dataSourceImpl;
3044     *encodingName = 0;
3045
3046     if (!m_mainFrame)
3047         return E_FAIL;
3048
3049     if (FAILED(m_mainFrame->provisionalDataSource(&dataSource)) || !dataSource) {
3050         hr = m_mainFrame->dataSource(&dataSource);
3051         if (FAILED(hr) || !dataSource)
3052             return hr;
3053     }
3054
3055     hr = dataSource->QueryInterface(IID_WebDataSource, (void**)&dataSourceImpl);
3056     if (FAILED(hr))
3057         return hr;
3058
3059     BString str = dataSourceImpl->documentLoader()->overrideEncoding();
3060     if (FAILED(hr))
3061         return hr;
3062
3063     if (!*encodingName)
3064         *encodingName = SysAllocStringLen(m_overrideEncoding.characters(), m_overrideEncoding.length());
3065
3066     if (!*encodingName && m_overrideEncoding.length())
3067         return E_OUTOFMEMORY;
3068
3069     return S_OK;
3070 }
3071
3072 HRESULT STDMETHODCALLTYPE WebView::setMediaStyle( 
3073     /* [in] */ BSTR /*media*/)
3074 {
3075     ASSERT_NOT_REACHED();
3076     return E_NOTIMPL;
3077 }
3078
3079 HRESULT STDMETHODCALLTYPE WebView::mediaStyle( 
3080     /* [retval][out] */ BSTR* /*media*/)
3081 {
3082     ASSERT_NOT_REACHED();
3083     return E_NOTIMPL;
3084 }
3085
3086 HRESULT STDMETHODCALLTYPE WebView::stringByEvaluatingJavaScriptFromString( 
3087     /* [in] */ BSTR script, // assumes input does not have "JavaScript" at the begining.
3088     /* [retval][out] */ BSTR* result)
3089 {
3090     if (!result) {
3091         ASSERT_NOT_REACHED();
3092         return E_POINTER;
3093     }
3094
3095     *result = 0;
3096
3097     Frame* coreFrame = core(m_mainFrame);
3098     if (!coreFrame)
3099         return E_FAIL;
3100
3101     JSC::JSValue scriptExecutionResult = coreFrame->script()->executeScript(WTF::String(script), true).jsValue();
3102     if (!scriptExecutionResult)
3103         return E_FAIL;
3104     else if (scriptExecutionResult.isString()) {
3105         JSLock lock(JSC::SilenceAssertionsOnly);
3106         JSC::ExecState* exec = coreFrame->script()->globalObject(mainThreadNormalWorld())->globalExec();
3107         *result = BString(ustringToString(scriptExecutionResult.getString(exec)));
3108     }
3109
3110     return S_OK;
3111 }
3112
3113 HRESULT STDMETHODCALLTYPE WebView::windowScriptObject( 
3114     /* [retval][out] */ IWebScriptObject** /*webScriptObject*/)
3115 {
3116     ASSERT_NOT_REACHED();
3117     return E_NOTIMPL;
3118 }
3119
3120 HRESULT STDMETHODCALLTYPE WebView::setPreferences( 
3121     /* [in] */ IWebPreferences* prefs)
3122 {
3123     if (!prefs)
3124         prefs = WebPreferences::sharedStandardPreferences();
3125
3126     if (m_preferences == prefs)
3127         return S_OK;
3128
3129     COMPtr<WebPreferences> webPrefs(Query, prefs);
3130     if (!webPrefs)
3131         return E_NOINTERFACE;
3132     webPrefs->willAddToWebView();
3133
3134     COMPtr<WebPreferences> oldPrefs = m_preferences;
3135
3136     IWebNotificationCenter* nc = WebNotificationCenter::defaultCenterInternal();
3137     nc->removeObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
3138
3139     BSTR identifier = 0;
3140     oldPrefs->identifier(&identifier);
3141     oldPrefs->didRemoveFromWebView();
3142     oldPrefs = 0; // Make sure we release the reference, since WebPreferences::removeReferenceForIdentifier will check for last reference to WebPreferences
3143
3144     m_preferences = webPrefs;
3145
3146     if (identifier) {
3147         WebPreferences::removeReferenceForIdentifier(identifier);
3148         SysFreeString(identifier);
3149     }
3150
3151     nc->addObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
3152
3153     m_preferences->postPreferencesChangesNotification();
3154
3155     return S_OK;
3156 }
3157
3158 HRESULT STDMETHODCALLTYPE WebView::preferences( 
3159     /* [retval][out] */ IWebPreferences** prefs)
3160 {
3161     if (!prefs)
3162         return E_POINTER;
3163     *prefs = m_preferences.get();
3164     if (m_preferences)
3165         m_preferences->AddRef();
3166     return S_OK;
3167 }
3168
3169 HRESULT STDMETHODCALLTYPE WebView::setPreferencesIdentifier( 
3170     /* [in] */ BSTR /*anIdentifier*/)
3171 {
3172     ASSERT_NOT_REACHED();
3173     return E_NOTIMPL;
3174 }
3175
3176 HRESULT STDMETHODCALLTYPE WebView::preferencesIdentifier( 
3177     /* [retval][out] */ BSTR* /*anIdentifier*/)
3178 {
3179     ASSERT_NOT_REACHED();
3180     return E_NOTIMPL;
3181 }
3182
3183 static void systemParameterChanged(WPARAM parameter)
3184 {
3185 #if PLATFORM(CG)
3186     if (parameter == SPI_SETFONTSMOOTHING || parameter == SPI_SETFONTSMOOTHINGTYPE || parameter == SPI_SETFONTSMOOTHINGCONTRAST || parameter == SPI_SETFONTSMOOTHINGORIENTATION)
3187         wkSystemFontSmoothingChanged();
3188 #endif
3189 }
3190
3191 void WebView::windowReceivedMessage(HWND, UINT message, WPARAM wParam, LPARAM)
3192 {
3193     switch (message) {
3194     case WM_NCACTIVATE:
3195         updateActiveStateSoon();
3196         if (!wParam)
3197             deleteBackingStoreSoon();
3198         break;
3199     case WM_SETTINGCHANGE:
3200         systemParameterChanged(wParam);
3201         break;
3202     }
3203 }
3204
3205 void WebView::updateActiveStateSoon() const
3206 {
3207     // This function is called while processing the WM_NCACTIVATE message.
3208     // While processing WM_NCACTIVATE when we are being deactivated, GetActiveWindow() will
3209     // still return our window. If we were to call updateActiveState() in that case, we would
3210     // wrongly think that we are still the active window. To work around this, we update our
3211     // active state after a 0-delay timer fires, at which point GetActiveWindow() will return
3212     // the newly-activated window.
3213
3214     SetTimer(m_viewWindow, UpdateActiveStateTimer, 0, 0);
3215 }
3216
3217 void WebView::deleteBackingStoreSoon()
3218 {
3219     if (pendingDeleteBackingStoreSet.size() > 2) {
3220         Vector<WebView*> views;
3221         HashSet<WebView*>::iterator end = pendingDeleteBackingStoreSet.end();
3222         for (HashSet<WebView*>::iterator it = pendingDeleteBackingStoreSet.begin(); it != end; ++it)
3223             views.append(*it);
3224         for (int i = 0; i < views.size(); ++i)
3225             views[i]->deleteBackingStore();
3226         ASSERT(pendingDeleteBackingStoreSet.isEmpty());
3227     }
3228
3229     pendingDeleteBackingStoreSet.add(this);
3230     m_deleteBackingStoreTimerActive = true;
3231     SetTimer(m_viewWindow, DeleteBackingStoreTimer, delayBeforeDeletingBackingStoreMsec, 0);
3232 }
3233
3234 void WebView::cancelDeleteBackingStoreSoon()
3235 {
3236     if (!m_deleteBackingStoreTimerActive)
3237         return;
3238     pendingDeleteBackingStoreSet.remove(this);
3239     m_deleteBackingStoreTimerActive = false;
3240     KillTimer(m_viewWindow, DeleteBackingStoreTimer);
3241 }
3242
3243 HRESULT STDMETHODCALLTYPE WebView::setHostWindow( 
3244     /* [in] */ OLE_HANDLE oleWindow)
3245 {
3246     HWND window = (HWND)(ULONG64)oleWindow;
3247     if (m_viewWindow) {
3248         if (window)
3249             SetParent(m_viewWindow, window);
3250         else if (!isBeingDestroyed()) {
3251             // Turn the WebView into a message-only window so it will no longer be a child of the
3252             // old host window and will be hidden from screen. We only do this when
3253             // isBeingDestroyed() is false because doing this while handling WM_DESTROY can leave
3254             // m_viewWindow in a weird state (see <http://webkit.org/b/29337>).
3255             SetParent(m_viewWindow, HWND_MESSAGE);
3256         }
3257     }
3258
3259     m_hostWindow = window;
3260
3261     windowAncestryDidChange();
3262
3263     return S_OK;
3264 }
3265
3266 HRESULT STDMETHODCALLTYPE WebView::hostWindow( 
3267     /* [retval][out] */ OLE_HANDLE* window)
3268 {
3269     *window = (OLE_HANDLE)(ULONG64)m_hostWindow;
3270     return S_OK;
3271 }
3272
3273
3274 static Frame *incrementFrame(Frame *curr, bool forward, bool wrapFlag)
3275 {
3276     return forward
3277         ? curr->tree()->traverseNextWithWrap(wrapFlag)
3278         : curr->tree()->traversePreviousWithWrap(wrapFlag);
3279 }
3280
3281 HRESULT STDMETHODCALLTYPE WebView::searchFor( 
3282     /* [in] */ BSTR str,
3283     /* [in] */ BOOL forward,
3284     /* [in] */ BOOL caseFlag,
3285     /* [in] */ BOOL wrapFlag,
3286     /* [retval][out] */ BOOL* found)
3287 {
3288     if (!found)
3289         return E_INVALIDARG;
3290     
3291     if (!m_page || !m_page->mainFrame())
3292         return E_UNEXPECTED;
3293
3294     if (!str || !SysStringLen(str))
3295         return E_INVALIDARG;
3296
3297     *found = m_page->findString(String(str, SysStringLen(str)), caseFlag ? TextCaseSensitive : TextCaseInsensitive, forward ? FindDirectionForward : FindDirectionBackward, wrapFlag);
3298     return S_OK;
3299 }
3300
3301 bool WebView::active()
3302 {
3303     HWND activeWindow = GetActiveWindow();
3304     return (activeWindow && m_topLevelParent == findTopLevelParent(activeWindow));
3305 }
3306
3307 void WebView::updateActiveState()
3308 {
3309     m_page->focusController()->setActive(active());
3310 }
3311
3312 HRESULT STDMETHODCALLTYPE WebView::updateFocusedAndActiveState()
3313 {
3314     updateActiveState();
3315
3316     bool active = m_page->focusController()->isActive();
3317     Frame* mainFrame = m_page->mainFrame();
3318     Frame* focusedFrame = m_page->focusController()->focusedOrMainFrame();
3319     mainFrame->selection()->setFocused(active && mainFrame == focusedFrame);
3320
3321     return S_OK;
3322 }
3323
3324 HRESULT STDMETHODCALLTYPE WebView::executeCoreCommandByName(BSTR bName, BSTR bValue)
3325 {
3326     String name(bName, SysStringLen(bName));
3327     String value(bValue, SysStringLen(bValue));
3328
3329     m_page->focusController()->focusedOrMainFrame()->editor()->command(name).execute(value);
3330
3331     return S_OK;
3332 }
3333
3334 HRESULT STDMETHODCALLTYPE WebView::clearMainFrameName()
3335 {
3336     m_page->mainFrame()->tree()->clearName();
3337
3338     return S_OK;
3339 }
3340
3341 HRESULT STDMETHODCALLTYPE WebView::markAllMatchesForText(
3342     BSTR str, BOOL caseSensitive, BOOL highlight, UINT limit, UINT* matches)
3343 {
3344     if (!matches)
3345         return E_INVALIDARG;
3346
3347     if (!m_page || !m_page->mainFrame())
3348         return E_UNEXPECTED;
3349
3350     if (!str || !SysStringLen(str))
3351         return E_INVALIDARG;
3352
3353     *matches = m_page->markAllMatchesForText(String(str, SysStringLen(str)), caseSensitive ? TextCaseSensitive : TextCaseInsensitive, highlight, limit);
3354     return S_OK;
3355 }
3356
3357 HRESULT STDMETHODCALLTYPE WebView::unmarkAllTextMatches()
3358 {
3359     if (!m_page || !m_page->mainFrame())
3360         return E_UNEXPECTED;
3361
3362     m_page->unmarkAllTextMatches();
3363     return S_OK;
3364 }
3365
3366 HRESULT STDMETHODCALLTYPE WebView::rectsForTextMatches(
3367     IEnumTextMatches** pmatches)
3368 {
3369     Vector<IntRect> allRects;
3370     WebCore::Frame* frame = m_page->mainFrame();
3371     do {
3372         if (Document* document = frame->document()) {
3373             IntRect visibleRect = frame->view()->visibleContentRect();
3374             Vector<IntRect> frameRects = document->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
3375             IntPoint frameOffset(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
3376             frameOffset = frame->view()->convertToContainingWindow(frameOffset);
3377
3378             Vector<IntRect>::iterator end = frameRects.end();
3379             for (Vector<IntRect>::iterator it = frameRects.begin(); it < end; it++) {
3380                 it->intersect(visibleRect);
3381                 it->move(frameOffset.x(), frameOffset.y());
3382                 allRects.append(*it);
3383             }
3384         }
3385         frame = incrementFrame(frame, true, false);
3386     } while (frame);
3387
3388     return createMatchEnumerator(&allRects, pmatches);
3389 }
3390
3391 HRESULT STDMETHODCALLTYPE WebView::generateSelectionImage(BOOL forceWhiteText, OLE_HANDLE* hBitmap)
3392 {
3393     *hBitmap = 0;
3394
3395     WebCore::Frame* frame = m_page->focusController()->focusedOrMainFrame();
3396
3397     if (frame) {
3398         HBITMAP bitmap = imageFromSelection(frame, forceWhiteText ? TRUE : FALSE);
3399         *hBitmap = (OLE_HANDLE)(ULONG64)bitmap;
3400     }
3401
3402     return S_OK;
3403 }
3404
3405 HRESULT STDMETHODCALLTYPE WebView::selectionRect(RECT* rc)
3406 {
3407     WebCore::Frame* frame = m_page->focusController()->focusedOrMainFrame();
3408
3409     if (frame) {
3410         IntRect ir = enclosingIntRect(frame->selection()->bounds());
3411         ir = frame->view()->convertToContainingWindow(ir);
3412         ir.move(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
3413         rc->left = ir.x();
3414         rc->top = ir.y();
3415         rc->bottom = rc->top + ir.height();
3416         rc->right = rc->left + ir.width();
3417     }
3418
3419     return S_OK;
3420 }
3421
3422 HRESULT STDMETHODCALLTYPE WebView::registerViewClass( 
3423     /* [in] */ IWebDocumentView* /*view*/,
3424     /* [in] */ IWebDocumentRepresentation* /*representation*/,
3425     /* [in] */ BSTR /*forMIMEType*/)
3426 {
3427     ASSERT_NOT_REACHED();
3428     return E_NOTIMPL;
3429 }
3430
3431 HRESULT STDMETHODCALLTYPE WebView::setGroupName( 
3432         /* [in] */ BSTR groupName)
3433 {
3434     if (!m_page)
3435         return S_OK;
3436     m_page->setGroupName(String(groupName, SysStringLen(groupName)));
3437     return S_OK;
3438 }
3439     
3440 HRESULT STDMETHODCALLTYPE WebView::groupName( 
3441         /* [retval][out] */ BSTR* groupName)
3442 {
3443     *groupName = 0;
3444     if (!m_page)
3445         return S_OK;
3446     String groupNameString = m_page->groupName();
3447     *groupName = SysAllocStringLen(groupNameString.characters(), groupNameString.length());
3448     if (!*groupName && groupNameString.length())
3449         return E_OUTOFMEMORY;
3450     return S_OK;
3451 }
3452     
3453 HRESULT STDMETHODCALLTYPE WebView::estimatedProgress( 
3454         /* [retval][out] */ double* estimatedProgress)
3455 {
3456     *estimatedProgress = m_page->progress()->estimatedProgress();
3457     return S_OK;
3458 }
3459     
3460 HRESULT STDMETHODCALLTYPE WebView::isLoading( 
3461         /* [retval][out] */ BOOL* isLoading)
3462 {
3463     COMPtr<IWebDataSource> dataSource;
3464     COMPtr<IWebDataSource> provisionalDataSource;
3465
3466     if (!isLoading)
3467         return E_POINTER;
3468
3469     *isLoading = FALSE;
3470
3471     if (!m_mainFrame)
3472         return E_FAIL;
3473
3474     if (SUCCEEDED(m_mainFrame->dataSource(&dataSource)))
3475         dataSource->isLoading(isLoading);
3476
3477     if (*isLoading)
3478         return S_OK;
3479
3480     if (SUCCEEDED(m_mainFrame->provisionalDataSource(&provisionalDataSource)))
3481         provisionalDataSource->isLoading(isLoading);
3482     return S_OK;
3483 }
3484     
3485 HRESULT STDMETHODCALLTYPE WebView::elementAtPoint( 
3486         /* [in] */ LPPOINT point,
3487         /* [retval][out] */ IPropertyBag** elementDictionary)
3488 {
3489     if (!elementDictionary) {
3490         ASSERT_NOT_REACHED();
3491         return E_POINTER;
3492     }
3493
3494     *elementDictionary = 0;
3495
3496     Frame* frame = core(m_mainFrame);
3497     if (!frame)
3498         return E_FAIL;
3499
3500     IntPoint webCorePoint = IntPoint(point->x, point->y);
3501     HitTestResult result = HitTestResult(webCorePoint);
3502     if (frame->contentRenderer())
3503         result = frame->eventHandler()->hitTestResultAtPoint(webCorePoint, false);
3504     *elementDictionary = WebElementPropertyBag::createInstance(result);
3505     return S_OK;
3506 }
3507     
3508 HRESULT STDMETHODCALLTYPE WebView::pasteboardTypesForSelection( 
3509     /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
3510 {
3511     ASSERT_NOT_REACHED();
3512     return E_NOTIMPL;
3513 }
3514     
3515 HRESULT STDMETHODCALLTYPE WebView::writeSelectionWithPasteboardTypes( 
3516         /* [size_is][in] */ BSTR* /*types*/,
3517         /* [in] */ int /*cTypes*/,
3518         /* [in] */ IDataObject* /*pasteboard*/)
3519 {
3520     ASSERT_NOT_REACHED();
3521     return E_NOTIMPL;
3522 }
3523     
3524 HRESULT STDMETHODCALLTYPE WebView::pasteboardTypesForElement( 
3525     /* [in] */ IPropertyBag* /*elementDictionary*/,
3526     /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
3527 {
3528     ASSERT_NOT_REACHED();
3529     return E_NOTIMPL;
3530 }
3531     
3532 HRESULT STDMETHODCALLTYPE WebView::writeElement( 
3533         /* [in] */ IPropertyBag* /*elementDictionary*/,
3534         /* [size_is][in] */ BSTR* /*withPasteboardTypes*/,
3535         /* [in] */ int /*cWithPasteboardTypes*/,
3536         /* [in] */ IDataObject* /*pasteboard*/)
3537 {
3538     ASSERT_NOT_REACHED();
3539     return E_NOTIMPL;
3540 }
3541     
3542 HRESULT STDMETHODCALLTYPE WebView::selectedText(
3543         /* [out, retval] */ BSTR* text)
3544 {
3545     if (!text) {
3546         ASSERT_NOT_REACHED();
3547         return E_POINTER;
3548     }
3549
3550     *text = 0;
3551
3552     Frame* focusedFrame = (m_page && m_page->focusController()) ? m_page->focusController()->focusedOrMainFrame() : 0;
3553     if (!focusedFrame)
3554         return E_FAIL;
3555
3556     String frameSelectedText = focusedFrame->editor()->selectedText();
3557     *text = SysAllocStringLen(frameSelectedText.characters(), frameSelectedText.length());
3558     if (!*text && frameSelectedText.length())
3559         return E_OUTOFMEMORY;
3560     return S_OK;
3561 }
3562
3563 HRESULT STDMETHODCALLTYPE WebView::centerSelectionInVisibleArea(
3564         /* [in] */ IUnknown* /* sender */)
3565 {
3566     Frame* coreFrame = core(m_mainFrame);
3567     if (!coreFrame)
3568         return E_FAIL;
3569
3570     coreFrame->selection()->revealSelection(ScrollAlignment::alignCenterAlways);
3571     return S_OK;
3572 }
3573
3574
3575 HRESULT STDMETHODCALLTYPE WebView::moveDragCaretToPoint( 
3576         /* [in] */ LPPOINT /*point*/)
3577 {
3578     ASSERT_NOT_REACHED();
3579     return E_NOTIMPL;
3580 }
3581     
3582 HRESULT STDMETHODCALLTYPE WebView::removeDragCaret( void)
3583 {
3584     ASSERT_NOT_REACHED();
3585     return E_NOTIMPL;
3586 }
3587     
3588 HRESULT STDMETHODCALLTYPE WebView::setDrawsBackground( 
3589         /* [in] */ BOOL /*drawsBackground*/)
3590 {
3591     ASSERT_NOT_REACHED();
3592     return E_NOTIMPL;
3593 }
3594     
3595 HRESULT STDMETHODCALLTYPE WebView::drawsBackground( 
3596         /* [retval][out] */ BOOL* /*drawsBackground*/)
3597 {
3598     ASSERT_NOT_REACHED();
3599     return E_NOTIMPL;
3600 }
3601     
3602 HRESULT STDMETHODCALLTYPE WebView::setMainFrameURL( 
3603         /* [in] */ BSTR /*urlString*/)
3604 {
3605     ASSERT_NOT_REACHED();
3606     return E_NOTIMPL;
3607 }
3608     
3609 HRESULT STDMETHODCALLTYPE WebView::mainFrameURL( 
3610         /* [retval][out] */ BSTR* urlString)
3611 {
3612     if (!urlString)
3613         return E_POINTER;
3614
3615     if (!m_mainFrame)
3616         return E_FAIL;
3617
3618     COMPtr<IWebDataSource> dataSource;
3619
3620     if&nb