Convert the PluginResource to be refcounted.

This is to make the future transition to a shared resource tracker (which
will use refcounted resources) easier. There should be no behavior change.

Review URL: http://codereview.chromium.org/7608030

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96324 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/ppapi/proxy/plugin_resource.h b/ppapi/proxy/plugin_resource.h
index 3abe9cb..703a5299 100644
--- a/ppapi/proxy/plugin_resource.h
+++ b/ppapi/proxy/plugin_resource.h
@@ -6,6 +6,7 @@
 #define PPAPI_PROXY_PLUGIN_RESOURCE_H_
 
 #include "base/basictypes.h"
+#include "base/memory/ref_counted.h"
 #include "ppapi/c/pp_instance.h"
 #include "ppapi/proxy/host_resource.h"
 #include "ppapi/proxy/plugin_dispatcher.h"
@@ -45,7 +46,8 @@
 FOR_ALL_PLUGIN_RESOURCES(DECLARE_RESOURCE_CLASS)
 #undef DECLARE_RESOURCE_CLASS
 
-class PluginResource : public ::ppapi::ResourceObjectBase {
+class PluginResource : public ::ppapi::ResourceObjectBase,
+                       public base::RefCounted<PluginResource> {
  public:
   PluginResource(const HostResource& resource);
   virtual ~PluginResource();
diff --git a/ppapi/proxy/plugin_resource_tracker.cc b/ppapi/proxy/plugin_resource_tracker.cc
index cabf9cb..a82ccef 100644
--- a/ppapi/proxy/plugin_resource_tracker.cc
+++ b/ppapi/proxy/plugin_resource_tracker.cc
@@ -30,8 +30,7 @@
 PluginResourceTracker::ResourceInfo::ResourceInfo() : ref_count(0) {
 }
 
-PluginResourceTracker::ResourceInfo::ResourceInfo(int rc,
-                                                  linked_ptr<PluginResource> r)
+PluginResourceTracker::ResourceInfo::ResourceInfo(int rc, PluginResource* r)
     : ref_count(rc),
       resource(r) {
 }
@@ -90,8 +89,7 @@
   return found->second.resource.get();
 }
 
-PP_Resource PluginResourceTracker::AddResource(
-    linked_ptr<PluginResource> object) {
+PP_Resource PluginResourceTracker::AddResource(PluginResource* object) {
   PP_Resource plugin_resource = ++last_resource_id_;
   DCHECK(resource_map_.find(plugin_resource) == resource_map_.end());
   resource_map_[plugin_resource] = ResourceInfo(1, object);
@@ -164,14 +162,14 @@
     // Keep a reference while removing in case the destructor ends up
     // re-entering. That way, when the destructor is called, it's out of the
     // maps.
-    linked_ptr<PluginResource> plugin_resource = found->second.resource;
+    scoped_refptr<PluginResource> plugin_resource = found->second.resource;
     PluginDispatcher* dispatcher =
         PluginDispatcher::GetForInstance(plugin_resource->instance());
     HostResource host_resource = plugin_resource->host_resource();
     if (!host_resource.is_null())
       host_resource_map_.erase(host_resource);
     resource_map_.erase(found);
-    plugin_resource.reset();
+    plugin_resource = NULL;
 
     // dispatcher can be NULL if the plugin held on to a resource after the
     // instance was destroyed. In that case the browser-side resource has
diff --git a/ppapi/proxy/plugin_resource_tracker.h b/ppapi/proxy/plugin_resource_tracker.h
index b96831c..d313a9f 100644
--- a/ppapi/proxy/plugin_resource_tracker.h
+++ b/ppapi/proxy/plugin_resource_tracker.h
@@ -9,7 +9,6 @@
 #include <utility>
 
 #include "base/compiler_specific.h"
-#include "base/memory/linked_ptr.h"
 #include "ppapi/c/pp_completion_callback.h"
 #include "ppapi/c/pp_instance.h"
 #include "ppapi/c/pp_stdint.h"
@@ -50,7 +49,9 @@
   // plugin-local PP_Resource ID that identifies the resource. Note that this
   // PP_Resource is not valid to send to the host, use
   // PluginResource.host_resource() to get that.
-  PP_Resource AddResource(linked_ptr<PluginResource> object);
+  //
+  // The resource tracker will take a reference to the given object.
+  PP_Resource AddResource(PluginResource* object);
 
   void AddRefResource(PP_Resource resource);
   void ReleaseResource(PP_Resource resource);
@@ -88,14 +89,14 @@
 
   struct ResourceInfo {
     ResourceInfo();
-    ResourceInfo(int ref_count, linked_ptr<PluginResource> r);
+    ResourceInfo(int ref_count, PluginResource* r);
     ResourceInfo(const ResourceInfo& other);
     ~ResourceInfo();
 
     ResourceInfo& operator=(const ResourceInfo& other);
 
     int ref_count;
-    linked_ptr<PluginResource> resource;  // May be NULL.
+    scoped_refptr<PluginResource> resource;  // May be NULL.
   };
 
   void ReleasePluginResourceRef(const PP_Resource& var,
diff --git a/ppapi/proxy/plugin_resource_tracker_unittest.cc b/ppapi/proxy/plugin_resource_tracker_unittest.cc
index 5a317128..5d673f52 100644
--- a/ppapi/proxy/plugin_resource_tracker_unittest.cc
+++ b/ppapi/proxy/plugin_resource_tracker_unittest.cc
@@ -56,8 +56,7 @@
   EXPECT_EQ(0, TrackedMockResource::tracked_alive_count);
   TrackedMockResource* object = new TrackedMockResource(serialized);
   EXPECT_EQ(1, TrackedMockResource::tracked_alive_count);
-  PP_Resource plugin_resource =
-      tracker().AddResource(linked_ptr<PluginResource>(object));
+  PP_Resource plugin_resource = tracker().AddResource(object);
 
   // Now that the object has been added, the return value should be the plugin
   // resource ID we already got.
diff --git a/ppapi/proxy/ppb_audio_config_proxy.cc b/ppapi/proxy/ppb_audio_config_proxy.cc
index e8eb056..f202b63 100644
--- a/ppapi/proxy/ppb_audio_config_proxy.cc
+++ b/ppapi/proxy/ppb_audio_config_proxy.cc
@@ -74,7 +74,7 @@
     PP_Instance instance,
     PP_AudioSampleRate sample_rate,
     uint32_t sample_frame_count) {
-  linked_ptr<AudioConfig> object(new AudioConfig(
+  scoped_refptr<AudioConfig> object(new AudioConfig(
       HostResource::MakeInstanceOnly(instance)));
   if (!object->Init(sample_rate, sample_frame_count))
     return 0;
diff --git a/ppapi/proxy/ppb_audio_proxy.cc b/ppapi/proxy/ppb_audio_proxy.cc
index f0db204e..9ec3b30 100644
--- a/ppapi/proxy/ppb_audio_proxy.cc
+++ b/ppapi/proxy/ppb_audio_proxy.cc
@@ -178,9 +178,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<Audio> object(new Audio(result, config_id,
-                                     audio_callback, user_data));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new Audio(result, config_id, audio_callback, user_data));
 }
 
 bool PPB_Audio_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_broker_proxy.cc b/ppapi/proxy/ppb_broker_proxy.cc
index 637c6a0..cc2bf86 100644
--- a/ppapi/proxy/ppb_broker_proxy.cc
+++ b/ppapi/proxy/ppb_broker_proxy.cc
@@ -181,8 +181,7 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<Broker> object(new Broker(result));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(new Broker(result));
 }
 
 bool PPB_Broker_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_buffer_proxy.cc b/ppapi/proxy/ppb_buffer_proxy.cc
index 19442ea9..57dba6c 100644
--- a/ppapi/proxy/ppb_buffer_proxy.cc
+++ b/ppapi/proxy/ppb_buffer_proxy.cc
@@ -116,8 +116,8 @@
     const HostResource& resource,
     base::SharedMemoryHandle shm_handle,
     uint32_t size) {
-  linked_ptr<Buffer> object(new Buffer(resource, shm_handle, size));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new Buffer(resource, shm_handle, size));
 }
 
 bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_context_3d_proxy.cc b/ppapi/proxy/ppb_context_3d_proxy.cc
index 9bf9098..1c33a22 100644
--- a/ppapi/proxy/ppb_context_3d_proxy.cc
+++ b/ppapi/proxy/ppb_context_3d_proxy.cc
@@ -578,7 +578,7 @@
 
   if (result.is_null())
     return 0;
-  linked_ptr<Context3D> context_3d(new Context3D(result));
+  scoped_refptr<Context3D> context_3d(new Context3D(result));
   if (!context_3d->CreateImplementation())
     return 0;
   return PluginResourceTracker::GetInstance()->AddResource(context_3d);
diff --git a/ppapi/proxy/ppb_file_chooser_proxy.cc b/ppapi/proxy/ppb_file_chooser_proxy.cc
index d8ea705..a5bb745 100644
--- a/ppapi/proxy/ppb_file_chooser_proxy.cc
+++ b/ppapi/proxy/ppb_file_chooser_proxy.cc
@@ -163,8 +163,8 @@
 
   if (result.is_null())
     return 0;
-  linked_ptr<FileChooser> object(new FileChooser(result));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new FileChooser(result));
 }
 
 bool PPB_FileChooser_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_file_ref_proxy.cc b/ppapi/proxy/ppb_file_ref_proxy.cc
