Refactor PPAPI proxy resource handling to maintain which host they came from,
and to map back to that host when calling functions on them. Adds a mapping
between resources generated by the hosts to a new list inside the plugin so
there can't be overlaps.

This means there are now two meanings for a PP_Resource, one in the plugin
process and one in the host process. This is potentially very confusing. I
introduced a new object called a HostResource that always represents a
"host" PP_Resource to try to prevent errors. In the plugin side of the proxy,
it only deals with PP_Resources valid in the plugin, and SerializedResources
valid in the host. It also encapsulates the associated instance, which
simplifies some code.

Each PluginResource object maintains its SerializedResource which the proxy
uses to send to the host for requests. This requires getting the PluginResource
object in more proxy calls.

This fixes a bug in var sending introduced in my previous patch. The var
releasing from EndSendPassRef used the host var rather than the plugin var. I
had to add more plumbing to get the dispatcher at this location and convert
to a plugin var.

I removed the separate file for ImageData and put it in ppb_image_data_proxy
like for the other resource types.

TEST=some unit tests included
BUG=none
Review URL: http://codereview.chromium.org/6334016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72879 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/ppapi/proxy/plugin_resource_tracker.h b/ppapi/proxy/plugin_resource_tracker.h
index ba6ddb3..ee7ad96 100644
--- a/ppapi/proxy/plugin_resource_tracker.h
+++ b/ppapi/proxy/plugin_resource_tracker.h
@@ -6,12 +6,15 @@
 #define PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_
 
 #include <map>
+#include <utility>
 
 #include "base/linked_ptr.h"
 #include "ppapi/c/pp_completion_callback.h"
+#include "ppapi/c/pp_instance.h"
 #include "ppapi/c/pp_stdint.h"
 #include "ppapi/c/pp_resource.h"
 #include "ppapi/c/pp_var.h"
+#include "ppapi/proxy/host_resource.h"
 
 template<typename T> struct DefaultSingletonTraits;
 
@@ -23,6 +26,11 @@
 
 class PluginResourceTracker {
  public:
+  // Called by tests that want to specify a specific ResourceTracker. This
+  // allows them to use a unique one each time and avoids singletons sticking
+  // around across tests.
+  static void SetInstanceForTest(PluginResourceTracker* tracker);
+
   // Returns the global singleton resource tracker for the plugin.
   static PluginResourceTracker* GetInstance();
 
@@ -30,38 +38,24 @@
   // there isn't one.
   PluginResource* GetResourceObject(PP_Resource pp_resource);
 
-  void AddResource(PP_Resource pp_resource, linked_ptr<PluginResource> object);
+  // Adds the given resource object to the tracked list, and returns the
+  // 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);
 
   void AddRefResource(PP_Resource resource);
   void ReleaseResource(PP_Resource resource);
 
-  // Checks if the resource just returned from the renderer is already
-  // tracked by the plugin side and adjusts the refcounts if so.
-  //
-  // When the browser returns a PP_Resource, it could be a new one, in which
-  // case the proxy wants to create a corresponding object and call
-  // AddResource() on it. However, if the resouce is already known, then the
-  // refcount needs to be adjusted in both the plugin and the renderer side
-  // and no such object needs to be created.
-  //
-  // Returns true if the resource was previously known. The refcount will be
-  // fixed up such that it's ready to use by the plugin. A proxy can then
-  // just return the resource without doing any more work.
-  //
-  // Typical usage:
-  //
-  //   PP_Resource result;
-  //   dispatcher->Send(new MyMessage(..., &result));
-  //   if (PluginResourceTracker::GetInstance()->
-  //           PreparePreviouslyTrackedResource(result))
-  //     return result;
-  //   ... create resource object ...
-  //   PluginResourceTracker::GetInstance()->AddResource(result, object);
-  //   return result;
-  bool PreparePreviouslyTrackedResource(PP_Resource resource);
+  // Given a host resource, maps it to an existing plugin resource ID if it
+  // exists, or returns 0 on failure.
+  PP_Resource PluginResourceForHostResource(
+      const HostResource& resource) const;
 
  private:
   friend struct DefaultSingletonTraits<PluginResourceTracker>;
+  friend class PluginResourceTrackerTest;
+  friend class PluginProxyTest;
 
   PluginResourceTracker();
   ~PluginResourceTracker();
@@ -86,8 +80,19 @@
   void SendReleaseResourceToHost(PP_Resource resource_id,
                                  PluginResource* resource);
 
+  // Map of plugin resource IDs to the information tracking that resource.
   typedef std::map<PP_Resource, ResourceInfo> ResourceMap;
   ResourceMap resource_map_;
+
+  // Map of host instance/resource pairs to a plugin resource ID.
+  typedef std::map<HostResource, PP_Resource> HostResourceMap;
+  HostResourceMap host_resource_map_;
+
+  // Tracks the last ID we've sent out as a plugin resource so we don't send
+  // duplicates.
+  PP_Resource last_resource_id_;
+
+  DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker);
 };
 
 }  // namespace proxy