Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: webkit/plugins/ppapi/host_var_tracker.cc

Issue 9786001: Avoid accessing VarTracker from multiple threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/shared_impl/var_tracker.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 11 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
12 12
13 using ppapi::ArrayBufferVar; 13 using ppapi::ArrayBufferVar;
14 using ppapi::NPObjectVar; 14 using ppapi::NPObjectVar;
15 15
16 namespace webkit { 16 namespace webkit {
17 namespace ppapi { 17 namespace ppapi {
18 18
19 HostVarTracker::HostVarTracker() { 19 HostVarTracker::HostVarTracker() {
20 } 20 }
21 21
22 HostVarTracker::~HostVarTracker() { 22 HostVarTracker::~HostVarTracker() {
23 } 23 }
24 24
25 ArrayBufferVar* HostVarTracker::CreateArrayBuffer(uint32 size_in_bytes) { 25 ArrayBufferVar* HostVarTracker::CreateArrayBuffer(uint32 size_in_bytes) {
26 return new HostArrayBufferVar(size_in_bytes); 26 return new HostArrayBufferVar(size_in_bytes);
27 } 27 }
28 28
29 void HostVarTracker::AddNPObjectVar(NPObjectVar* object_var) { 29 void HostVarTracker::AddNPObjectVar(NPObjectVar* object_var) {
30 DCHECK(CalledOnValidThread());
31
30 InstanceMap::iterator found_instance = instance_map_.find( 32 InstanceMap::iterator found_instance = instance_map_.find(
31 object_var->pp_instance()); 33 object_var->pp_instance());
32 if (found_instance == instance_map_.end()) { 34 if (found_instance == instance_map_.end()) {
33 // Lazily create the instance map. 35 // Lazily create the instance map.
34 DCHECK(object_var->pp_instance() != 0); 36 DCHECK(object_var->pp_instance() != 0);
35 found_instance = instance_map_.insert(std::make_pair( 37 found_instance = instance_map_.insert(std::make_pair(
36 object_var->pp_instance(), 38 object_var->pp_instance(),
37 linked_ptr<NPObjectToNPObjectVarMap>(new NPObjectToNPObjectVarMap))). 39 linked_ptr<NPObjectToNPObjectVarMap>(new NPObjectToNPObjectVarMap))).
38 first; 40 first;
39 } 41 }
40 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); 42 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get();
41 43
42 DCHECK(np_object_map->find(object_var->np_object()) == 44 DCHECK(np_object_map->find(object_var->np_object()) ==
43 np_object_map->end()) << "NPObjectVar already in map"; 45 np_object_map->end()) << "NPObjectVar already in map";
44 np_object_map->insert( 46 np_object_map->insert(
45 std::make_pair(object_var->np_object(), object_var)); 47 std::make_pair(object_var->np_object(), object_var));
46 } 48 }
47 49
48 void HostVarTracker::RemoveNPObjectVar(NPObjectVar* object_var) { 50 void HostVarTracker::RemoveNPObjectVar(NPObjectVar* object_var) {
51 DCHECK(CalledOnValidThread());
52
49 InstanceMap::iterator found_instance = instance_map_.find( 53 InstanceMap::iterator found_instance = instance_map_.find(
50 object_var->pp_instance()); 54 object_var->pp_instance());
51 if (found_instance == instance_map_.end()) { 55 if (found_instance == instance_map_.end()) {
52 NOTREACHED() << "NPObjectVar has invalid instance."; 56 NOTREACHED() << "NPObjectVar has invalid instance.";
53 return; 57 return;
54 } 58 }
55 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); 59 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get();
56 60
57 NPObjectToNPObjectVarMap::iterator found_object = 61 NPObjectToNPObjectVarMap::iterator found_object =
58 np_object_map->find(object_var->np_object()); 62 np_object_map->find(object_var->np_object());
59 if (found_object == np_object_map->end()) { 63 if (found_object == np_object_map->end()) {
60 NOTREACHED() << "NPObjectVar not registered."; 64 NOTREACHED() << "NPObjectVar not registered.";
61 return; 65 return;
62 } 66 }
63 if (found_object->second != object_var) { 67 if (found_object->second != object_var) {
64 NOTREACHED() << "NPObjectVar doesn't match."; 68 NOTREACHED() << "NPObjectVar doesn't match.";
65 return; 69 return;
66 } 70 }
67 np_object_map->erase(found_object); 71 np_object_map->erase(found_object);
68 72
69 // Clean up when the map is empty. 73 // Clean up when the map is empty.
70 if (np_object_map->empty()) 74 if (np_object_map->empty())
71 instance_map_.erase(found_instance); 75 instance_map_.erase(found_instance);
72 } 76 }
73 77
74 NPObjectVar* HostVarTracker::NPObjectVarForNPObject(PP_Instance instance, 78 NPObjectVar* HostVarTracker::NPObjectVarForNPObject(PP_Instance instance,
75 NPObject* np_object) { 79 NPObject* np_object) {
80 DCHECK(CalledOnValidThread());
81
76 InstanceMap::iterator found_instance = instance_map_.find(instance); 82 InstanceMap::iterator found_instance = instance_map_.find(instance);
77 if (found_instance == instance_map_.end()) 83 if (found_instance == instance_map_.end())
78 return NULL; // No such instance. 84 return NULL; // No such instance.
79 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); 85 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get();
80 86
81 NPObjectToNPObjectVarMap::iterator found_object = 87 NPObjectToNPObjectVarMap::iterator found_object =
82 np_object_map->find(np_object); 88 np_object_map->find(np_object);
83 if (found_object == np_object_map->end()) 89 if (found_object == np_object_map->end())
84 return NULL; // No such object. 90 return NULL; // No such object.
85 return found_object->second; 91 return found_object->second;
86 } 92 }
87 93
88 int HostVarTracker::GetLiveNPObjectVarsForInstance(PP_Instance instance) const { 94 int HostVarTracker::GetLiveNPObjectVarsForInstance(PP_Instance instance) const {
95 DCHECK(CalledOnValidThread());
96
89 InstanceMap::const_iterator found = instance_map_.find(instance); 97 InstanceMap::const_iterator found = instance_map_.find(instance);
90 if (found == instance_map_.end()) 98 if (found == instance_map_.end())
91 return 0; 99 return 0;
92 return static_cast<int>(found->second->size()); 100 return static_cast<int>(found->second->size());
93 } 101 }
94 102
95 void HostVarTracker::ForceFreeNPObjectsForInstance(PP_Instance instance) { 103 void HostVarTracker::ForceFreeNPObjectsForInstance(PP_Instance instance) {
104 DCHECK(CalledOnValidThread());
105
96 InstanceMap::iterator found_instance = instance_map_.find(instance); 106 InstanceMap::iterator found_instance = instance_map_.find(instance);
97 if (found_instance == instance_map_.end()) 107 if (found_instance == instance_map_.end())
98 return; // Nothing to do. 108 return; // Nothing to do.
99 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); 109 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get();
100 110
101 // Force delete all var references. Need to make a copy so we can iterate over 111 // Force delete all var references. Need to make a copy so we can iterate over
102 // the map while deleting stuff from it. 112 // the map while deleting stuff from it.
103 NPObjectToNPObjectVarMap np_object_map_copy = *np_object_map; 113 NPObjectToNPObjectVarMap np_object_map_copy = *np_object_map;
104 NPObjectToNPObjectVarMap::iterator cur_var = 114 NPObjectToNPObjectVarMap::iterator cur_var =
105 np_object_map_copy.begin(); 115 np_object_map_copy.begin();
106 while (cur_var != np_object_map_copy.end()) { 116 while (cur_var != np_object_map_copy.end()) {
107 NPObjectToNPObjectVarMap::iterator current = cur_var++; 117 NPObjectToNPObjectVarMap::iterator current = cur_var++;
108 current->second->InstanceDeleted(); 118 current->second->InstanceDeleted();
109 np_object_map->erase(current->first); 119 np_object_map->erase(current->first);
110 } 120 }
111 121
112 // Remove the record for this instance since it should be empty. 122 // Remove the record for this instance since it should be empty.
113 DCHECK(np_object_map->empty()); 123 DCHECK(np_object_map->empty());
114 instance_map_.erase(found_instance); 124 instance_map_.erase(found_instance);
115 } 125 }
116 126
117 } // namespace ppapi 127 } // namespace ppapi
118 } // namespace webkit 128 } // namespace webkit
OLDNEW
« no previous file with comments | « ppapi/shared_impl/var_tracker.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698