| 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/var_tracker.h" | 5 #include "ppapi/shared_impl/var_tracker.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 track_with_no_reference_count(0) { | 26 track_with_no_reference_count(0) { |
| 27 } | 27 } |
| 28 | 28 |
| 29 VarTracker::VarTracker() : last_var_id_(0) { | 29 VarTracker::VarTracker() : last_var_id_(0) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 VarTracker::~VarTracker() { | 32 VarTracker::~VarTracker() { |
| 33 } | 33 } |
| 34 | 34 |
| 35 int32 VarTracker::AddVar(Var* var) { | 35 int32 VarTracker::AddVar(Var* var) { |
| 36 DCHECK(CalledOnValidThread()); |
| 37 |
| 36 return AddVarInternal(var, ADD_VAR_TAKE_ONE_REFERENCE); | 38 return AddVarInternal(var, ADD_VAR_TAKE_ONE_REFERENCE); |
| 37 } | 39 } |
| 38 | 40 |
| 39 Var* VarTracker::GetVar(int32 var_id) const { | 41 Var* VarTracker::GetVar(int32 var_id) const { |
| 42 DCHECK(CalledOnValidThread()); |
| 43 |
| 40 VarMap::const_iterator result = live_vars_.find(var_id); | 44 VarMap::const_iterator result = live_vars_.find(var_id); |
| 41 if (result == live_vars_.end()) | 45 if (result == live_vars_.end()) |
| 42 return NULL; | 46 return NULL; |
| 43 return result->second.var.get(); | 47 return result->second.var.get(); |
| 44 } | 48 } |
| 45 | 49 |
| 46 Var* VarTracker::GetVar(const PP_Var& var) const { | 50 Var* VarTracker::GetVar(const PP_Var& var) const { |
| 51 DCHECK(CalledOnValidThread()); |
| 52 |
| 47 if (!IsVarTypeRefcounted(var.type)) | 53 if (!IsVarTypeRefcounted(var.type)) |
| 48 return NULL; | 54 return NULL; |
| 49 return GetVar(static_cast<int32>(var.value.as_id)); | 55 return GetVar(static_cast<int32>(var.value.as_id)); |
| 50 } | 56 } |
| 51 | 57 |
| 52 bool VarTracker::AddRefVar(int32 var_id) { | 58 bool VarTracker::AddRefVar(int32 var_id) { |
| 59 DCHECK(CalledOnValidThread()); |
| 60 |
| 53 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) | 61 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) |
| 54 << var_id << " is not a PP_Var ID."; | 62 << var_id << " is not a PP_Var ID."; |
| 55 VarMap::iterator found = live_vars_.find(var_id); | 63 VarMap::iterator found = live_vars_.find(var_id); |
| 56 if (found == live_vars_.end()) { | 64 if (found == live_vars_.end()) { |
| 57 NOTREACHED(); // Invalid var. | 65 NOTREACHED(); // Invalid var. |
| 58 return false; | 66 return false; |
| 59 } | 67 } |
| 60 | 68 |
| 61 VarInfo& info = found->second; | 69 VarInfo& info = found->second; |
| 62 if (info.ref_count == 0) { | 70 if (info.ref_count == 0) { |
| 63 // All live vars with no refcount should be tracked objects. | 71 // All live vars with no refcount should be tracked objects. |
| 64 DCHECK(info.track_with_no_reference_count > 0); | 72 DCHECK(info.track_with_no_reference_count > 0); |
| 65 DCHECK(info.var->GetType() == PP_VARTYPE_OBJECT); | 73 DCHECK(info.var->GetType() == PP_VARTYPE_OBJECT); |
| 66 | 74 |
| 67 TrackedObjectGettingOneRef(found); | 75 TrackedObjectGettingOneRef(found); |
| 68 } | 76 } |
| 69 | 77 |
| 70 // Basic refcount increment. | 78 // Basic refcount increment. |
| 71 info.ref_count++; | 79 info.ref_count++; |
| 72 return true; | 80 return true; |
| 73 } | 81 } |
| 74 | 82 |
| 75 bool VarTracker::AddRefVar(const PP_Var& var) { | 83 bool VarTracker::AddRefVar(const PP_Var& var) { |
| 84 DCHECK(CalledOnValidThread()); |
| 85 |
| 76 if (!IsVarTypeRefcounted(var.type)) | 86 if (!IsVarTypeRefcounted(var.type)) |
| 77 return false; | 87 return false; |
| 78 return AddRefVar(static_cast<int32>(var.value.as_id)); | 88 return AddRefVar(static_cast<int32>(var.value.as_id)); |
| 79 } | 89 } |
| 80 | 90 |
| 81 bool VarTracker::ReleaseVar(int32 var_id) { | 91 bool VarTracker::ReleaseVar(int32 var_id) { |
| 92 DCHECK(CalledOnValidThread()); |
| 93 |
| 82 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) | 94 DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) |
| 83 << var_id << " is not a PP_Var ID."; | 95 << var_id << " is not a PP_Var ID."; |
| 84 VarMap::iterator found = live_vars_.find(var_id); | 96 VarMap::iterator found = live_vars_.find(var_id); |
| 85 if (found == live_vars_.end()) { | 97 if (found == live_vars_.end()) { |
| 86 NOTREACHED() << "Unref-ing an invalid var"; | 98 NOTREACHED() << "Unref-ing an invalid var"; |
| 87 return false; | 99 return false; |
| 88 } | 100 } |
| 89 | 101 |
| 90 VarInfo& info = found->second; | 102 VarInfo& info = found->second; |
| 91 if (info.ref_count == 0) { | 103 if (info.ref_count == 0) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 103 // All other var types can just be released. | 115 // All other var types can just be released. |
| 104 DCHECK(info.track_with_no_reference_count == 0); | 116 DCHECK(info.track_with_no_reference_count == 0); |
| 105 info.var->ResetVarID(); | 117 info.var->ResetVarID(); |
| 106 live_vars_.erase(found); | 118 live_vars_.erase(found); |
| 107 } | 119 } |
| 108 } | 120 } |
| 109 return true; | 121 return true; |
| 110 } | 122 } |
| 111 | 123 |
| 112 bool VarTracker::ReleaseVar(const PP_Var& var) { | 124 bool VarTracker::ReleaseVar(const PP_Var& var) { |
| 125 DCHECK(CalledOnValidThread()); |
| 126 |
| 113 if (!IsVarTypeRefcounted(var.type)) | 127 if (!IsVarTypeRefcounted(var.type)) |
| 114 return false; | 128 return false; |
| 115 return ReleaseVar(static_cast<int32>(var.value.as_id)); | 129 return ReleaseVar(static_cast<int32>(var.value.as_id)); |
| 116 } | 130 } |
| 117 | 131 |
| 118 int32 VarTracker::AddVarInternal(Var* var, AddVarRefMode mode) { | 132 int32 VarTracker::AddVarInternal(Var* var, AddVarRefMode mode) { |
| 119 // If the plugin manages to create millions of strings. | 133 // If the plugin manages to create millions of strings. |
| 120 if (last_var_id_ == std::numeric_limits<int32>::max() >> kPPIdTypeBits) | 134 if (last_var_id_ == std::numeric_limits<int32>::max() >> kPPIdTypeBits) |
| 121 return 0; | 135 return 0; |
| 122 | 136 |
| 123 int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); | 137 int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); |
| 124 live_vars_.insert(std::make_pair(new_id, | 138 live_vars_.insert(std::make_pair(new_id, |
| 125 VarInfo(var, mode == ADD_VAR_TAKE_ONE_REFERENCE ? 1 : 0))); | 139 VarInfo(var, mode == ADD_VAR_TAKE_ONE_REFERENCE ? 1 : 0))); |
| 126 | 140 |
| 127 return new_id; | 141 return new_id; |
| 128 } | 142 } |
| 129 | 143 |
| 130 VarTracker::VarMap::iterator VarTracker::GetLiveVar(int32 id) { | 144 VarTracker::VarMap::iterator VarTracker::GetLiveVar(int32 id) { |
| 131 return live_vars_.find(id); | 145 return live_vars_.find(id); |
| 132 } | 146 } |
| 133 | 147 |
| 134 int VarTracker::GetRefCountForObject(const PP_Var& plugin_object) { | 148 int VarTracker::GetRefCountForObject(const PP_Var& plugin_object) { |
| 149 DCHECK(CalledOnValidThread()); |
| 150 |
| 135 VarMap::iterator found = GetLiveVar(plugin_object); | 151 VarMap::iterator found = GetLiveVar(plugin_object); |
| 136 if (found == live_vars_.end()) | 152 if (found == live_vars_.end()) |
| 137 return -1; | 153 return -1; |
| 138 return found->second.ref_count; | 154 return found->second.ref_count; |
| 139 } | 155 } |
| 140 | 156 |
| 141 int VarTracker::GetTrackedWithNoReferenceCountForObject( | 157 int VarTracker::GetTrackedWithNoReferenceCountForObject( |
| 142 const PP_Var& plugin_object) { | 158 const PP_Var& plugin_object) { |
| 159 DCHECK(CalledOnValidThread()); |
| 160 |
| 143 VarMap::iterator found = GetLiveVar(plugin_object); | 161 VarMap::iterator found = GetLiveVar(plugin_object); |
| 144 if (found == live_vars_.end()) | 162 if (found == live_vars_.end()) |
| 145 return -1; | 163 return -1; |
| 146 return found->second.track_with_no_reference_count; | 164 return found->second.track_with_no_reference_count; |
| 147 } | 165 } |
| 148 | 166 |
| 149 VarTracker::VarMap::iterator VarTracker::GetLiveVar(const PP_Var& var) { | 167 VarTracker::VarMap::iterator VarTracker::GetLiveVar(const PP_Var& var) { |
| 150 return live_vars_.find(static_cast<int32>(var.value.as_id)); | 168 return live_vars_.find(static_cast<int32>(var.value.as_id)); |
| 151 } | 169 } |
| 152 | 170 |
| 153 VarTracker::VarMap::const_iterator VarTracker::GetLiveVar( | 171 VarTracker::VarMap::const_iterator VarTracker::GetLiveVar( |
| 154 const PP_Var& var) const { | 172 const PP_Var& var) const { |
| 155 return live_vars_.find(static_cast<int32>(var.value.as_id)); | 173 return live_vars_.find(static_cast<int32>(var.value.as_id)); |
| 156 } | 174 } |
| 157 | 175 |
| 158 bool VarTracker::IsVarTypeRefcounted(PP_VarType type) const { | 176 bool VarTracker::IsVarTypeRefcounted(PP_VarType type) const { |
| 159 return type >= PP_VARTYPE_STRING; | 177 return type >= PP_VARTYPE_STRING; |
| 160 } | 178 } |
| 161 | 179 |
| 162 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes) { | 180 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes) { |
| 181 DCHECK(CalledOnValidThread()); |
| 182 |
| 163 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); | 183 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); |
| 164 if (!array_buffer) | 184 if (!array_buffer) |
| 165 return PP_MakeNull(); | 185 return PP_MakeNull(); |
| 166 return array_buffer->GetPPVar(); | 186 return array_buffer->GetPPVar(); |
| 167 } | 187 } |
| 168 | 188 |
| 169 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes, | 189 PP_Var VarTracker::MakeArrayBufferPPVar(uint32 size_in_bytes, |
| 170 const void* data) { | 190 const void* data) { |
| 191 DCHECK(CalledOnValidThread()); |
| 192 |
| 171 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); | 193 scoped_refptr<ArrayBufferVar> array_buffer(CreateArrayBuffer(size_in_bytes)); |
| 172 if (!array_buffer) | 194 if (!array_buffer) |
| 173 return PP_MakeNull(); | 195 return PP_MakeNull(); |
| 174 memcpy(array_buffer->Map(), data, size_in_bytes); | 196 memcpy(array_buffer->Map(), data, size_in_bytes); |
| 175 return array_buffer->GetPPVar(); | 197 return array_buffer->GetPPVar(); |
| 176 } | 198 } |
| 177 | 199 |
| 178 std::vector<PP_Var> VarTracker::GetLiveVars() { | 200 std::vector<PP_Var> VarTracker::GetLiveVars() { |
| 201 DCHECK(CalledOnValidThread()); |
| 202 |
| 179 std::vector<PP_Var> var_vector; | 203 std::vector<PP_Var> var_vector; |
| 180 var_vector.reserve(live_vars_.size()); | 204 var_vector.reserve(live_vars_.size()); |
| 181 for (VarMap::const_iterator iter = live_vars_.begin(); | 205 for (VarMap::const_iterator iter = live_vars_.begin(); |
| 182 iter != live_vars_.end(); | 206 iter != live_vars_.end(); |
| 183 ++iter) { | 207 ++iter) { |
| 184 var_vector.push_back(iter->second.var->GetPPVar()); | 208 var_vector.push_back(iter->second.var->GetPPVar()); |
| 185 } | 209 } |
| 186 return var_vector; | 210 return var_vector; |
| 187 } | 211 } |
| 188 | 212 |
| 189 void VarTracker::TrackedObjectGettingOneRef(VarMap::const_iterator obj) { | 213 void VarTracker::TrackedObjectGettingOneRef(VarMap::const_iterator obj) { |
| 190 // Anybody using tracked objects should override this. | 214 // Anybody using tracked objects should override this. |
| 191 NOTREACHED(); | 215 NOTREACHED(); |
| 192 } | 216 } |
| 193 | 217 |
| 194 void VarTracker::ObjectGettingZeroRef(VarMap::iterator iter) { | 218 void VarTracker::ObjectGettingZeroRef(VarMap::iterator iter) { |
| 195 DeleteObjectInfoIfNecessary(iter); | 219 DeleteObjectInfoIfNecessary(iter); |
| 196 } | 220 } |
| 197 | 221 |
| 198 bool VarTracker::DeleteObjectInfoIfNecessary(VarMap::iterator iter) { | 222 bool VarTracker::DeleteObjectInfoIfNecessary(VarMap::iterator iter) { |
| 199 if (iter->second.ref_count != 0 || | 223 if (iter->second.ref_count != 0 || |
| 200 iter->second.track_with_no_reference_count != 0) | 224 iter->second.track_with_no_reference_count != 0) |
| 201 return false; // Object still alive. | 225 return false; // Object still alive. |
| 202 iter->second.var->ResetVarID(); | 226 iter->second.var->ResetVarID(); |
| 203 live_vars_.erase(iter); | 227 live_vars_.erase(iter); |
| 204 return true; | 228 return true; |
| 205 } | 229 } |
| 206 | 230 |
| 207 } // namespace ppapi | 231 } // namespace ppapi |
| OLD | NEW |