Index: webkit/glue/plugins/pepper_resource.h |
diff --git a/webkit/glue/plugins/pepper_resource.h b/webkit/glue/plugins/pepper_resource.h |
index efcc2115d52b0e944602c4e194101f6eacb6a82a..fdb9d012ec66f7713c6c33f06c25db6dd8c0a5ac 100644 |
--- a/webkit/glue/plugins/pepper_resource.h |
+++ b/webkit/glue/plugins/pepper_resource.h |
@@ -8,6 +8,7 @@ |
#include "base/basictypes.h" |
#include "base/ref_counted.h" |
#include "third_party/ppapi/c/pp_resource.h" |
+#include "webkit/glue/plugins/pepper_resource_tracker.h" |
namespace pepper { |
@@ -28,12 +29,26 @@ class Resource : public base::RefCountedThreadSafe<Resource> { |
explicit Resource(PluginModule* module); |
virtual ~Resource(); |
+ // Returns NULL if the resource is invalid or is a different type. |
+ template<typename T> |
+ static scoped_refptr<T> GetAs(PP_Resource res) { |
+ scoped_refptr<Resource> resource = ResourceTracker::Get()->GetResource(res); |
+ 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 |
+ // resource does not match the specified type. Specializations of this |
+ // template call into As* functions. |
+ template <typename T> T* Cast() { return NULL; } |
+ |
+ private: |
// Type-specific getters for individual resource types. These will return |
- // NULL if the resource does not match the specified type. |
+ // NULL if the resource does not match the specified type. Used by the Cast() |
+ // function. |
virtual Buffer* AsBuffer() { return NULL; } |
virtual DeviceContext2D* AsDeviceContext2D() { return NULL; } |
virtual DirectoryReader* AsDirectoryReader() { return NULL; } |
@@ -45,12 +60,29 @@ class Resource : public base::RefCountedThreadSafe<Resource> { |
virtual URLRequestInfo* AsURLRequestInfo() { return NULL; } |
virtual URLResponseInfo* AsURLResponseInfo() { return NULL; } |
- private: |
PluginModule* module_; // Non-owning pointer to our module. |
DISALLOW_COPY_AND_ASSIGN(Resource); |
}; |
+// Cast() specializations. |
+#define DEFINE_RESOURCE_CAST(Type) \ |
+ template <> inline Type* Resource::Cast<Type>() { \ |
+ return As##Type(); \ |
+ } |
+ |
+DEFINE_RESOURCE_CAST(Buffer) |
+DEFINE_RESOURCE_CAST(DeviceContext2D) |
+DEFINE_RESOURCE_CAST(DirectoryReader) |
+DEFINE_RESOURCE_CAST(FileChooser) |
+DEFINE_RESOURCE_CAST(FileIO) |
+DEFINE_RESOURCE_CAST(FileRef) |
+DEFINE_RESOURCE_CAST(ImageData) |
+DEFINE_RESOURCE_CAST(URLLoader) |
+DEFINE_RESOURCE_CAST(URLRequestInfo) |
+DEFINE_RESOURCE_CAST(URLResponseInfo) |
+ |
+#undef DEFINE_RESOURCE_CAST |
} // namespace pepper |
darin (slow to review)
2010/06/28 19:57:07
nit: add a new line above the close of the namespa
|
#endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_H_ |