| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_PLUGINS_PPAPI_RESOURCE_H_ | 5 #ifndef WEBKIT_PLUGINS_PPAPI_RESOURCE_H_ |
| 6 #define WEBKIT_PLUGINS_PPAPI_RESOURCE_H_ | 6 #define WEBKIT_PLUGINS_PPAPI_RESOURCE_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" |
| 9 #include "ppapi/c/pp_resource.h" | 10 #include "ppapi/c/pp_resource.h" |
| 10 #include "ppapi/shared_impl/resource_object_base.h" | 11 #include "ppapi/shared_impl/resource.h" |
| 11 #include "webkit/plugins/ppapi/resource_tracker.h" | 12 #include "webkit/plugins/ppapi/resource_tracker.h" |
| 12 | 13 |
| 13 namespace webkit { | 14 namespace webkit { |
| 14 namespace ppapi { | 15 namespace ppapi { |
| 15 | 16 |
| 16 class Resource : public ::ppapi::ResourceObjectBase { | 17 class Resource : public ::ppapi::Resource { |
| 17 public: | 18 public: |
| 18 explicit Resource(PluginInstance* instance); | 19 explicit Resource(PluginInstance* instance); |
| 19 virtual ~Resource(); | 20 virtual ~Resource(); |
| 20 | 21 |
| 21 // Returns the instance owning this resource. This is generally to be | 22 // Returns the instance owning this resource. This is generally to be |
| 22 // non-NULL except if the instance is destroyed and some code internal to the | 23 // non-NULL except if the instance is destroyed and some code internal to the |
| 23 // PPAPI implementation is keeping a reference for some reason. | 24 // PPAPI implementation is keeping a reference for some reason. |
| 24 PluginInstance* instance() const { return instance_; } | 25 PluginInstance* instance() const { return instance_; } |
| 25 | 26 |
| 26 // Clears the instance pointer when the associated PluginInstance will be | |
| 27 // destroyed. | |
| 28 // | |
| 29 // If you override this, be sure to call the base class' implementation. | |
| 30 virtual void ClearInstance(); | |
| 31 | |
| 32 // Returns an resource id of this object. If the object doesn't have a | 27 // Returns an resource id of this object. If the object doesn't have a |
| 33 // resource id, new one is created with plugin refcount of 1. If it does, | 28 // resource id, new one is created with plugin refcount of 1. If it does, |
| 34 // the refcount is incremented. Use this when you need to return a new | 29 // the refcount is incremented. Use this when you need to return a new |
| 35 // reference to the plugin. | 30 // reference to the plugin. |
| 36 PP_Resource GetReference(); | 31 PP_Resource GetReference(); |
| 37 | 32 |
| 38 // Returns the resource ID of this object OR NULL IF THERE IS NONE ASSIGNED. | |
| 39 // This will happen if the plugin doesn't have a reference to the given | |
| 40 // resource. The resource will not be addref'ed. | |
| 41 // | |
| 42 // This should only be used as an input parameter to the plugin for status | |
| 43 // updates in the proxy layer, where if the plugin has no reference, it will | |
| 44 // just give up since nothing needs to be updated. | |
| 45 // | |
| 46 // Generally you should use GetReference instead. This is why it has this | |
| 47 // obscure name rather than pp_resource(). | |
| 48 PP_Resource GetReferenceNoAddRef() const; | |
| 49 | |
| 50 // When you need to ensure that a resource has a reference, but you do not | 33 // When you need to ensure that a resource has a reference, but you do not |
| 51 // want to increase the refcount (for example, if you need to call a plugin | 34 // want to increase the refcount (for example, if you need to call a plugin |
| 52 // callback function with a reference), you can use this class. For example: | 35 // callback function with a reference), you can use this class. For example: |
| 53 // | 36 // |
| 54 // plugin_callback(.., ScopedResourceId(resource).id, ...); | 37 // plugin_callback(.., ScopedResourceId(resource).id, ...); |
| 55 class ScopedResourceId { | 38 class ScopedResourceId { |
| 56 public: | 39 public: |
| 57 explicit ScopedResourceId(Resource* resource) | 40 explicit ScopedResourceId(Resource* resource) |
| 58 : id(resource->GetReference()) {} | 41 : id(resource->GetReference()) {} |
| 59 ~ScopedResourceId() { | 42 ~ScopedResourceId() { |
| 60 ResourceTracker::Get()->UnrefResource(id); | 43 ResourceTracker::Get()->ReleaseResource(id); |
| 61 } | 44 } |
| 62 const PP_Resource id; | 45 const PP_Resource id; |
| 63 }; | 46 }; |
| 64 | 47 |
| 65 // Called by the resource tracker when the last reference from the plugin | 48 // Resource implementation. |
| 66 // was released. For a few types of resources, the resource could still | 49 virtual void LastPluginRefWasDeleted() OVERRIDE; |
| 67 // stay alive if there are other references held by the PPAPI implementation | 50 virtual void InstanceWasDeleted() OVERRIDE; |
| 68 // (possibly for callbacks and things). | |
| 69 // | |
| 70 // If you override this, be sure to call the base class' implementation. | |
| 71 virtual void LastPluginRefWasDeleted(); | |
| 72 | 51 |
| 73 private: | 52 private: |
| 74 // If referenced by a plugin, holds the id of this resource object. Do not | |
| 75 // access this member directly, because it is possible that the plugin holds | |
| 76 // no references to the object, and therefore the resource_id_ is zero. Use | |
| 77 // either GetReference() to obtain a new resource_id and increase the | |
| 78 // refcount, or TemporaryReference when you do not want to increase the | |
| 79 // refcount. | |
| 80 PP_Resource resource_id_; | |
| 81 | |
| 82 // Non-owning pointer to our instance. See getter above. | 53 // Non-owning pointer to our instance. See getter above. |
| 83 PluginInstance* instance_; | 54 PluginInstance* instance_; |
| 84 | 55 |
| 85 DISALLOW_COPY_AND_ASSIGN(Resource); | 56 DISALLOW_COPY_AND_ASSIGN(Resource); |
| 86 }; | 57 }; |
| 87 | 58 |
| 88 } // namespace ppapi | 59 } // namespace ppapi |
| 89 } // namespace webkit | 60 } // namespace webkit |
| 90 | 61 |
| 91 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_H_ | 62 #endif // WEBKIT_PLUGINS_PPAPI_RESOURCE_H_ |
| OLD | NEW |