index 5e4b706..d94a3f2 100644
--- a/ppapi/proxy/ppb_file_ref_proxy.cc
+++ b/ppapi/proxy/ppb_file_ref_proxy.cc
@@ -228,8 +228,8 @@
   if (serialized.resource.is_null())
     return 0;  // Resource invalid.
 
-  linked_ptr<FileRef> object(new FileRef(serialized));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new FileRef(serialized));
 }
 
 void PPB_FileRef_Proxy::OnMsgCreate(const HostResource& file_system,
diff --git a/ppapi/proxy/ppb_file_system_proxy.cc b/ppapi/proxy/ppb_file_system_proxy.cc
index c13a163..4851144 100644
--- a/ppapi/proxy/ppb_file_system_proxy.cc
+++ b/ppapi/proxy/ppb_file_system_proxy.cc
@@ -144,8 +144,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<FileSystem> object(new FileSystem(result, type));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new FileSystem(result, type));
 }
 
 bool PPB_FileSystem_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_flash_menu_proxy.cc b/ppapi/proxy/ppb_flash_menu_proxy.cc
index a9c195c..c3d26aa 100644
--- a/ppapi/proxy/ppb_flash_menu_proxy.cc
+++ b/ppapi/proxy/ppb_flash_menu_proxy.cc
@@ -122,8 +122,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<FlashMenu> menu(new FlashMenu(result));
-  return PluginResourceTracker::GetInstance()->AddResource(menu);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new FlashMenu(result));
 }
 
 bool PPB_Flash_Menu_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_flash_net_connector_proxy.cc b/ppapi/proxy/ppb_flash_net_connector_proxy.cc
