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_TRACKER_H_ |
| 6 #define PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/hash_tables.h" |
| 12 #include "base/memory/linked_ptr.h" |
| 13 #include "ppapi/c/pp_instance.h" |
| 14 #include "ppapi/c/pp_resource.h" |
| 15 |
| 16 namespace ppapi { |
| 17 |
| 18 class Resource; |
| 19 |
| 20 class ResourceTracker { |
| 21 public: |
| 22 ResourceTracker(); |
| 23 virtual ~ResourceTracker(); |
| 24 |
| 25 // The returned pointer will be NULL if there is no resource. The reference |
| 26 // count of the resource is unaffected. |
| 27 Resource* GetResource(PP_Resource res) const; |
| 28 |
| 29 void AddRefResource(PP_Resource res); |
| 30 void ReleaseResource(PP_Resource res); |
| 31 |
| 32 // Notifies the tracker that a new instance has been created. This must be |
| 33 // called before creating any resources associated with the instance. |
| 34 void DidCreateInstance(PP_Instance instance); |
| 35 |
| 36 // Called when an instance is being deleted. All plugin refs for the |
| 37 // associated resources will be force freed, and the resources (if they still |
| 38 // exist) will be disassociated from the instance. |
| 39 void DidDeleteInstance(PP_Instance instance); |
| 40 |
| 41 // Returns the number of resources associated with the given instance. |
| 42 // Returns 0 if the instance isn't known. |
| 43 int GetLiveObjectsForInstance(PP_Instance instance) const; |
| 44 |
| 45 protected: |
| 46 // This calls AddResource and RemoveResource. |
| 47 friend class Resource; |
| 48 |
| 49 // Adds the given resource to the tracker, associating it with the instance |
| 50 // stored in the resource object. The new resource ID is returned, and the |
| 51 // resource will have 0 plugin refcount. This is called by the resource |
| 52 // constructor. |
| 53 // |
| 54 // Returns 0 if the resource could not be added. |
| 55 virtual PP_Resource AddResource(Resource* object); |
| 56 |
| 57 // The opposite of AddResource, this removes the tracking information for |
| 58 // the given resource. It's called from the resource destructor. |
| 59 virtual void RemoveResource(Resource* object); |
| 60 |
| 61 private: |
| 62 typedef std::set<PP_Resource> ResourceSet; |
| 63 |
| 64 struct InstanceData { |
| 65 // Lists all resources associated with the given instance as non-owning |
| 66 // pointers. This allows us to notify those resources that the instance is |
| 67 // going away (otherwise, they may crash if they outlive the instance). |
| 68 ResourceSet resources; |
| 69 }; |
| 70 typedef base::hash_map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; |
| 71 |
| 72 InstanceMap instance_map_; |
| 73 |
| 74 // For each PP_Resource, keep the object pointer and a plugin use count. |
| 75 // This use count is different then Resource object's RefCount, and is |
| 76 // manipulated using this AddRefResource/UnrefResource. When the plugin use |
| 77 // count is positive, we keep an extra ref on the Resource on |
| 78 // behalf of the plugin. When it drops to 0, we free that ref, keeping |
| 79 // the resource in the list. |
| 80 // |
| 81 // A resource will be in this list as long as the object is alive. |
| 82 typedef std::pair<Resource*, int> ResourceAndRefCount; |
| 83 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; |
| 84 ResourceMap live_resources_; |
| 85 |
| 86 int32 last_resource_value_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); |
| 89 }; |
| 90 |
| 91 } // namespace ppapi |
| 92 |
| 93 #endif // PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
OLD | NEW |