| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ | 5 #ifndef PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
| 6 #define PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ | 6 #define PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 8 #include <set> | 10 #include <set> |
| 9 | 11 |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/macros.h" |
| 12 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 16 #include "base/threading/thread_checker_impl.h" | 18 #include "base/threading/thread_checker_impl.h" |
| 17 #include "ppapi/c/pp_instance.h" | 19 #include "ppapi/c/pp_instance.h" |
| 18 #include "ppapi/c/pp_resource.h" | 20 #include "ppapi/c/pp_resource.h" |
| 19 #include "ppapi/shared_impl/ppapi_shared_export.h" | 21 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 20 | 22 |
| 21 namespace ppapi { | 23 namespace ppapi { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 87 |
| 86 // The opposite of AddResource, this removes the tracking information for | 88 // The opposite of AddResource, this removes the tracking information for |
| 87 // the given resource. It's called from the resource destructor. | 89 // the given resource. It's called from the resource destructor. |
| 88 virtual void RemoveResource(Resource* object); | 90 virtual void RemoveResource(Resource* object); |
| 89 | 91 |
| 90 private: | 92 private: |
| 91 // Calls NotifyLastPluginRefWasDeleted on the given resource object and | 93 // Calls NotifyLastPluginRefWasDeleted on the given resource object and |
| 92 // cancels pending callbacks for the resource. | 94 // cancels pending callbacks for the resource. |
| 93 void LastPluginRefWasDeleted(Resource* object); | 95 void LastPluginRefWasDeleted(Resource* object); |
| 94 | 96 |
| 95 int32 GetNextResourceValue(); | 97 int32_t GetNextResourceValue(); |
| 96 | 98 |
| 97 // In debug mode, checks whether |res| comes from the same resource tracker. | 99 // In debug mode, checks whether |res| comes from the same resource tracker. |
| 98 bool CanOperateOnResource(PP_Resource res); | 100 bool CanOperateOnResource(PP_Resource res); |
| 99 | 101 |
| 100 typedef std::set<PP_Resource> ResourceSet; | 102 typedef std::set<PP_Resource> ResourceSet; |
| 101 | 103 |
| 102 struct InstanceData { | 104 struct InstanceData { |
| 103 // Lists all resources associated with the given instance as non-owning | 105 // Lists all resources associated with the given instance as non-owning |
| 104 // pointers. This allows us to notify those resources that the instance is | 106 // pointers. This allows us to notify those resources that the instance is |
| 105 // going away (otherwise, they may crash if they outlive the instance). | 107 // going away (otherwise, they may crash if they outlive the instance). |
| 106 ResourceSet resources; | 108 ResourceSet resources; |
| 107 }; | 109 }; |
| 108 typedef base::hash_map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; | 110 typedef base::hash_map<PP_Instance, linked_ptr<InstanceData> > InstanceMap; |
| 109 | 111 |
| 110 InstanceMap instance_map_; | 112 InstanceMap instance_map_; |
| 111 | 113 |
| 112 // For each PP_Resource, keep the object pointer and a plugin use count. | 114 // For each PP_Resource, keep the object pointer and a plugin use count. |
| 113 // This use count is different then Resource object's RefCount, and is | 115 // This use count is different then Resource object's RefCount, and is |
| 114 // manipulated using this AddRefResource/UnrefResource. When the plugin use | 116 // manipulated using this AddRefResource/UnrefResource. When the plugin use |
| 115 // count is positive, we keep an extra ref on the Resource on | 117 // count is positive, we keep an extra ref on the Resource on |
| 116 // behalf of the plugin. When it drops to 0, we free that ref, keeping | 118 // behalf of the plugin. When it drops to 0, we free that ref, keeping |
| 117 // the resource in the list. | 119 // the resource in the list. |
| 118 // | 120 // |
| 119 // A resource will be in this list as long as the object is alive. | 121 // A resource will be in this list as long as the object is alive. |
| 120 typedef std::pair<Resource*, int> ResourceAndRefCount; | 122 typedef std::pair<Resource*, int> ResourceAndRefCount; |
| 121 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; | 123 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; |
| 122 ResourceMap live_resources_; | 124 ResourceMap live_resources_; |
| 123 | 125 |
| 124 int32 last_resource_value_; | 126 int32_t last_resource_value_; |
| 125 | 127 |
| 126 // On the host side, we want to check that we are only called on the main | 128 // On the host side, we want to check that we are only called on the main |
| 127 // thread. This is to protect us from accidentally using the tracker from | 129 // thread. This is to protect us from accidentally using the tracker from |
| 128 // other threads (especially the IO thread). On the plugin side, the tracker | 130 // other threads (especially the IO thread). On the plugin side, the tracker |
| 129 // is protected by the proxy lock and is thread-safe, so this will be NULL. | 131 // is protected by the proxy lock and is thread-safe, so this will be NULL. |
| 130 scoped_ptr<base::ThreadChecker> thread_checker_; | 132 scoped_ptr<base::ThreadChecker> thread_checker_; |
| 131 | 133 |
| 132 base::WeakPtrFactory<ResourceTracker> weak_ptr_factory_; | 134 base::WeakPtrFactory<ResourceTracker> weak_ptr_factory_; |
| 133 | 135 |
| 134 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); | 136 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); |
| 135 }; | 137 }; |
| 136 | 138 |
| 137 } // namespace ppapi | 139 } // namespace ppapi |
| 138 | 140 |
| 139 #endif // PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ | 141 #endif // PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
| OLD | NEW |