1 // Copyright (c) 2012 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.
5 #ifndef PPAPI_SHARED_IMPL_RESOURCE_H_
6 #define PPAPI_SHARED_IMPL_RESOURCE_H_
8 #include <stddef.h> // For NULL.
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "ppapi/c/pp_instance.h"
15 #include "ppapi/c/pp_resource.h"
16 #include "ppapi/c/ppb_console.h"
17 #include "ppapi/shared_impl/host_resource.h"
19 // All resource types should be added here. This implements our hand-rolled
20 // RTTI system since we don't compile with "real" RTTI.
21 #define FOR_ALL_PPAPI_RESOURCE_APIS(F) \
22 F(ExtensionsCommon_API) \
24 F(PPB_AudioConfig_API) \
25 F(PPB_AudioInput_API) \
26 F(PPB_AudioTrusted_API) \
28 F(PPB_Broker_Instance_API) \
29 F(PPB_BrowserFont_Singleton_API) \
30 F(PPB_BrowserFont_Trusted_API) \
32 F(PPB_DeviceRef_API) \
33 F(PPB_Ext_CrxFileSystem_Private_API) \
34 F(PPB_FileChooser_API) \
37 F(PPB_FileSystem_API) \
39 F(PPB_Flash_Clipboard_API) \
40 F(PPB_Flash_DRM_API) \
41 F(PPB_Flash_File_API) \
42 F(PPB_Flash_FontFile_API) \
43 F(PPB_Flash_Fullscreen_API) \
44 F(PPB_Flash_Functions_API) \
45 F(PPB_Flash_Menu_API) \
46 F(PPB_Flash_MessageLoop_API) \
48 F(PPB_Graphics2D_API) \
49 F(PPB_Graphics3D_API) \
50 F(PPB_HostResolver_API) \
51 F(PPB_HostResolver_Private_API) \
52 F(PPB_ImageData_API) \
53 F(PPB_InputEvent_API) \
54 F(PPB_LayerCompositor_API) \
55 F(PPB_MessageLoop_API) \
56 F(PPB_NetAddress_API) \
57 F(PPB_NetworkList_API) \
58 F(PPB_NetworkMonitor_Private_API) \
61 F(PPB_ResourceArray_API) \
62 F(PPB_Scrollbar_API) \
63 F(PPB_Talk_Private_API) \
64 F(PPB_TrueTypeFont_API) \
65 F(PPB_TrueTypeFont_Singleton_API) \
66 F(PPB_TCPServerSocket_Private_API) \
67 F(PPB_TCPSocket_API) \
68 F(PPB_TCPSocket_Private_API) \
69 F(PPB_UDPSocket_API) \
70 F(PPB_UDPSocket_Private_API) \
71 F(PPB_URLLoader_API) \
72 F(PPB_URLRequestInfo_API) \
73 F(PPB_URLResponseInfo_API) \
74 F(PPB_VideoCapture_API) \
75 F(PPB_VideoDecoder_API) \
76 F(PPB_VideoDestination_Private_API) \
77 F(PPB_VideoLayer_API) \
78 F(PPB_VideoSource_Private_API) \
80 F(PPB_WebSocket_API) \
82 F(PPB_X509Certificate_Private_API)
90 // Normally we shouldn't reply on proxy here, but this is to support
91 // OnReplyReceived. See that comment.
93 class ResourceMessageReplyParams
;
96 // Forward declare all the resource APIs.
98 #define DECLARE_RESOURCE_CLASS(RESOURCE) class RESOURCE;
99 FOR_ALL_PPAPI_RESOURCE_APIS(DECLARE_RESOURCE_CLASS
)
100 #undef DECLARE_RESOURCE_CLASS
103 // Resources have slightly different registration behaviors when the're an
104 // in-process ("impl") resource in the host (renderer) process, or when they're
105 // a proxied resource in the plugin process. This enum differentiates those
107 enum ResourceObjectType
{
112 class PPAPI_SHARED_EXPORT Resource
: public base::RefCounted
<Resource
> {
114 // Constructor for impl and non-proxied, instance-only objects.
116 // For constructing "impl" (non-proxied) objects, this just takes the
117 // associated instance, and generates a new resource ID. The host resource
118 // will be the same as the newly-generated resource ID. For all objects in
119 // the renderer (host) process, you'll use this constructor and call it with
122 // For proxied objects, this will create an "instance-only" object which
123 // lives only in the plugin and doesn't have a corresponding object in the
124 // host. If you have a host resource ID, use the constructor below which
125 // takes that HostResource value.
126 Resource(ResourceObjectType type
, PP_Instance instance
);
128 // For constructing given a host resource.
130 // For OBJECT_IS_PROXY objects, this takes the resource generated in the host
131 // side, stores it, and allocates a "local" resource ID for use in the
134 // For OBJECT_IS_IMPL, the host resource ID must be 0, since there should be
135 // no host resource generated (impl objects should generate their own). The
136 // reason for supporting this constructor at all for the IMPL case is that
137 // some shared objects use a host resource for both modes to keep things the
139 Resource(ResourceObjectType type
, const HostResource
& host_resource
);
141 // Constructor for untracked objects. These have no associated instance. Use
142 // this with care, as the object is likely to persist for the lifetime of the
143 // plugin module. This is appropriate in some rare cases, like the
144 // PPB_MessageLoop resource for the main thread.
146 explicit Resource(Untracked
);
150 PP_Instance
pp_instance() const { return host_resource_
.instance(); }
152 // Returns the resource ID for this object in the current process without
153 // adjusting the refcount. See also GetReference().
154 PP_Resource
pp_resource() const { return pp_resource_
; }
156 // Returns the host resource which identifies the resource in the host side
157 // of the process in the case of proxied objects. For in-process objects,
158 // this just identifies the in-process resource ID & instance.
159 const HostResource
& host_resource() { return host_resource_
; }
161 // Adds a ref on behalf of the plugin and returns the resource ID. This is
162 // normally used when returning a resource to the plugin, where it's
163 // expecting the returned resource to have ownership of a ref passed.
164 // See also pp_resource() to avoid the AddRef.
165 PP_Resource
GetReference();
167 // Called by the resource tracker when the last reference from the plugin
168 // was released. For a few types of resources, the resource could still
169 // stay alive if there are other references held by the PPAPI implementation
170 // (possibly for callbacks and things).
172 // Note that subclasses except PluginResource should override
173 // LastPluginRefWasDeleted() to be notified.
174 virtual void NotifyLastPluginRefWasDeleted();
176 // Called by the resource tracker when the instance is going away but the
177 // object is still alive (this is not the common case, since it requires
178 // something in the implementation to be keeping a ref that keeps the
181 // You will want to override this if your resource does some kind of
182 // background processing (like maybe network loads) on behalf of the plugin
183 // and you want to stop that when the plugin is deleted.
185 // Note that subclasses except PluginResource should override
186 // InstanceWasDeleted() to be notified.
187 virtual void NotifyInstanceWasDeleted();
189 // Dynamic casting for this object. Returns the pointer to the given type if
190 // it's supported. Derived classes override the functions they support to
191 // return the interface.
192 #define DEFINE_TYPE_GETTER(RESOURCE) \
193 virtual thunk::RESOURCE* As##RESOURCE();
194 FOR_ALL_PPAPI_RESOURCE_APIS(DEFINE_TYPE_GETTER
)
195 #undef DEFINE_TYPE_GETTER
197 // Template-based dynamic casting. See specializations below.
198 template <typename T
> T
* GetAs() { return NULL
; }
200 // Called when a PpapiPluginMsg_ResourceReply reply is received for a
201 // previous CallRenderer. The message is the nested reply message, which may
202 // be an empty message (depending on what the host sends).
204 // The default implementation will assert (if you send a request, you should
205 // override this function).
207 // (This function would make more conceptual sense on PluginResource but we
208 // need to call this function from general code that doesn't know how to
209 // distinguish the classes.)
210 virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams
& params
,
211 const IPC::Message
& msg
);
214 // Logs a message to the console from this resource.
215 void Log(PP_LogLevel level
, const std::string
& message
);
217 // Removes the resource from the ResourceTracker's tables. This normally
218 // happens as part of Resource destruction, but if a subclass destructor
219 // has a risk of re-entering destruction via the ResourceTracker, it can
220 // call this explicitly to get rid of the table entry before continuing
221 // with the destruction. If the resource is not in the ResourceTracker's
222 // tables, silently does nothing. See http://crbug.com/159429.
223 void RemoveFromResourceTracker();
225 // Notifications for subclasses.
226 virtual void LastPluginRefWasDeleted() {}
227 virtual void InstanceWasDeleted() {}
230 // See the getters above.
231 PP_Resource pp_resource_
;
232 HostResource host_resource_
;
234 DISALLOW_IMPLICIT_CONSTRUCTORS(Resource
);
237 // Template-based dynamic casting. These specializations forward to the
238 // AsXXX virtual functions to return whether the given type is supported.
239 #define DEFINE_RESOURCE_CAST(RESOURCE) \
240 template<> inline thunk::RESOURCE* Resource::GetAs() { \
241 return As##RESOURCE(); \
243 FOR_ALL_PPAPI_RESOURCE_APIS(DEFINE_RESOURCE_CAST
)
244 #undef DEFINE_RESOURCE_CAST
248 #endif // PPAPI_SHARED_IMPL_RESOURCE_H_