| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium 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 WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_H_ | |
| 6 #define WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/ref_counted.h" | |
| 10 #include "ppapi/c/pp_resource.h" | |
| 11 #include "webkit/glue/plugins/pepper_resource_tracker.h" | |
| 12 | |
| 13 namespace pepper { | |
| 14 | |
| 15 // If you inherit from resource, make sure you add the class name here. | |
| 16 #define FOR_ALL_RESOURCES(F) \ | |
| 17 F(Audio) \ | |
| 18 F(AudioConfig) \ | |
| 19 F(Buffer) \ | |
| 20 F(DirectoryReader) \ | |
| 21 F(FileChooser) \ | |
| 22 F(FileIO) \ | |
| 23 F(FileRef) \ | |
| 24 F(FileSystem) \ | |
| 25 F(Font) \ | |
| 26 F(Graphics2D) \ | |
| 27 F(Graphics3D) \ | |
| 28 F(ImageData) \ | |
| 29 F(MockResource) \ | |
| 30 F(ObjectVar) \ | |
| 31 F(PluginModule) \ | |
| 32 F(PrivateFontFile) \ | |
| 33 F(Scrollbar) \ | |
| 34 F(StringVar) \ | |
| 35 F(Transport) \ | |
| 36 F(URLLoader) \ | |
| 37 F(URLRequestInfo) \ | |
| 38 F(URLResponseInfo) \ | |
| 39 F(Var) \ | |
| 40 F(VarObjectClass) \ | |
| 41 F(VideoDecoder) \ | |
| 42 F(Widget) | |
| 43 | |
| 44 // Forward declaration of Resource classes. | |
| 45 #define DECLARE_RESOURCE_CLASS(RESOURCE) class RESOURCE; | |
| 46 FOR_ALL_RESOURCES(DECLARE_RESOURCE_CLASS) | |
| 47 #undef DECLARE_RESOURCE_CLASS | |
| 48 | |
| 49 class Resource : public base::RefCountedThreadSafe<Resource> { | |
| 50 public: | |
| 51 explicit Resource(PluginModule* module); | |
| 52 virtual ~Resource(); | |
| 53 | |
| 54 // Returns NULL if the resource is invalid or is a different type. | |
| 55 template<typename T> | |
| 56 static scoped_refptr<T> GetAs(PP_Resource res) { | |
| 57 scoped_refptr<Resource> resource = ResourceTracker::Get()->GetResource(res); | |
| 58 return resource ? resource->Cast<T>() : NULL; | |
| 59 } | |
| 60 | |
| 61 PluginModule* module() const { return module_; } | |
| 62 | |
| 63 // Cast the resource into a specified type. This will return NULL if the | |
| 64 // resource does not match the specified type. Specializations of this | |
| 65 // template call into As* functions. | |
| 66 template <typename T> T* Cast() { return NULL; } | |
| 67 | |
| 68 // Returns an resource id of this object. If the object doesn't have a | |
| 69 // resource id, new one is created with plugin refcount of 1. If it does, | |
| 70 // the refcount is incremented. Use this when you need to return a new | |
| 71 // reference to the plugin. | |
| 72 PP_Resource GetReference(); | |
| 73 | |
| 74 // Returns the resource ID of this object OR NULL IF THERE IS NONE ASSIGNED. | |
| 75 // This will happen if the plugin doesn't have a reference to the given | |
| 76 // resource. The resource will not be addref'ed. | |
| 77 // | |
| 78 // This should only be used as an input parameter to the plugin for status | |
| 79 // updates in the proxy layer, where if the plugin has no reference, it will | |
| 80 // just give up since nothing needs to be updated. | |
| 81 // | |
| 82 // Generally you should use GetReference instead. This is why it has this | |
| 83 // obscure name rather than pp_resource(). | |
| 84 PP_Resource GetReferenceNoAddRef() const; | |
| 85 | |
| 86 // When you need to ensure that a resource has a reference, but you do not | |
| 87 // want to increase the refcount (for example, if you need to call a plugin | |
| 88 // callback function with a reference), you can use this class. For example: | |
| 89 // | |
| 90 // plugin_callback(.., ScopedResourceId(resource).id, ...); | |
| 91 class ScopedResourceId { | |
| 92 public: | |
| 93 explicit ScopedResourceId(Resource* resource) | |
| 94 : id(resource->GetReference()) {} | |
| 95 ~ScopedResourceId() { | |
| 96 ResourceTracker::Get()->UnrefResource(id); | |
| 97 } | |
| 98 const PP_Resource id; | |
| 99 }; | |
| 100 | |
| 101 private: | |
| 102 // Type-specific getters for individual resource types. These will return | |
| 103 // NULL if the resource does not match the specified type. Used by the Cast() | |
| 104 // function. | |
| 105 #define DEFINE_TYPE_GETTER(RESOURCE) \ | |
| 106 virtual RESOURCE* As##RESOURCE(); | |
| 107 FOR_ALL_RESOURCES(DEFINE_TYPE_GETTER) | |
| 108 #undef DEFINE_TYPE_GETTER | |
| 109 | |
| 110 // If referenced by a plugin, holds the id of this resource object. Do not | |
| 111 // access this member directly, because it is possible that the plugin holds | |
| 112 // no references to the object, and therefore the resource_id_ is zero. Use | |
| 113 // either GetReference() to obtain a new resource_id and increase the | |
| 114 // refcount, or TemporaryReference when you do not want to increase the | |
| 115 // refcount. | |
| 116 PP_Resource resource_id_; | |
| 117 | |
| 118 // Non-owning pointer to our module. | |
| 119 PluginModule* module_; | |
| 120 | |
| 121 // Called by the resource tracker when the last plugin reference has been | |
| 122 // dropped. | |
| 123 friend class ResourceTracker; | |
| 124 void StoppedTracking(); | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(Resource); | |
| 127 }; | |
| 128 | |
| 129 // Cast() specializations. | |
| 130 #define DEFINE_RESOURCE_CAST(Type) \ | |
| 131 template <> inline Type* Resource::Cast<Type>() { \ | |
| 132 return As##Type(); \ | |
| 133 } | |
| 134 | |
| 135 FOR_ALL_RESOURCES(DEFINE_RESOURCE_CAST) | |
| 136 #undef DEFINE_RESOURCE_CAST | |
| 137 | |
| 138 } // namespace pepper | |
| 139 | |
| 140 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_RESOURCE_H_ | |
| OLD | NEW |