From 3a2cf7d1376ae33054b878232fb38b8fbed29e31 Mon Sep 17 00:00:00 2001 From: dmichael Date: Thu, 16 Oct 2014 08:53:40 -0700 Subject: [PATCH] Pepper: Access PepperMediaDeviceManager through a WeakPtr Its lifetime is scoped to the RenderFrame, and it might go away before the hosts that refer to it. BUG=423030 Review URL: https://codereview.chromium.org/653243003 Cr-Commit-Position: refs/heads/master@{#299897} --- content/renderer/pepper/pepper_device_enumeration_host_helper.cc | 5 +++-- content/renderer/pepper/pepper_device_enumeration_host_helper.h | 5 +++-- .../pepper/pepper_device_enumeration_host_helper_unittest.cc | 6 ++++-- content/renderer/pepper/pepper_media_device_manager.cc | 5 +++-- content/renderer/pepper/pepper_media_device_manager.h | 3 ++- content/renderer/pepper/pepper_platform_audio_input.cc | 2 +- content/renderer/pepper/pepper_platform_video_capture.cc | 2 +- 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/content/renderer/pepper/pepper_device_enumeration_host_helper.cc b/content/renderer/pepper/pepper_device_enumeration_host_helper.cc index d983f4a5816f..edc0840bb92a 100644 --- a/content/renderer/pepper/pepper_device_enumeration_host_helper.cc +++ b/content/renderer/pepper/pepper_device_enumeration_host_helper.cc @@ -46,6 +46,7 @@ class PepperDeviceEnumerationHostHelper::ScopedRequest // EnumerateDevicesCallbackBody() to ensure that we always call |callback| // asynchronously. sync_call_ = true; + DCHECK(owner_->delegate_); request_id_ = owner_->delegate_->EnumerateDevices( owner_->device_type_, owner_->document_url_, @@ -54,7 +55,7 @@ class PepperDeviceEnumerationHostHelper::ScopedRequest } ~ScopedRequest() { - if (requested_) { + if (requested_ && owner_->delegate_) { owner_->delegate_->StopEnumerateDevices(request_id_); } } @@ -91,7 +92,7 @@ class PepperDeviceEnumerationHostHelper::ScopedRequest PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper( ppapi::host::ResourceHost* resource_host, - Delegate* delegate, + base::WeakPtr delegate, PP_DeviceType_Dev device_type, const GURL& document_url) : resource_host_(resource_host), diff --git a/content/renderer/pepper/pepper_device_enumeration_host_helper.h b/content/renderer/pepper/pepper_device_enumeration_host_helper.h index 088d4661c0ec..ea1a7fcb9755 100644 --- a/content/renderer/pepper/pepper_device_enumeration_host_helper.h +++ b/content/renderer/pepper/pepper_device_enumeration_host_helper.h @@ -10,6 +10,7 @@ #include "base/basictypes.h" #include "base/callback_forward.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" #include "content/common/content_export.h" #include "ppapi/c/dev/ppb_device_ref_dev.h" #include "ppapi/host/host_message_context.h" @@ -58,7 +59,7 @@ class CONTENT_EXPORT PepperDeviceEnumerationHostHelper { // |resource_host| and |delegate| must outlive this object. PepperDeviceEnumerationHostHelper(ppapi::host::ResourceHost* resource_host, - Delegate* delegate, + base::WeakPtr delegate, PP_DeviceType_Dev device_type, const GURL& document_url); ~PepperDeviceEnumerationHostHelper(); @@ -93,7 +94,7 @@ class CONTENT_EXPORT PepperDeviceEnumerationHostHelper { // Non-owning pointers. ppapi::host::ResourceHost* resource_host_; - Delegate* delegate_; + base::WeakPtr delegate_; PP_DeviceType_Dev device_type_; GURL document_url_; diff --git a/content/renderer/pepper/pepper_device_enumeration_host_helper_unittest.cc b/content/renderer/pepper/pepper_device_enumeration_host_helper_unittest.cc index 69c7a31860b4..d9944d45d0f3 100644 --- a/content/renderer/pepper/pepper_device_enumeration_host_helper_unittest.cc +++ b/content/renderer/pepper/pepper_device_enumeration_host_helper_unittest.cc @@ -7,6 +7,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/logging.h" +#include "base/memory/weak_ptr.h" #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h" #include "ppapi/c/pp_errors.h" #include "ppapi/host/host_message_context.h" @@ -24,7 +25,8 @@ namespace content { namespace { -class TestDelegate : public PepperDeviceEnumerationHostHelper::Delegate { +class TestDelegate : public PepperDeviceEnumerationHostHelper::Delegate, + public base::SupportsWeakPtr { public: TestDelegate() : last_used_id_(0) {} @@ -76,7 +78,7 @@ class PepperDeviceEnumerationHostHelperTest : public testing::Test { : ppapi_host_(&sink_, ppapi::PpapiPermissions()), resource_host_(&ppapi_host_, 12345, 67890), device_enumeration_(&resource_host_, - &delegate_, + delegate_.AsWeakPtr(), PP_DEVICETYPE_DEV_AUDIOCAPTURE, GURL("http://example.com")) {} diff --git a/content/renderer/pepper/pepper_media_device_manager.cc b/content/renderer/pepper/pepper_media_device_manager.cc index 0d54964ebc43..676b2fb44b93 100644 --- a/content/renderer/pepper/pepper_media_device_manager.cc +++ b/content/renderer/pepper/pepper_media_device_manager.cc @@ -25,13 +25,14 @@ ppapi::DeviceRefData FromStreamDeviceInfo(const StreamDeviceInfo& info) { } // namespace -PepperMediaDeviceManager* PepperMediaDeviceManager::GetForRenderFrame( +base::WeakPtr +PepperMediaDeviceManager::GetForRenderFrame( RenderFrame* render_frame) { PepperMediaDeviceManager* handler = PepperMediaDeviceManager::Get(render_frame); if (!handler) handler = new PepperMediaDeviceManager(render_frame); - return handler; + return handler->AsWeakPtr(); } PepperMediaDeviceManager::PepperMediaDeviceManager(RenderFrame* render_frame) diff --git a/content/renderer/pepper/pepper_media_device_manager.h b/content/renderer/pepper/pepper_media_device_manager.h index ef2df60d5e17..d141cf378770 100644 --- a/content/renderer/pepper/pepper_media_device_manager.h +++ b/content/renderer/pepper/pepper_media_device_manager.h @@ -23,7 +23,8 @@ class PepperMediaDeviceManager public RenderFrameObserverTracker, public base::SupportsWeakPtr { public: - static PepperMediaDeviceManager* GetForRenderFrame(RenderFrame* render_frame); + static base::WeakPtr GetForRenderFrame( + RenderFrame* render_frame); virtual ~PepperMediaDeviceManager(); // PepperDeviceEnumerationHostHelper::Delegate implementation: diff --git a/content/renderer/pepper/pepper_platform_audio_input.cc b/content/renderer/pepper/pepper_platform_audio_input.cc index b1d62f0694ac..9b4612068058 100644 --- a/content/renderer/pepper/pepper_platform_audio_input.cc +++ b/content/renderer/pepper/pepper_platform_audio_input.cc @@ -289,7 +289,7 @@ PepperMediaDeviceManager* PepperPlatformAudioInput::GetMediaDeviceManager() { RenderFrameImpl* const render_frame = RenderFrameImpl::FromRoutingID(render_frame_id_); return render_frame ? - PepperMediaDeviceManager::GetForRenderFrame(render_frame) : NULL; + PepperMediaDeviceManager::GetForRenderFrame(render_frame).get() : NULL; } } // namespace content diff --git a/content/renderer/pepper/pepper_platform_video_capture.cc b/content/renderer/pepper/pepper_platform_video_capture.cc index f883d1b3ae08..0d011f3fdc42 100644 --- a/content/renderer/pepper/pepper_platform_video_capture.cc +++ b/content/renderer/pepper/pepper_platform_video_capture.cc @@ -152,7 +152,7 @@ PepperMediaDeviceManager* PepperPlatformVideoCapture::GetMediaDeviceManager() { RenderFrameImpl* const render_frame = RenderFrameImpl::FromRoutingID(render_frame_id_); return render_frame ? - PepperMediaDeviceManager::GetForRenderFrame(render_frame) : NULL; + PepperMediaDeviceManager::GetForRenderFrame(render_frame).get() : NULL; } } // namespace content -- 2.11.4.GIT