Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: content/renderer/pepper_plugin_delegate_impl.cc

Issue 8970016: refactoring mouse lock to support pepper and WebKit (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes & WebKit APIs are bound now. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/pepper_plugin_delegate_impl.h" 5 #include "content/renderer/pepper_plugin_delegate_impl.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <queue> 8 #include <queue>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 637
638 virtual int CaptureFrameRate() OVERRIDE { 638 virtual int CaptureFrameRate() OVERRIDE {
639 return handler_proxy_->state().frame_rate; 639 return handler_proxy_->state().frame_rate;
640 } 640 }
641 641
642 private: 642 private:
643 scoped_ptr<media::VideoCaptureHandlerProxy> handler_proxy_; 643 scoped_ptr<media::VideoCaptureHandlerProxy> handler_proxy_;
644 media::VideoCapture* video_capture_; 644 media::VideoCapture* video_capture_;
645 }; 645 };
646 646
647 class PluginInstanceLockTarget : public MouseLockDispatcher::LockTarget {
648 public:
649 PluginInstanceLockTarget(webkit::ppapi::PluginInstance* plugin)
650 : plugin_(plugin) {}
651
652 virtual void OnLockMouseACK(bool succeeded) OVERRIDE {
653 plugin_->OnLockMouseACK(succeeded);
654 }
655
656 virtual void OnMouseLockLost() OVERRIDE {
657 plugin_->OnMouseLockLost();
658 }
659
660 virtual bool HandleMouseLockedInputEvent(
661 const WebKit::WebMouseEvent &event) OVERRIDE {
662 plugin_->HandleMouseLockedInputEvent(event);
663 return true;
664 }
665
666 webkit::ppapi::PluginInstance* plugin_;
yzshen1 2012/01/25 18:10:01 Better to place it in a private section.
scheib 2012/01/26 01:37:59 Done.
667 };
668
647 } // namespace 669 } // namespace
648 670
649 BrokerDispatcherWrapper::BrokerDispatcherWrapper() { 671 BrokerDispatcherWrapper::BrokerDispatcherWrapper() {
650 } 672 }
651 673
652 BrokerDispatcherWrapper::~BrokerDispatcherWrapper() { 674 BrokerDispatcherWrapper::~BrokerDispatcherWrapper() {
653 } 675 }
654 676
655 bool BrokerDispatcherWrapper::Init( 677 bool BrokerDispatcherWrapper::Init(
656 base::ProcessHandle broker_process_handle, 678 base::ProcessHandle broker_process_handle,
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 // Temporarily, just call back. 867 // Temporarily, just call back.
846 client->BrokerConnected(ppapi::PlatformFileToInt(plugin_handle), result); 868 client->BrokerConnected(ppapi::PlatformFileToInt(plugin_handle), result);
847 } 869 }
848 870
849 PepperPluginDelegateImpl::PepperPluginDelegateImpl(RenderViewImpl* render_view) 871 PepperPluginDelegateImpl::PepperPluginDelegateImpl(RenderViewImpl* render_view)
850 : content::RenderViewObserver(render_view), 872 : content::RenderViewObserver(render_view),
851 render_view_(render_view), 873 render_view_(render_view),
852 has_saved_context_menu_action_(false), 874 has_saved_context_menu_action_(false),
853 saved_context_menu_action_(0), 875 saved_context_menu_action_(0),
854 focused_plugin_(NULL), 876 focused_plugin_(NULL),
855 mouse_lock_owner_(NULL),
856 mouse_locked_(false),
857 pending_lock_request_(false),
858 pending_unlock_request_(false),
859 last_mouse_event_target_(NULL) { 877 last_mouse_event_target_(NULL) {
860 } 878 }
861 879
862 PepperPluginDelegateImpl::~PepperPluginDelegateImpl() { 880 PepperPluginDelegateImpl::~PepperPluginDelegateImpl() {
yzshen1 2012/01/25 18:10:01 Can you double check here to make sure the map of
scheib 2012/01/26 01:37:59 Done.
863 DCHECK(!mouse_lock_owner_);
864 } 881 }
865 882
866 scoped_refptr<webkit::ppapi::PluginModule> 883 scoped_refptr<webkit::ppapi::PluginModule>
867 PepperPluginDelegateImpl::CreatePepperPluginModule( 884 PepperPluginDelegateImpl::CreatePepperPluginModule(
868 const webkit::WebPluginInfo& webplugin_info, 885 const webkit::WebPluginInfo& webplugin_info,
869 bool* pepper_plugin_was_registered) { 886 bool* pepper_plugin_was_registered) {
870 *pepper_plugin_was_registered = true; 887 *pepper_plugin_was_registered = true;
871 888
872 // See if a module has already been loaded for this plugin. 889 // See if a module has already been loaded for this plugin.
873 FilePath path(webplugin_info.path); 890 FilePath path(webplugin_info.path);
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1141 return focused_plugin_->IsPluginAcceptingCompositionEvents(); 1158 return focused_plugin_->IsPluginAcceptingCompositionEvents();
1142 } 1159 }
1143 1160
1144 bool PepperPluginDelegateImpl::CanComposeInline() const { 1161 bool PepperPluginDelegateImpl::CanComposeInline() const {
1145 return IsPluginAcceptingCompositionEvents(); 1162 return IsPluginAcceptingCompositionEvents();
1146 } 1163 }
1147 1164
1148 void PepperPluginDelegateImpl::PluginCrashed( 1165 void PepperPluginDelegateImpl::PluginCrashed(
1149 webkit::ppapi::PluginInstance* instance) { 1166 webkit::ppapi::PluginInstance* instance) {
1150 render_view_->PluginCrashed(instance->module()->path()); 1167 render_view_->PluginCrashed(instance->module()->path());
1151 1168 UnSetAndDeleteLockTargetAdapter(instance);
1152 UnlockMouse(instance);
1153 } 1169 }
1154 1170
1155 void PepperPluginDelegateImpl::InstanceCreated( 1171 void PepperPluginDelegateImpl::InstanceCreated(
1156 webkit::ppapi::PluginInstance* instance) { 1172 webkit::ppapi::PluginInstance* instance) {
1157 active_instances_.insert(instance); 1173 active_instances_.insert(instance);
1158 1174
1159 // Set the initial focus. 1175 // Set the initial focus.
1160 instance->SetContentAreaFocus(render_view_->has_focus()); 1176 instance->SetContentAreaFocus(render_view_->has_focus());
1161 } 1177 }
1162 1178
1163 void PepperPluginDelegateImpl::InstanceDeleted( 1179 void PepperPluginDelegateImpl::InstanceDeleted(
1164 webkit::ppapi::PluginInstance* instance) { 1180 webkit::ppapi::PluginInstance* instance) {
1165 active_instances_.erase(instance); 1181 active_instances_.erase(instance);
1182 UnSetAndDeleteLockTargetAdapter(instance);
1166 1183
1167 if (mouse_lock_owner_ && mouse_lock_owner_ == instance) {
1168 // UnlockMouse() will determine whether a ViewHostMsg_UnlockMouse needs to
1169 // be sent, and set internal state properly. We only need to forget about
1170 // the current |mouse_lock_owner_|.
1171 UnlockMouse(mouse_lock_owner_);
1172 mouse_lock_owner_ = NULL;
1173 }
1174 if (last_mouse_event_target_ == instance) 1184 if (last_mouse_event_target_ == instance)
1175 last_mouse_event_target_ = NULL; 1185 last_mouse_event_target_ = NULL;
1176 if (focused_plugin_ == instance) 1186 if (focused_plugin_ == instance)
1177 PluginFocusChanged(instance, false); 1187 PluginFocusChanged(instance, false);
1178 } 1188 }
1179 1189
1180 SkBitmap* PepperPluginDelegateImpl::GetSadPluginBitmap() { 1190 SkBitmap* PepperPluginDelegateImpl::GetSadPluginBitmap() {
1181 return content::GetContentClient()->renderer()->GetSadPluginBitmap(); 1191 return content::GetContentClient()->renderer()->GetSadPluginBitmap();
1182 } 1192 }
1183 1193
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1354 for (std::set<webkit::ppapi::PluginInstance*>::iterator i = 1364 for (std::set<webkit::ppapi::PluginInstance*>::iterator i =
1355 active_instances_.begin(); 1365 active_instances_.begin();
1356 i != active_instances_.end(); ++i) 1366 i != active_instances_.end(); ++i)
1357 (*i)->PageVisibilityChanged(is_visible); 1367 (*i)->PageVisibilityChanged(is_visible);
1358 } 1368 }
1359 1369
1360 bool PepperPluginDelegateImpl::IsPluginFocused() const { 1370 bool PepperPluginDelegateImpl::IsPluginFocused() const {
1361 return focused_plugin_ != NULL; 1371 return focused_plugin_ != NULL;
1362 } 1372 }
1363 1373
1364 void PepperPluginDelegateImpl::OnLockMouseACK(bool succeeded) { 1374 void PepperPluginDelegateImpl::WillHandleMouseEvent() {
1365 DCHECK(!mouse_locked_ && pending_lock_request_);
1366
1367 mouse_locked_ = succeeded;
1368 pending_lock_request_ = false;
1369 if (pending_unlock_request_ && !succeeded) {
1370 // We have sent an unlock request after the lock request. However, since
1371 // the lock request has failed, the unlock request will be ignored by the
1372 // browser side and there won't be any response to it.
1373 pending_unlock_request_ = false;
1374 }
1375 // If the PluginInstance has been deleted, |mouse_lock_owner_| can be NULL.
1376 if (mouse_lock_owner_) {
1377 webkit::ppapi::PluginInstance* last_mouse_lock_owner = mouse_lock_owner_;
1378 if (!succeeded) {
1379 // Reset |mouse_lock_owner_| to NULL before calling OnLockMouseACK(), so
1380 // that if OnLockMouseACK() results in calls to any mouse lock method
1381 // (e.g., LockMouse()), the method will see consistent internal state.
1382 mouse_lock_owner_ = NULL;
1383 }
1384
1385 last_mouse_lock_owner->OnLockMouseACK(succeeded ? PP_OK : PP_ERROR_FAILED);
1386 }
1387 }
1388
1389 void PepperPluginDelegateImpl::OnMouseLockLost() {
1390 DCHECK(mouse_locked_ && !pending_lock_request_);
1391
1392 mouse_locked_ = false;
1393 pending_unlock_request_ = false;
1394 // If the PluginInstance has been deleted, |mouse_lock_owner_| can be NULL.
1395 if (mouse_lock_owner_) {
1396 // Reset |mouse_lock_owner_| to NULL before calling OnMouseLockLost(), so
1397 // that if OnMouseLockLost() results in calls to any mouse lock method
1398 // (e.g., LockMouse()), the method will see consistent internal state.
1399 webkit::ppapi::PluginInstance* last_mouse_lock_owner = mouse_lock_owner_;
1400 mouse_lock_owner_ = NULL;
1401
1402 last_mouse_lock_owner->OnMouseLockLost();
1403 }
1404 }
1405
1406 bool PepperPluginDelegateImpl::HandleMouseEvent(
1407 const WebKit::WebMouseEvent& event) {
1408 // This method is called for every mouse event that the render view receives. 1375 // This method is called for every mouse event that the render view receives.
1409 // And then the mouse event is forwarded to WebKit, which dispatches it to the 1376 // And then the mouse event is forwarded to WebKit, which dispatches it to the
1410 // event target. Potentially a Pepper plugin will receive the event. 1377 // event target. Potentially a Pepper plugin will receive the event.
1411 // In order to tell whether a plugin gets the last mouse event and which it 1378 // In order to tell whether a plugin gets the last mouse event and which it
1412 // is, we set |last_mouse_event_target_| to NULL here. If a plugin gets the 1379 // is, we set |last_mouse_event_target_| to NULL here. If a plugin gets the
1413 // event, it will notify us via DidReceiveMouseEvent() and set itself as 1380 // event, it will notify us via DidReceiveMouseEvent() and set itself as
1414 // |last_mouse_event_target_|. 1381 // |last_mouse_event_target_|.
1415 last_mouse_event_target_ = NULL; 1382 last_mouse_event_target_ = NULL;
1416
1417 if (mouse_locked_) {
1418 if (mouse_lock_owner_) {
1419 // |cursor_info| is ignored since it is hidden when the mouse is locked.
1420 WebKit::WebCursorInfo cursor_info;
1421 mouse_lock_owner_->HandleInputEvent(event, &cursor_info);
1422 }
1423
1424 // If the mouse is locked, only the current owner of the mouse lock can
1425 // process mouse events.
1426 return true;
1427 }
1428 return false;
1429 } 1383 }
1430 1384
1431 bool PepperPluginDelegateImpl::OpenFileSystem( 1385 bool PepperPluginDelegateImpl::OpenFileSystem(
1432 const GURL& url, 1386 const GURL& url,
1433 fileapi::FileSystemType type, 1387 fileapi::FileSystemType type,
1434 long long size, 1388 long long size,
1435 fileapi::FileSystemCallbackDispatcher* dispatcher) { 1389 fileapi::FileSystemCallbackDispatcher* dispatcher) {
1436 FileSystemDispatcher* file_system_dispatcher = 1390 FileSystemDispatcher* file_system_dispatcher =
1437 ChildThread::current()->file_system_dispatcher(); 1391 ChildThread::current()->file_system_dispatcher();
1438 return file_system_dispatcher->OpenFileSystem( 1392 return file_system_dispatcher->OpenFileSystem(
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
1956 DLOG(WARNING) << "Browser failed to allocate shared memory"; 1910 DLOG(WARNING) << "Browser failed to allocate shared memory";
1957 return NULL; 1911 return NULL;
1958 } 1912 }
1959 return new base::SharedMemory(handle, false); 1913 return new base::SharedMemory(handle, false);
1960 } 1914 }
1961 1915
1962 ppapi::Preferences PepperPluginDelegateImpl::GetPreferences() { 1916 ppapi::Preferences PepperPluginDelegateImpl::GetPreferences() {
1963 return ppapi::Preferences(render_view_->webkit_preferences()); 1917 return ppapi::Preferences(render_view_->webkit_preferences());
1964 } 1918 }
1965 1919
1966 void PepperPluginDelegateImpl::LockMouse( 1920 bool PepperPluginDelegateImpl::LockMouse(
1967 webkit::ppapi::PluginInstance* instance) { 1921 webkit::ppapi::PluginInstance* instance) {
1968 DCHECK(instance);
1969 if (!MouseLockedOrPending()) {
1970 DCHECK(!mouse_lock_owner_);
1971 pending_lock_request_ = true;
1972 mouse_lock_owner_ = instance;
1973 1922
1974 render_view_->Send( 1923 return render_view_->mouse_lock_dispatcher()->LockMouse(
1975 new ViewHostMsg_LockMouse(render_view_->routing_id())); 1924 GetOrCreateLockTargetAdapter(instance));
1976 } else if (instance != mouse_lock_owner_) {
1977 // Another plugin instance is using mouse lock. Fail immediately.
1978 instance->OnLockMouseACK(PP_ERROR_FAILED);
1979 } else {
1980 if (mouse_locked_) {
1981 instance->OnLockMouseACK(PP_OK);
1982 } else if (pending_lock_request_) {
1983 instance->OnLockMouseACK(PP_ERROR_INPROGRESS);
1984 } else {
1985 // The only case left here is
1986 // !mouse_locked_ && !pending_lock_request_ && pending_unlock_request_,
1987 // which is not possible.
1988 NOTREACHED();
1989 instance->OnLockMouseACK(PP_ERROR_FAILED);
1990 }
1991 }
1992 } 1925 }
1993 1926
1994 void PepperPluginDelegateImpl::UnlockMouse( 1927 void PepperPluginDelegateImpl::UnlockMouse(
1995 webkit::ppapi::PluginInstance* instance) { 1928 webkit::ppapi::PluginInstance* instance) {
1996 DCHECK(instance); 1929 render_view_->mouse_lock_dispatcher()->UnlockMouse(
1930 GetOrCreateLockTargetAdapter(instance));
1931 }
1997 1932
1998 // If no one is using mouse lock or the user is not |instance|, ignore 1933 bool PepperPluginDelegateImpl::IsMouseLocked(
1999 // the unlock request. 1934 webkit::ppapi::PluginInstance* instance) {
2000 if (MouseLockedOrPending() && mouse_lock_owner_ == instance) { 1935 return render_view_->mouse_lock_dispatcher()->IsMouseLockedTo(
2001 if (mouse_locked_ || pending_lock_request_) { 1936 GetOrCreateLockTargetAdapter(instance));
2002 DCHECK(!mouse_locked_ || !pending_lock_request_);
2003 if (!pending_unlock_request_) {
2004 pending_unlock_request_ = true;
2005
2006 render_view_->Send(
2007 new ViewHostMsg_UnlockMouse(render_view_->routing_id()));
2008 }
2009 } else {
2010 // The only case left here is
2011 // !mouse_locked_ && !pending_lock_request_ && pending_unlock_request_,
2012 // which is not possible.
2013 NOTREACHED();
2014 }
2015 }
2016 } 1937 }
2017 1938
2018 void PepperPluginDelegateImpl::DidChangeCursor( 1939 void PepperPluginDelegateImpl::DidChangeCursor(
2019 webkit::ppapi::PluginInstance* instance, 1940 webkit::ppapi::PluginInstance* instance,
2020 const WebKit::WebCursorInfo& cursor) { 1941 const WebKit::WebCursorInfo& cursor) {
2021 // Update the cursor appearance immediately if the requesting plugin is the 1942 // Update the cursor appearance immediately if the requesting plugin is the
2022 // one which receives the last mouse event. Otherwise, the new cursor won't be 1943 // one which receives the last mouse event. Otherwise, the new cursor won't be
2023 // picked up until the plugin gets the next input event. That is bad if, e.g., 1944 // picked up until the plugin gets the next input event. That is bad if, e.g.,
2024 // the plugin would like to set an invisible cursor when there isn't any user 1945 // the plugin would like to set an invisible cursor when there isn't any user
2025 // input for a while. 1946 // input for a while.
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
2165 2086
2166 bool PepperPluginDelegateImpl::CanUseSocketAPIs() { 2087 bool PepperPluginDelegateImpl::CanUseSocketAPIs() {
2167 WebView* webview = render_view_->webview(); 2088 WebView* webview = render_view_->webview();
2168 WebFrame* main_frame = webview ? webview->mainFrame() : NULL; 2089 WebFrame* main_frame = webview ? webview->mainFrame() : NULL;
2169 GURL url(main_frame ? GURL(main_frame->document().url()) : GURL()); 2090 GURL url(main_frame ? GURL(main_frame->document().url()) : GURL());
2170 if (!url.is_valid()) 2091 if (!url.is_valid())
2171 return false; 2092 return false;
2172 2093
2173 return content::GetContentClient()->renderer()->AllowSocketAPI(url); 2094 return content::GetContentClient()->renderer()->AllowSocketAPI(url);
2174 } 2095 }
2096
2097 MouseLockDispatcher::LockTarget*
2098 PepperPluginDelegateImpl::GetOrCreateLockTargetAdapter(
2099 webkit::ppapi::PluginInstance* instance) {
2100 MouseLockDispatcher::LockTarget* target = mouse_lock_instances_[instance];
2101 if (target)
2102 return target;
2103
2104 return mouse_lock_instances_[instance] =
2105 new PluginInstanceLockTarget(instance);
2106 }
2107
2108 void PepperPluginDelegateImpl::UnSetAndDeleteLockTargetAdapter(
2109 webkit::ppapi::PluginInstance* instance) {
2110 MouseLockDispatcher::LockTarget* target = mouse_lock_instances_[instance];
yzshen1 2012/01/25 18:10:01 If |instance| is not in the map. This line will in
scheib 2012/01/26 01:37:59 Done.
2111 if (target) {
2112 render_view_->mouse_lock_dispatcher()->OnLockTargetDestroyed(target);
2113 delete target;
2114 mouse_lock_instances_.erase(instance);
2115 }
2116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698