diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-01 01:38:12 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-01 01:38:12 +0000 |
commit | 5d5f7af66e100999851a743a1e15cd1641df72c6 (patch) | |
tree | ec926577892e9fc373881abc2fa89eb4af8fc22a | |
parent | 7075f0b9eb6fd3dd20f8900c4f05ae69f152a089 (diff) | |
download | chromium_src-5d5f7af66e100999851a743a1e15cd1641df72c6.zip chromium_src-5d5f7af66e100999851a743a1e15cd1641df72c6.tar.gz chromium_src-5d5f7af66e100999851a743a1e15cd1641df72c6.tar.bz2 |
Only allow to lock the mouse when the tab is in fullscreen mode.
BUG=41781
TEST=Manual test in ppapi/examples/mouse_lock.
Review URL: http://codereview.chromium.org/8072011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103612 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/browser.cc | 37 | ||||
-rw-r--r-- | chrome/browser/ui/browser.h | 1 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host.cc | 7 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host.h | 35 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host_delegate.cc | 4 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host_delegate.h | 1 | ||||
-rw-r--r-- | content/browser/renderer_host/render_widget_host.cc | 22 | ||||
-rw-r--r-- | content/browser/renderer_host/render_widget_host.h | 6 | ||||
-rw-r--r-- | content/browser/tab_contents/tab_contents.cc | 4 | ||||
-rw-r--r-- | content/browser/tab_contents/tab_contents.h | 1 | ||||
-rw-r--r-- | content/browser/tab_contents/tab_contents_delegate.cc | 4 | ||||
-rw-r--r-- | content/browser/tab_contents/tab_contents_delegate.h | 1 | ||||
-rw-r--r-- | ppapi/examples/mouse_lock/mouse_lock.cc | 5 | ||||
-rw-r--r-- | ppapi/examples/mouse_lock/mouse_lock.html | 72 |
14 files changed, 155 insertions, 45 deletions
diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index a608b8e..7f8349c 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -3795,25 +3795,48 @@ void Browser::ToggleFullscreenModeForTab(TabContents* tab, bool enter_fullscreen) { if (tab != GetSelectedTabContents()) return; - fullscreened_tab_ = enter_fullscreen ? - TabContentsWrapper::GetCurrentWrapperForContents(tab) : NULL; - bool in_correct_mode_for_tab_fullscreen; + + if (enter_fullscreen) { + fullscreened_tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab); + bool in_correct_mode_for_tab_fullscreen; #if defined(OS_MACOSX) - in_correct_mode_for_tab_fullscreen = window_->InPresentationMode(); + in_correct_mode_for_tab_fullscreen = window_->InPresentationMode(); #else - in_correct_mode_for_tab_fullscreen = window_->IsFullscreen(); + in_correct_mode_for_tab_fullscreen = window_->IsFullscreen(); #endif - if (enter_fullscreen && !in_correct_mode_for_tab_fullscreen) - tab_caused_fullscreen_ = true; + if (!in_correct_mode_for_tab_fullscreen) + tab_caused_fullscreen_ = true; + } + if (tab_caused_fullscreen_) { #if defined(OS_MACOSX) TogglePresentationMode(); #else ToggleFullscreenMode(); #endif + } else if (!enter_fullscreen) { + // If currently there is a tab in "tab fullscreen" mode and fullscreen was + // not caused by it (i.e., previously it was in "browser fullscreen" mode), + // we need to switch back to "browser fullscreen" mode. In this case, all we + // have to do is notifying the tab that it has exited "tab fullscreen" mode. + NotifyTabOfFullscreenExitIfNecessary(); } } +bool Browser::IsFullscreenForTab(const TabContents* tab) const { + const TabContentsWrapper* wrapper = + TabContentsWrapper::GetCurrentWrapperForContents(tab); + bool result = wrapper && wrapper == fullscreened_tab_; + DCHECK(!result || tab == GetSelectedTabContents()); +#if defined(OS_MACOSX) + DCHECK(!result || window_->InPresentationMode()); +#else + DCHECK(!result || window_->IsFullscreen()); +#endif + + return result; +} + void Browser::JSOutOfMemory(TabContents* tab) { JSOutOfMemoryHelper(tab); } diff --git a/chrome/browser/ui/browser.h b/chrome/browser/ui/browser.h index 588a0cf..f93e3cd 100644 --- a/chrome/browser/ui/browser.h +++ b/chrome/browser/ui/browser.h @@ -963,6 +963,7 @@ class Browser : public TabHandlerDelegate, const FilePath& path) OVERRIDE; virtual void ToggleFullscreenModeForTab(TabContents* tab, bool enter_fullscreen) OVERRIDE; + virtual bool IsFullscreenForTab(const TabContents* tab) const OVERRIDE; virtual void JSOutOfMemory(TabContents* tab) OVERRIDE; virtual void RegisterProtocolHandler(TabContents* tab, const std::string& protocol, diff --git a/content/browser/renderer_host/render_view_host.cc b/content/browser/renderer_host/render_view_host.cc index e051c0f..431d87b 100644 --- a/content/browser/renderer_host/render_view_host.cc +++ b/content/browser/renderer_host/render_view_host.cc @@ -1131,6 +1131,11 @@ void RenderViewHost::NotifyRendererResponsive() { delegate_->RendererResponsive(this); } +bool RenderViewHost::CanLockMouse() const { + // Only allow to lock the mouse when the current tab is in fullscreen mode. + return delegate_->IsFullscreenForCurrentTab(); +} + void RenderViewHost::OnMsgFocus() { delegate_->Activate(); } @@ -1223,6 +1228,8 @@ void RenderViewHost::SetAltErrorPageURL(const GURL& url) { } void RenderViewHost::ExitFullscreen() { + UnlockMouseIfNecessary(); + Send(new ViewMsg_ExitFullscreen(routing_id())); } diff --git a/content/browser/renderer_host/render_view_host.h b/content/browser/renderer_host/render_view_host.h index 180f3ce..ef541c8 100644 --- a/content/browser/renderer_host/render_view_host.h +++ b/content/browser/renderer_host/render_view_host.h @@ -9,6 +9,7 @@ #include <string> #include <vector> +#include "base/compiler_specific.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/observer_list.h" @@ -325,14 +326,16 @@ class CONTENT_EXPORT RenderViewHost : public RenderWidgetHost { } // RenderWidgetHost public overrides. - virtual void Shutdown(); - virtual bool IsRenderView() const; - virtual bool OnMessageReceived(const IPC::Message& msg); - virtual void GotFocus(); - virtual void LostCapture(); - virtual void ForwardMouseEvent(const WebKit::WebMouseEvent& mouse_event); - virtual void OnMouseActivate(); - virtual void ForwardKeyboardEvent(const NativeWebKeyboardEvent& key_event); + virtual void Shutdown() OVERRIDE; + virtual bool IsRenderView() const OVERRIDE; + virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; + virtual void GotFocus() OVERRIDE; + virtual void LostCapture() OVERRIDE; + virtual void ForwardMouseEvent( + const WebKit::WebMouseEvent& mouse_event) OVERRIDE; + virtual void OnMouseActivate() OVERRIDE; + virtual void ForwardKeyboardEvent( + const NativeWebKeyboardEvent& key_event) OVERRIDE; // Creates a new RenderView with the given route id. void CreateNewWindow(int route_id, @@ -450,13 +453,15 @@ class CONTENT_EXPORT RenderViewHost : public RenderWidgetHost { // RenderWidgetHost protected overrides. virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, - bool* is_keyboard_shortcut); - virtual void UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event); - virtual void OnUserGesture(); - virtual void NotifyRendererUnresponsive(); - virtual void NotifyRendererResponsive(); - virtual void OnMsgFocus(); - virtual void OnMsgBlur(); + bool* is_keyboard_shortcut) OVERRIDE; + virtual void UnhandledKeyboardEvent( + const NativeWebKeyboardEvent& event) OVERRIDE; + virtual void OnUserGesture() OVERRIDE; + virtual void NotifyRendererUnresponsive() OVERRIDE; + virtual void NotifyRendererResponsive() OVERRIDE; + virtual bool CanLockMouse() const OVERRIDE; + virtual void OnMsgFocus() OVERRIDE; + virtual void OnMsgBlur() OVERRIDE; // IPC message handlers. void OnMsgShowView(int route_id, diff --git a/content/browser/renderer_host/render_view_host_delegate.cc b/content/browser/renderer_host/render_view_host_delegate.cc index be281c5..6cd4a0b 100644 --- a/content/browser/renderer_host/render_view_host_delegate.cc +++ b/content/browser/renderer_host/render_view_host_delegate.cc @@ -43,3 +43,7 @@ bool RenderViewHostDelegate::PreHandleKeyboardEvent( const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) { return false; } + +bool RenderViewHostDelegate::IsFullscreenForCurrentTab() const { + return false; +} diff --git a/content/browser/renderer_host/render_view_host_delegate.h b/content/browser/renderer_host/render_view_host_delegate.h index d807111..2fa528a 100644 --- a/content/browser/renderer_host/render_view_host_delegate.h +++ b/content/browser/renderer_host/render_view_host_delegate.h @@ -373,6 +373,7 @@ class CONTENT_EXPORT RenderViewHostDelegate : public IPC::Channel::Listener { // Notification that the page wants to go into or out of fullscreen mode. virtual void ToggleFullscreenMode(bool enter_fullscreen) {} + virtual bool IsFullscreenForCurrentTab() const; // The contents' preferred size changed. virtual void UpdatePreferredSize(const gfx::Size& pref_size) {} diff --git a/content/browser/renderer_host/render_widget_host.cc b/content/browser/renderer_host/render_widget_host.cc index b40f69c..37b72d8 100644 --- a/content/browser/renderer_host/render_widget_host.cc +++ b/content/browser/renderer_host/render_widget_host.cc @@ -378,8 +378,7 @@ void RenderWidgetHost::Focus() { } void RenderWidgetHost::Blur() { - if (IsMouseLocked()) - view_->UnlockMouse(); + UnlockMouseIfNecessary(); Send(new ViewMsg_SetFocus(routing_id_, false)); } @@ -393,8 +392,7 @@ void RenderWidgetHost::LostMouseLock() { } void RenderWidgetHost::ViewDestroyed() { - if (IsMouseLocked()) - view_->UnlockMouse(); + UnlockMouseIfNecessary(); // TODO(evanm): tracking this may no longer be necessary; // eliminate this function if so. @@ -796,6 +794,15 @@ void RenderWidgetHost::ImeCancelComposition() { std::vector<WebKit::WebCompositionUnderline>(), 0, 0)); } +bool RenderWidgetHost::CanLockMouse() const { + return false; +} + +void RenderWidgetHost::UnlockMouseIfNecessary() { + if (IsMouseLocked()) + view_->UnlockMouse(); +} + bool RenderWidgetHost::IsMouseLocked() const { return view_ ? view_->mouse_locked() : false; } @@ -1146,9 +1153,7 @@ void RenderWidgetHost::OnMsgDidActivateAcceleratedCompositing(bool activated) { } void RenderWidgetHost::OnMsgLockMouse() { - // TODO(yzshen): Only allow to lock the mouse when in fullscreen mode, and - // make sure that the mouse is unlocked when leaving fullscreen mode. - if (!view_ || !view_->HasFocus() || !view_->LockMouse()) { + if (!CanLockMouse() || !view_ || !view_->HasFocus()|| !view_->LockMouse()) { Send(new ViewMsg_LockMouse_ACK(routing_id_, false)); } else { Send(new ViewMsg_LockMouse_ACK(routing_id_, true)); @@ -1156,8 +1161,7 @@ void RenderWidgetHost::OnMsgLockMouse() { } void RenderWidgetHost::OnMsgUnlockMouse() { - if (IsMouseLocked()) - view_->UnlockMouse(); + UnlockMouseIfNecessary(); } #if defined(OS_POSIX) diff --git a/content/browser/renderer_host/render_widget_host.h b/content/browser/renderer_host/render_widget_host.h index 9b6a14c..894e551 100644 --- a/content/browser/renderer_host/render_widget_host.h +++ b/content/browser/renderer_host/render_widget_host.h @@ -468,6 +468,12 @@ class CONTENT_EXPORT RenderWidgetHost : public IPC::Channel::Listener, virtual void NotifyRendererUnresponsive() {} virtual void NotifyRendererResponsive() {} + // RenderViewHost overrides this method to impose further restrictions on when + // to allow mouse lock. For now, it only allows to lock the mouse when the + // current tab is in fullscreen mode. + virtual bool CanLockMouse() const; + + void UnlockMouseIfNecessary(); bool IsMouseLocked() const; protected: diff --git a/content/browser/tab_contents/tab_contents.cc b/content/browser/tab_contents/tab_contents.cc index 74bc332..548f6df 100644 --- a/content/browser/tab_contents/tab_contents.cc +++ b/content/browser/tab_contents/tab_contents.cc @@ -512,6 +512,10 @@ void TabContents::ToggleFullscreenMode(bool enter_fullscreen) { delegate_->ToggleFullscreenModeForTab(this, enter_fullscreen); } +bool TabContents::IsFullscreenForCurrentTab() const { + return delegate_ ? delegate_->IsFullscreenForTab(this) : false; +} + void TabContents::UpdatePreferredSize(const gfx::Size& pref_size) { if (delegate_) delegate_->UpdatePreferredSize(this, pref_size); diff --git a/content/browser/tab_contents/tab_contents.h b/content/browser/tab_contents/tab_contents.h index 635f7e0..7ecd21cf 100644 --- a/content/browser/tab_contents/tab_contents.h +++ b/content/browser/tab_contents/tab_contents.h @@ -698,6 +698,7 @@ class CONTENT_EXPORT TabContents : public PageNavigator, virtual void RunFileChooser(RenderViewHost* render_view_host, const ViewHostMsg_RunFileChooser_Params& params); virtual void ToggleFullscreenMode(bool enter_fullscreen) OVERRIDE; + virtual bool IsFullscreenForCurrentTab() const OVERRIDE; virtual void UpdatePreferredSize(const gfx::Size& pref_size) OVERRIDE; // RenderViewHostManager::Delegate ------------------------------------------- diff --git a/content/browser/tab_contents/tab_contents_delegate.cc b/content/browser/tab_contents/tab_contents_delegate.cc index 07a5fc5..dec1c13 100644 --- a/content/browser/tab_contents/tab_contents_delegate.cc +++ b/content/browser/tab_contents/tab_contents_delegate.cc @@ -283,6 +283,10 @@ void TabContentsDelegate::ToggleFullscreenModeForTab(TabContents* tab, bool enter_fullscreen) { } +bool TabContentsDelegate::IsFullscreenForTab(const TabContents* tab) const { + return false; +} + void TabContentsDelegate::JSOutOfMemory(TabContents* tab) { } diff --git a/content/browser/tab_contents/tab_contents_delegate.h b/content/browser/tab_contents/tab_contents_delegate.h index 3a0cdad..d1bb423 100644 --- a/content/browser/tab_contents/tab_contents_delegate.h +++ b/content/browser/tab_contents/tab_contents_delegate.h @@ -307,6 +307,7 @@ class CONTENT_EXPORT TabContentsDelegate { // Called when the renderer puts a tab into or out of fullscreen mode. virtual void ToggleFullscreenModeForTab(TabContents* tab, bool enter_fullscreen); + virtual bool IsFullscreenForTab(const TabContents* tab) const; // Called when a Javascript out of memory notification is received. virtual void JSOutOfMemory(TabContents* tab); diff --git a/ppapi/examples/mouse_lock/mouse_lock.cc b/ppapi/examples/mouse_lock/mouse_lock.cc index 95806cf5..0b86a4b 100644 --- a/ppapi/examples/mouse_lock/mouse_lock.cc +++ b/ppapi/examples/mouse_lock/mouse_lock.cc @@ -53,8 +53,9 @@ class MyInstance : public pp::Instance, public pp::MouseLock_Dev { !mouse_locked_) { LockMouse( callback_factory_.NewRequiredCallback(&MyInstance::DidLockMouse)); - } else if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_RIGHT && - mouse_locked_) { + } else if ( + mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE && + mouse_locked_) { UnlockMouse(); } return true; diff --git a/ppapi/examples/mouse_lock/mouse_lock.html b/ppapi/examples/mouse_lock/mouse_lock.html index 3f8841fa..39bd4e2 100644 --- a/ppapi/examples/mouse_lock/mouse_lock.html +++ b/ppapi/examples/mouse_lock/mouse_lock.html @@ -7,19 +7,67 @@ --> <head> <title>Mouse Lock Example</title> + <style type="text/css"> + :-webkit-full-screen { + width: 100%; + height: 100%; + } + </style> + <script> + var fullscreen = false; + function ToggleFullscreen() { + if (fullscreen) { + document.webkitCancelFullScreen(); + } else { + document.getElementById('container').webkitRequestFullScreen( + Element.ALLOW_KEYBOARD_INPUT); + } + fullscreen = !fullscreen; + } + </script> </head> - <body title="This tooltip should not be shown if the mouse is locked."> -<ul> - <li>Lock mouse: left click in either of the two boxes; or right click in - either of the boxes to ensure that it is focused and then press Enter key. - </li> - <li>Unlock mouse: lose focus, press Esc key, or right click.</li> -</ul> -<object id="plugin" type="application/x-ppapi-example-mouse-lock" - width="300" height="300" border="2px"></object> -<div></div> -<object id="plugin" type="application/x-ppapi-example-mouse-lock" - width="300" height="300" border="2px"></object> +<div id="container"> + <ul> + <li>There are two different kinds of fullscreen mode - "tab fullscreen" and + "browser fullscreen". + <ul> + <li>"tab fullscreen" refers to when a tab enters fullscreen mode via the + JS or Pepper fullscreen API;</li> + <li>"browser fullscreen" refers to the user putting the browser itself + into fullscreen mode from the UI (e.g., pressing F11).</li> + </ul> + <span style="font-weight:bold"> + NOTE: Mouse lock is only allowed in "tab fullscreen" mode. + </span> + </li> + <li>Lock mouse: + <ul> + <li>left click in either of the two boxes; or</li> + <li>right click in either of the boxes to ensure that it is focused and + then press Enter key. (You could verify that the tooltip window is + dismissed properly by this second approach.)</li> + </ul> + </li> + <li>Unlock mouse: + <ul> + <li>lose focus; or</li> + <li>press Esc key; or</li> + <li>middle click; or</li> + <li>exit from the "tab fullscreen" mode.</li> + </ul> + </li> + </ul> + <object id="plugin" type="application/x-ppapi-example-mouse-lock" + width="300" height="300" border="2px"></object> + <div></div> + <object id="plugin" type="application/x-ppapi-example-mouse-lock" + width="300" height="300" border="2px"></object> + <div> + <button id="toggle_fullscreen" onclick="ToggleFullscreen();"> + Toggle Tab Fullscreen + </button> + </div> +</div> </body> </html> |