index ba2c2185d..6d5a9ce 100644
--- a/ppapi/proxy/ppb_flash_net_connector_proxy.cc
+++ b/ppapi/proxy/ppb_flash_net_connector_proxy.cc
@@ -230,8 +230,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<FlashNetConnector> object(new FlashNetConnector(result));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new FlashNetConnector(result));
 }
 
 bool PPB_Flash_NetConnector_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_flash_tcp_socket_proxy.cc b/ppapi/proxy/ppb_flash_tcp_socket_proxy.cc
index 6a6ab50..c474ed00 100644
--- a/ppapi/proxy/ppb_flash_tcp_socket_proxy.cc
+++ b/ppapi/proxy/ppb_flash_tcp_socket_proxy.cc
@@ -9,7 +9,6 @@
 #include <map>
 
 #include "base/logging.h"
-#include "base/memory/linked_ptr.h"
 #include "base/message_loop.h"
 #include "base/scoped_ptr.h"
 #include "base/task.h"
@@ -375,9 +374,8 @@
   if (socket_id == 0)
     return 0;
 
-  linked_ptr<FlashTCPSocket> object(new FlashTCPSocket(
-      HostResource::MakeInstanceOnly(instance), socket_id));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new FlashTCPSocket(HostResource::MakeInstanceOnly(instance), socket_id));
 }
 
 bool PPB_Flash_TCPSocket_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_graphics_2d_proxy.cc b/ppapi/proxy/ppb_graphics_2d_proxy.cc
