| 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 "webkit/plugins/ppapi/host_var_tracker.h" | 5 #include "webkit/plugins/ppapi/host_var_tracker.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ppapi/c/pp_var.h" | 8 #include "ppapi/c/pp_var.h" |
| 9 #include "webkit/plugins/ppapi/host_array_buffer_var.h" | 9 #include "webkit/plugins/ppapi/host_array_buffer_var.h" |
| 10 #include "webkit/plugins/ppapi/npobject_var.h" | 10 #include "webkit/plugins/ppapi/npobject_var.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 93 |
| 94 int HostVarTracker::GetLiveNPObjectVarsForInstance(PP_Instance instance) const { | 94 int HostVarTracker::GetLiveNPObjectVarsForInstance(PP_Instance instance) const { |
| 95 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
| 96 | 96 |
| 97 InstanceMap::const_iterator found = instance_map_.find(instance); | 97 InstanceMap::const_iterator found = instance_map_.find(instance); |
| 98 if (found == instance_map_.end()) | 98 if (found == instance_map_.end()) |
| 99 return 0; | 99 return 0; |
| 100 return static_cast<int>(found->second->size()); | 100 return static_cast<int>(found->second->size()); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void HostVarTracker::ForceFreeNPObjectsForInstance(PP_Instance instance) { | 103 void HostVarTracker::DidDeleteInstance(PP_Instance instance) { |
| 104 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 105 | 105 |
| 106 InstanceMap::iterator found_instance = instance_map_.find(instance); | 106 InstanceMap::iterator found_instance = instance_map_.find(instance); |
| 107 if (found_instance == instance_map_.end()) | 107 if (found_instance == instance_map_.end()) |
| 108 return; // Nothing to do. | 108 return; // Nothing to do. |
| 109 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 109 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
| 110 | 110 |
| 111 // Force delete all var references. It's possible that deleting an object "A" | 111 // Force delete all var references. It's possible that deleting an object "A" |
| 112 // will cause it to delete another object "B" it references, thus removing "B" | 112 // will cause it to delete another object "B" it references, thus removing "B" |
| 113 // from instance_map_. Therefore, we need to make a copy over which we can | 113 // from instance_map_. Therefore, we need to make a copy over which we can |
| 114 // iterate safely. Furthermore, the maps contain WeakPtrs so that we can | 114 // iterate safely. Furthermore, the maps contain WeakPtrs so that we can |
| 115 // detect if the object is gone so that we don't dereference invalid memory. | 115 // detect if the object is gone so that we don't dereference invalid memory. |
| 116 NPObjectToNPObjectVarMap np_object_map_copy = *np_object_map; | 116 NPObjectToNPObjectVarMap np_object_map_copy = *np_object_map; |
| 117 NPObjectToNPObjectVarMap::iterator cur_var = | 117 NPObjectToNPObjectVarMap::iterator cur_var = |
| 118 np_object_map_copy.begin(); | 118 np_object_map_copy.begin(); |
| 119 while (cur_var != np_object_map_copy.end()) { | 119 while (cur_var != np_object_map_copy.end()) { |
| 120 NPObjectToNPObjectVarMap::iterator current = cur_var++; | 120 NPObjectToNPObjectVarMap::iterator current = cur_var++; |
| 121 ForceReleaseNPObject(current->second); | 121 ForceReleaseNPObject(current->second); |
| 122 np_object_map->erase(current->first); | 122 np_object_map->erase(current->first); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Remove the record for this instance since it should be empty. | 125 // Remove the record for this instance since it should be empty. |
| 126 DCHECK(np_object_map->empty()); | 126 DCHECK(np_object_map->empty()); |
| 127 instance_map_.erase(found_instance); | 127 instance_map_.erase(found_instance); |
| 128 } | 128 } |
| 129 | 129 |
| 130 void HostVarTracker::ForceReleaseNPObject( | 130 void HostVarTracker::ForceReleaseNPObject( |
| 131 const base::WeakPtr< ::ppapi::NPObjectVar>& object) { | 131 const base::WeakPtr< ::ppapi::NPObjectVar>& object) { |
| 132 // There's a chance that the object was already deleted before we got here. | 132 // There's a chance that the object was already deleted before we got here. |
| 133 // See ForceFreeNPObjectsForInstance for further explanation. If the object | 133 // See DidDeleteInstance for further explanation. If the object was deleted, |
| 134 // was deleted, the WeakPtr will return NULL. | 134 // the WeakPtr will return NULL. |
| 135 if (!object.get()) | 135 if (!object.get()) |
| 136 return; | 136 return; |
| 137 object->InstanceDeleted(); | 137 object->InstanceDeleted(); |
| 138 VarMap::iterator iter = live_vars_.find(object->GetExistingVarID()); | 138 VarMap::iterator iter = live_vars_.find(object->GetExistingVarID()); |
| 139 if (iter == live_vars_.end()) { | 139 if (iter == live_vars_.end()) { |
| 140 NOTREACHED(); | 140 NOTREACHED(); |
| 141 return; | 141 return; |
| 142 } | 142 } |
| 143 iter->second.ref_count = 0; | 143 iter->second.ref_count = 0; |
| 144 DCHECK(iter->second.track_with_no_reference_count == 0); | 144 DCHECK(iter->second.track_with_no_reference_count == 0); |
| 145 DeleteObjectInfoIfNecessary(iter); | 145 DeleteObjectInfoIfNecessary(iter); |
| 146 } | 146 } |
| 147 | 147 |
| 148 } // namespace ppapi | 148 } // namespace ppapi |
| 149 } // namespace webkit | 149 } // namespace webkit |
| OLD | NEW |