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