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 #include "ppapi/shared_impl/resource_tracker.h" | 5 #include "ppapi/shared_impl/resource_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "ppapi/shared_impl/callback_tracker.h" | 10 #include "ppapi/shared_impl/callback_tracker.h" |
11 #include "ppapi/shared_impl/id_assignment.h" | 11 #include "ppapi/shared_impl/id_assignment.h" |
12 #include "ppapi/shared_impl/ppapi_globals.h" | 12 #include "ppapi/shared_impl/ppapi_globals.h" |
13 #include "ppapi/shared_impl/proxy_lock.h" | 13 #include "ppapi/shared_impl/proxy_lock.h" |
14 #include "ppapi/shared_impl/resource.h" | 14 #include "ppapi/shared_impl/resource.h" |
15 | 15 |
16 namespace ppapi { | 16 namespace ppapi { |
17 | 17 |
18 ResourceTracker::ResourceTracker(ThreadMode thread_mode) | 18 ResourceTracker::ResourceTracker() |
19 : last_resource_value_(0), | 19 : last_resource_value_(0), |
20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
21 if (thread_mode == SINGLE_THREADED) | |
22 thread_checker_.reset(new base::ThreadChecker); | |
23 } | 21 } |
24 | 22 |
25 ResourceTracker::~ResourceTracker() { | 23 ResourceTracker::~ResourceTracker() { |
26 } | 24 } |
27 | 25 |
28 void ResourceTracker::CheckThreadingPreconditions() const { | 26 Resource* ResourceTracker::GetResource(PP_Resource res) const { |
29 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | 27 CHECK(thread_checker_.CalledOnValidThread()); |
30 ProxyLock::AssertAcquired(); | 28 ProxyLock::AssertAcquired(); |
31 } | |
32 | |
33 Resource* ResourceTracker::GetResource(PP_Resource res) const { | |
34 CheckThreadingPreconditions(); | |
35 ResourceMap::const_iterator i = live_resources_.find(res); | 29 ResourceMap::const_iterator i = live_resources_.find(res); |
36 if (i == live_resources_.end()) | 30 if (i == live_resources_.end()) |
37 return NULL; | 31 return NULL; |
38 return i->second.first; | 32 return i->second.first; |
39 } | 33 } |
40 | 34 |
41 void ResourceTracker::AddRefResource(PP_Resource res) { | 35 void ResourceTracker::AddRefResource(PP_Resource res) { |
42 CheckThreadingPreconditions(); | 36 CHECK(thread_checker_.CalledOnValidThread()); |
43 DLOG_IF(ERROR, !CheckIdType(res, PP_ID_TYPE_RESOURCE)) | 37 DLOG_IF(ERROR, !CheckIdType(res, PP_ID_TYPE_RESOURCE)) |
44 << res << " is not a PP_Resource."; | 38 << res << " is not a PP_Resource."; |
45 ResourceMap::iterator i = live_resources_.find(res); | 39 ResourceMap::iterator i = live_resources_.find(res); |
46 if (i == live_resources_.end()) | 40 if (i == live_resources_.end()) |
47 return; | 41 return; |
48 | 42 |
49 // Prevent overflow of refcount. | 43 // Prevent overflow of refcount. |
50 if (i->second.second == | 44 if (i->second.second == |
51 std::numeric_limits<ResourceAndRefCount::second_type>::max()) | 45 std::numeric_limits<ResourceAndRefCount::second_type>::max()) |
52 return; | 46 return; |
53 | 47 |
54 // When we go from 0 to 1 plugin ref count, keep an additional "real" ref | 48 // When we go from 0 to 1 plugin ref count, keep an additional "real" ref |
55 // on its behalf. | 49 // on its behalf. |
56 if (i->second.second == 0) | 50 if (i->second.second == 0) |
57 i->second.first->AddRef(); | 51 i->second.first->AddRef(); |
58 | 52 |
59 i->second.second++; | 53 i->second.second++; |
60 return; | 54 return; |
61 } | 55 } |
62 | 56 |
63 void ResourceTracker::ReleaseResource(PP_Resource res) { | 57 void ResourceTracker::ReleaseResource(PP_Resource res) { |
64 CheckThreadingPreconditions(); | 58 CHECK(thread_checker_.CalledOnValidThread()); |
65 DLOG_IF(ERROR, !CheckIdType(res, PP_ID_TYPE_RESOURCE)) | 59 DLOG_IF(ERROR, !CheckIdType(res, PP_ID_TYPE_RESOURCE)) |
66 << res << " is not a PP_Resource."; | 60 << res << " is not a PP_Resource."; |
67 ResourceMap::iterator i = live_resources_.find(res); | 61 ResourceMap::iterator i = live_resources_.find(res); |
68 if (i == live_resources_.end()) | 62 if (i == live_resources_.end()) |
69 return; | 63 return; |
70 | 64 |
71 // Prevent underflow of refcount. | 65 // Prevent underflow of refcount. |
72 if (i->second.second == 0) | 66 if (i->second.second == 0) |
73 return; | 67 return; |
74 | 68 |
(...skipping 10 matching lines...) Expand all Loading... |
85 | 79 |
86 void ResourceTracker::ReleaseResourceSoon(PP_Resource res) { | 80 void ResourceTracker::ReleaseResourceSoon(PP_Resource res) { |
87 MessageLoop::current()->PostNonNestableTask( | 81 MessageLoop::current()->PostNonNestableTask( |
88 FROM_HERE, | 82 FROM_HERE, |
89 base::Bind(&ResourceTracker::ReleaseResource, | 83 base::Bind(&ResourceTracker::ReleaseResource, |
90 weak_ptr_factory_.GetWeakPtr(), | 84 weak_ptr_factory_.GetWeakPtr(), |
91 res)); | 85 res)); |
92 } | 86 } |
93 | 87 |
94 void ResourceTracker::DidCreateInstance(PP_Instance instance) { | 88 void ResourceTracker::DidCreateInstance(PP_Instance instance) { |
95 CheckThreadingPreconditions(); | 89 CHECK(thread_checker_.CalledOnValidThread()); |
96 // Due to the infrastructure of some tests, the instance is registered | 90 // Due to the infrastructure of some tests, the instance is registered |
97 // twice in a few cases. It would be nice not to do that and assert here | 91 // twice in a few cases. It would be nice not to do that and assert here |
98 // instead. | 92 // instead. |
99 if (instance_map_.find(instance) != instance_map_.end()) | 93 if (instance_map_.find(instance) != instance_map_.end()) |
100 return; | 94 return; |
101 instance_map_[instance] = linked_ptr<InstanceData>(new InstanceData); | 95 instance_map_[instance] = linked_ptr<InstanceData>(new InstanceData); |
102 } | 96 } |
103 | 97 |
104 void ResourceTracker::DidDeleteInstance(PP_Instance instance) { | 98 void ResourceTracker::DidDeleteInstance(PP_Instance instance) { |
105 CheckThreadingPreconditions(); | 99 CHECK(thread_checker_.CalledOnValidThread()); |
106 InstanceMap::iterator found_instance = instance_map_.find(instance); | 100 InstanceMap::iterator found_instance = instance_map_.find(instance); |
107 | 101 |
108 // Due to the infrastructure of some tests, the instance is unregistered | 102 // Due to the infrastructure of some tests, the instance is unregistered |
109 // twice in a few cases. It would be nice not to do that and assert here | 103 // twice in a few cases. It would be nice not to do that and assert here |
110 // instead. | 104 // instead. |
111 if (found_instance == instance_map_.end()) | 105 if (found_instance == instance_map_.end()) |
112 return; | 106 return; |
113 | 107 |
114 InstanceData& data = *found_instance->second; | 108 InstanceData& data = *found_instance->second; |
115 | 109 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 ResourceMap::iterator found_resource = live_resources_.find(*cur); | 144 ResourceMap::iterator found_resource = live_resources_.find(*cur); |
151 if (found_resource != live_resources_.end()) | 145 if (found_resource != live_resources_.end()) |
152 found_resource->second.first->NotifyInstanceWasDeleted(); | 146 found_resource->second.first->NotifyInstanceWasDeleted(); |
153 cur++; | 147 cur++; |
154 } | 148 } |
155 | 149 |
156 instance_map_.erase(instance); | 150 instance_map_.erase(instance); |
157 } | 151 } |
158 | 152 |
159 int ResourceTracker::GetLiveObjectsForInstance(PP_Instance instance) const { | 153 int ResourceTracker::GetLiveObjectsForInstance(PP_Instance instance) const { |
160 CheckThreadingPreconditions(); | 154 CHECK(thread_checker_.CalledOnValidThread()); |
161 InstanceMap::const_iterator found = instance_map_.find(instance); | 155 InstanceMap::const_iterator found = instance_map_.find(instance); |
162 if (found == instance_map_.end()) | 156 if (found == instance_map_.end()) |
163 return 0; | 157 return 0; |
164 return static_cast<int>(found->second->resources.size()); | 158 return static_cast<int>(found->second->resources.size()); |
165 } | 159 } |
166 | 160 |
167 PP_Resource ResourceTracker::AddResource(Resource* object) { | 161 PP_Resource ResourceTracker::AddResource(Resource* object) { |
168 CheckThreadingPreconditions(); | 162 CHECK(thread_checker_.CalledOnValidThread()); |
169 // If the plugin manages to create too many resources, don't do crazy stuff. | 163 // If the plugin manages to create too many resources, don't do crazy stuff. |
170 if (last_resource_value_ == kMaxPPId) | 164 if (last_resource_value_ == kMaxPPId) |
171 return 0; | 165 return 0; |
172 | 166 |
173 // Allocate an ID. Note there's a rare error condition below that means we | 167 // Allocate an ID. Note there's a rare error condition below that means we |
174 // could end up not using |new_id|, but that's harmless. | 168 // could end up not using |new_id|, but that's harmless. |
175 PP_Resource new_id = MakeTypedId(++last_resource_value_, PP_ID_TYPE_RESOURCE); | 169 PP_Resource new_id = MakeTypedId(++last_resource_value_, PP_ID_TYPE_RESOURCE); |
176 | 170 |
177 // Some objects have a 0 instance, meaning they aren't associated with any | 171 // Some objects have a 0 instance, meaning they aren't associated with any |
178 // instance, so they won't be in |instance_map_|. This is (as of this writing) | 172 // instance, so they won't be in |instance_map_|. This is (as of this writing) |
(...skipping 11 matching lines...) Expand all Loading... |
190 return 0; | 184 return 0; |
191 } | 185 } |
192 found->second->resources.insert(new_id); | 186 found->second->resources.insert(new_id); |
193 } | 187 } |
194 | 188 |
195 live_resources_[new_id] = ResourceAndRefCount(object, 0); | 189 live_resources_[new_id] = ResourceAndRefCount(object, 0); |
196 return new_id; | 190 return new_id; |
197 } | 191 } |
198 | 192 |
199 void ResourceTracker::RemoveResource(Resource* object) { | 193 void ResourceTracker::RemoveResource(Resource* object) { |
200 CheckThreadingPreconditions(); | 194 CHECK(thread_checker_.CalledOnValidThread()); |
201 PP_Resource pp_resource = object->pp_resource(); | 195 PP_Resource pp_resource = object->pp_resource(); |
202 InstanceMap::iterator found = instance_map_.find(object->pp_instance()); | 196 InstanceMap::iterator found = instance_map_.find(object->pp_instance()); |
203 if (found != instance_map_.end()) | 197 if (found != instance_map_.end()) |
204 found->second->resources.erase(pp_resource); | 198 found->second->resources.erase(pp_resource); |
205 live_resources_.erase(pp_resource); | 199 live_resources_.erase(pp_resource); |
206 } | 200 } |
207 | 201 |
208 void ResourceTracker::LastPluginRefWasDeleted(Resource* object) { | 202 void ResourceTracker::LastPluginRefWasDeleted(Resource* object) { |
209 // Bug http://crbug.com/134611 indicates that sometimes the resource tracker | 203 // Bug http://crbug.com/134611 indicates that sometimes the resource tracker |
210 // is null here. This should never be the case since if we have a resource in | 204 // is null here. This should never be the case since if we have a resource in |
(...skipping 10 matching lines...) Expand all Loading... |
221 CHECK(object->pp_instance() || is_message_loop); | 215 CHECK(object->pp_instance() || is_message_loop); |
222 CallbackTracker* callback_tracker = | 216 CallbackTracker* callback_tracker = |
223 PpapiGlobals::Get()->GetCallbackTrackerForInstance(object->pp_instance()); | 217 PpapiGlobals::Get()->GetCallbackTrackerForInstance(object->pp_instance()); |
224 CHECK(callback_tracker || is_message_loop); | 218 CHECK(callback_tracker || is_message_loop); |
225 if (callback_tracker) | 219 if (callback_tracker) |
226 callback_tracker->PostAbortForResource(object->pp_resource()); | 220 callback_tracker->PostAbortForResource(object->pp_resource()); |
227 object->NotifyLastPluginRefWasDeleted(); | 221 object->NotifyLastPluginRefWasDeleted(); |
228 } | 222 } |
229 | 223 |
230 } // namespace ppapi | 224 } // namespace ppapi |
OLD | NEW |