index c74e244d..a003792 100644
--- a/ppapi/proxy/ppb_graphics_2d_proxy.cc
+++ b/ppapi/proxy/ppb_graphics_2d_proxy.cc
@@ -182,9 +182,8 @@
       &result));
   if (result.is_null())
     return 0;
-  linked_ptr<Graphics2D> graphics_2d(new Graphics2D(result, size,
-                                                    is_always_opaque));
-  return PluginResourceTracker::GetInstance()->AddResource(graphics_2d);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new Graphics2D(result, size, is_always_opaque));
 }
 
 bool PPB_Graphics2D_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_graphics_3d_proxy.cc b/ppapi/proxy/ppb_graphics_3d_proxy.cc
index 11250d9..a82c9a5 100644
--- a/ppapi/proxy/ppb_graphics_3d_proxy.cc
+++ b/ppapi/proxy/ppb_graphics_3d_proxy.cc
@@ -448,7 +448,7 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<Graphics3D> graphics_3d(new Graphics3D(result));
+  scoped_refptr<Graphics3D> graphics_3d(new Graphics3D(result));
   if (!graphics_3d->Init())
     return 0;
 
diff --git a/ppapi/proxy/ppb_input_event_proxy.cc b/ppapi/proxy/ppb_input_event_proxy.cc
index 4c265de..056bd29a 100644
--- a/ppapi/proxy/ppb_input_event_proxy.cc
+++ b/ppapi/proxy/ppb_input_event_proxy.cc
@@ -121,9 +121,8 @@
 PP_Resource PPB_InputEvent_Proxy::CreateProxyResource(
     PP_Instance instance,
     const InputEventData& data) {
-  linked_ptr<InputEvent> object(new InputEvent(
-      HostResource::MakeInstanceOnly(instance), data));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new InputEvent(HostResource::MakeInstanceOnly(instance), data));
 }
 
 bool PPB_InputEvent_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_pdf_proxy.cc b/ppapi/proxy/ppb_pdf_proxy.cc
index f7dbb28..6df53557 100644
--- a/ppapi/proxy/ppb_pdf_proxy.cc
+++ b/ppapi/proxy/ppb_pdf_proxy.cc
@@ -75,8 +75,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<PrivateFontFile> object(new PrivateFontFile(result));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new PrivateFontFile(result));
 }
 
 bool GetFontTableForPrivateFontFile(PP_Resource font_file,
diff --git a/ppapi/proxy/ppb_surface_3d_proxy.cc b/ppapi/proxy/ppb_surface_3d_proxy.cc
index 2e432bc..e522155 100644
--- a/ppapi/proxy/ppb_surface_3d_proxy.cc
+++ b/ppapi/proxy/ppb_surface_3d_proxy.cc
@@ -136,7 +136,7 @@
 
   if (result.is_null())
     return 0;
-  linked_ptr<Surface3D> surface_3d(new Surface3D(result));
+  scoped_refptr<Surface3D> surface_3d(new Surface3D(result));
   PP_Resource resource =
       PluginResourceTracker::GetInstance()->AddResource(surface_3d);
   surface_3d->set_resource(resource);
diff --git a/ppapi/proxy/ppb_url_loader_proxy.cc b/ppapi/proxy/ppb_url_loader_proxy.cc
index 1fc4db1..70a3126 100644
--- a/ppapi/proxy/ppb_url_loader_proxy.cc
+++ b/ppapi/proxy/ppb_url_loader_proxy.cc
@@ -357,8 +357,8 @@
 // static
 PP_Resource PPB_URLLoader_Proxy::TrackPluginResource(
     const HostResource& url_loader_resource) {
-  linked_ptr<URLLoader> object(new URLLoader(url_loader_resource));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new URLLoader(url_loader_resource));
 }
 
 // static
