OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Native Client Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
| 6 #define NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
| 7 |
| 8 #include "native_client/src/include/nacl_base.h" |
| 9 #include "native_client/src/include/ref_counted.h" |
| 10 #include "native_client/src/shared/ppapi_proxy/plugin_resource_tracker.h" |
| 11 #include "ppapi/c/pp_resource.h" |
| 12 |
| 13 namespace ppapi_proxy { |
| 14 |
| 15 class PluginModule; |
| 16 |
| 17 // If you inherit from resource, make sure you add the class name here. |
| 18 #define FOR_ALL_RESOURCES(F) \ |
| 19 F(Audio) \ |
| 20 F(AudioConfig) \ |
| 21 F(Buffer) \ |
| 22 F(Graphics2D) \ |
| 23 F(Graphics3D) \ |
| 24 F(ImageData) |
| 25 |
| 26 // Forward declaration of PluginResource classes. |
| 27 #define DECLARE_RESOURCE_CLASS(RESOURCE) class RESOURCE; |
| 28 FOR_ALL_RESOURCES(DECLARE_RESOURCE_CLASS) |
| 29 #undef DECLARE_RESOURCE_CLASS |
| 30 |
| 31 class PluginResource : public nacl::RefCounted<PluginResource> { |
| 32 public: |
| 33 explicit PluginResource(PluginModule* module); |
| 34 virtual ~PluginResource(); |
| 35 |
| 36 // Returns NULL if the resource is invalid or is a different type. |
| 37 template<typename T> |
| 38 static scoped_refptr<T> GetAs(PP_Resource res) { |
| 39 scoped_refptr<PluginResource> resource = |
| 40 PluginResourceTracker::Get()->GetResource(res); |
| 41 return resource ? resource->Cast<T>() : NULL; |
| 42 } |
| 43 |
| 44 PluginModule* module() const { return module_; } |
| 45 |
| 46 // Cast the resource into a specified type. This will return NULL if the |
| 47 // resource does not match the specified type. Specializations of this |
| 48 // template call into As* functions. |
| 49 template <typename T> T* Cast() { return NULL; } |
| 50 |
| 51 // Returns an resource id of this object. If the object doesn't have a |
| 52 // resource id, new one is created with plugin refcount of 1. If it does, |
| 53 // the refcount is incremented. Use this when you need to return a new |
| 54 // reference to the plugin. |
| 55 PP_Resource GetReference(); |
| 56 |
| 57 // When you need to ensure that a resource has a reference, but you do not |
| 58 // want to increase the refcount (for example, if you need to call a plugin |
| 59 // callback function with a reference), you can use this class. For example: |
| 60 // |
| 61 // plugin_callback(.., ScopedResourceId(resource).id, ...); |
| 62 class ScopedResourceId { |
| 63 public: |
| 64 explicit ScopedResourceId(PluginResource* resource) |
| 65 : id(resource->GetReference()) {} |
| 66 ~ScopedResourceId() { |
| 67 PluginResourceTracker::Get()->UnrefResource(id); |
| 68 } |
| 69 const PP_Resource id; |
| 70 }; |
| 71 |
| 72 private: |
| 73 // Type-specific getters for individual resource types. These will return |
| 74 // NULL if the resource does not match the specified type. Used by the Cast() |
| 75 // function. |
| 76 #define DEFINE_TYPE_GETTER(RESOURCE) \ |
| 77 virtual RESOURCE* As##RESOURCE() { return NULL; } |
| 78 FOR_ALL_RESOURCES(DEFINE_TYPE_GETTER) |
| 79 #undef DEFINE_TYPE_GETTER |
| 80 |
| 81 // Call this macro in the derived class declaration to actually implement the |
| 82 // type getter. |
| 83 #define IMPLEMENT_RESOURCE(RESOURCE) \ |
| 84 virtual RESOURCE* As##RESOURCE() { return this; } |
| 85 |
| 86 private: |
| 87 // If referenced by a plugin, holds the id of this resource object. Do not |
| 88 // access this member directly, because it is possible that the plugin holds |
| 89 // no references to the object, and therefore the resource_id_ is zero. Use |
| 90 // either GetReference() to obtain a new resource_id and increase the |
| 91 // refcount, or TemporaryReference when you do not want to increase the |
| 92 // refcount. |
| 93 PP_Resource resource_id_; |
| 94 |
| 95 // Non-owning pointer to our module. |
| 96 PluginModule* module_; |
| 97 |
| 98 // Called by the resource tracker when the last plugin reference has been |
| 99 // dropped. |
| 100 friend class PluginResourceTracker; |
| 101 void StoppedTracking(); |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(PluginResource); |
| 104 }; |
| 105 |
| 106 // Cast() specializations. |
| 107 #define DEFINE_RESOURCE_CAST(Type) \ |
| 108 template <> inline Type* PluginResource::Cast<Type>() { \ |
| 109 return As##Type(); \ |
| 110 } |
| 111 |
| 112 FOR_ALL_RESOURCES(DEFINE_RESOURCE_CAST) |
| 113 #undef DEFINE_RESOURCE_CAST |
| 114 |
| 115 #undef FOR_ALL_RESOURCES |
| 116 } // namespace ppapi_proxy |
| 117 |
| 118 #endif // NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
| 119 |
OLD | NEW |