From 0a1fd8b040ee0a6186f1f1db60e6489d6cc8f7fb Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Mon, 2 Apr 2012 19:05:42 +0000 Subject: [PATCH] Avoid calling OnSetFocus multiple times (issue #563). git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@569 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- libcef/browser_impl_mac.mm | 12 ++++++++---- libcef/browser_webview_mac.h | 2 ++ libcef/browser_webview_mac.mm | 15 +++++++++------ libcef/webwidget_host_win.cc | 25 +++++++++++++++---------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/libcef/browser_impl_mac.mm b/libcef/browser_impl_mac.mm index 04d24ac3c..e18587638 100644 --- a/libcef/browser_impl_mac.mm +++ b/libcef/browser_impl_mac.mm @@ -130,12 +130,16 @@ void CefBrowserImpl::UIT_SetFocus(WebWidgetHost* host, bool enable) { if (!host) return; - NSView* view = host->view_handle(); - if (!view) + BrowserWebView* browserView = (BrowserWebView*)host->view_handle(); + if (!browserView) return; - if (enable) - [[view window] makeFirstResponder:view]; + if (enable) { + // Guard against calling OnSetFocus twice. + browserView.in_setfocus = true; + [[browserView window] makeFirstResponder:browserView]; + browserView.in_setfocus = false; + } } bool CefBrowserImpl::UIT_ViewDocumentString(WebKit::WebFrame *frame) { diff --git a/libcef/browser_webview_mac.h b/libcef/browser_webview_mac.h index 802aa140c..3d7792ccf 100644 --- a/libcef/browser_webview_mac.h +++ b/libcef/browser_webview_mac.h @@ -22,6 +22,7 @@ struct WebDropData; @private CefBrowserImpl* browser_; // weak NSTrackingArea* trackingArea_; + bool is_in_setfocus_; scoped_nsobject dragSource_; scoped_nsobject dropTarget_; @@ -58,6 +59,7 @@ struct WebDropData; offset:(NSPoint)offset; @property (nonatomic, assign) CefBrowserImpl* browser; +@property (nonatomic, assign) bool in_setfocus; @end diff --git a/libcef/browser_webview_mac.mm b/libcef/browser_webview_mac.mm index 9ccd98fa7..de4ad88d8 100644 --- a/libcef/browser_webview_mac.mm +++ b/libcef/browser_webview_mac.mm @@ -21,6 +21,7 @@ @implementation BrowserWebView @synthesize browser = browser_; +@synthesize in_setfocus = is_in_setfocus_; - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; @@ -163,12 +164,14 @@ - (BOOL)becomeFirstResponder { if (browser_ && browser_->UIT_GetWebView()) { - CefRefPtr client = browser_->GetClient(); - if (client.get()) { - CefRefPtr handler = client->GetFocusHandler(); - if (handler.get() && - handler->OnSetFocus(browser_, FOCUS_SOURCE_SYSTEM)) { - return NO; + if (!is_in_setfocus_) { + CefRefPtr client = browser_->GetClient(); + if (client.get()) { + CefRefPtr handler = client->GetFocusHandler(); + if (handler.get() && + handler->OnSetFocus(browser_, FOCUS_SOURCE_SYSTEM)) { + return NO; + } } } diff --git a/libcef/webwidget_host_win.cc b/libcef/webwidget_host_win.cc index 89fc8c6d4..825c6a910 100644 --- a/libcef/webwidget_host_win.cc +++ b/libcef/webwidget_host_win.cc @@ -601,16 +601,21 @@ void WebWidgetHost::MouseEvent(UINT message, WPARAM wparam, LPARAM lparam) { if (!popup()) { SetCapture(view_); - HWND parent_hwnd = ::GetParent(view_); - if (parent_hwnd) { - CefRefPtr browser = - static_cast(ui::GetWindowUserData(parent_hwnd)); - if (browser.get()) { - // This mimics a temporary workaround in RenderWidgetHostViewWin - // for bug 765011 to get focus when the mouse is clicked. This - // happens after the mouse down event is sent to the renderer - // because normally Windows does a WM_SETFOCUS after WM_LBUTTONDOWN. - browser->SetFocus(true); + if (::GetFocus() != view_) { + // Set focus to this window. + HWND parent_hwnd = ::GetParent(view_); + if (parent_hwnd) { + CefRefPtr browser = + static_cast( + ui::GetWindowUserData(parent_hwnd)); + if (browser.get()) { + // This mimics a temporary workaround in RenderWidgetHostViewWin + // for bug 765011 to get focus when the mouse is clicked. This + // happens after the mouse down event is sent to the renderer + // because normally Windows does a WM_SETFOCUS after + // WM_LBUTTONDOWN. + browser->SetFocus(true); + } } } }