summaryrefslogtreecommitdiffstats
path: root/webkit/glue/plugins/pepper_resource.h
diff options
context:
space:
mode:
Diffstat (limited to 'webkit/glue/plugins/pepper_resource.h')
-rw-r--r--webkit/glue/plugins/pepper_resource.h48
1 files changed, 43 insertions, 5 deletions
diff --git a/webkit/glue/plugins/pepper_resource.h b/webkit/glue/plugins/pepper_resource.h
index 4e347cb..417a06b 100644
--- a/webkit/glue/plugins/pepper_resource.h
+++ b/webkit/glue/plugins/pepper_resource.h
@@ -5,6 +5,7 @@
#ifndef WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_H_
#define WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_H_
+#include "base/logging.h"
#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "third_party/ppapi/c/pp_resource.h"
@@ -29,8 +30,8 @@ class Widget;
class Resource : public base::RefCountedThreadSafe<Resource> {
public:
- explicit Resource(PluginModule* module);
- virtual ~Resource();
+ explicit Resource(PluginModule* module) : resource_id_(0), module_(module) {}
+ virtual ~Resource() {}
// Returns NULL if the resource is invalid or is a different type.
template<typename T>
@@ -39,8 +40,6 @@ class Resource : public base::RefCountedThreadSafe<Resource> {
return resource ? resource->Cast<T>() : NULL;
}
- PP_Resource GetResource() const;
-
PluginModule* module() const { return module_; }
// Cast the resource into a specified type. This will return NULL if the
@@ -48,6 +47,27 @@ class Resource : public base::RefCountedThreadSafe<Resource> {
// template call into As* functions.
template <typename T> T* Cast() { return NULL; }
+ // Returns an resource id of this object. If the object doesn't have a
+ // resource id, new one is created with plugin refcount of 1. If it does,
+ // the refcount is incremented. Use this when you need to return a new
+ // reference to the plugin.
+ PP_Resource GetReference();
+
+ // When you need to ensure that a resource has a reference, but you do not
+ // want to increase the refcount (for example, if you need to call a plugin
+ // callback function with a reference), you can use this class. For example:
+ //
+ // plugin_callback(.., ScopedResourceId(resource).id, ...);
+ class ScopedResourceId {
+ public:
+ explicit ScopedResourceId(Resource* resource)
+ : id(resource->GetReference()) {}
+ ~ScopedResourceId() {
+ ResourceTracker::Get()->UnrefResource(id);
+ }
+ const PP_Resource id;
+ };
+
private:
// Type-specific getters for individual resource types. These will return
// NULL if the resource does not match the specified type. Used by the Cast()
@@ -66,7 +86,25 @@ class Resource : public base::RefCountedThreadSafe<Resource> {
virtual URLResponseInfo* AsURLResponseInfo() { return NULL; }
virtual Widget* AsWidget() { return NULL; }
- PluginModule* module_; // Non-owning pointer to our module.
+ private:
+ // If referenced by a plugin, holds the id of this resource object. Do not
+ // access this member directly, because it is possible that the plugin holds
+ // no references to the object, and therefore the resource_id_ is zero. Use
+ // either GetReference() to obtain a new resource_id and increase the
+ // refcount, or TemporaryReference when you do not want to increase the
+ // refcount.
+ PP_Resource resource_id_;
+
+ // Non-owning pointer to our module.
+ PluginModule* module_;
+
+ // Called by the resource tracker when the last plugin reference has been
+ // dropped.
+ friend class ResourceTracker;
+ void StoppedTracking() {
+ DCHECK(resource_id_ != 0);
+ resource_id_ = 0;
+ }
DISALLOW_COPY_AND_ASSIGN(Resource);
};