OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 PPAPI_SHARED_IMPL_RESOURCE_H_ |
| 6 #define PPAPI_SHARED_IMPL_RESOURCE_H_ |
| 7 |
| 8 #include <stddef.h> // For NULL. |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "ppapi/c/pp_instance.h" |
| 13 #include "ppapi/c/pp_resource.h" |
| 14 #include "ppapi/shared_impl/host_resource.h" |
| 15 |
| 16 // All resource types should be added here. This implements our hand-rolled |
| 17 // RTTI system since we don't compile with "real" RTTI. |
| 18 #define FOR_ALL_PPAPI_RESOURCE_APIS(F) \ |
| 19 F(PPB_AudioConfig_API) \ |
| 20 F(PPB_AudioTrusted_API) \ |
| 21 F(PPB_Audio_API) \ |
| 22 F(PPB_Broker_API) \ |
| 23 F(PPB_Buffer_API) \ |
| 24 F(PPB_BufferTrusted_API) \ |
| 25 F(PPB_Context3D_API) \ |
| 26 F(PPB_DirectoryReader_API) \ |
| 27 F(PPB_FileChooser_API) \ |
| 28 F(PPB_FileIO_API) \ |
| 29 F(PPB_FileRef_API) \ |
| 30 F(PPB_FileSystem_API) \ |
| 31 F(PPB_Find_API) \ |
| 32 F(PPB_Flash_Menu_API) \ |
| 33 F(PPB_Flash_NetConnector_API) \ |
| 34 F(PPB_Flash_TCPSocket_API) \ |
| 35 F(PPB_Font_API) \ |
| 36 F(PPB_Graphics2D_API) \ |
| 37 F(PPB_Graphics3D_API) \ |
| 38 F(PPB_ImageData_API) \ |
| 39 F(PPB_InputEvent_API) \ |
| 40 F(PPB_LayerCompositor_API) \ |
| 41 F(PPB_PDFFont_API) \ |
| 42 F(PPB_Scrollbar_API) \ |
| 43 F(PPB_Surface3D_API) \ |
| 44 F(PPB_Transport_API) \ |
| 45 F(PPB_URLLoader_API) \ |
| 46 F(PPB_URLRequestInfo_API) \ |
| 47 F(PPB_URLResponseInfo_API) \ |
| 48 F(PPB_VideoCapture_API) \ |
| 49 F(PPB_VideoDecoder_API) \ |
| 50 F(PPB_VideoLayer_API) \ |
| 51 F(PPB_Widget_API) |
| 52 |
| 53 namespace ppapi { |
| 54 |
| 55 // Forward declare all the resource APIs. |
| 56 namespace thunk { |
| 57 #define DECLARE_RESOURCE_CLASS(RESOURCE) class RESOURCE; |
| 58 FOR_ALL_PPAPI_RESOURCE_APIS(DECLARE_RESOURCE_CLASS) |
| 59 #undef DECLARE_RESOURCE_CLASS |
| 60 } // namespace thunk |
| 61 |
| 62 class Resource : public base::RefCounted<Resource> { |
| 63 public: |
| 64 // For constructing non-proxied objects. This just takes the associated |
| 65 // instance, and generates a new resource ID. The host resource will be the |
| 66 // same as the newly-generated resource ID. |
| 67 explicit Resource(PP_Instance instance); |
| 68 |
| 69 // For constructing proxied objects. This takes the resource generated in |
| 70 // the host side, stores it, and allocates a "local" resource ID for use in |
| 71 // the current process. |
| 72 explicit Resource(const HostResource& host_resource); |
| 73 |
| 74 virtual ~Resource(); |
| 75 |
| 76 PP_Instance pp_instance() const { return host_resource_.instance(); } |
| 77 |
| 78 // Returns the resource ID for this object in the current process without |
| 79 // adjusting the refcount. See also GetReference(). |
| 80 PP_Resource pp_resource() const { return pp_resource_; } |
| 81 |
| 82 // Returns the host resource which identifies the resource in the host side |
| 83 // of the process in the case of proxied objects. For in-process objects, |
| 84 // this just identifies the in-process resource ID & instance. |
| 85 const HostResource& host_resource() { return host_resource_; } |
| 86 |
| 87 // Adds a ref on behalf of the plugin and returns the resource ID. This is |
| 88 // normally used when returning a resource to the plugin, where it's |
| 89 // expecting the returned resource to have ownership of a ref passed. |
| 90 // See also pp_resource() to avoid the AddRef. |
| 91 PP_Resource GetReference(); |
| 92 |
| 93 // Called by the resource tracker when the last reference from the plugin |
| 94 // was released. For a few types of resources, the resource could still |
| 95 // stay alive if there are other references held by the PPAPI implementation |
| 96 // (possibly for callbacks and things). |
| 97 virtual void LastPluginRefWasDeleted(); |
| 98 |
| 99 // Called by the resource tracker when the instance is going away but the |
| 100 // object is still alive (this is not the common case, since it requires |
| 101 // something in the implementation to be keeping a ref that keeps the |
| 102 // resource alive. |
| 103 // |
| 104 // You will want to override this if your resource does some kind of |
| 105 // background processing (like maybe network loads) on behalf of the plugin |
| 106 // and you want to stop that when the plugin is deleted. |
| 107 // |
| 108 // Be sure to call this version which clears the instance ID. |
| 109 virtual void InstanceWasDeleted(); |
| 110 |
| 111 // Dynamic casting for this object. Returns the pointer to the given type if |
| 112 // it's supported. Derived classes override the functions they support to |
| 113 // return the interface. |
| 114 #define DEFINE_TYPE_GETTER(RESOURCE) \ |
| 115 virtual thunk::RESOURCE* As##RESOURCE(); |
| 116 FOR_ALL_PPAPI_RESOURCE_APIS(DEFINE_TYPE_GETTER) |
| 117 #undef DEFINE_TYPE_GETTER |
| 118 |
| 119 // Template-based dynamic casting. See specializations below. |
| 120 template <typename T> T* GetAs() { return NULL; } |
| 121 |
| 122 private: |
| 123 // See the getters above. |
| 124 PP_Resource pp_resource_; |
| 125 HostResource host_resource_; |
| 126 |
| 127 DISALLOW_IMPLICIT_CONSTRUCTORS(Resource); |
| 128 }; |
| 129 |
| 130 // Template-based dynamic casting. These specializations forward to the |
| 131 // AsXXX virtual functions to return whether the given type is supported. |
| 132 #define DEFINE_RESOURCE_CAST(RESOURCE) \ |
| 133 template<> inline thunk::RESOURCE* Resource::GetAs() { \ |
| 134 return As##RESOURCE(); \ |
| 135 } |
| 136 FOR_ALL_PPAPI_RESOURCE_APIS(DEFINE_RESOURCE_CAST) |
| 137 #undef DEFINE_RESOURCE_CAST |
| 138 |
| 139 } // namespace ppapi |
| 140 |
| 141 #endif // PPAPI_SHARED_IMPL_RESOURCE_H_ |
OLD | NEW |