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