| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 24 matching lines...) Expand all Loading... |
| 35 found_instance = instance_map_.insert(std::make_pair( | 35 found_instance = instance_map_.insert(std::make_pair( |
| 36 object_var->pp_instance(), | 36 object_var->pp_instance(), |
| 37 linked_ptr<NPObjectToNPObjectVarMap>(new NPObjectToNPObjectVarMap))). | 37 linked_ptr<NPObjectToNPObjectVarMap>(new NPObjectToNPObjectVarMap))). |
| 38 first; | 38 first; |
| 39 } | 39 } |
| 40 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 40 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
| 41 | 41 |
| 42 DCHECK(np_object_map->find(object_var->np_object()) == | 42 DCHECK(np_object_map->find(object_var->np_object()) == |
| 43 np_object_map->end()) << "NPObjectVar already in map"; | 43 np_object_map->end()) << "NPObjectVar already in map"; |
| 44 np_object_map->insert( | 44 np_object_map->insert( |
| 45 std::make_pair(object_var->np_object(), object_var)); | 45 std::make_pair(object_var->np_object(), object_var->AsWeakPtr())); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void HostVarTracker::RemoveNPObjectVar(NPObjectVar* object_var) { | 48 void HostVarTracker::RemoveNPObjectVar(NPObjectVar* object_var) { |
| 49 InstanceMap::iterator found_instance = instance_map_.find( | 49 InstanceMap::iterator found_instance = instance_map_.find( |
| 50 object_var->pp_instance()); | 50 object_var->pp_instance()); |
| 51 if (found_instance == instance_map_.end()) { | 51 if (found_instance == instance_map_.end()) { |
| 52 NOTREACHED() << "NPObjectVar has invalid instance."; | 52 NOTREACHED() << "NPObjectVar has invalid instance."; |
| 53 return; | 53 return; |
| 54 } | 54 } |
| 55 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 55 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 return 0; | 91 return 0; |
| 92 return static_cast<int>(found->second->size()); | 92 return static_cast<int>(found->second->size()); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void HostVarTracker::ForceFreeNPObjectsForInstance(PP_Instance instance) { | 95 void HostVarTracker::ForceFreeNPObjectsForInstance(PP_Instance instance) { |
| 96 InstanceMap::iterator found_instance = instance_map_.find(instance); | 96 InstanceMap::iterator found_instance = instance_map_.find(instance); |
| 97 if (found_instance == instance_map_.end()) | 97 if (found_instance == instance_map_.end()) |
| 98 return; // Nothing to do. | 98 return; // Nothing to do. |
| 99 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 99 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
| 100 | 100 |
| 101 // Force delete all var references. Need to make a copy so we can iterate over | 101 // Force delete all var references. It's possible that deleting an object "A" |
| 102 // the map while deleting stuff from it. | 102 // will cause it to delete another object "B" it references, thus removing "B" |
| 103 // from instance_map_. Therefore, we need to make a copy over which we can |
| 104 // iterate safely. Furthermore, the maps contain WeakPtrs so that we can |
| 105 // detect if the object is gone so that we don't dereference invalid memory. |
| 103 NPObjectToNPObjectVarMap np_object_map_copy = *np_object_map; | 106 NPObjectToNPObjectVarMap np_object_map_copy = *np_object_map; |
| 104 NPObjectToNPObjectVarMap::iterator cur_var = | 107 NPObjectToNPObjectVarMap::iterator cur_var = |
| 105 np_object_map_copy.begin(); | 108 np_object_map_copy.begin(); |
| 106 while (cur_var != np_object_map_copy.end()) { | 109 while (cur_var != np_object_map_copy.end()) { |
| 107 NPObjectToNPObjectVarMap::iterator current = cur_var++; | 110 NPObjectToNPObjectVarMap::iterator current = cur_var++; |
| 108 current->second->InstanceDeleted(); | 111 ForceReleaseNPObject(current->second); |
| 109 np_object_map->erase(current->first); | 112 np_object_map->erase(current->first); |
| 110 } | 113 } |
| 111 | 114 |
| 112 // Remove the record for this instance since it should be empty. | 115 // Remove the record for this instance since it should be empty. |
| 113 DCHECK(np_object_map->empty()); | 116 DCHECK(np_object_map->empty()); |
| 114 instance_map_.erase(found_instance); | 117 instance_map_.erase(found_instance); |
| 115 } | 118 } |
| 116 | 119 |
| 120 void HostVarTracker::ForceReleaseNPObject( |
| 121 const base::WeakPtr< ::ppapi::NPObjectVar>& object) { |
| 122 // There's a chance that the object was already deleted before we got here. |
| 123 // See ForceFreeNPObjectsForInstance for further explanation. If the object |
| 124 // was deleted, the WeakPtr will return NULL. |
| 125 if (!object.get()) |
| 126 return; |
| 127 object->InstanceDeleted(); |
| 128 VarMap::iterator iter = live_vars_.find(object->GetExistingVarID()); |
| 129 if (iter == live_vars_.end()) { |
| 130 NOTREACHED(); |
| 131 return; |
| 132 } |
| 133 iter->second.ref_count = 0; |
| 134 DCHECK(iter->second.track_with_no_reference_count == 0); |
| 135 DeleteObjectInfoIfNecessary(iter); |
| 136 } |
| 137 |
| 117 } // namespace ppapi | 138 } // namespace ppapi |
| 118 } // namespace webkit | 139 } // namespace webkit |
| OLD | NEW |