diff --git a/ppapi/proxy/ppb_url_request_info_proxy.cc b/ppapi/proxy/ppb_url_request_info_proxy.cc
index 9d545a2..e2b96cb 100644
--- a/ppapi/proxy/ppb_url_request_info_proxy.cc
+++ b/ppapi/proxy/ppb_url_request_info_proxy.cc
@@ -139,8 +139,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<URLRequestInfo> object(new URLRequestInfo(result));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new URLRequestInfo(result));
 }
 
 bool PPB_URLRequestInfo_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_url_response_info_proxy.cc b/ppapi/proxy/ppb_url_response_info_proxy.cc
index 7d6f17d..b9df466 100644
--- a/ppapi/proxy/ppb_url_response_info_proxy.cc
+++ b/ppapi/proxy/ppb_url_response_info_proxy.cc
@@ -103,8 +103,8 @@
 // static
 PP_Resource PPB_URLResponseInfo_Proxy::CreateResponseForResource(
     const HostResource& resource) {
-  linked_ptr<URLResponseInfo> object(new URLResponseInfo(resource));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new URLResponseInfo(resource));
 }
 
 bool PPB_URLResponseInfo_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_video_capture_proxy.cc b/ppapi/proxy/ppb_video_capture_proxy.cc
index dc253d14..e957315 100644
--- a/ppapi/proxy/ppb_video_capture_proxy.cc
+++ b/ppapi/proxy/ppb_video_capture_proxy.cc
@@ -301,8 +301,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<VideoCapture> object(new VideoCapture(result));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new VideoCapture(result));
 }
 
 bool PPB_VideoCapture_Proxy::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/ppb_video_decoder_proxy.cc b/ppapi/proxy/ppb_video_decoder_proxy.cc
index 97c2348..dc65d9f 100644
--- a/ppapi/proxy/ppb_video_decoder_proxy.cc
+++ b/ppapi/proxy/ppb_video_decoder_proxy.cc
@@ -75,7 +75,7 @@
   if (enter_context.failed())
     return NULL;
 
-  scoped_ptr<VideoDecoder> decoder(new VideoDecoder(resource));
+  scoped_refptr<VideoDecoder> decoder(new VideoDecoder(resource));
   if (decoder->Init(context3d_id, enter_context.object(), config))
     return decoder.release();
   return NULL;
@@ -253,10 +253,8 @@
   if (result.is_null())
     return 0;
 
-  linked_ptr<VideoDecoder> video_decoder(
+  return PluginResourceTracker::GetInstance()->AddResource(
       VideoDecoder::Create(result, context3d_id, config));
-
-  return PluginResourceTracker::GetInstance()->AddResource(video_decoder);
 }
 
 void PPB_VideoDecoder_Proxy::OnMsgCreate(
diff --git a/ppapi/proxy/resource_creation_proxy.cc b/ppapi/proxy/resource_creation_proxy.cc
index 18f4819..596f7a5 100644
--- a/ppapi/proxy/resource_creation_proxy.cc
+++ b/ppapi/proxy/resource_creation_proxy.cc
@@ -158,9 +158,8 @@
   if (!ppapi::FontImpl::IsPPFontDescriptionValid(*description))
     return 0;
 
-  linked_ptr<Font> object(new Font(HostResource::MakeInstanceOnly(instance),
-                                   *description));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new Font(HostResource::MakeInstanceOnly(instance), *description));
 }
 
 PP_Resource ResourceCreationProxy::CreateGraphics2D(PP_Instance instance,
@@ -192,8 +191,8 @@
   PP_ImageDataDesc desc;
   memcpy(&desc, image_data_desc.data(), sizeof(PP_ImageDataDesc));
 
-  linked_ptr<ImageData> object(new ImageData(result, desc, image_handle));
-  return PluginResourceTracker::GetInstance()->AddResource(object);
+  return PluginResourceTracker::GetInstance()->AddResource(
+      new ImageData(result, desc, image_handle));
 }
 
 PP_Resource ResourceCreationProxy::CreateKeyboardInputEvent(