2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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"
45 #include "WebGeolocationClient.h"
46 #include "WebGeolocationPosition.h"
47 #include "WebIconDatabase.h"
48 #include "WebInspector.h"
49 #include "WebInspectorClient.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>
135 #if ENABLE(CLIENT_BASED_GEOLOCATION)
136 #include <WebCore/GeolocationController.h>
137 #include <WebCore/GeolocationError.h>
141 #include <CoreGraphics/CGContext.h>
145 #include <CoreFoundation/CoreFoundation.h>
149 #include <CFNetwork/CFURLCachePriv.h>
150 #include <CFNetwork/CFURLProtocolPriv.h>
151 #include <WebCore/CookieStorageCFNet.h>
152 #include <WebKitSystemInterface/WebKitSystemInterface.h>
160 #include <windowsx.h>
161 #include <wtf/HashSet.h>
162 #include <wtf/text/CString.h>
163 #include <wtf/text/StringConcatenate.h>
165 // Soft link functions for gestures and panning feedback
166 SOFT_LINK_LIBRARY(USER32);
167 SOFT_LINK_OPTIONAL(USER32, GetGestureInfo, BOOL, WINAPI, (HGESTUREINFO, PGESTUREINFO));
168 SOFT_LINK_OPTIONAL(USER32, SetGestureConfig, BOOL, WINAPI, (HWND, DWORD, UINT, PGESTURECONFIG, UINT));
169 SOFT_LINK_OPTIONAL(USER32, CloseGestureInfoHandle, BOOL, WINAPI, (HGESTUREINFO));
170 SOFT_LINK_LIBRARY(Uxtheme);
171 SOFT_LINK_OPTIONAL(Uxtheme, BeginPanningFeedback, BOOL, WINAPI, (HWND));
172 SOFT_LINK_OPTIONAL(Uxtheme, EndPanningFeedback, BOOL, WINAPI, (HWND, BOOL));
173 SOFT_LINK_OPTIONAL(Uxtheme, UpdatePanningFeedback, BOOL, WINAPI, (HWND, LONG, LONG, BOOL));
175 using namespace WebCore;
179 static HMODULE accessibilityLib;
180 static HashSet<WebView*> pendingDeleteBackingStoreSet;
182 static String osVersion();
183 static String webKitVersion();
185 WebView* kit(Page* page)
187 return page ? static_cast<WebChromeClient*>(page->chrome()->client())->webView() : 0;
190 class PreferencesChangedOrRemovedObserver : public IWebNotificationObserver {
192 static PreferencesChangedOrRemovedObserver* sharedInstance();
195 PreferencesChangedOrRemovedObserver() {}
196 ~PreferencesChangedOrRemovedObserver() {}
198 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void**) { return E_FAIL; }
199 virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 0; }
200 virtual ULONG STDMETHODCALLTYPE Release(void) { return 0; }
203 // IWebNotificationObserver
204 virtual HRESULT STDMETHODCALLTYPE onNotify(
205 /* [in] */ IWebNotification* notification);
208 HRESULT notifyPreferencesChanged(WebCacheModel);
209 HRESULT notifyPreferencesRemoved(WebCacheModel);
212 PreferencesChangedOrRemovedObserver* PreferencesChangedOrRemovedObserver::sharedInstance()
214 static PreferencesChangedOrRemovedObserver* shared = new PreferencesChangedOrRemovedObserver;
218 HRESULT PreferencesChangedOrRemovedObserver::onNotify(IWebNotification* notification)
222 COMPtr<IUnknown> unkPrefs;
223 hr = notification->getObject(&unkPrefs);
227 COMPtr<IWebPreferences> preferences(Query, unkPrefs);
229 return E_NOINTERFACE;
231 WebCacheModel cacheModel;
232 hr = preferences->cacheModel(&cacheModel);
237 hr = notification->name(&nameBSTR);
241 name.adoptBSTR(nameBSTR);
243 if (wcscmp(name, WebPreferences::webPreferencesChangedNotification()) == 0)
244 return notifyPreferencesChanged(cacheModel);
246 if (wcscmp(name, WebPreferences::webPreferencesRemovedNotification()) == 0)
247 return notifyPreferencesRemoved(cacheModel);
249 ASSERT_NOT_REACHED();
253 HRESULT PreferencesChangedOrRemovedObserver::notifyPreferencesChanged(WebCacheModel cacheModel)
257 if (!WebView::didSetCacheModel() || cacheModel > WebView::cacheModel())
258 WebView::setCacheModel(cacheModel);
259 else if (cacheModel < WebView::cacheModel()) {
260 WebCacheModel sharedPreferencesCacheModel;
261 hr = WebPreferences::sharedStandardPreferences()->cacheModel(&sharedPreferencesCacheModel);
264 WebView::setCacheModel(max(sharedPreferencesCacheModel, WebView::maxCacheModelInAnyInstance()));
270 HRESULT PreferencesChangedOrRemovedObserver::notifyPreferencesRemoved(WebCacheModel cacheModel)
274 if (cacheModel == WebView::cacheModel()) {
275 WebCacheModel sharedPreferencesCacheModel;
276 hr = WebPreferences::sharedStandardPreferences()->cacheModel(&sharedPreferencesCacheModel);
279 WebView::setCacheModel(max(sharedPreferencesCacheModel, WebView::maxCacheModelInAnyInstance()));
286 const LPCWSTR kWebViewWindowClassName = L"WebViewWindowClass";
288 const int WM_XP_THEMECHANGED = 0x031A;
289 const int WM_VISTA_MOUSEHWHEEL = 0x020E;
291 static const int maxToolTipWidth = 250;
293 static const int delayBeforeDeletingBackingStoreMsec = 5000;
295 static ATOM registerWebView();
297 static void initializeStaticObservers();
299 static HRESULT updateSharedSettingsFromPreferencesIfNeeded(IWebPreferences*);
301 HRESULT createMatchEnumerator(Vector<IntRect>* rects, IEnumTextMatches** matches);
303 static bool continuousSpellCheckingEnabled;
304 static bool grammarCheckingEnabled;
306 static bool s_didSetCacheModel;
307 static WebCacheModel s_cacheModel = WebCacheModelDocumentViewer;
310 UpdateActiveStateTimer = 1,
311 DeleteBackingStoreTimer = 2,
314 // WebView ----------------------------------------------------------------
316 bool WebView::s_allowSiteSpecificHacks = false;
321 , m_deletionHasBegun(false)
327 , m_hasCustomDropTarget(false)
328 , m_useBackForwardList(true)
329 , m_userAgentOverridden(false)
330 , m_zoomMultiplier(1.0f)
331 , m_zoomsTextOnly(false)
332 , m_mouseActivated(false)
334 , m_currentCharacterCode(0)
335 , m_isBeingDestroyed(false)
337 , m_hasSpellCheckerDocumentTag(false)
338 , m_smartInsertDeleteEnabled(false)
340 , m_inIMEComposition(0)
342 , m_closeWindowTimer(0)
343 , m_topLevelParent(0)
344 , m_deleteBackingStoreTimerActive(false)
345 , m_transparent(false)
346 , m_selectTrailingWhitespaceEnabled(false)
351 #if USE(ACCELERATED_COMPOSITING)
352 , m_isAcceleratedCompositing(false)
354 , m_nextDisplayIsSynchronous(false)
357 JSC::initializeThreading();
358 WTF::initializeMainThread();
360 m_backingStoreSize.cx = m_backingStoreSize.cy = 0;
362 CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, IID_IDropTargetHelper,(void**)&m_dropTargetHelper);
364 initializeStaticObservers();
366 WebPreferences* sharedPreferences = WebPreferences::sharedStandardPreferences();
368 if (SUCCEEDED(sharedPreferences->continuousSpellCheckingEnabled(&enabled)))
369 continuousSpellCheckingEnabled = !!enabled;
370 if (SUCCEEDED(sharedPreferences->grammarCheckingEnabled(&enabled)))
371 grammarCheckingEnabled = !!enabled;
375 gClassNameCount.add("WebView");
380 deleteBackingStore();
382 // the tooltip window needs to be explicitly destroyed since it isn't a WS_CHILD
383 if (::IsWindow(m_toolTipHwnd))
384 ::DestroyWindow(m_toolTipHwnd);
387 ASSERT(!m_preferences);
388 ASSERT(!m_viewWindow);
390 #if USE(ACCELERATED_COMPOSITING)
391 ASSERT(!m_layerTreeHost);
392 ASSERT(!m_backingLayer);
397 gClassNameCount.remove("WebView");
400 WebView* WebView::createInstance()
402 WebView* instance = new WebView();
407 void initializeStaticObservers()
409 static bool initialized;
414 IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
415 notifyCenter->addObserver(PreferencesChangedOrRemovedObserver::sharedInstance(), WebPreferences::webPreferencesChangedNotification(), 0);
416 notifyCenter->addObserver(PreferencesChangedOrRemovedObserver::sharedInstance(), WebPreferences::webPreferencesRemovedNotification(), 0);
419 static HashSet<WebView*>& allWebViewsSet()
421 static HashSet<WebView*> allWebViewsSet;
422 return allWebViewsSet;
425 void WebView::addToAllWebViewsSet()
427 allWebViewsSet().add(this);
430 void WebView::removeFromAllWebViewsSet()
432 allWebViewsSet().remove(this);
435 void WebView::setCacheModel(WebCacheModel cacheModel)
438 if (s_didSetCacheModel && cacheModel == s_cacheModel)
441 RetainPtr<CFURLCacheRef> cfurlCache(AdoptCF, CFURLCacheCopySharedURLCache());
442 RetainPtr<CFStringRef> cfurlCacheDirectory(AdoptCF, wkCopyFoundationCacheDirectory());
443 if (!cfurlCacheDirectory)
444 cfurlCacheDirectory.adoptCF(WebCore::localUserSpecificStorageDirectory().createCFString());
446 // As a fudge factor, use 1000 instead of 1024, in case the reported byte
447 // count doesn't align exactly to a megabyte boundary.
448 unsigned long long memSize = WebMemorySize() / 1024 / 1000;
449 unsigned long long diskFreeSize = WebVolumeFreeSize(cfurlCacheDirectory.get()) / 1024 / 1000;
451 unsigned cacheTotalCapacity = 0;
452 unsigned cacheMinDeadCapacity = 0;
453 unsigned cacheMaxDeadCapacity = 0;
454 double deadDecodedDataDeletionInterval = 0;
456 unsigned pageCacheCapacity = 0;
458 CFIndex cfurlCacheMemoryCapacity = 0;
459 CFIndex cfurlCacheDiskCapacity = 0;
461 switch (cacheModel) {
462 case WebCacheModelDocumentViewer: {
463 // Page cache capacity (in pages)
464 pageCacheCapacity = 0;
466 // Object cache capacities (in bytes)
468 cacheTotalCapacity = 96 * 1024 * 1024;
469 else if (memSize >= 1536)
470 cacheTotalCapacity = 64 * 1024 * 1024;
471 else if (memSize >= 1024)
472 cacheTotalCapacity = 32 * 1024 * 1024;
473 else if (memSize >= 512)
474 cacheTotalCapacity = 16 * 1024 * 1024;
476 cacheMinDeadCapacity = 0;
477 cacheMaxDeadCapacity = 0;
479 // Foundation memory cache capacity (in bytes)
480 cfurlCacheMemoryCapacity = 0;
482 // Foundation disk cache capacity (in bytes)
483 cfurlCacheDiskCapacity = CFURLCacheDiskCapacity(cfurlCache.get());
487 case WebCacheModelDocumentBrowser: {
488 // Page cache capacity (in pages)
490 pageCacheCapacity = 3;
491 else if (memSize >= 512)
492 pageCacheCapacity = 2;
493 else if (memSize >= 256)
494 pageCacheCapacity = 1;
496 pageCacheCapacity = 0;
498 // Object cache capacities (in bytes)
500 cacheTotalCapacity = 96 * 1024 * 1024;
501 else if (memSize >= 1536)
502 cacheTotalCapacity = 64 * 1024 * 1024;
503 else if (memSize >= 1024)
504 cacheTotalCapacity = 32 * 1024 * 1024;
505 else if (memSize >= 512)
506 cacheTotalCapacity = 16 * 1024 * 1024;
508 cacheMinDeadCapacity = cacheTotalCapacity / 8;
509 cacheMaxDeadCapacity = cacheTotalCapacity / 4;
511 // Foundation memory cache capacity (in bytes)
513 cfurlCacheMemoryCapacity = 4 * 1024 * 1024;
514 else if (memSize >= 1024)
515 cfurlCacheMemoryCapacity = 2 * 1024 * 1024;
516 else if (memSize >= 512)
517 cfurlCacheMemoryCapacity = 1 * 1024 * 1024;
519 cfurlCacheMemoryCapacity = 512 * 1024;
521 // Foundation disk cache capacity (in bytes)
522 if (diskFreeSize >= 16384)
523 cfurlCacheDiskCapacity = 50 * 1024 * 1024;
524 else if (diskFreeSize >= 8192)
525 cfurlCacheDiskCapacity = 40 * 1024 * 1024;
526 else if (diskFreeSize >= 4096)
527 cfurlCacheDiskCapacity = 30 * 1024 * 1024;
529 cfurlCacheDiskCapacity = 20 * 1024 * 1024;
533 case WebCacheModelPrimaryWebBrowser: {
534 // Page cache capacity (in pages)
535 // (Research indicates that value / page drops substantially after 3 pages.)
537 pageCacheCapacity = 5;
538 else if (memSize >= 1024)
539 pageCacheCapacity = 4;
540 else if (memSize >= 512)
541 pageCacheCapacity = 3;
542 else if (memSize >= 256)
543 pageCacheCapacity = 2;
545 pageCacheCapacity = 1;
547 // Object cache capacities (in bytes)
548 // (Testing indicates that value / MB depends heavily on content and
549 // browsing pattern. Even growth above 128MB can have substantial
550 // value / MB for some content / browsing patterns.)
552 cacheTotalCapacity = 128 * 1024 * 1024;
553 else if (memSize >= 1536)
554 cacheTotalCapacity = 96 * 1024 * 1024;
555 else if (memSize >= 1024)
556 cacheTotalCapacity = 64 * 1024 * 1024;
557 else if (memSize >= 512)
558 cacheTotalCapacity = 32 * 1024 * 1024;
560 cacheMinDeadCapacity = cacheTotalCapacity / 4;
561 cacheMaxDeadCapacity = cacheTotalCapacity / 2;
563 // This code is here to avoid a PLT regression. We can remove it if we
564 // can prove that the overall system gain would justify the regression.
565 cacheMaxDeadCapacity = max(24u, cacheMaxDeadCapacity);
567 deadDecodedDataDeletionInterval = 60;
569 // Foundation memory cache capacity (in bytes)
570 // (These values are small because WebCore does most caching itself.)
572 cfurlCacheMemoryCapacity = 4 * 1024 * 1024;
573 else if (memSize >= 512)
574 cfurlCacheMemoryCapacity = 2 * 1024 * 1024;
575 else if (memSize >= 256)
576 cfurlCacheMemoryCapacity = 1 * 1024 * 1024;
578 cfurlCacheMemoryCapacity = 512 * 1024;
580 // Foundation disk cache capacity (in bytes)
581 if (diskFreeSize >= 16384)
582 cfurlCacheDiskCapacity = 175 * 1024 * 1024;
583 else if (diskFreeSize >= 8192)
584 cfurlCacheDiskCapacity = 150 * 1024 * 1024;
585 else if (diskFreeSize >= 4096)
586 cfurlCacheDiskCapacity = 125 * 1024 * 1024;
587 else if (diskFreeSize >= 2048)
588 cfurlCacheDiskCapacity = 100 * 1024 * 1024;
589 else if (diskFreeSize >= 1024)
590 cfurlCacheDiskCapacity = 75 * 1024 * 1024;
592 cfurlCacheDiskCapacity = 50 * 1024 * 1024;
597 ASSERT_NOT_REACHED();
600 // Don't shrink a big disk cache, since that would cause churn.
601 cfurlCacheDiskCapacity = max(cfurlCacheDiskCapacity, CFURLCacheDiskCapacity(cfurlCache.get()));
603 memoryCache()->setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
604 memoryCache()->setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
605 pageCache()->setCapacity(pageCacheCapacity);
607 CFURLCacheSetMemoryCapacity(cfurlCache.get(), cfurlCacheMemoryCapacity);
608 CFURLCacheSetDiskCapacity(cfurlCache.get(), cfurlCacheDiskCapacity);
610 s_didSetCacheModel = true;
611 s_cacheModel = cacheModel;
616 WebCacheModel WebView::cacheModel()
621 bool WebView::didSetCacheModel()
623 return s_didSetCacheModel;
626 WebCacheModel WebView::maxCacheModelInAnyInstance()
628 WebCacheModel cacheModel = WebCacheModelDocumentViewer;
630 HashSet<WebView*>::iterator end = allWebViewsSet().end();
631 for (HashSet<WebView*>::iterator it = allWebViewsSet().begin(); it != end; ++it) {
632 COMPtr<IWebPreferences> pref;
633 if (FAILED((*it)->preferences(&pref)))
635 WebCacheModel prefCacheModel = WebCacheModelDocumentViewer;
636 if (FAILED(pref->cacheModel(&prefCacheModel)))
639 cacheModel = max(cacheModel, prefCacheModel);
645 HRESULT STDMETHODCALLTYPE WebView::close()
652 #if USE(ACCELERATED_COMPOSITING)
653 setAcceleratedCompositing(false);
656 WebNotificationCenter::defaultCenterInternal()->postNotificationName(_bstr_t(WebViewWillCloseNotification).GetBSTR(), static_cast<IWebView*>(this), 0);
658 if (m_uiDelegatePrivate)
659 m_uiDelegatePrivate->webViewClosing(this);
661 removeFromAllWebViewsSet();
664 if (Frame* frame = m_page->mainFrame())
665 frame->loader()->detachFromParent();
668 if (m_mouseOutTracker) {
669 m_mouseOutTracker->dwFlags = TME_CANCEL;
670 ::TrackMouseEvent(m_mouseOutTracker.get());
671 m_mouseOutTracker.set(0);
677 // We can't check IsWindow(m_viewWindow) here, because that will return true even while
678 // we're already handling WM_DESTROY. So we check !isBeingDestroyed() instead.
679 if (!isBeingDestroyed())
680 DestroyWindow(m_viewWindow);
681 // Either we just destroyed m_viewWindow, or it's in the process of being destroyed. Either
682 // way, we clear it out to make sure we don't try to use it later.
688 setDownloadDelegate(0);
689 setEditingDelegate(0);
690 setFrameLoadDelegate(0);
691 setFrameLoadDelegatePrivate(0);
692 setHistoryDelegate(0);
693 setPolicyDelegate(0);
694 setResourceLoadDelegate(0);
697 setPluginHalterDelegate(0);
700 m_webInspector->webViewClosed();
705 registerForIconNotification(false);
706 IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
707 notifyCenter->removeObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
709 if (COMPtr<WebPreferences> preferences = m_preferences) {
711 preferences->identifier(&identifier);
714 preferences->didRemoveFromWebView();
715 // Make sure we release the reference, since WebPreferences::removeReferenceForIdentifier will check for last reference to WebPreferences
718 WebPreferences::removeReferenceForIdentifier(identifier);
719 SysFreeString(identifier);
723 deleteBackingStore();
727 void WebView::repaint(const WebCore::IntRect& windowRect, bool contentChanged, bool immediate, bool repaintContentOnly)
729 #if USE(ACCELERATED_COMPOSITING)
730 if (isAcceleratedCompositing()) {
731 // The contentChanged, immediate, and repaintContentOnly parameters are all based on a non-
732 // compositing painting/scrolling model.
733 addToDirtyRegion(windowRect);
738 if (!repaintContentOnly) {
739 RECT rect = windowRect;
740 ::InvalidateRect(m_viewWindow, &rect, false);
743 addToDirtyRegion(windowRect);
745 if (repaintContentOnly)
746 updateBackingStore(core(topLevelFrame())->view());
748 ::UpdateWindow(m_viewWindow);
752 void WebView::deleteBackingStore()
754 pendingDeleteBackingStoreSet.remove(this);
756 if (m_deleteBackingStoreTimerActive) {
757 KillTimer(m_viewWindow, DeleteBackingStoreTimer);
758 m_deleteBackingStoreTimerActive = false;
760 m_backingStoreBitmap.clear();
761 m_backingStoreDirtyRegion.clear();
762 m_backingStoreSize.cx = m_backingStoreSize.cy = 0;
765 bool WebView::ensureBackingStore()
768 ::GetClientRect(m_viewWindow, &windowRect);
769 LONG width = windowRect.right - windowRect.left;
770 LONG height = windowRect.bottom - windowRect.top;
771 if (width > 0 && height > 0 && (width != m_backingStoreSize.cx || height != m_backingStoreSize.cy)) {
772 deleteBackingStore();
774 m_backingStoreSize.cx = width;
775 m_backingStoreSize.cy = height;
776 BitmapInfo bitmapInfo = BitmapInfo::createBottomUp(IntSize(m_backingStoreSize));
779 m_backingStoreBitmap = RefCountedHBITMAP::create(::CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0));
786 void WebView::addToDirtyRegion(const IntRect& dirtyRect)
788 // FIXME: We want an assert here saying that the dirtyRect is inside the clienRect,
789 // but it was being hit during our layout tests, and is being investigated in
790 // http://webkit.org/b/29350.
792 #if USE(ACCELERATED_COMPOSITING)
793 if (isAcceleratedCompositing()) {
794 m_backingLayer->setNeedsDisplayInRect(dirtyRect);
799 HRGN newRegion = ::CreateRectRgn(dirtyRect.x(), dirtyRect.y(),
800 dirtyRect.right(), dirtyRect.bottom());
801 addToDirtyRegion(newRegion);
804 void WebView::addToDirtyRegion(HRGN newRegion)
806 #if USE(ACCELERATED_COMPOSITING)
807 ASSERT(!isAcceleratedCompositing());
810 LOCAL_GDI_COUNTER(0, __FUNCTION__);
812 if (m_backingStoreDirtyRegion) {
813 HRGN combinedRegion = ::CreateRectRgn(0,0,0,0);
814 ::CombineRgn(combinedRegion, m_backingStoreDirtyRegion->handle(), newRegion, RGN_OR);
815 ::DeleteObject(newRegion);
816 m_backingStoreDirtyRegion = RefCountedHRGN::create(combinedRegion);
818 m_backingStoreDirtyRegion = RefCountedHRGN::create(newRegion);
820 if (m_uiDelegatePrivate)
821 m_uiDelegatePrivate->webViewDidInvalidate(this);
824 void WebView::scrollBackingStore(FrameView* frameView, int dx, int dy, const IntRect& scrollViewRect, const IntRect& clipRect)
826 #if USE(ACCELERATED_COMPOSITING)
827 if (isAcceleratedCompositing()) {
828 // FIXME: We should be doing something smarter here, like moving tiles around and painting
829 // any newly-exposed tiles. <http://webkit.org/b/52714>
830 m_backingLayer->setNeedsDisplayInRect(scrollViewRect);
835 LOCAL_GDI_COUNTER(0, __FUNCTION__);
837 // If there's no backing store we don't need to update it
838 if (!m_backingStoreBitmap) {
839 if (m_uiDelegatePrivate)
840 m_uiDelegatePrivate->webViewScrolled(this);
845 // Make a region to hold the invalidated scroll area.
846 HRGN updateRegion = ::CreateRectRgn(0, 0, 0, 0);
848 // Collect our device context info and select the bitmap to scroll.
849 HDC windowDC = ::GetDC(m_viewWindow);
850 HDC bitmapDC = ::CreateCompatibleDC(windowDC);
851 HGDIOBJ oldBitmap = ::SelectObject(bitmapDC, m_backingStoreBitmap->handle());
853 // Scroll the bitmap.
854 RECT scrollRectWin(scrollViewRect);
855 RECT clipRectWin(clipRect);
856 ::ScrollDC(bitmapDC, dx, dy, &scrollRectWin, &clipRectWin, updateRegion, 0);
858 ::GetRgnBox(updateRegion, ®ionBox);
863 // Add the dirty region to the backing store's dirty region.
864 addToDirtyRegion(updateRegion);
866 if (m_uiDelegatePrivate)
867 m_uiDelegatePrivate->webViewScrolled(this);
869 // Update the backing store.
870 updateBackingStore(frameView, bitmapDC, false);
873 ::SelectObject(bitmapDC, oldBitmap);
874 ::DeleteDC(bitmapDC);
875 ::ReleaseDC(m_viewWindow, windowDC);
878 void WebView::sizeChanged(const IntSize& newSize)
880 deleteBackingStore();
882 if (Frame* coreFrame = core(topLevelFrame()))
883 coreFrame->view()->resize(newSize);
885 #if USE(ACCELERATED_COMPOSITING)
887 m_layerTreeHost->resize();
888 if (m_backingLayer) {
889 m_backingLayer->setSize(newSize);
890 m_backingLayer->setNeedsDisplay();
895 // This emulates the Mac smarts for painting rects intelligently. This is very
896 // important for us, since we double buffer based off dirty rects.
897 static void getUpdateRects(HRGN region, const IntRect& dirtyRect, Vector<IntRect>& rects)
899 ASSERT_ARG(region, region);
901 const int cRectThreshold = 10;
902 const float cWastedSpaceThreshold = 0.75f;
906 DWORD regionDataSize = GetRegionData(region, sizeof(RGNDATA), NULL);
907 if (!regionDataSize) {
908 rects.append(dirtyRect);
912 Vector<unsigned char> buffer(regionDataSize);
913 RGNDATA* regionData = reinterpret_cast<RGNDATA*>(buffer.data());
914 GetRegionData(region, regionDataSize, regionData);
915 if (regionData->rdh.nCount > cRectThreshold) {
916 rects.append(dirtyRect);
920 double singlePixels = 0.0;
923 for (i = 0, rect = reinterpret_cast<RECT*>(regionData->Buffer); i < regionData->rdh.nCount; i++, rect++)
924 singlePixels += (rect->right - rect->left) * (rect->bottom - rect->top);
926 double unionPixels = dirtyRect.width() * dirtyRect.height();
927 double wastedSpace = 1.0 - (singlePixels / unionPixels);
928 if (wastedSpace <= cWastedSpaceThreshold) {
929 rects.append(dirtyRect);
933 for (i = 0, rect = reinterpret_cast<RECT*>(regionData->Buffer); i < regionData->rdh.nCount; i++, rect++)
937 void WebView::updateBackingStore(FrameView* frameView, HDC dc, bool backingStoreCompletelyDirty, WindowsToPaint windowsToPaint)
939 #if USE(ACCELERATED_COMPOSITING)
940 ASSERT(!isAcceleratedCompositing());
943 LOCAL_GDI_COUNTER(0, __FUNCTION__);
948 windowDC = ::GetDC(m_viewWindow);
949 bitmapDC = ::CreateCompatibleDC(windowDC);
950 ::SelectObject(bitmapDC, m_backingStoreBitmap->handle());
953 if (m_backingStoreBitmap && (m_backingStoreDirtyRegion || backingStoreCompletelyDirty)) {
954 // Do a layout first so that everything we render to the backing store is always current.
955 if (Frame* coreFrame = core(m_mainFrame))
956 if (FrameView* view = coreFrame->view())
957 view->updateLayoutAndStyleIfNeededRecursive();
959 Vector<IntRect> paintRects;
960 if (!backingStoreCompletelyDirty && m_backingStoreDirtyRegion) {
962 ::GetRgnBox(m_backingStoreDirtyRegion->handle(), ®ionBox);
963 getUpdateRects(m_backingStoreDirtyRegion->handle(), regionBox, paintRects);
966 ::GetClientRect(m_viewWindow, &clientRect);
967 paintRects.append(clientRect);
970 for (unsigned i = 0; i < paintRects.size(); ++i)
971 paintIntoBackingStore(frameView, bitmapDC, paintRects[i], windowsToPaint);
973 if (m_uiDelegatePrivate)
974 m_uiDelegatePrivate->webViewPainted(this);
976 m_backingStoreDirtyRegion.clear();
980 ::DeleteDC(bitmapDC);
981 ::ReleaseDC(m_viewWindow, windowDC);
987 void WebView::paint(HDC dc, LPARAM options)
989 LOCAL_GDI_COUNTER(0, __FUNCTION__);
991 #if USE(ACCELERATED_COMPOSITING)
992 if (isAcceleratedCompositing()) {
993 flushPendingGraphicsLayerChanges();
994 // Flushing might have taken us out of compositing mode.
995 if (isAcceleratedCompositing()) {
996 // FIXME: We need to paint into dc (if provided). <http://webkit.org/b/52578>
997 m_layerTreeHost->paint();
998 ::ValidateRect(m_viewWindow, 0);
1004 Frame* coreFrame = core(m_mainFrame);
1007 FrameView* frameView = coreFrame->view();
1011 OwnPtr<HRGN> region;
1012 int regionType = NULLREGION;
1014 WindowsToPaint windowsToPaint;
1016 region.set(CreateRectRgn(0,0,0,0));
1017 regionType = GetUpdateRgn(m_viewWindow, region.get(), false);
1018 hdc = BeginPaint(m_viewWindow, &ps);
1019 rcPaint = ps.rcPaint;
1020 // We're painting to the screen, and our child windows can handle
1021 // painting themselves to the screen.
1022 windowsToPaint = PaintWebViewOnly;
1025 ::GetClientRect(m_viewWindow, &rcPaint);
1026 if (options & PRF_ERASEBKGND)
1027 ::FillRect(hdc, &rcPaint, (HBRUSH)GetStockObject(WHITE_BRUSH));
1028 // Since we aren't painting to the screen, we want to paint all our
1029 // children into the HDC.
1030 windowsToPaint = PaintWebViewAndChildren;
1033 bool backingStoreCompletelyDirty = ensureBackingStore();
1034 if (!m_backingStoreBitmap) {
1036 EndPaint(m_viewWindow, &ps);
1042 HDC bitmapDC = ::CreateCompatibleDC(hdc);
1043 HGDIOBJ oldBitmap = ::SelectObject(bitmapDC, m_backingStoreBitmap->handle());
1045 // Update our backing store if needed.
1046 updateBackingStore(frameView, bitmapDC, backingStoreCompletelyDirty, windowsToPaint);
1048 // Now we blit the updated backing store
1049 IntRect windowDirtyRect = rcPaint;
1051 // Apply the same heuristic for this update region too.
1052 Vector<IntRect> blitRects;
1053 if (region && regionType == COMPLEXREGION)
1054 getUpdateRects(region.get(), windowDirtyRect, blitRects);
1056 blitRects.append(windowDirtyRect);
1058 for (unsigned i = 0; i < blitRects.size(); ++i)
1059 paintIntoWindow(bitmapDC, hdc, blitRects[i]);
1061 ::SelectObject(bitmapDC, oldBitmap);
1062 ::DeleteDC(bitmapDC);
1065 EndPaint(m_viewWindow, &ps);
1070 cancelDeleteBackingStoreSoon();
1072 deleteBackingStoreSoon();
1075 void WebView::paintIntoBackingStore(FrameView* frameView, HDC bitmapDC, const IntRect& dirtyRect, WindowsToPaint windowsToPaint)
1077 #if USE(ACCELERATED_COMPOSITING)
1078 ASSERT(!isAcceleratedCompositing());
1081 LOCAL_GDI_COUNTER(0, __FUNCTION__);
1083 // FIXME: We want an assert here saying that the dirtyRect is inside the clienRect,
1084 // but it was being hit during our layout tests, and is being investigated in
1085 // http://webkit.org/b/29350.
1087 RECT rect = dirtyRect;
1089 #if FLASH_BACKING_STORE_REDRAW
1090 HDC dc = ::GetDC(m_viewWindow);
1091 OwnPtr<HBRUSH> yellowBrush(CreateSolidBrush(RGB(255, 255, 0)));
1092 FillRect(dc, &rect, yellowBrush.get());
1095 paintIntoWindow(bitmapDC, dc, dirtyRect);
1096 ::ReleaseDC(m_viewWindow, dc);
1099 GraphicsContext gc(bitmapDC, m_transparent);
1100 gc.setShouldIncludeChildWindows(windowsToPaint == PaintWebViewAndChildren);
1103 gc.clearRect(dirtyRect);
1105 FillRect(bitmapDC, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
1107 COMPtr<IWebUIDelegatePrivate2> uiPrivate(Query, m_uiDelegate);
1109 uiPrivate->drawBackground(this, reinterpret_cast<OLE_HANDLE>(bitmapDC), &rect);
1111 if (frameView && frameView->frame() && frameView->frame()->contentRenderer()) {
1113 frameView->paint(&gc, dirtyRect);
1118 void WebView::paintIntoWindow(HDC bitmapDC, HDC windowDC, const IntRect& dirtyRect)
1120 #if USE(ACCELERATED_COMPOSITING)
1121 ASSERT(!isAcceleratedCompositing());
1124 LOCAL_GDI_COUNTER(0, __FUNCTION__);
1125 #if FLASH_WINDOW_REDRAW
1126 OwnPtr<HBRUSH> greenBrush = CreateSolidBrush(RGB(0, 255, 0));
1127 RECT rect = dirtyRect;
1128 FillRect(windowDC, &rect, greenBrush.get());
1133 // Blit the dirty rect from the backing store into the same position
1134 // in the destination DC.
1135 BitBlt(windowDC, dirtyRect.x(), dirtyRect.y(), dirtyRect.width(), dirtyRect.height(), bitmapDC,
1136 dirtyRect.x(), dirtyRect.y(), SRCCOPY);
1139 void WebView::frameRect(RECT* rect)
1141 ::GetWindowRect(m_viewWindow, rect);
1144 class WindowCloseTimer : public WebCore::SuspendableTimer {
1146 static WindowCloseTimer* create(WebView*);
1149 WindowCloseTimer(ScriptExecutionContext*, WebView*);
1150 virtual void contextDestroyed();
1151 virtual void fired();
1156 WindowCloseTimer* WindowCloseTimer::create(WebView* webView)
1158 ASSERT_ARG(webView, webView);
1159 Frame* frame = core(webView->topLevelFrame());
1164 Document* document = frame->document();
1169 return new WindowCloseTimer(document, webView);
1172 WindowCloseTimer::WindowCloseTimer(ScriptExecutionContext* context, WebView* webView)
1173 : SuspendableTimer(context)
1174 , m_webView(webView)
1176 ASSERT_ARG(context, context);
1177 ASSERT_ARG(webView, webView);
1180 void WindowCloseTimer::contextDestroyed()
1182 SuspendableTimer::contextDestroyed();
1186 void WindowCloseTimer::fired()
1188 m_webView->closeWindowTimerFired();
1191 void WebView::closeWindowSoon()
1193 if (m_closeWindowTimer)
1196 m_closeWindowTimer = WindowCloseTimer::create(this);
1197 if (!m_closeWindowTimer)
1199 m_closeWindowTimer->startOneShot(0);
1204 void WebView::closeWindowTimerFired()
1210 void WebView::closeWindow()
1212 if (m_hasSpellCheckerDocumentTag) {
1213 if (m_editingDelegate)
1214 m_editingDelegate->closeSpellDocument(this);
1215 m_hasSpellCheckerDocumentTag = false;
1218 COMPtr<IWebUIDelegate> ui;
1219 if (SUCCEEDED(uiDelegate(&ui)))
1220 ui->webViewClose(this);
1223 bool WebView::canHandleRequest(const WebCore::ResourceRequest& request)
1225 // On the mac there's an about url protocol implementation but CFNetwork doesn't have that.
1226 if (equalIgnoringCase(String(request.url().protocol()), "about"))
1230 if (CFURLProtocolCanHandleRequest(request.cfURLRequest()))
1233 // FIXME: Mac WebKit calls _representationExistsForURLScheme here
1240 String WebView::standardUserAgentWithApplicationName(const String& applicationName)
1242 if (applicationName.isEmpty())
1243 return makeString("Mozilla/5.0 (Windows; U; ", osVersion(), "; ", defaultLanguage(), ") AppleWebKit/", webKitVersion(), " (KHTML, like Gecko)");
1244 return makeString("Mozilla/5.0 (Windows; U; ", osVersion(), "; ", defaultLanguage(), ") AppleWebKit/", webKitVersion(), " (KHTML, like Gecko) ", applicationName);
1247 Page* WebView::page()
1252 bool WebView::handleContextMenuEvent(WPARAM wParam, LPARAM lParam)
1254 // Translate the screen coordinates into window coordinates
1255 POINT coords = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
1256 if (coords.x == -1 || coords.y == -1) {
1257 // The contextMenuController() holds onto the last context menu that was popped up on the
1258 // page until a new one is created. We need to clear this menu before propagating the event
1259 // through the DOM so that we can detect if we create a new menu for this event, since we
1260 // won't create a new menu if the DOM swallows the event and the defaultEventHandler does
1262 m_page->contextMenuController()->clearContextMenu();
1264 Frame* focusedFrame = m_page->focusController()->focusedOrMainFrame();
1265 return focusedFrame->eventHandler()->sendContextMenuEventForKey();
1268 if (!::ScreenToClient(m_viewWindow, &coords))
1272 lParam = MAKELPARAM(coords.x, coords.y);
1274 m_page->contextMenuController()->clearContextMenu();
1276 IntPoint documentPoint(m_page->mainFrame()->view()->windowToContents(coords));
1277 HitTestResult result = m_page->mainFrame()->eventHandler()->hitTestResultAtPoint(documentPoint, false);
1278 Frame* targetFrame = result.innerNonSharedNode() ? result.innerNonSharedNode()->document()->frame() : m_page->focusController()->focusedOrMainFrame();
1280 targetFrame->view()->setCursor(pointerCursor());
1281 PlatformMouseEvent mouseEvent(m_viewWindow, WM_RBUTTONUP, wParam, lParam);
1282 bool handledEvent = targetFrame->eventHandler()->sendContextMenuEvent(mouseEvent);
1286 ContextMenuController* contextMenuController = m_page->contextMenuController();
1289 ContextMenu* coreMenu = contextMenuController->contextMenu();
1293 Node* node = contextMenuController->hitTestResult().innerNonSharedNode();
1297 Frame* frame = node->document()->frame();
1301 FrameView* view = frame->view();
1305 POINT point(view->contentsToWindow(contextMenuController->hitTestResult().point()));
1307 // Translate the point to screen coordinates
1308 if (!::ClientToScreen(m_viewWindow, &point))
1311 BOOL hasCustomMenus = false;
1313 m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1316 m_uiDelegate->trackCustomPopupMenu((IWebView*)this, (OLE_HANDLE)(ULONG64)coreMenu->nativeMenu(), &point);
1318 // Surprisingly, TPM_RIGHTBUTTON means that items are selectable with either the right OR left mouse button
1319 UINT flags = TPM_RIGHTBUTTON | TPM_TOPALIGN | TPM_VERPOSANIMATION | TPM_HORIZONTAL
1320 | TPM_LEFTALIGN | TPM_HORPOSANIMATION;
1321 ::TrackPopupMenuEx(coreMenu->nativeMenu(), flags, point.x, point.y, m_viewWindow, 0);
1327 bool WebView::onMeasureItem(WPARAM /*wParam*/, LPARAM lParam)
1332 BOOL hasCustomMenus = false;
1333 m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1334 if (!hasCustomMenus)
1337 m_uiDelegate->measureCustomMenuItem((IWebView*)this, (void*)lParam);
1341 bool WebView::onDrawItem(WPARAM /*wParam*/, LPARAM lParam)
1346 BOOL hasCustomMenus = false;
1347 m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1348 if (!hasCustomMenus)
1351 m_uiDelegate->drawCustomMenuItem((IWebView*)this, (void*)lParam);
1355 bool WebView::onInitMenuPopup(WPARAM wParam, LPARAM /*lParam*/)
1360 HMENU menu = (HMENU)wParam;
1364 BOOL hasCustomMenus = false;
1365 m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1366 if (!hasCustomMenus)
1369 m_uiDelegate->addCustomMenuDrawingData((IWebView*)this, (OLE_HANDLE)(ULONG64)menu);
1373 bool WebView::onUninitMenuPopup(WPARAM wParam, LPARAM /*lParam*/)
1378 HMENU menu = (HMENU)wParam;
1382 BOOL hasCustomMenus = false;
1383 m_uiDelegate->hasCustomMenuImplementation(&hasCustomMenus);
1384 if (!hasCustomMenus)
1387 m_uiDelegate->cleanUpCustomMenuDrawingData((IWebView*)this, (OLE_HANDLE)(ULONG64)menu);
1391 void WebView::performContextMenuAction(WPARAM wParam, LPARAM lParam, bool byPosition)
1393 ContextMenu* menu = m_page->contextMenuController()->contextMenu();
1396 ContextMenuItem* item = byPosition ? menu->itemAtIndex((unsigned)wParam) : menu->itemWithAction((ContextMenuAction)wParam);
1399 m_page->contextMenuController()->contextMenuItemSelected(item);
1402 bool WebView::handleMouseEvent(UINT message, WPARAM wParam, LPARAM lParam)
1404 static LONG globalClickCount;
1405 static IntPoint globalPrevPoint;
1406 static MouseButton globalPrevButton;
1407 static LONG globalPrevMouseDownTime;
1409 if (message == WM_CANCELMODE) {
1410 m_page->mainFrame()->eventHandler()->lostMouseCapture();
1414 // Create our event.
1415 // On WM_MOUSELEAVE we need to create a mouseout event, so we force the position
1416 // of the event to be at (MINSHORT, MINSHORT).
1417 LPARAM position = (message == WM_MOUSELEAVE) ? ((MINSHORT << 16) | MINSHORT) : lParam;
1418 PlatformMouseEvent mouseEvent(m_viewWindow, message, wParam, position, m_mouseActivated);
1420 setMouseActivated(false);
1422 bool insideThreshold = abs(globalPrevPoint.x() - mouseEvent.pos().x()) < ::GetSystemMetrics(SM_CXDOUBLECLK) &&
1423 abs(globalPrevPoint.y() - mouseEvent.pos().y()) < ::GetSystemMetrics(SM_CYDOUBLECLK);
1424 LONG messageTime = ::GetMessageTime();
1426 bool handled = false;
1428 if (message == WM_LBUTTONDOWN || message == WM_MBUTTONDOWN || message == WM_RBUTTONDOWN) {
1429 // FIXME: I'm not sure if this is the "right" way to do this
1430 // but without this call, we never become focused since we don't allow
1431 // the default handling of mouse events.
1432 SetFocus(m_viewWindow);
1434 // Always start capturing events when the mouse goes down in our HWND.
1435 ::SetCapture(m_viewWindow);
1437 if (((messageTime - globalPrevMouseDownTime) < (LONG)::GetDoubleClickTime()) &&
1439 mouseEvent.button() == globalPrevButton)
1442 // Reset the click count.
1443 globalClickCount = 1;
1444 globalPrevMouseDownTime = messageTime;
1445 globalPrevButton = mouseEvent.button();
1446 globalPrevPoint = mouseEvent.pos();
1448 mouseEvent.setClickCount(globalClickCount);
1449 handled = m_page->mainFrame()->eventHandler()->handleMousePressEvent(mouseEvent);
1450 } else if (message == WM_LBUTTONDBLCLK || message == WM_MBUTTONDBLCLK || message == WM_RBUTTONDBLCLK) {
1452 mouseEvent.setClickCount(globalClickCount);
1453 handled = m_page->mainFrame()->eventHandler()->handleMousePressEvent(mouseEvent);
1454 } else if (message == WM_LBUTTONUP || message == WM_MBUTTONUP || message == WM_RBUTTONUP) {
1455 // Record the global position and the button of the up.
1456 globalPrevButton = mouseEvent.button();
1457 globalPrevPoint = mouseEvent.pos();
1458 mouseEvent.setClickCount(globalClickCount);
1459 m_page->mainFrame()->eventHandler()->handleMouseReleaseEvent(mouseEvent);
1461 } else if (message == WM_MOUSELEAVE && m_mouseOutTracker) {
1462 // Once WM_MOUSELEAVE is fired windows clears this tracker
1463 // so there is no need to disable it ourselves.
1464 m_mouseOutTracker.set(0);
1465 m_page->mainFrame()->eventHandler()->mouseMoved(mouseEvent);
1467 } else if (message == WM_MOUSEMOVE) {
1468 if (!insideThreshold)
1469 globalClickCount = 0;
1470 mouseEvent.setClickCount(globalClickCount);
1471 handled = m_page->mainFrame()->eventHandler()->mouseMoved(mouseEvent);
1472 if (!m_mouseOutTracker) {
1473 m_mouseOutTracker.set(new TRACKMOUSEEVENT);
1474 m_mouseOutTracker->cbSize = sizeof(TRACKMOUSEEVENT);
1475 m_mouseOutTracker->dwFlags = TME_LEAVE;
1476 m_mouseOutTracker->hwndTrack = m_viewWindow;
1477 ::TrackMouseEvent(m_mouseOutTracker.get());
1483 bool WebView::gestureNotify(WPARAM wParam, LPARAM lParam)
1485 GESTURENOTIFYSTRUCT* gn = reinterpret_cast<GESTURENOTIFYSTRUCT*>(lParam);
1487 Frame* coreFrame = core(m_mainFrame);
1491 ScrollView* view = coreFrame->view();
1495 // If we don't have this function, we shouldn't be receiving this message
1496 ASSERT(SetGestureConfigPtr());
1498 bool hitScrollbar = false;
1499 POINT gestureBeginPoint = {gn->ptsLocation.x, gn->ptsLocation.y};
1500 HitTestRequest request(HitTestRequest::ReadOnly);
1501 for (Frame* childFrame = m_page->mainFrame(); childFrame; childFrame = EventHandler::subframeForTargetNode(m_gestureTargetNode.get())) {
1502 FrameView* frameView = childFrame->view();
1505 RenderView* renderView = childFrame->document()->renderView();
1508 RenderLayer* layer = renderView->layer();
1512 HitTestResult result(frameView->screenToContents(gestureBeginPoint));
1513 layer->hitTest(request, result);
1514 m_gestureTargetNode = result.innerNode();
1517 hitScrollbar = result.scrollbar();
1520 if (!hitScrollbar) {
1521 // The hit testing above won't detect if we've hit the main frame's vertical scrollbar. Check that manually now.
1523 GetWindowRect(m_viewWindow, &webViewRect);
1524 hitScrollbar = view->verticalScrollbar() && (gestureBeginPoint.x > (webViewRect.right - view->verticalScrollbar()->theme()->scrollbarThickness()));
1527 bool canBeScrolled = false;
1528 if (m_gestureTargetNode) {
1529 for (RenderObject* renderer = m_gestureTargetNode->renderer(); renderer; renderer = renderer->parent()) {
1530 if (renderer->isBox() && toRenderBox(renderer)->canBeScrolledAndHasScrollableArea()) {
1531 canBeScrolled = true;
1537 // We always allow two-fingered panning with inertia and a gutter (which limits movement to one
1538 // direction in most cases).
1539 DWORD dwPanWant = GC_PAN | GC_PAN_WITH_INERTIA | GC_PAN_WITH_GUTTER;
1540 // We never allow single-fingered horizontal panning. That gesture is reserved for creating text
1541 // selections. This matches IE.
1542 DWORD dwPanBlock = GC_PAN_WITH_SINGLE_FINGER_HORIZONTALLY;
1544 if (hitScrollbar || !canBeScrolled) {
1545 // The part of the page under the gesture can't be scrolled, or the gesture is on a scrollbar.
1546 // Disallow single-fingered vertical panning in this case, too, so we'll fall back to the default
1547 // behavior (which allows the scrollbar thumb to be dragged, text selections to be made, etc.).
1548 dwPanBlock |= GC_PAN_WITH_SINGLE_FINGER_VERTICALLY;
1550 // The part of the page the gesture is under can be scrolled, and we're not under a scrollbar.
1551 // Allow single-fingered vertical panning in this case, so the user will be able to pan the page
1552 // with one or two fingers.
1553 dwPanWant |= GC_PAN_WITH_SINGLE_FINGER_VERTICALLY;
1556 GESTURECONFIG gc = { GID_PAN, dwPanWant, dwPanBlock };
1557 return SetGestureConfigPtr()(m_viewWindow, 0, 1, &gc, sizeof(GESTURECONFIG));
1560 bool WebView::gesture(WPARAM wParam, LPARAM lParam)
1562 // We want to bail out if we don't have either of these functions
1563 if (!GetGestureInfoPtr() || !CloseGestureInfoHandlePtr())
1566 HGESTUREINFO gestureHandle = reinterpret_cast<HGESTUREINFO>(lParam);
1568 GESTUREINFO gi = {0};
1569 gi.cbSize = sizeof(GESTUREINFO);
1571 if (!GetGestureInfoPtr()(gestureHandle, reinterpret_cast<PGESTUREINFO>(&gi)))
1576 m_lastPanX = gi.ptsLocation.x;
1577 m_lastPanY = gi.ptsLocation.y;
1581 m_gestureTargetNode = 0;
1584 // Where are the fingers currently?
1585 long currentX = gi.ptsLocation.x;
1586 long currentY = gi.ptsLocation.y;
1587 // How far did we pan in each direction?
1588 long deltaX = currentX - m_lastPanX;
1589 long deltaY = currentY - m_lastPanY;
1590 // Calculate the overpan for window bounce
1591 m_yOverpan -= m_lastPanY - currentY;
1592 m_xOverpan -= m_lastPanX - currentX;
1593 // Update our class variables with updated values
1594 m_lastPanX = currentX;
1595 m_lastPanY = currentY;
1597 Frame* coreFrame = core(m_mainFrame);
1599 CloseGestureInfoHandlePtr()(gestureHandle);
1603 if (!m_gestureTargetNode || !m_gestureTargetNode->renderer())
1606 // We negate here since panning up moves the content up, but moves the scrollbar down.
1607 m_gestureTargetNode->renderer()->enclosingLayer()->scrollByRecursively(-deltaX, -deltaY);
1609 if (!(UpdatePanningFeedbackPtr() && BeginPanningFeedbackPtr() && EndPanningFeedbackPtr())) {
1610 CloseGestureInfoHandlePtr()(gestureHandle);
1614 if (gi.dwFlags & GF_BEGIN) {
1615 BeginPanningFeedbackPtr()(m_viewWindow);
1617 } else if (gi.dwFlags & GF_END) {
1618 EndPanningFeedbackPtr()(m_viewWindow, true);
1622 ScrollView* view = coreFrame->view();
1624 CloseGestureInfoHandlePtr()(gestureHandle);
1627 Scrollbar* vertScrollbar = view->verticalScrollbar();
1628 if (!vertScrollbar) {
1629 CloseGestureInfoHandlePtr()(gestureHandle);
1633 // FIXME: Support Horizontal Window Bounce. <https://webkit.org/b/28500>.
1634 // FIXME: If the user starts panning down after a window bounce has started, the window doesn't bounce back
1635 // until they release their finger. <https://webkit.org/b/28501>.
1636 if (vertScrollbar->currentPos() == 0)
1637 UpdatePanningFeedbackPtr()(m_viewWindow, 0, m_yOverpan, gi.dwFlags & GF_INERTIA);
1638 else if (vertScrollbar->currentPos() >= vertScrollbar->maximum())
1639 UpdatePanningFeedbackPtr()(m_viewWindow, 0, m_yOverpan, gi.dwFlags & GF_INERTIA);
1641 CloseGestureInfoHandlePtr()(gestureHandle);
1648 // If we get to this point, the gesture has not been handled. We forward
1649 // the call to DefWindowProc by returning false, and we don't need to
1650 // to call CloseGestureInfoHandle.
1651 // http://msdn.microsoft.com/en-us/library/dd353228(VS.85).aspx
1655 bool WebView::mouseWheel(WPARAM wParam, LPARAM lParam, bool isMouseHWheel)
1657 // Ctrl+Mouse wheel doesn't ever go into WebCore. It is used to
1658 // zoom instead (Mac zooms the whole Desktop, but Windows browsers trigger their
1659 // own local zoom modes for Ctrl+wheel).
1660 if (wParam & MK_CONTROL) {
1661 short delta = short(HIWORD(wParam));
1669 // 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.
1670 HWND focusedWindow = GetFocus();
1671 if (focusedWindow && focusedWindow != m_viewWindow) {
1672 // 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).
1673 TCHAR className[256];
1675 // Make sure truncation won't affect the comparison.
1676 ASSERT(WTF_ARRAY_LENGTH(className) > _tcslen(PopupMenuWin::popupClassName()));
1678 if (GetClassName(focusedWindow, className, WTF_ARRAY_LENGTH(className)) && !_tcscmp(className, PopupMenuWin::popupClassName())) {
1679 // We don't let the WebView scroll here for two reasons - 1) To match Firefox behavior, 2) If we do scroll, we lose the
1680 // focus ring around the select menu.
1681 SetFocus(m_viewWindow);
1686 PlatformWheelEvent wheelEvent(m_viewWindow, wParam, lParam, isMouseHWheel);
1687 Frame* coreFrame = core(m_mainFrame);
1691 return coreFrame->eventHandler()->handleWheelEvent(wheelEvent);
1694 bool WebView::verticalScroll(WPARAM wParam, LPARAM /*lParam*/)
1696 ScrollDirection direction;
1697 ScrollGranularity granularity;
1698 switch (LOWORD(wParam)) {
1700 granularity = ScrollByLine;
1701 direction = ScrollDown;
1704 granularity = ScrollByLine;
1705 direction = ScrollUp;
1708 granularity = ScrollByDocument;
1709 direction = ScrollDown;
1712 granularity = ScrollByDocument;
1713 direction = ScrollUp;
1720 Frame* frame = m_page->focusController()->focusedOrMainFrame();
1721 return frame->eventHandler()->scrollRecursively(direction, granularity);
1724 bool WebView::horizontalScroll(WPARAM wParam, LPARAM /*lParam*/)
1726 ScrollDirection direction;
1727 ScrollGranularity granularity;
1728 switch (LOWORD(wParam)) {
1730 granularity = ScrollByLine;
1731 direction = ScrollLeft;
1734 granularity = ScrollByLine;
1735 direction = ScrollRight;
1738 granularity = ScrollByDocument;
1739 direction = ScrollLeft;
1742 granularity = ScrollByDocument;
1743 direction = ScrollRight;
1749 Frame* frame = m_page->focusController()->focusedOrMainFrame();
1750 return frame->eventHandler()->scrollRecursively(direction, granularity);
1754 bool WebView::execCommand(WPARAM wParam, LPARAM /*lParam*/)
1756 Frame* frame = m_page->focusController()->focusedOrMainFrame();
1757 switch (LOWORD(wParam)) {
1759 return frame->editor()->command("SelectAll").execute();
1761 return frame->editor()->command("Undo").execute();
1763 return frame->editor()->command("Redo").execute();
1768 bool WebView::keyUp(WPARAM virtualKeyCode, LPARAM keyData, bool systemKeyDown)
1770 PlatformKeyboardEvent keyEvent(m_viewWindow, virtualKeyCode, keyData, PlatformKeyboardEvent::KeyUp, systemKeyDown);
1772 Frame* frame = m_page->focusController()->focusedOrMainFrame();
1773 m_currentCharacterCode = 0;
1775 return frame->eventHandler()->keyEvent(keyEvent);
1778 static const unsigned CtrlKey = 1 << 0;
1779 static const unsigned AltKey = 1 << 1;
1780 static const unsigned ShiftKey = 1 << 2;
1783 struct KeyDownEntry {
1784 unsigned virtualKey;
1789 struct KeyPressEntry {
1795 static const KeyDownEntry keyDownEntries[] = {
1796 { VK_LEFT, 0, "MoveLeft" },
1797 { VK_LEFT, ShiftKey, "MoveLeftAndModifySelection" },
1798 { VK_LEFT, CtrlKey, "MoveWordLeft" },
1799 { VK_LEFT, CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection" },
1800 { VK_RIGHT, 0, "MoveRight" },
1801 { VK_RIGHT, ShiftKey, "MoveRightAndModifySelection" },
1802 { VK_RIGHT, CtrlKey, "MoveWordRight" },
1803 { VK_RIGHT, CtrlKey | ShiftKey, "MoveWordRightAndModifySelection" },
1804 { VK_UP, 0, "MoveUp" },
1805 { VK_UP, ShiftKey, "MoveUpAndModifySelection" },
1806 { VK_PRIOR, ShiftKey, "MovePageUpAndModifySelection" },
1807 { VK_DOWN, 0, "MoveDown" },
1808 { VK_DOWN, ShiftKey, "MoveDownAndModifySelection" },
1809 { VK_NEXT, ShiftKey, "MovePageDownAndModifySelection" },
1810 { VK_PRIOR, 0, "MovePageUp" },
1811 { VK_NEXT, 0, "MovePageDown" },
1812 { VK_HOME, 0, "MoveToBeginningOfLine" },
1813 { VK_HOME, ShiftKey, "MoveToBeginningOfLineAndModifySelection" },
1814 { VK_HOME, CtrlKey, "MoveToBeginningOfDocument" },
1815 { VK_HOME, CtrlKey | ShiftKey, "MoveToBeginningOfDocumentAndModifySelection" },
1817 { VK_END, 0, "MoveToEndOfLine" },
1818 { VK_END, ShiftKey, "MoveToEndOfLineAndModifySelection" },
1819 { VK_END, CtrlKey, "MoveToEndOfDocument" },
1820 { VK_END, CtrlKey | ShiftKey, "MoveToEndOfDocumentAndModifySelection" },
1822 { VK_BACK, 0, "DeleteBackward" },
1823 { VK_BACK, ShiftKey, "DeleteBackward" },
1824 { VK_DELETE, 0, "DeleteForward" },
1825 { VK_BACK, CtrlKey, "DeleteWordBackward" },
1826 { VK_DELETE, CtrlKey, "DeleteWordForward" },
1828 { 'B', CtrlKey, "ToggleBold" },
1829 { 'I', CtrlKey, "ToggleItalic" },
1831 { VK_ESCAPE, 0, "Cancel" },
1832 { VK_OEM_PERIOD, CtrlKey, "Cancel" },
1833 { VK_TAB, 0, "InsertTab" },
1834 { VK_TAB, ShiftKey, "InsertBacktab" },
1835 { VK_RETURN, 0, "InsertNewline" },
1836 { VK_RETURN, CtrlKey, "InsertNewline" },
1837 { VK_RETURN, AltKey, "InsertNewline" },
1838 { VK_RETURN, ShiftKey, "InsertNewline" },
1839 { VK_RETURN, AltKey | ShiftKey, "InsertNewline" },
1841 // It's not quite clear whether clipboard shortcuts and Undo/Redo should be handled
1842 // in the application or in WebKit. We chose WebKit.
1843 { 'C', CtrlKey, "Copy" },
1844 { 'V', CtrlKey, "Paste" },
1845 { 'X', CtrlKey, "Cut" },
1846 { 'A', CtrlKey, "SelectAll" },
1847 { VK_INSERT, CtrlKey, "Copy" },
1848 { VK_DELETE, ShiftKey, "Cut" },
1849 { VK_INSERT, ShiftKey, "Paste" },
1850 { 'Z', CtrlKey, "Undo" },
1851 { 'Z', CtrlKey | ShiftKey, "Redo" },
1854 static const KeyPressEntry keyPressEntries[] = {
1855 { '\t', 0, "InsertTab" },
1856 { '\t', ShiftKey, "InsertBacktab" },
1857 { '\r', 0, "InsertNewline" },
1858 { '\r', CtrlKey, "InsertNewline" },
1859 { '\r', AltKey, "InsertNewline" },
1860 { '\r', ShiftKey, "InsertNewline" },
1861 { '\r', AltKey | ShiftKey, "InsertNewline" },
1864 const char* WebView::interpretKeyEvent(const KeyboardEvent* evt)
1866 ASSERT(evt->type() == eventNames().keydownEvent || evt->type() == eventNames().keypressEvent);
1868 static HashMap<int, const char*>* keyDownCommandsMap = 0;
1869 static HashMap<int, const char*>* keyPressCommandsMap = 0;
1871 if (!keyDownCommandsMap) {
1872 keyDownCommandsMap = new HashMap<int, const char*>;
1873 keyPressCommandsMap = new HashMap<int, const char*>;
1875 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyDownEntries); ++i)
1876 keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].name);
1878 for (size_t i = 0; i < WTF_ARRAY_LENGTH(keyPressEntries); ++i)
1879 keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, keyPressEntries[i].name);
1882 unsigned modifiers = 0;
1883 if (evt->shiftKey())
1884 modifiers |= ShiftKey;
1886 modifiers |= AltKey;
1888 modifiers |= CtrlKey;
1890 if (evt->type() == eventNames().keydownEvent) {
1891 int mapKey = modifiers << 16 | evt->keyCode();
1892 return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
1895 int mapKey = modifiers << 16 | evt->charCode();
1896 return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
1899 bool WebView::handleEditingKeyboardEvent(KeyboardEvent* evt)
1901 Node* node = evt->target()->toNode();
1903 Frame* frame = node->document()->frame();
1906 const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
1907 if (!keyEvent || keyEvent->isSystemKey()) // do not treat this as text input if it's a system key event
1910 Editor::Command command = frame->editor()->command(interpretKeyEvent(evt));
1912 if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
1913 // WebKit doesn't have enough information about mode to decide how commands that just insert text if executed via Editor should be treated,
1914 // so we leave it upon WebCore to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
1915 // (e.g. Tab that inserts a Tab character, or Enter).
1916 return !command.isTextInsertion() && command.execute(evt);
1919 if (command.execute(evt))
1922 // Don't insert null or control characters as they can result in unexpected behaviour
1923 if (evt->charCode() < ' ')
1926 return frame->editor()->insertText(evt->keyEvent()->text(), evt);
1929 bool WebView::keyDown(WPARAM virtualKeyCode, LPARAM keyData, bool systemKeyDown)
1931 Frame* frame = m_page->focusController()->focusedOrMainFrame();
1932 if (virtualKeyCode == VK_CAPITAL)
1933 frame->eventHandler()->capsLockStateMayHaveChanged();
1935 PlatformKeyboardEvent keyEvent(m_viewWindow, virtualKeyCode, keyData, PlatformKeyboardEvent::RawKeyDown, systemKeyDown);
1936 bool handled = frame->eventHandler()->keyEvent(keyEvent);
1938 // These events cannot be canceled, and we have no default handling for them.
1939 // FIXME: match IE list more closely, see <http://msdn2.microsoft.com/en-us/library/ms536938.aspx>.
1940 if (systemKeyDown && virtualKeyCode != VK_RETURN)
1944 // FIXME: remove WM_UNICHAR, too
1946 // WM_SYSCHAR events should not be removed, because access keys are implemented in WebCore in WM_SYSCHAR handler.
1948 ::PeekMessage(&msg, m_viewWindow, WM_CHAR, WM_CHAR, PM_REMOVE);
1952 // We need to handle back/forward using either Backspace(+Shift) or Ctrl+Left/Right Arrow keys.
1953 if ((virtualKeyCode == VK_BACK && keyEvent.shiftKey()) || (virtualKeyCode == VK_RIGHT && keyEvent.ctrlKey()))
1954 return m_page->goForward();
1955 if (virtualKeyCode == VK_BACK || (virtualKeyCode == VK_LEFT && keyEvent.ctrlKey()))
1956 return m_page->goBack();
1958 // Need to scroll the page if the arrow keys, pgup/dn, or home/end are hit.
1959 ScrollDirection direction;
1960 ScrollGranularity granularity;
1961 switch (virtualKeyCode) {
1963 granularity = ScrollByLine;
1964 direction = ScrollLeft;
1967 granularity = ScrollByLine;
1968 direction = ScrollRight;
1971 granularity = ScrollByLine;
1972 direction = ScrollUp;
1975 granularity = ScrollByLine;
1976 direction = ScrollDown;
1979 granularity = ScrollByDocument;
1980 direction = ScrollUp;
1983 granularity = ScrollByDocument;
1984 direction = ScrollDown;
1987 granularity = ScrollByPage;
1988 direction = ScrollUp;
1991 granularity = ScrollByPage;
1992 direction = ScrollDown;
1998 return frame->eventHandler()->scrollRecursively(direction, granularity);
2001 bool WebView::keyPress(WPARAM charCode, LPARAM keyData, bool systemKeyDown)
2003 Frame* frame = m_page->focusController()->focusedOrMainFrame();
2005 PlatformKeyboardEvent keyEvent(m_viewWindow, charCode, keyData, PlatformKeyboardEvent::Char, systemKeyDown);
2006 // IE does not dispatch keypress event for WM_SYSCHAR.
2008 return frame->eventHandler()->handleAccessKey(keyEvent);
2009 return frame->eventHandler()->keyEvent(keyEvent);
2012 bool WebView::registerWebViewWindowClass()
2014 static bool haveRegisteredWindowClass = false;
2015 if (haveRegisteredWindowClass)
2018 haveRegisteredWindowClass = true;
2022 wcex.cbSize = sizeof(WNDCLASSEX);
2024 wcex.style = CS_DBLCLKS;
2025 wcex.lpfnWndProc = WebViewWndProc;
2026 wcex.cbClsExtra = 0;
2027 wcex.cbWndExtra = 4; // 4 bytes for the IWebView pointer
2028 wcex.hInstance = gInstance;
2030 wcex.hCursor = ::LoadCursor(0, IDC_ARROW);
2031 wcex.hbrBackground = 0;
2032 wcex.lpszMenuName = 0;
2033 wcex.lpszClassName = kWebViewWindowClassName;
2036 return !!RegisterClassEx(&wcex);
2039 static HWND findTopLevelParent(HWND window)
2044 HWND current = window;
2045 for (HWND parent = GetParent(current); current; current = parent, parent = GetParent(parent))
2046 if (!parent || !(GetWindowLongPtr(current, GWL_STYLE) & (WS_POPUP | WS_CHILD)))
2048 ASSERT_NOT_REACHED();
2052 LRESULT CALLBACK WebView::WebViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2054 LRESULT lResult = 0;
2055 LONG_PTR longPtr = GetWindowLongPtr(hWnd, 0);
2056 WebView* webView = reinterpret_cast<WebView*>(longPtr);
2057 WebFrame* mainFrameImpl = webView ? webView->topLevelFrame() : 0;
2058 if (!mainFrameImpl || webView->isBeingDestroyed())
2059 return DefWindowProc(hWnd, message, wParam, lParam);
2061 // hold a ref, since the WebView could go away in an event handler.
2062 COMPtr<WebView> protector(webView);
2065 // Windows Media Player has a modal message loop that will deliver messages
2066 // to us at inappropriate times and we will crash if we handle them when
2067 // they are delivered. We repost paint messages so that we eventually get
2068 // a chance to paint once the modal loop has exited, but other messages
2069 // aren't safe to repost, so we just drop them.
2070 if (PluginView::isCallingPlugin()) {
2071 if (message == WM_PAINT)
2072 PostMessage(hWnd, message, wParam, lParam);
2076 bool handled = true;
2080 webView->paint(0, 0);
2083 case WM_PRINTCLIENT:
2084 webView->paint((HDC)wParam, lParam);
2087 webView->setIsBeingDestroyed();
2090 case WM_GESTURENOTIFY:
2091 handled = webView->gestureNotify(wParam, lParam);
2094 handled = webView->gesture(wParam, lParam);
2097 case WM_LBUTTONDOWN:
2098 case WM_MBUTTONDOWN:
2099 case WM_RBUTTONDOWN:
2100 case WM_LBUTTONDBLCLK:
2101 case WM_MBUTTONDBLCLK:
2102 case WM_RBUTTONDBLCLK:
2108 if (Frame* coreFrame = core(mainFrameImpl))
2109 if (coreFrame->view()->didFirstLayout())
2110 handled = webView->handleMouseEvent(message, wParam, lParam);
2113 case WM_VISTA_MOUSEHWHEEL:
2114 if (Frame* coreFrame = core(mainFrameImpl))
2115 if (coreFrame->view()->didFirstLayout())
2116 handled = webView->mouseWheel(wParam, lParam, message == WM_VISTA_MOUSEHWHEEL);
2119 handled = webView->keyDown(wParam, lParam, true);
2122 handled = webView->keyDown(wParam, lParam);
2125 handled = webView->keyUp(wParam, lParam, true);
2128 handled = webView->keyUp(wParam, lParam);
2131 handled = webView->keyPress(wParam, lParam, true);
2134 handled = webView->keyPress(wParam, lParam);
2136 // FIXME: We need to check WM_UNICHAR to support supplementary characters (that don't fit in 16 bits).
2139 webView->sizeChanged(IntSize(LOWORD(lParam), HIWORD(lParam)));
2142 lResult = DefWindowProc(hWnd, message, wParam, lParam);
2144 // The window is being hidden (e.g., because we switched tabs).
2145 // Null out our backing store.
2146 webView->deleteBackingStore();
2150 COMPtr<IWebUIDelegate> uiDelegate;
2151 COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2152 if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2153 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate)
2154 uiDelegatePrivate->webViewReceivedFocus(webView);
2156 FocusController* focusController = webView->page()->focusController();
2157 if (Frame* frame = focusController->focusedFrame()) {
2158 // Send focus events unless the previously focused window is a
2159 // child of ours (for example a plugin).
2160 if (!IsChild(hWnd, reinterpret_cast<HWND>(wParam)))
2161 focusController->setFocused(true);
2163 focusController->setFocused(true);
2166 case WM_KILLFOCUS: {
2167 COMPtr<IWebUIDelegate> uiDelegate;
2168 COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2169 HWND newFocusWnd = reinterpret_cast<HWND>(wParam);
2170 if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2171 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate)
2172 uiDelegatePrivate->webViewLostFocus(webView, (OLE_HANDLE)(ULONG64)newFocusWnd);
2174 FocusController* focusController = webView->page()->focusController();
2175 Frame* frame = focusController->focusedOrMainFrame();
2176 webView->resetIME(frame);
2177 // Send blur events unless we're losing focus to a child of ours.
2178 if (!IsChild(hWnd, newFocusWnd))
2179 focusController->setFocused(false);
2181 // If we are pan-scrolling when we lose focus, stop the pan scrolling.
2182 frame->eventHandler()->stopAutoscrollTimer();
2186 case WM_WINDOWPOSCHANGED:
2187 if (reinterpret_cast<WINDOWPOS*>(lParam)->flags & SWP_SHOWWINDOW)
2188 webView->updateActiveStateSoon();
2201 webView->delete_(0);
2205 handled = webView->execCommand(wParam, lParam);
2206 else // If the high word of wParam is 0, the message is from a menu
2207 webView->performContextMenuAction(wParam, lParam, false);
2209 case WM_MENUCOMMAND:
2210 webView->performContextMenuAction(wParam, lParam, true);
2212 case WM_CONTEXTMENU:
2213 handled = webView->handleContextMenuEvent(wParam, lParam);
2215 case WM_INITMENUPOPUP:
2216 handled = webView->onInitMenuPopup(wParam, lParam);
2218 case WM_MEASUREITEM:
2219 handled = webView->onMeasureItem(wParam, lParam);
2222 handled = webView->onDrawItem(wParam, lParam);
2224 case WM_UNINITMENUPOPUP:
2225 handled = webView->onUninitMenuPopup(wParam, lParam);
2227 case WM_XP_THEMECHANGED:
2228 if (Frame* coreFrame = core(mainFrameImpl)) {
2229 webView->deleteBackingStore();
2230 coreFrame->page()->theme()->themeChanged();
2231 ScrollbarTheme::nativeTheme()->themeChanged();
2233 ::GetClientRect(hWnd, &windowRect);
2234 ::InvalidateRect(hWnd, &windowRect, false);
2235 #if USE(ACCELERATED_COMPOSITING)
2236 if (webView->isAcceleratedCompositing())
2237 webView->m_backingLayer->setNeedsDisplay();
2241 case WM_MOUSEACTIVATE:
2242 webView->setMouseActivated(true);
2245 case WM_GETDLGCODE: {
2246 COMPtr<IWebUIDelegate> uiDelegate;
2247 COMPtr<IWebUIDelegatePrivate> uiDelegatePrivate;
2248 LONG_PTR dlgCode = 0;
2251 LPMSG lpMsg = (LPMSG)lParam;
2252 if (lpMsg->message == WM_KEYDOWN)
2253 keyCode = (UINT) lpMsg->wParam;
2255 if (SUCCEEDED(webView->uiDelegate(&uiDelegate)) && uiDelegate
2256 && SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegatePrivate, (void**) &uiDelegatePrivate)) && uiDelegatePrivate
2257 && SUCCEEDED(uiDelegatePrivate->webViewGetDlgCode(webView, keyCode, &dlgCode)))
2263 handled = webView->onGetObject(wParam, lParam, lResult);
2265 case WM_IME_STARTCOMPOSITION:
2266 handled = webView->onIMEStartComposition();
2268 case WM_IME_REQUEST:
2269 lResult = webView->onIMERequest(wParam, lParam);
2271 case WM_IME_COMPOSITION:
2272 handled = webView->onIMEComposition(lParam);
2274 case WM_IME_ENDCOMPOSITION:
2275 handled = webView->onIMEEndComposition();
2278 handled = webView->onIMEChar(wParam, lParam);
2281 handled = webView->onIMENotify(wParam, lParam, &lResult);
2284 handled = webView->onIMESelect(wParam, lParam);
2286 case WM_IME_SETCONTEXT:
2287 handled = webView->onIMESetContext(wParam, lParam);
2291 case UpdateActiveStateTimer:
2292 KillTimer(hWnd, UpdateActiveStateTimer);
2293 webView->updateActiveState();
2295 case DeleteBackingStoreTimer:
2296 webView->deleteBackingStore();
2301 handled = ::SetCursor(webView->m_lastSetCursor);
2304 handled = webView->verticalScroll(wParam, lParam);
2307 handled = webView->horizontalScroll(wParam, lParam);
2315 lResult = DefWindowProc(hWnd, message, wParam, lParam);
2317 // Let the client know whether we consider this message handled.
2318 return (message == WM_KEYDOWN || message == WM_SYSKEYDOWN || message == WM_KEYUP || message == WM_SYSKEYUP) ? !handled : lResult;
2321 bool WebView::developerExtrasEnabled() const
2323 if (m_preferences->developerExtrasDisabledByOverride())
2328 return SUCCEEDED(m_preferences->developerExtrasEnabled(&enabled)) && enabled;
2334 static String osVersion()
2337 OSVERSIONINFO versionInfo = {0};
2338 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
2339 GetVersionEx(&versionInfo);
2341 if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
2342 if (versionInfo.dwMajorVersion == 4) {
2343 if (versionInfo.dwMinorVersion == 0)
2344 osVersion = "Windows 95";
2345 else if (versionInfo.dwMinorVersion == 10)
2346 osVersion = "Windows 98";
2347 else if (versionInfo.dwMinorVersion == 90)
2348 osVersion = "Windows 98; Win 9x 4.90";
2350 } else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
2351 osVersion = makeString("Windows NT ", String::number(versionInfo.dwMajorVersion), '.', String::number(versionInfo.dwMinorVersion));
2353 if (!osVersion.length())
2354 osVersion = makeString("Windows ", String::number(versionInfo.dwMajorVersion), '.', String::number(versionInfo.dwMinorVersion));
2358 static String webKitVersion()
2360 String versionStr = "420+";
2363 struct LANGANDCODEPAGE {
2368 TCHAR path[MAX_PATH];
2369 GetModuleFileName(gInstance, path, WTF_ARRAY_LENGTH(path));
2371 DWORD versionSize = GetFileVersionInfoSize(path, &handle);
2374 data = malloc(versionSize);
2377 if (!GetFileVersionInfo(path, 0, versionSize, data))
2380 if (!VerQueryValue(data, TEXT("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate))
2383 _stprintf_s(key, WTF_ARRAY_LENGTH(key), TEXT("\\StringFileInfo\\%04x%04x\\ProductVersion"), lpTranslate[0].wLanguage, lpTranslate[0].wCodePage);
2384 LPCTSTR productVersion;
2385 UINT productVersionLength;
2386 if (!VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&productVersion, &productVersionLength))
2388 versionStr = String(productVersion, productVersionLength - 1);
2396 const String& WebView::userAgentForKURL(const KURL&)
2398 if (m_userAgentOverridden)
2399 return m_userAgentCustom;
2401 if (!m_userAgentStandard.length())
2402 m_userAgentStandard = WebView::standardUserAgentWithApplicationName(m_applicationName);
2403 return m_userAgentStandard;
2406 // IUnknown -------------------------------------------------------------------
2408 HRESULT STDMETHODCALLTYPE WebView::QueryInterface(REFIID riid, void** ppvObject)
2411 if (IsEqualGUID(riid, CLSID_WebView))
2413 else if (IsEqualGUID(riid, IID_IUnknown))
2414 *ppvObject = static_cast<IWebView*>(this);
2415 else if (IsEqualGUID(riid, IID_IWebView))
2416 *ppvObject = static_cast<IWebView*>(this);
2417 else if (IsEqualGUID(riid, IID_IWebViewPrivate))
2418 *ppvObject = static_cast<IWebViewPrivate*>(this);
2419 else if (IsEqualGUID(riid, IID_IWebIBActions))
2420 *ppvObject = static_cast<IWebIBActions*>(this);
2421 else if (IsEqualGUID(riid, IID_IWebViewCSS))
2422 *ppvObject = static_cast<IWebViewCSS*>(this);
2423 else if (IsEqualGUID(riid, IID_IWebViewEditing))
2424 *ppvObject = static_cast<IWebViewEditing*>(this);
2425 else if (IsEqualGUID(riid, IID_IWebViewUndoableEditing))
2426 *ppvObject = static_cast<IWebViewUndoableEditing*>(this);
2427 else if (IsEqualGUID(riid, IID_IWebViewEditingActions))
2428 *ppvObject = static_cast<IWebViewEditingActions*>(this);
2429 else if (IsEqualGUID(riid, IID_IWebNotificationObserver))
2430 *ppvObject = static_cast<IWebNotificationObserver*>(this);
2431 else if (IsEqualGUID(riid, IID_IDropTarget))
2432 *ppvObject = static_cast<IDropTarget*>(this);
2434 return E_NOINTERFACE;
2440 ULONG STDMETHODCALLTYPE WebView::AddRef(void)
2442 ASSERT(!m_deletionHasBegun);
2443 return ++m_refCount;
2446 ULONG STDMETHODCALLTYPE WebView::Release(void)
2448 ASSERT(!m_deletionHasBegun);
2450 if (m_refCount == 1) {
2451 // Call close() now so that clients don't have to. (It's harmless to call close() multiple
2452 // times.) We do this here instead of in our destructor because close() can cause AddRef()
2453 // and Release() to be called, and if that happened in our destructor we would be destroyed
2458 ULONG newRef = --m_refCount;
2460 #if !ASSERT_DISABLED
2461 m_deletionHasBegun = true;
2469 // IWebView --------------------------------------------------------------------
2471 HRESULT STDMETHODCALLTYPE WebView::canShowMIMEType(
2472 /* [in] */ BSTR mimeType,
2473 /* [retval][out] */ BOOL* canShow)
2475 String mimeTypeStr(mimeType, SysStringLen(mimeType));
2480 *canShow = MIMETypeRegistry::isSupportedImageMIMEType(mimeTypeStr) ||
2481 MIMETypeRegistry::isSupportedNonImageMIMEType(mimeTypeStr) ||
2482 (m_page && m_page->pluginData() && m_page->pluginData()->supportsMimeType(mimeTypeStr)) ||
2483 shouldUseEmbeddedView(mimeTypeStr);
2488 HRESULT STDMETHODCALLTYPE WebView::canShowMIMETypeAsHTML(
2489 /* [in] */ BSTR /*mimeType*/,
2490 /* [retval][out] */ BOOL* canShow)
2497 HRESULT STDMETHODCALLTYPE WebView::MIMETypesShownAsHTML(
2498 /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
2500 ASSERT_NOT_REACHED();
2504 HRESULT STDMETHODCALLTYPE WebView::setMIMETypesShownAsHTML(
2505 /* [size_is][in] */ BSTR* /*mimeTypes*/,
2506 /* [in] */ int /*cMimeTypes*/)
2508 ASSERT_NOT_REACHED();
2512 HRESULT STDMETHODCALLTYPE WebView::URLFromPasteboard(
2513 /* [in] */ IDataObject* /*pasteboard*/,
2514 /* [retval][out] */ BSTR* /*url*/)
2516 ASSERT_NOT_REACHED();
2520 HRESULT STDMETHODCALLTYPE WebView::URLTitleFromPasteboard(
2521 /* [in] */ IDataObject* /*pasteboard*/,
2522 /* [retval][out] */ BSTR* /*urlTitle*/)
2524 ASSERT_NOT_REACHED();
2528 static void WebKitSetApplicationCachePathIfNecessary()
2530 static bool initialized = false;
2534 String path = localUserSpecificStorageDirectory();
2536 cacheStorage().setCacheDirectory(path);
2541 bool WebView::shouldInitializeTrackPointHack()
2543 static bool shouldCreateScrollbars;
2544 static bool hasRunTrackPointCheck;
2546 if (hasRunTrackPointCheck)
2547 return shouldCreateScrollbars;
2549 hasRunTrackPointCheck = true;
2550 const TCHAR trackPointKeys[][50] = { TEXT("Software\\Lenovo\\TrackPoint"),
2551 TEXT("Software\\Lenovo\\UltraNav"),
2552 TEXT("Software\\Alps\\Apoint\\TrackPoint"),
2553 TEXT("Software\\Synaptics\\SynTPEnh\\UltraNavUSB"),
2554 TEXT("Software\\Synaptics\\SynTPEnh\\UltraNavPS2") };
2556 for (int i = 0; i < 5; ++i) {
2558 int readKeyResult = ::RegOpenKeyEx(HKEY_CURRENT_USER, trackPointKeys[i], 0, KEY_READ, &trackPointKey);
2559 ::RegCloseKey(trackPointKey);
2560 if (readKeyResult == ERROR_SUCCESS) {
2561 shouldCreateScrollbars = true;
2562 return shouldCreateScrollbars;
2566 return shouldCreateScrollbars;
2569 HRESULT STDMETHODCALLTYPE WebView::initWithFrame(
2570 /* [in] */ RECT frame,
2571 /* [in] */ BSTR frameName,
2572 /* [in] */ BSTR groupName)
2579 registerWebViewWindowClass();
2581 m_viewWindow = CreateWindowEx(0, kWebViewWindowClassName, 0, WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
2582 frame.left, frame.top, frame.right - frame.left, frame.bottom - frame.top, m_hostWindow ? m_hostWindow : HWND_MESSAGE, 0, gInstance, 0);
2583 ASSERT(::IsWindow(m_viewWindow));
2585 if (shouldInitializeTrackPointHack()) {
2586 // If we detected a registry key belonging to a TrackPoint driver, then create fake trackpoint
2587 // scrollbars, so the WebView will receive WM_VSCROLL and WM_HSCROLL messages. We create one
2588 // vertical scrollbar and one horizontal to allow for receiving both types of messages.
2589 ::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTHSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_HORZ, 0, 0, 0, 0, m_viewWindow, 0, gInstance, 0);
2590 ::CreateWindow(TEXT("SCROLLBAR"), TEXT("FAKETRACKPOINTVSCROLLBAR"), WS_CHILD | WS_VISIBLE | SBS_VERT, 0, 0, 0, 0, m_viewWindow, 0, gInstance, 0);
2593 hr = registerDragDrop();
2597 WebPreferences* sharedPreferences = WebPreferences::sharedStandardPreferences();
2598 sharedPreferences->willAddToWebView();
2599 m_preferences = sharedPreferences;
2601 static bool didOneTimeInitialization;
2602 if (!didOneTimeInitialization) {
2603 InitializeLoggingChannelsIfNecessary();
2604 #if ENABLE(DATABASE)
2605 WebKitInitializeWebDatabasesIfNecessary();
2607 WebKitSetApplicationCachePathIfNecessary();
2608 WebPlatformStrategies::initialize();
2609 Settings::setMinDOMTimerInterval(0.004);
2611 didOneTimeInitialization = true;
2614 #if USE(SAFARI_THEME)
2615 BOOL shouldPaintNativeControls;
2616 if (SUCCEEDED(m_preferences->shouldPaintNativeControls(&shouldPaintNativeControls)))
2617 Settings::setShouldPaintNativeControls(shouldPaintNativeControls);
2620 BOOL useHighResolutionTimer;
2621 if (SUCCEEDED(m_preferences->shouldUseHighResolutionTimers(&useHighResolutionTimer)))
2622 Settings::setShouldUseHighResolutionTimers(useHighResolutionTimer);
2624 Page::PageClients pageClients;
2625 pageClients.chromeClient = new WebChromeClient(this);
2626 pageClients.contextMenuClient = new WebContextMenuClient(this);
2627 pageClients.editorClient = new WebEditorClient(this);
2628 pageClients.dragClient = new WebDragClient(this);
2629 pageClients.inspectorClient = new WebInspectorClient(this);
2630 pageClients.pluginHalterClient = new WebPluginHalterClient(this);
2631 #if ENABLE(CLIENT_BASED_GEOLOCATION)
2632 pageClients.geolocationClient = new WebGeolocationClient(this);
2634 m_page = new Page(pageClients);
2636 BSTR localStoragePath;
2637 if (SUCCEEDED(m_preferences->localStorageDatabasePath(&localStoragePath))) {
2638 m_page->settings()->setLocalStorageDatabasePath(String(localStoragePath, SysStringLen(localStoragePath)));
2639 SysFreeString(localStoragePath);
2644 if (SUCCEEDED(m_uiDelegate->ftpDirectoryTemplatePath(this, &path))) {
2645 m_page->settings()->setFTPDirectoryTemplatePath(String(path, SysStringLen(path)));
2646 SysFreeString(path);
2650 WebFrame* webFrame = WebFrame::createInstance();
2651 RefPtr<Frame> coreFrame = webFrame->init(this, m_page, 0);
2652 m_mainFrame = webFrame;
2653 webFrame->Release(); // The WebFrame is owned by the Frame, so release our reference to it.
2655 coreFrame->tree()->setName(String(frameName, SysStringLen(frameName)));
2657 setGroupName(groupName);
2659 addToAllWebViewsSet();
2661 #pragma warning(suppress: 4244)
2662 SetWindowLongPtr(m_viewWindow, 0, (LONG_PTR)this);
2663 ShowWindow(m_viewWindow, SW_SHOW);
2665 initializeToolTipWindow();
2666 windowAncestryDidChange();
2668 IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
2669 notifyCenter->addObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
2670 m_preferences->postPreferencesChangesNotification();
2672 setSmartInsertDeleteEnabled(TRUE);
2676 static bool initCommonControls()
2678 static bool haveInitialized = false;
2679 if (haveInitialized)
2682 INITCOMMONCONTROLSEX init;
2683 init.dwSize = sizeof(init);
2684 init.dwICC = ICC_TREEVIEW_CLASSES;
2685 haveInitialized = !!::InitCommonControlsEx(&init);
2686 return haveInitialized;
2689 void WebView::initializeToolTipWindow()
2691 if (!initCommonControls())
2694 m_toolTipHwnd = CreateWindowEx(WS_EX_TRANSPARENT, TOOLTIPS_CLASS, 0, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
2695 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2696 m_viewWindow, 0, 0, 0);
2700 TOOLINFO info = {0};
2701 info.cbSize = sizeof(info);
2702 info.uFlags = TTF_IDISHWND | TTF_SUBCLASS ;
2703 info.uId = reinterpret_cast<UINT_PTR>(m_viewWindow);
2705 ::SendMessage(m_toolTipHwnd, TTM_ADDTOOL, 0, reinterpret_cast<LPARAM>(&info));
2706 ::SendMessage(m_toolTipHwnd, TTM_SETMAXTIPWIDTH, 0, maxToolTipWidth);
2708 ::SetWindowPos(m_toolTipHwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
2711 void WebView::setToolTip(const String& toolTip)
2716 if (toolTip == m_toolTip)
2719 m_toolTip = toolTip;
2721 if (!m_toolTip.isEmpty()) {
2722 TOOLINFO info = {0};
2723 info.cbSize = sizeof(info);
2724 info.uFlags = TTF_IDISHWND;
2725 info.uId = reinterpret_cast<UINT_PTR>(m_viewWindow);
2726 info.lpszText = const_cast<UChar*>(m_toolTip.charactersWithNullTermination());
2727 ::SendMessage(m_toolTipHwnd, TTM_UPDATETIPTEXT, 0, reinterpret_cast<LPARAM>(&info));
2730 ::SendMessage(m_toolTipHwnd, TTM_ACTIVATE, !m_toolTip.isEmpty(), 0);
2733 HRESULT WebView::notifyDidAddIcon(IWebNotification* notification)
2735 COMPtr<IPropertyBag> propertyBag;
2736 HRESULT hr = notification->userInfo(&propertyBag);
2742 COMPtr<CFDictionaryPropertyBag> dictionaryPropertyBag;
2743 hr = propertyBag->QueryInterface(&dictionaryPropertyBag);
2747 CFDictionaryRef dictionary = dictionaryPropertyBag->dictionary();
2751 CFTypeRef value = CFDictionaryGetValue(dictionary, WebIconDatabase::iconDatabaseNotificationUserInfoURLKey());
2754 if (CFGetTypeID(value) != CFStringGetTypeID())
2757 String mainFrameURL;
2759 mainFrameURL = m_mainFrame->url().string();
2761 if (!mainFrameURL.isEmpty() && mainFrameURL == String((CFStringRef)value))
2762 dispatchDidReceiveIconFromWebFrame(m_mainFrame);
2767 void WebView::registerForIconNotification(bool listen)
2769 IWebNotificationCenter* nc = WebNotificationCenter::defaultCenterInternal();
2771 nc->addObserver(this, WebIconDatabase::iconDatabaseDidAddIconNotification(), 0);
2773 nc->removeObserver(this, WebIconDatabase::iconDatabaseDidAddIconNotification(), 0);
2776 void WebView::dispatchDidReceiveIconFromWebFrame(WebFrame* frame)
2778 registerForIconNotification(false);
2780 if (m_frameLoadDelegate)
2781 // FIXME: <rdar://problem/5491010> - Pass in the right HBITMAP.
2782 m_frameLoadDelegate->didReceiveIcon(this, 0, frame);
2785 HRESULT STDMETHODCALLTYPE WebView::setUIDelegate(
2786 /* [in] */ IWebUIDelegate* d)
2790 if (m_uiDelegatePrivate)
2791 m_uiDelegatePrivate = 0;
2794 if (FAILED(d->QueryInterface(IID_IWebUIDelegatePrivate, (void**)&m_uiDelegatePrivate)))
2795 m_uiDelegatePrivate = 0;
2801 HRESULT STDMETHODCALLTYPE WebView::uiDelegate(
2802 /* [out][retval] */ IWebUIDelegate** d)
2807 return m_uiDelegate.copyRefTo(d);
2810 HRESULT STDMETHODCALLTYPE WebView::setResourceLoadDelegate(
2811 /* [in] */ IWebResourceLoadDelegate* d)
2813 m_resourceLoadDelegate = d;
2817 HRESULT STDMETHODCALLTYPE WebView::resourceLoadDelegate(
2818 /* [out][retval] */ IWebResourceLoadDelegate** d)
2820 if (!m_resourceLoadDelegate)
2823 return m_resourceLoadDelegate.copyRefTo(d);
2826 HRESULT STDMETHODCALLTYPE WebView::setDownloadDelegate(
2827 /* [in] */ IWebDownloadDelegate* d)
2829 m_downloadDelegate = d;
2833 HRESULT STDMETHODCALLTYPE WebView::downloadDelegate(
2834 /* [out][retval] */ IWebDownloadDelegate** d)
2836 if (!m_downloadDelegate)
2839 return m_downloadDelegate.copyRefTo(d);
2842 HRESULT STDMETHODCALLTYPE WebView::setFrameLoadDelegate(
2843 /* [in] */ IWebFrameLoadDelegate* d)
2845 m_frameLoadDelegate = d;
2849 HRESULT STDMETHODCALLTYPE WebView::frameLoadDelegate(
2850 /* [out][retval] */ IWebFrameLoadDelegate** d)
2852 if (!m_frameLoadDelegate)
2855 return m_frameLoadDelegate.copyRefTo(d);
2858 HRESULT STDMETHODCALLTYPE WebView::setPolicyDelegate(
2859 /* [in] */ IWebPolicyDelegate* d)
2861 m_policyDelegate = d;
2865 HRESULT STDMETHODCALLTYPE WebView::policyDelegate(
2866 /* [out][retval] */ IWebPolicyDelegate** d)
2868 if (!m_policyDelegate)
2870 return m_policyDelegate.copyRefTo(d);
2873 HRESULT STDMETHODCALLTYPE WebView::mainFrame(
2874 /* [out][retval] */ IWebFrame** frame)
2877 ASSERT_NOT_REACHED();
2881 *frame = m_mainFrame;
2885 m_mainFrame->AddRef();
2889 HRESULT STDMETHODCALLTYPE WebView::focusedFrame(
2890 /* [out][retval] */ IWebFrame** frame)
2893 ASSERT_NOT_REACHED();
2898 Frame* f = m_page->focusController()->focusedFrame();
2902 WebFrame* webFrame = kit(f);
2906 return webFrame->QueryInterface(IID_IWebFrame, (void**) frame);
2909 HRESULT STDMETHODCALLTYPE WebView::backForwardList(
2910 /* [out][retval] */ IWebBackForwardList** list)
2912 if (!m_useBackForwardList)
2915 *list = WebBackForwardList::createInstance(static_cast<WebCore::BackForwardListImpl*>(m_page->backForwardList()));
2920 HRESULT STDMETHODCALLTYPE WebView::setMaintainsBackForwardList(
2921 /* [in] */ BOOL flag)
2923 m_useBackForwardList = !!flag;
2927 HRESULT STDMETHODCALLTYPE WebView::goBack(
2928 /* [retval][out] */ BOOL* succeeded)
2930 *succeeded = m_page->goBack();
2934 HRESULT STDMETHODCALLTYPE WebView::goForward(
2935 /* [retval][out] */ BOOL* succeeded)
2937 *succeeded = m_page->goForward();
2941 HRESULT STDMETHODCALLTYPE WebView::goToBackForwardItem(
2942 /* [in] */ IWebHistoryItem* item,
2943 /* [retval][out] */ BOOL* succeeded)
2947 COMPtr<WebHistoryItem> webHistoryItem;
2948 HRESULT hr = item->QueryInterface(&webHistoryItem);
2952 m_page->goToItem(webHistoryItem->historyItem(), FrameLoadTypeIndexedBackForward);
2958 HRESULT STDMETHODCALLTYPE WebView::setTextSizeMultiplier(
2959 /* [in] */ float multiplier)
2963 setZoomMultiplier(multiplier, true);
2967 HRESULT STDMETHODCALLTYPE WebView::setPageSizeMultiplier(
2968 /* [in] */ float multiplier)
2972 setZoomMultiplier(multiplier, false);
2976 void WebView::setZoomMultiplier(float multiplier, bool isTextOnly)
2978 m_zoomMultiplier = multiplier;
2979 m_zoomsTextOnly = isTextOnly;
2981 if (Frame* coreFrame = core(m_mainFrame)) {
2982 if (m_zoomsTextOnly)
2983 coreFrame->setPageAndTextZoomFactors(1, multiplier);
2985 coreFrame->setPageAndTextZoomFactors(multiplier, 1);
2989 HRESULT STDMETHODCALLTYPE WebView::textSizeMultiplier(
2990 /* [retval][out] */ float* multiplier)
2992 *multiplier = zoomMultiplier(true);
2996 HRESULT STDMETHODCALLTYPE WebView::pageSizeMultiplier(
2997 /* [retval][out] */ float* multiplier)
2999 *multiplier = zoomMultiplier(false);
3003 float WebView::zoomMultiplier(bool isTextOnly)
3005 if (isTextOnly != m_zoomsTextOnly)
3007 return m_zoomMultiplier;
3010 HRESULT STDMETHODCALLTYPE WebView::setApplicationNameForUserAgent(
3011 /* [in] */ BSTR applicationName)
3013 m_applicationName = String(applicationName, SysStringLen(applicationName));
3014 m_userAgentStandard = String();
3018 HRESULT STDMETHODCALLTYPE WebView::applicationNameForUserAgent(
3019 /* [retval][out] */ BSTR* applicationName)
3021 *applicationName = SysAllocStringLen(m_applicationName.characters(), m_applicationName.length());
3022 if (!*applicationName && m_applicationName.length())
3023 return E_OUTOFMEMORY;
3027 HRESULT STDMETHODCALLTYPE WebView::setCustomUserAgent(
3028 /* [in] */ BSTR userAgentString)
3030 m_userAgentOverridden = userAgentString;
3031 m_userAgentCustom = String(userAgentString, SysStringLen(userAgentString));
3035 HRESULT STDMETHODCALLTYPE WebView::customUserAgent(
3036 /* [retval][out] */ BSTR* userAgentString)
3038 *userAgentString = 0;
3039 if (!m_userAgentOverridden)
3041 *userAgentString = SysAllocStringLen(m_userAgentCustom.characters(), m_userAgentCustom.length());
3042 if (!*userAgentString && m_userAgentCustom.length())
3043 return E_OUTOFMEMORY;
3047 HRESULT STDMETHODCALLTYPE WebView::userAgentForURL(
3048 /* [in] */ BSTR url,
3049 /* [retval][out] */ BSTR* userAgent)
3051 String userAgentString = userAgentForKURL(MarshallingHelpers::BSTRToKURL(url));
3052 *userAgent = SysAllocStringLen(userAgentString.characters(), userAgentString.length());
3053 if (!*userAgent && userAgentString.length())
3054 return E_OUTOFMEMORY;
3058 HRESULT STDMETHODCALLTYPE WebView::supportsTextEncoding(
3059 /* [retval][out] */ BOOL* supports)
3065 HRESULT STDMETHODCALLTYPE WebView::setCustomTextEncodingName(
3066 /* [in] */ BSTR encodingName)
3073 hr = customTextEncodingName(&oldEncoding);
3077 if (oldEncoding != encodingName && (!oldEncoding || !encodingName || _tcscmp(oldEncoding, encodingName))) {
3078 if (Frame* coreFrame = core(m_mainFrame))
3079 coreFrame->loader()->reloadWithOverrideEncoding(String(encodingName, SysStringLen(encodingName)));
3085 HRESULT STDMETHODCALLTYPE WebView::customTextEncodingName(
3086 /* [retval][out] */ BSTR* encodingName)
3089 COMPtr<IWebDataSource> dataSource;
3090 COMPtr<WebDataSource> dataSourceImpl;
3096 if (FAILED(m_mainFrame->provisionalDataSource(&dataSource)) || !dataSource) {
3097 hr = m_mainFrame->dataSource(&dataSource);
3098 if (FAILED(hr) || !dataSource)
3102 hr = dataSource->QueryInterface(IID_WebDataSource, (void**)&dataSourceImpl);
3106 BString str = dataSourceImpl->documentLoader()->overrideEncoding();
3111 *encodingName = SysAllocStringLen(m_overrideEncoding.characters(), m_overrideEncoding.length());
3113 if (!*encodingName && m_overrideEncoding.length())
3114 return E_OUTOFMEMORY;
3119 HRESULT STDMETHODCALLTYPE WebView::setMediaStyle(
3120 /* [in] */ BSTR /*media*/)
3122 ASSERT_NOT_REACHED();
3126 HRESULT STDMETHODCALLTYPE WebView::mediaStyle(
3127 /* [retval][out] */ BSTR* /*media*/)
3129 ASSERT_NOT_REACHED();
3133 HRESULT STDMETHODCALLTYPE WebView::stringByEvaluatingJavaScriptFromString(
3134 /* [in] */ BSTR script, // assumes input does not have "JavaScript" at the begining.
3135 /* [retval][out] */ BSTR* result)
3138 ASSERT_NOT_REACHED();
3144 Frame* coreFrame = core(m_mainFrame);
3148 JSC::JSValue scriptExecutionResult = coreFrame->script()->executeScript(WTF::String(script), true).jsValue();
3149 if (!scriptExecutionResult)
3151 else if (scriptExecutionResult.isString()) {
3152 JSLock lock(JSC::SilenceAssertionsOnly);
3153 JSC::ExecState* exec = coreFrame->script()->globalObject(mainThreadNormalWorld())->globalExec();
3154 *result = BString(ustringToString(scriptExecutionResult.getString(exec)));
3160 HRESULT STDMETHODCALLTYPE WebView::windowScriptObject(
3161 /* [retval][out] */ IWebScriptObject** /*webScriptObject*/)
3163 ASSERT_NOT_REACHED();
3167 HRESULT STDMETHODCALLTYPE WebView::setPreferences(
3168 /* [in] */ IWebPreferences* prefs)
3171 prefs = WebPreferences::sharedStandardPreferences();
3173 if (m_preferences == prefs)
3176 COMPtr<WebPreferences> webPrefs(Query, prefs);
3178 return E_NOINTERFACE;
3179 webPrefs->willAddToWebView();
3181 COMPtr<WebPreferences> oldPrefs = m_preferences;
3183 IWebNotificationCenter* nc = WebNotificationCenter::defaultCenterInternal();
3184 nc->removeObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
3186 BSTR identifier = 0;
3187 oldPrefs->identifier(&identifier);
3188 oldPrefs->didRemoveFromWebView();
3189 oldPrefs = 0; // Make sure we release the reference, since WebPreferences::removeReferenceForIdentifier will check for last reference to WebPreferences
3191 m_preferences = webPrefs;
3194 WebPreferences::removeReferenceForIdentifier(identifier);
3195 SysFreeString(identifier);
3198 nc->addObserver(this, WebPreferences::webPreferencesChangedNotification(), static_cast<IWebPreferences*>(m_preferences.get()));
3200 m_preferences->postPreferencesChangesNotification();
3205 HRESULT STDMETHODCALLTYPE WebView::preferences(
3206 /* [retval][out] */ IWebPreferences** prefs)
3210 *prefs = m_preferences.get();
3212 m_preferences->AddRef();
3216 HRESULT STDMETHODCALLTYPE WebView::setPreferencesIdentifier(
3217 /* [in] */ BSTR /*anIdentifier*/)
3219 ASSERT_NOT_REACHED();
3223 HRESULT STDMETHODCALLTYPE WebView::preferencesIdentifier(
3224 /* [retval][out] */ BSTR* /*anIdentifier*/)
3226 ASSERT_NOT_REACHED();
3230 static void systemParameterChanged(WPARAM parameter)
3233 if (parameter == SPI_SETFONTSMOOTHING || parameter == SPI_SETFONTSMOOTHINGTYPE || parameter == SPI_SETFONTSMOOTHINGCONTRAST || parameter == SPI_SETFONTSMOOTHINGORIENTATION)
3234 wkSystemFontSmoothingChanged();
3238 void WebView::windowReceivedMessage(HWND, UINT message, WPARAM wParam, LPARAM)
3242 updateActiveStateSoon();
3244 deleteBackingStoreSoon();
3246 case WM_SETTINGCHANGE:
3247 systemParameterChanged(wParam);
3252 void WebView::updateActiveStateSoon() const
3254 // This function is called while processing the WM_NCACTIVATE message.
3255 // While processing WM_NCACTIVATE when we are being deactivated, GetActiveWindow() will
3256 // still return our window. If we were to call updateActiveState() in that case, we would
3257 // wrongly think that we are still the active window. To work around this, we update our
3258 // active state after a 0-delay timer fires, at which point GetActiveWindow() will return
3259 // the newly-activated window.
3261 SetTimer(m_viewWindow, UpdateActiveStateTimer, 0, 0);
3264 void WebView::deleteBackingStoreSoon()
3266 if (pendingDeleteBackingStoreSet.size() > 2) {
3267 Vector<WebView*> views;
3268 HashSet<WebView*>::iterator end = pendingDeleteBackingStoreSet.end();
3269 for (HashSet<WebView*>::iterator it = pendingDeleteBackingStoreSet.begin(); it != end; ++it)
3271 for (int i = 0; i < views.size(); ++i)
3272 views[i]->deleteBackingStore();
3273 ASSERT(pendingDeleteBackingStoreSet.isEmpty());
3276 pendingDeleteBackingStoreSet.add(this);
3277 m_deleteBackingStoreTimerActive = true;
3278 SetTimer(m_viewWindow, DeleteBackingStoreTimer, delayBeforeDeletingBackingStoreMsec, 0);
3281 void WebView::cancelDeleteBackingStoreSoon()
3283 if (!m_deleteBackingStoreTimerActive)
3285 pendingDeleteBackingStoreSet.remove(this);
3286 m_deleteBackingStoreTimerActive = false;
3287 KillTimer(m_viewWindow, DeleteBackingStoreTimer);
3290 HRESULT STDMETHODCALLTYPE WebView::setHostWindow(
3291 /* [in] */ OLE_HANDLE oleWindow)
3293 HWND window = (HWND)(ULONG64)oleWindow;
3296 SetParent(m_viewWindow, window);
3297 else if (!isBeingDestroyed()) {
3298 // Turn the WebView into a message-only window so it will no longer be a child of the
3299 // old host window and will be hidden from screen. We only do this when
3300 // isBeingDestroyed() is false because doing this while handling WM_DESTROY can leave
3301 // m_viewWindow in a weird state (see <http://webkit.org/b/29337>).
3302 SetParent(m_viewWindow, HWND_MESSAGE);
3306 m_hostWindow = window;
3308 windowAncestryDidChange();
3313 HRESULT STDMETHODCALLTYPE WebView::hostWindow(
3314 /* [retval][out] */ OLE_HANDLE* window)
3316 *window = (OLE_HANDLE)(ULONG64)m_hostWindow;
3321 static Frame *incrementFrame(Frame *curr, bool forward, bool wrapFlag)
3324 ? curr->tree()->traverseNextWithWrap(wrapFlag)
3325 : curr->tree()->traversePreviousWithWrap(wrapFlag);
3328 HRESULT STDMETHODCALLTYPE WebView::searchFor(
3329 /* [in] */ BSTR str,
3330 /* [in] */ BOOL forward,
3331 /* [in] */ BOOL caseFlag,
3332 /* [in] */ BOOL wrapFlag,
3333 /* [retval][out] */ BOOL* found)
3336 return E_INVALIDARG;
3338 if (!m_page || !m_page->mainFrame())
3339 return E_UNEXPECTED;
3341 if (!str || !SysStringLen(str))
3342 return E_INVALIDARG;
3344 *found = m_page->findString(String(str, SysStringLen(str)), caseFlag ? TextCaseSensitive : TextCaseInsensitive, forward ? FindDirectionForward : FindDirectionBackward, wrapFlag);
3348 bool WebView::active()
3350 HWND activeWindow = GetActiveWindow();
3351 return (activeWindow && m_topLevelParent == findTopLevelParent(activeWindow));
3354 void WebView::updateActiveState()
3356 m_page->focusController()->setActive(active());
3359 HRESULT STDMETHODCALLTYPE WebView::updateFocusedAndActiveState()
3361 updateActiveState();
3363 bool active = m_page->focusController()->isActive();
3364 Frame* mainFrame = m_page->mainFrame();
3365 Frame* focusedFrame = m_page->focusController()->focusedOrMainFrame();
3366 mainFrame->selection()->setFocused(active && mainFrame == focusedFrame);
3371 HRESULT STDMETHODCALLTYPE WebView::executeCoreCommandByName(BSTR bName, BSTR bValue)
3373 String name(bName, SysStringLen(bName));
3374 String value(bValue, SysStringLen(bValue));
3376 m_page->focusController()->focusedOrMainFrame()->editor()->command(name).execute(value);
3381 HRESULT STDMETHODCALLTYPE WebView::clearMainFrameName()
3383 m_page->mainFrame()->tree()->clearName();
3388 HRESULT STDMETHODCALLTYPE WebView::markAllMatchesForText(
3389 BSTR str, BOOL caseSensitive, BOOL highlight, UINT limit, UINT* matches)
3392 return E_INVALIDARG;
3394 if (!m_page || !m_page->mainFrame())
3395 return E_UNEXPECTED;
3397 if (!str || !SysStringLen(str))
3398 return E_INVALIDARG;
3400 *matches = m_page->markAllMatchesForText(String(str, SysStringLen(str)), caseSensitive ? TextCaseSensitive : TextCaseInsensitive, highlight, limit);
3404 HRESULT STDMETHODCALLTYPE WebView::unmarkAllTextMatches()
3406 if (!m_page || !m_page->mainFrame())
3407 return E_UNEXPECTED;
3409 m_page->unmarkAllTextMatches();
3413 HRESULT STDMETHODCALLTYPE WebView::rectsForTextMatches(
3414 IEnumTextMatches** pmatches)
3416 Vector<IntRect> allRects;
3417 WebCore::Frame* frame = m_page->mainFrame();
3419 if (Document* document = frame->document()) {
3420 IntRect visibleRect = frame->view()->visibleContentRect();
3421 Vector<IntRect> frameRects = document->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
3422 IntPoint frameOffset(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
3423 frameOffset = frame->view()->convertToContainingWindow(frameOffset);
3425 Vector<IntRect>::iterator end = frameRects.end();
3426 for (Vector<IntRect>::iterator it = frameRects.begin(); it < end; it++) {
3427 it->intersect(visibleRect);
3428 it->move(frameOffset.x(), frameOffset.y());
3429 allRects.append(*it);
3432 frame = incrementFrame(frame, true, false);
3435 return createMatchEnumerator(&allRects, pmatches);
3438 HRESULT STDMETHODCALLTYPE WebView::generateSelectionImage(BOOL forceWhiteText, OLE_HANDLE* hBitmap)
3442 WebCore::Frame* frame = m_page->focusController()->focusedOrMainFrame();
3445 HBITMAP bitmap = imageFromSelection(frame, forceWhiteText ? TRUE : FALSE);
3446 *hBitmap = (OLE_HANDLE)(ULONG64)bitmap;
3452 HRESULT STDMETHODCALLTYPE WebView::selectionRect(RECT* rc)
3454 WebCore::Frame* frame = m_page->focusController()->focusedOrMainFrame();
3457 IntRect ir = enclosingIntRect(frame->selection()->bounds());
3458 ir = frame->view()->convertToContainingWindow(ir);
3459 ir.move(-frame->view()->scrollOffset().width(), -frame->view()->scrollOffset().height());
3462 rc->bottom = rc->top + ir.height();
3463 rc->right = rc->left + ir.width();
3469 HRESULT STDMETHODCALLTYPE WebView::registerViewClass(
3470 /* [in] */ IWebDocumentView* /*view*/,
3471 /* [in] */ IWebDocumentRepresentation* /*representation*/,
3472 /* [in] */ BSTR /*forMIMEType*/)
3474 ASSERT_NOT_REACHED();
3478 HRESULT STDMETHODCALLTYPE WebView::setGroupName(
3479 /* [in] */ BSTR groupName)
3483 m_page->setGroupName(String(groupName, SysStringLen(groupName)));
3487 HRESULT STDMETHODCALLTYPE WebView::groupName(
3488 /* [retval][out] */ BSTR* groupName)
3493 String groupNameString = m_page->groupName();
3494 *groupName = SysAllocStringLen(groupNameString.characters(), groupNameString.length());
3495 if (!*groupName && groupNameString.length())
3496 return E_OUTOFMEMORY;
3500 HRESULT STDMETHODCALLTYPE WebView::estimatedProgress(
3501 /* [retval][out] */ double* estimatedProgress)
3503 *estimatedProgress = m_page->progress()->estimatedProgress();
3507 HRESULT STDMETHODCALLTYPE WebView::isLoading(
3508 /* [retval][out] */ BOOL* isLoading)
3510 COMPtr<IWebDataSource> dataSource;
3511 COMPtr<IWebDataSource> provisionalDataSource;
3521 if (SUCCEEDED(m_mainFrame->dataSource(&dataSource)))
3522 dataSource->isLoading(isLoading);
3527 if (SUCCEEDED(m_mainFrame->provisionalDataSource(&provisionalDataSource)))
3528 provisionalDataSource->isLoading(isLoading);
3532 HRESULT STDMETHODCALLTYPE WebView::elementAtPoint(
3533 /* [in] */ LPPOINT point,
3534 /* [retval][out] */ IPropertyBag** elementDictionary)
3536 if (!elementDictionary) {
3537 ASSERT_NOT_REACHED();
3541 *elementDictionary = 0;
3543 Frame* frame = core(m_mainFrame);
3547 IntPoint webCorePoint = IntPoint(point->x, point->y);
3548 HitTestResult result = HitTestResult(webCorePoint);
3549 if (frame->contentRenderer())
3550 result = frame->eventHandler()->hitTestResultAtPoint(webCorePoint, false);
3551 *elementDictionary = WebElementPropertyBag::createInstance(result);
3555 HRESULT STDMETHODCALLTYPE WebView::pasteboardTypesForSelection(
3556 /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
3558 ASSERT_NOT_REACHED();
3562 HRESULT STDMETHODCALLTYPE WebView::writeSelectionWithPasteboardTypes(
3563 /* [size_is][in] */ BSTR* /*types*/,
3564 /* [in] */ int /*cTypes*/,
3565 /* [in] */ IDataObject* /*pasteboard*/)
3567 ASSERT_NOT_REACHED();
3571 HRESULT STDMETHODCALLTYPE WebView::pasteboardTypesForElement(
3572 /* [in] */ IPropertyBag* /*elementDictionary*/,
3573 /* [retval][out] */ IEnumVARIANT** /*enumVariant*/)
3575 ASSERT_NOT_REACHED();
3579 HRESULT STDMETHODCALLTYPE WebView::writeElement(
3580 /* [in] */ IPropertyBag* /*elementDictionary*/,
3581 /* [size_is][in] */ BSTR* /*withPasteboardTypes*/,
3582 /* [in] */ int /*cWithPasteboardTypes*/,
3583 /* [in] */ IDataObject* /*pasteboard*/)
3585 ASSERT_NOT_REACHED();
3589 HRESULT STDMETHODCALLTYPE WebView::selectedText(
3590 /* [out, retval] */ BSTR* text)
3593 ASSERT_NOT_REACHED();
3599 Frame* focusedFrame = (m_page && m_page->focusController()) ? m_page->focusController()->focusedOrMainFrame() : 0;
3603 String frameSelectedText = focusedFrame->editor()->selectedText();
3604 *text = SysAllocStringLen(frameSelectedText.characters(), frameSelectedText.length());
3605 if (!*text && frameSelectedText.length())
3606 return E_OUTOFMEMORY;
3610 HRESULT STDMETHODCALLTYPE WebView::centerSelectionInVisibleArea(
3611 /* [in] */ IUnknown* /* sender */)
3613 Frame* coreFrame = core(m_mainFrame);
3617 coreFrame->selection()->revealSelection(ScrollAlignment::alignCenterAlways);
3622 HRESULT STDMETHODCALLTYPE WebView::moveDragCaretToPoint(
3623 /* [in] */ LPPOINT /*point*/)
3625 ASSERT_NOT_REACHED();
3629 HRESULT STDMETHODCALLTYPE WebView::removeDragCaret( void)
3631 ASSERT_NOT_REACHED();
3635 HRESULT STDMETHODCALLTYPE WebView::setDrawsBackground(
3636 /* [in] */ BOOL /*drawsBackground*/)
3638 ASSERT_NOT_REACHED();
3642 HRESULT STDMETHODCALLTYPE WebView::drawsBackground(
3643 /* [retval][out] */ BOOL* /*drawsBackground*/)
3645 ASSERT_NOT_REACHED();
3649 HRESULT STDMETHODCALLTYPE WebView::setMainFrameURL(
3650 /* [in] */ BSTR /*urlString*/)
3652 ASSERT_NOT_REACHED();
3656 HRESULT STDMETHODCALLTYPE WebView::mainFrameURL(
3657 /* [retval][out] */ BSTR* urlString)
3665 COMPtr<IWebDataSource> dataSource;
3667 if (FAILED(m_mainFrame->provisionalDataSource(&dataSource))) {
3668 if (FAILED(m_mainFrame->dataSource(&dataSource)))
3677 COMPtr<IWebMutableURLRequest> request;
3678 if (FAILED(dataSource->request(&request)) || !request)
3681 if (FAILED(request->URL(urlString)))
3687 HRESULT STDMETHODCALLTYPE WebView::mainFrameDocument(
3688 /* [retval][out] */ IDOMDocument** document)
3694 return m_mainFrame->DOMDocument(document);
3697 HRESULT STDMETHODCALLTYPE WebView::mainFrameTitle(
3698 /* [retval][out] */ BSTR* /*title*/)
3700 ASSERT_NOT_REACHED();
3704 HRESULT STDMETHODCALLTYPE WebView::mainFrameIcon(
3705 /* [retval][out] */ OLE_HANDLE* /*hBitmap*/)
3707 ASSERT_NOT_REACHED();
3711 HRESULT STDMETHODCALLTYPE WebView::registerURLSchemeAsLocal(
3712 /* [in] */ BSTR scheme)
3717 SchemeRegistry::registerURLSchemeAsLocal(String(scheme, ::SysStringLen(scheme)));
3722 // IWebIBActions ---------------------------------------------------------------
3724 HRESULT STDMETHODCALLTYPE WebView::takeStringURLFrom(
3725 /* [in] */ IUnknown* /*sender*/)
3727 ASSERT_NOT_REACHED();
3731 HRESULT STDMETHODCALLTYPE WebView::stopLoading(
3732 /* [in] */ IUnknown* /*sender*/)
3737 return m_mainFrame->stopLoading();
3740 HRESULT STDMETHODCALLTYPE WebView::reload(
3741 /* [in] */ IUnknown* /*sender*/)
3746 return m_mainFrame->reload();
3749 HRESULT STDMETHODCALLTYPE WebView::canGoBack(
3750 /* [in] */ IUnknown* /*sender*/,
3751 /* [retval][out] */ BOOL* result)
3753 *result = !!(m_page->backForwardList()->backItem() && !m_page->defersLoading());
3757 HRESULT STDMETHODCALLTYPE WebView::goBack(
3758 /* [in] */ IUnknown* /*sender*/)
3760 ASSERT_NOT_REACHED();
3764 HRESULT STDMETHODCALLTYPE WebView::canGoForward(
3765 /* [in] */ IUnknown* /*sender*/,
3766 /* [retval][out] */ BOOL* result)
3768 *result = !!(m_page->backForwardList()->forwardItem() && !m_page->defersLoading());
3772 HRESULT STDMETHODCALLTYPE WebView::goForward(
3773 /* [in] */ IUnknown* /*sender*/)
3775 ASSERT_NOT_REACHED();
3779 // FIXME: This code should move into WebCore so it can be shared by all the WebKits.
3780 #define MinimumZoomMultiplier 0.5f
3781 #define MaximumZoomMultiplier 3.0f
3782 #define ZoomMultiplierRatio 1.2f
3784 HRESULT STDMETHODCALLTYPE WebView::canMakeTextLarger(
3785 /* [in] */ IUnknown* /*sender*/,
3786 /* [retval][out] */ BOOL* result)
3788 bool canGrowMore = canZoomIn(m_zoomsTextOnly);
3789 *result = canGrowMore ? TRUE : FALSE;
3793 HRESULT STDMETHODCALLTYPE WebView::canZoomPageIn(
3794 /* [in] */ IUnknown* /*sender*/,
3795 /* [retval][out] */ BOOL* result)
3797 bool canGrowMore = canZoomIn(false);
3798 *result = canGrowMore ? TRUE : FALSE;
3802 bool WebView::canZoomIn(bool isTextOnly)
3804 return zoomMultiplier(isTextOnly) * ZoomMultiplierRatio < MaximumZoomMultiplier;
3807 HRESULT STDMETHODCALLTYPE WebView::makeTextLarger(
3808 /* [in] */ IUnknown* /*sender*/)
3810 return zoomIn(m_zoomsTextOnly);
3813 HRESULT STDMETHODCALLTYPE WebView::zoomPageIn(
3814 /* [in] */ IUnknown* /*sender*/)
3816 return zoomIn(false);
3819 HRESULT WebView::zoomIn(bool isTextOnly)
3821 if (!canZoomIn(isTextOnly))
3823 setZoomMultiplier(zoomMultiplier(isTextOnly) * ZoomMultiplierRatio, isTextOnly);
3827 HRESULT STDMETHODCALLTYPE WebView::canMakeTextSmaller(
3828 /* [in] */ IUnknown* /*sender*/,
3829 /* [retval][out] */ BOOL* result)
3831 bool canShrinkMore = canZoomOut(m_zoomsTextOnly);
3832 *result = canShrinkMore ? TRUE : FALSE;
3836 HRESULT STDMETHODCALLTYPE WebView::canZoomPageOut(
3837 /* [in] */ IUnknown* /*sender*/,
3838 /* [retval][out] */ BOOL* result)
3840 bool canShrinkMore = canZoomOut(false);
3841 *result = canShrinkMore ? TRUE : FALSE;
3845 bool WebView::canZoomOut(bool isTextOnly)
3847 return zoomMultiplier(isTextOnly) / ZoomMultiplierRatio > MinimumZoomMultiplier;
3850 HRESULT STDMETHODCALLTYPE WebView::makeTextSmaller(
3851 /* [in] */ IUnknown* /*sender*/)
3853 return zoomOut(m_zoomsTextOnly);
3856 HRESULT STDMETHODCALLTYPE WebView::zoomPageOut(
3857 /* [in] */ IUnknown* /*sender*/)
3859 return zoomOut(false);
3862 HRESULT WebView::zoomOut(bool isTextOnly)
3864 if (!canZoomOut(isTextOnly))
3866 setZoomMultiplier(zoomMultiplier(isTextOnly) / ZoomMultiplierRatio, isTextOnly);
3870 HRESULT STDMETHODCALLTYPE WebView::canMakeTextStandardSize(
3871 /* [in] */ IUnknown* /*sender*/,
3872 /* [retval][out] */ BOOL* result)
3874 // Since we always reset text zoom and page zoom together, this should continue to return an answer about text zoom even if its not enabled.
3875 bool notAlreadyStandard = canResetZoom(true);
3876 *result = notAlreadyStandard ? TRUE : FALSE;
3880 HRESULT STDMETHODCALLTYPE WebView::canResetPageZoom(
3881 /* [in] */ IUnknown* /*sender*/,
3882 /* [retval][out] */ BOOL* result)
3884 bool notAlreadyStandard = canResetZoom(false);
3885 *result = notAlreadyStandard ? TRUE : FALSE;
3889 bool WebView::canResetZoom(bool isTextOnly)
3891 return zoomMultiplier(isTextOnly) != 1.0f;
3894 HRESULT STDMETHODCALLTYPE WebView::makeTextStandardSize(
3895 /* [in] */ IUnknown* /*sender*/)
3897 return resetZoom(true);
3900 HRESULT STDMETHODCALLTYPE WebView::resetPageZoom(
3901 /* [in] */ IUnknown* /*sender*/)
3903 return resetZoom(false);
3906 HRESULT WebView::resetZoom(bool isTextOnly)
3908 if (!canResetZoom(isTextOnly))
3910 setZoomMultiplier(1.0f, isTextOnly);
3914 HRESULT STDMETHODCALLTYPE WebView::toggleContinuousSpellChecking(
3915 /* [in] */ IUnknown* /*sender*/)
3919 if (FAILED(hr = isContinuousSpellCheckingEnabled(&enabled)))
3921 return setContinuousSpellCheckingEnabled(enabled ? FALSE : TRUE);
3924 HRESULT STDMETHODCALLTYPE WebView::toggleSmartInsertDelete(
3925 /* [in] */ IUnknown* /*sender*/)
3927 BOOL enabled = FALSE;
3928 HRESULT hr = smartInsertDeleteEnabled(&enabled);
3932 return setSmartInsertDeleteEnabled(enabled ? FALSE : TRUE);
3935 HRESULT STDMETHODCALLTYPE WebView::toggleGrammarChecking(
3936 /* [in] */ IUnknown* /*sender*/)
3939 HRESULT hr = isGrammarCheckingEnabled(&enabled);
3943 return setGrammarCheckingEnabled(enabled ? FALSE : TRUE);
3946 HRESULT STDMETHODCALLTYPE WebView::reloadFromOrigin(
3947 /* [in] */ IUnknown* /*sender*/)
3952 return m_mainFrame->reloadFromOrigin();
3955 // IWebViewCSS -----------------------------------------------------------------
3957 HRESULT STDMETHODCALLTYPE WebView::computedStyleForElement(
3958 /* [in] */ IDOMElement* /*element*/,
3959 /* [in] */ BSTR /*pseudoElement*/,
3960 /* [retval][out] */ IDOMCSSStyleDeclaration** /*style*/)
3962 ASSERT_NOT_REACHED();
3966 // IWebViewEditing -------------------------------------------------------------
3968 HRESULT STDMETHODCALLTYPE WebView::editableDOMRangeForPoint(
3969 /* [in] */ LPPOINT /*point*/,
3970 /* [retval][out] */ IDOMRange** /*range*/)
3972 ASSERT_NOT_REACHED();
3976 HRESULT STDMETHODCALLTYPE WebView::setSelectedDOMRange(
3977 /* [in] */ IDOMRange* /*range*/,
3978 /* [in] */ WebSelectionAffinity /*affinity*/)
3980 ASSERT_NOT_REACHED();
3984 HRESULT STDMETHODCALLTYPE WebView::selectedDOMRange(
3985 /* [retval][out] */ IDOMRange** /*range*/)
3987 ASSERT_NOT_REACHED();
3991 HRESULT STDMETHODCALLTYPE WebView::selectionAffinity(
3992 /* [retval][out][retval][out] */ WebSelectionAffinity* /*affinity*/)
3994 ASSERT_NOT_REACHED();
3998 HRESULT STDMETHODCALLTYPE WebView::setEditable(
3999 /* [in] */ BOOL /*flag*/)
4001 ASSERT_NOT_REACHED();
4005 HRESULT STDMETHODCALLTYPE WebView::isEditable(
4006 /* [retval][out] */ BOOL* /*isEditable*/)
4008 ASSERT_NOT_REACHED();
4012 HRESULT STDMETHODCALLTYPE WebView::setTypingStyle(
4013 /* [in] */ IDOMCSSStyleDeclaration* /*style*/)
4015 ASSERT_NOT_REACHED();
4019 HRESULT STDMETHODCALLTYPE WebView::typingStyle(
4020 /* [retval][out] */ IDOMCSSStyleDeclaration** /*style*/)
4022 ASSERT_NOT_REACHED();
4026 HRESULT STDMETHODCALLTYPE WebView::setSmartInsertDeleteEnabled(
4027 /* [in] */ BOOL flag)
4029 m_smartInsertDeleteEnabled = !!flag;
4030 if (m_smartInsertDeleteEnabled)
4031 setSelectTrailingWhitespaceEnabled(false);
4035 HRESULT STDMETHODCALLTYPE WebView::smartInsertDeleteEnabled(
4036 /* [retval][out] */ BOOL* enabled)
4038 *enabled = m_smartInsertDeleteEnabled ? TRUE : FALSE;
4042 HRESULT STDMETHODCALLTYPE WebView::setSelectTrailingWhitespaceEnabled(
4043 /* [in] */ BOOL flag)
4045 m_selectTrailingWhitespaceEnabled = !!flag;
4046 if (m_selectTrailingWhitespaceEnabled)
4047 setSmartInsertDeleteEnabled(false);
4051 HRESULT STDMETHODCALLTYPE WebView::isSelectTrailingWhitespaceEnabled(
4052 /* [retval][out] */ BOOL* enabled)
4054 *enabled = m_selectTrailingWhitespaceEnabled ? TRUE : FALSE;
4058 HRESULT STDMETHODCALLTYPE WebView::setContinuousSpellCheckingEnabled(
4059 /* [in] */ BOOL flag)
4061 if (continuousSpellCheckingEnabled != !!flag) {
4062 continuousSpellCheckingEnabled = !!flag;
4063 COMPtr<IWebPreferences> prefs;
4064 if (SUCCEEDED(preferences(&prefs)))
4065 prefs->setContinuousSpellCheckingEnabled(flag);
4068 BOOL spellCheckingEnabled;
4069 if (SUCCEEDED(isContinuousSpellCheckingEnabled(&spellCheckingEnabled)) && spellCheckingEnabled)
4070 preflightSpellChecker();
4072 m_mainFrame->unmarkAllMisspellings();
4077 HRESULT STDMETHODCALLTYPE WebView::isContinuousSpellCheckingEnabled(
4078 /* [retval][out] */ BOOL* enabled)
4080 *enabled = (continuousSpellCheckingEnabled && continuousCheckingAllowed()) ? TRUE : FALSE;
4084 HRESULT STDMETHODCALLTYPE WebView::spellCheckerDocumentTag(
4085 /* [retval][out] */ int* tag)
4087 // we just use this as a flag to indicate that we've spell checked the document
4088 // and need to close the spell checker out when the view closes.
4090 m_hasSpellCheckerDocumentTag = true;
4094 static COMPtr<IWebEditingDelegate> spellingDelegateForTimer;
4096 static void preflightSpellCheckerNow()
4098 spellingDelegateForTimer->preflightChosenSpellServer();
4099 spellingDelegateForTimer = 0;
4102 static void CALLBACK preflightSpellCheckerTimerCallback(HWND, UINT, UINT_PTR id, DWORD)
4105 preflightSpellCheckerNow();
4108 void WebView::preflightSpellChecker()
4110 // As AppKit does, we wish to delay tickling the shared spellchecker into existence on application launch.
4111 if (!m_editingDelegate)
4115 spellingDelegateForTimer = m_editingDelegate;
4116 if (SUCCEEDED(m_editingDelegate->sharedSpellCheckerExists(&exists)) && exists)
4117 preflightSpellCheckerNow();
4119 ::SetTimer(0, 0, 2000, preflightSpellCheckerTimerCallback);
4122 bool WebView::continuousCheckingAllowed()
4124 static bool allowContinuousSpellChecking = true;
4125 static bool readAllowContinuousSpellCheckingDefault = false;
4126 if (!readAllowContinuousSpellCheckingDefault) {
4127 COMPtr<IWebPreferences> prefs;
4128 if (SUCCEEDED(preferences(&prefs))) {
4130 prefs->allowContinuousSpellChecking(&allowed);
4131 allowContinuousSpellChecking = !!allowed;
4133 readAllowContinuousSpellCheckingDefault = true;
4135 return allowContinuousSpellChecking;
4138 HRESULT STDMETHODCALLTYPE WebView::undoManager(
4139 /* [retval][out] */ IWebUndoManager** /*manager*/)
4141 ASSERT_NOT_REACHED();
4145 HRESULT STDMETHODCALLTYPE WebView::setEditingDelegate(
4146 /* [in] */ IWebEditingDelegate* d)
4148 m_editingDelegate = d;
4152 HRESULT STDMETHODCALLTYPE WebView::editingDelegate(
4153 /* [retval][out] */ IWebEditingDelegate** d)
4156 ASSERT_NOT_REACHED();
4160 *d = m_editingDelegate.get();
4168 HRESULT STDMETHODCALLTYPE WebView::styleDeclarationWithText(
4169 /* [in] */ BSTR /*text*/,
4170 /* [retval][out] */ IDOMCSSStyleDeclaration** /*style*/)
4172 ASSERT_NOT_REACHED();
4176 HRESULT STDMETHODCALLTYPE WebView::hasSelectedRange(
4177 /* [retval][out] */ BOOL* hasSelectedRange)
4179 *hasSelectedRange = m_page->mainFrame()->selection()->isRange();
4183 HRESULT STDMETHODCALLTYPE WebView::cutEnabled(
4184 /* [retval][out] */ BOOL* enabled)
4186 Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4187 *enabled = editor->canCut() || editor->canDHTMLCut();
4191 HRESULT STDMETHODCALLTYPE WebView::copyEnabled(
4192 /* [retval][out] */ BOOL* enabled)
4194 Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4195 *enabled = editor->canCopy() || editor->canDHTMLCopy();
4199 HRESULT STDMETHODCALLTYPE WebView::pasteEnabled(
4200 /* [retval][out] */ BOOL* enabled)
4202 Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4203 *enabled = editor->canPaste() || editor->canDHTMLPaste();
4207 HRESULT STDMETHODCALLTYPE WebView::deleteEnabled(
4208 /* [retval][out] */ BOOL* enabled)
4210 *enabled = m_page->focusController()->focusedOrMainFrame()->editor()->canDelete();
4214 HRESULT STDMETHODCALLTYPE WebView::editingEnabled(
4215 /* [retval][out] */ BOOL* enabled)
4217 *enabled = m_page->focusController()->focusedOrMainFrame()->editor()->canEdit();
4221 HRESULT STDMETHODCALLTYPE WebView::isGrammarCheckingEnabled(
4222 /* [retval][out] */ BOOL* enabled)
4224 *enabled = grammarCheckingEnabled ? TRUE : FALSE;
4228 HRESULT STDMETHODCALLTYPE WebView::setGrammarCheckingEnabled(
4231 if (!m_editingDelegate) {
4232 LOG_ERROR("No NSSpellChecker");
4236 if (grammarCheckingEnabled == !!enabled)
4239 grammarCheckingEnabled = !!enabled;
4240 COMPtr<IWebPreferences> prefs;
4241 if (SUCCEEDED(preferences(&prefs)))
4242 prefs->setGrammarCheckingEnabled(enabled);
4244 m_editingDelegate->updateGrammar();
4246 // We call _preflightSpellChecker when turning continuous spell checking on, but we don't need to do that here
4247 // because grammar checking only occurs on code paths that already preflight spell checking appropriately.
4249 BOOL grammarEnabled;
4250 if (SUCCEEDED(isGrammarCheckingEnabled(&grammarEnabled)) && !grammarEnabled)
4251 m_mainFrame->unmarkAllBadGrammar();
4256 // IWebViewUndoableEditing -----------------------------------------------------
4258 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithNode(
4259 /* [in] */ IDOMNode* /*node*/)
4261 ASSERT_NOT_REACHED();
4265 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithText(
4266 /* [in] */ BSTR text)
4268 String textString(text, ::SysStringLen(text));
4269 Position start = m_page->mainFrame()->selection()->selection().start();
4270 m_page->focusController()->focusedOrMainFrame()->editor()->insertText(textString, 0);
4271 m_page->mainFrame()->selection()->setBase(start);
4275 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithMarkupString(
4276 /* [in] */ BSTR /*markupString*/)
4278 ASSERT_NOT_REACHED();
4282 HRESULT STDMETHODCALLTYPE WebView::replaceSelectionWithArchive(
4283 /* [in] */ IWebArchive* /*archive*/)
4285 ASSERT_NOT_REACHED();
4289 HRESULT STDMETHODCALLTYPE WebView::deleteSelection( void)
4291 Editor* editor = m_page->focusController()->focusedOrMainFrame()->editor();
4292 editor->deleteSelectionWithSmartDelete(editor->canSmartCopyOrDelete());
4296 HRESULT STDMETHODCALLTYPE WebView::clearSelection( void)
4298 m_page->focusController()->focusedOrMainFrame()->selection()->clear();
4302 HRESULT STDMETHODCALLTYPE WebView::applyStyle(
4303 /* [in] */ IDOMCSSStyleDeclaration* /*style*/)
4305 ASSERT_NOT_REACHED();
4309 // IWebViewEditingActions ------------------------------------------------------
4311 HRESULT STDMETHODCALLTYPE WebView::copy(
4312 /* [in] */ IUnknown* /*sender*/)
4314 m_page->focusController()->focusedOrMainFrame()->editor()->command("Copy").execute();
4318 HRESULT STDMETHODCALLTYPE WebView::cut(
4319 /* [in] */ IUnknown* /*sender*/)
4321 m_page->focusController()->focusedOrMainFrame()->editor()->command("Cut").execute();