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" |
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 #ifdef ENABLE_PEPPER_THREADING | |
17 #define CHECK_VALID_THREAD() do {} while(false) | |
18 #else | |
19 #define CHECK_VALID_THREAD() do { CalledOnValidThread(); } while(false) | |
yzshen1
2012/04/12 22:21:23
We need to DCHECK the result of CalledOnValidThrea
| |
20 #endif | |
21 | |
16 namespace webkit { | 22 namespace webkit { |
17 namespace ppapi { | 23 namespace ppapi { |
18 | 24 |
19 HostVarTracker::HostVarTracker() { | 25 HostVarTracker::HostVarTracker() { |
20 } | 26 } |
21 | 27 |
22 HostVarTracker::~HostVarTracker() { | 28 HostVarTracker::~HostVarTracker() { |
23 } | 29 } |
24 | 30 |
25 ArrayBufferVar* HostVarTracker::CreateArrayBuffer(uint32 size_in_bytes) { | 31 ArrayBufferVar* HostVarTracker::CreateArrayBuffer(uint32 size_in_bytes) { |
26 return new HostArrayBufferVar(size_in_bytes); | 32 return new HostArrayBufferVar(size_in_bytes); |
27 } | 33 } |
28 | 34 |
29 void HostVarTracker::AddNPObjectVar(NPObjectVar* object_var) { | 35 void HostVarTracker::AddNPObjectVar(NPObjectVar* object_var) { |
30 DCHECK(CalledOnValidThread()); | 36 CHECK_VALID_THREAD(); |
31 | 37 |
32 InstanceMap::iterator found_instance = instance_map_.find( | 38 InstanceMap::iterator found_instance = instance_map_.find( |
33 object_var->pp_instance()); | 39 object_var->pp_instance()); |
34 if (found_instance == instance_map_.end()) { | 40 if (found_instance == instance_map_.end()) { |
35 // Lazily create the instance map. | 41 // Lazily create the instance map. |
36 DCHECK(object_var->pp_instance() != 0); | 42 DCHECK(object_var->pp_instance() != 0); |
37 found_instance = instance_map_.insert(std::make_pair( | 43 found_instance = instance_map_.insert(std::make_pair( |
38 object_var->pp_instance(), | 44 object_var->pp_instance(), |
39 linked_ptr<NPObjectToNPObjectVarMap>(new NPObjectToNPObjectVarMap))). | 45 linked_ptr<NPObjectToNPObjectVarMap>(new NPObjectToNPObjectVarMap))). |
40 first; | 46 first; |
41 } | 47 } |
42 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 48 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
43 | 49 |
44 DCHECK(np_object_map->find(object_var->np_object()) == | 50 DCHECK(np_object_map->find(object_var->np_object()) == |
45 np_object_map->end()) << "NPObjectVar already in map"; | 51 np_object_map->end()) << "NPObjectVar already in map"; |
46 np_object_map->insert( | 52 np_object_map->insert( |
47 std::make_pair(object_var->np_object(), object_var->AsWeakPtr())); | 53 std::make_pair(object_var->np_object(), object_var->AsWeakPtr())); |
48 } | 54 } |
49 | 55 |
50 void HostVarTracker::RemoveNPObjectVar(NPObjectVar* object_var) { | 56 void HostVarTracker::RemoveNPObjectVar(NPObjectVar* object_var) { |
51 DCHECK(CalledOnValidThread()); | 57 CHECK_VALID_THREAD(); |
52 | 58 |
53 InstanceMap::iterator found_instance = instance_map_.find( | 59 InstanceMap::iterator found_instance = instance_map_.find( |
54 object_var->pp_instance()); | 60 object_var->pp_instance()); |
55 if (found_instance == instance_map_.end()) { | 61 if (found_instance == instance_map_.end()) { |
56 NOTREACHED() << "NPObjectVar has invalid instance."; | 62 NOTREACHED() << "NPObjectVar has invalid instance."; |
57 return; | 63 return; |
58 } | 64 } |
59 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 65 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
60 | 66 |
61 NPObjectToNPObjectVarMap::iterator found_object = | 67 NPObjectToNPObjectVarMap::iterator found_object = |
62 np_object_map->find(object_var->np_object()); | 68 np_object_map->find(object_var->np_object()); |
63 if (found_object == np_object_map->end()) { | 69 if (found_object == np_object_map->end()) { |
64 NOTREACHED() << "NPObjectVar not registered."; | 70 NOTREACHED() << "NPObjectVar not registered."; |
65 return; | 71 return; |
66 } | 72 } |
67 if (found_object->second != object_var) { | 73 if (found_object->second != object_var) { |
68 NOTREACHED() << "NPObjectVar doesn't match."; | 74 NOTREACHED() << "NPObjectVar doesn't match."; |
69 return; | 75 return; |
70 } | 76 } |
71 np_object_map->erase(found_object); | 77 np_object_map->erase(found_object); |
72 | 78 |
73 // Clean up when the map is empty. | 79 // Clean up when the map is empty. |
74 if (np_object_map->empty()) | 80 if (np_object_map->empty()) |
75 instance_map_.erase(found_instance); | 81 instance_map_.erase(found_instance); |
76 } | 82 } |
77 | 83 |
78 NPObjectVar* HostVarTracker::NPObjectVarForNPObject(PP_Instance instance, | 84 NPObjectVar* HostVarTracker::NPObjectVarForNPObject(PP_Instance instance, |
79 NPObject* np_object) { | 85 NPObject* np_object) { |
80 DCHECK(CalledOnValidThread()); | 86 CHECK_VALID_THREAD(); |
81 | 87 |
82 InstanceMap::iterator found_instance = instance_map_.find(instance); | 88 InstanceMap::iterator found_instance = instance_map_.find(instance); |
83 if (found_instance == instance_map_.end()) | 89 if (found_instance == instance_map_.end()) |
84 return NULL; // No such instance. | 90 return NULL; // No such instance. |
85 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 91 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
86 | 92 |
87 NPObjectToNPObjectVarMap::iterator found_object = | 93 NPObjectToNPObjectVarMap::iterator found_object = |
88 np_object_map->find(np_object); | 94 np_object_map->find(np_object); |
89 if (found_object == np_object_map->end()) | 95 if (found_object == np_object_map->end()) |
90 return NULL; // No such object. | 96 return NULL; // No such object. |
91 return found_object->second; | 97 return found_object->second; |
92 } | 98 } |
93 | 99 |
94 int HostVarTracker::GetLiveNPObjectVarsForInstance(PP_Instance instance) const { | 100 int HostVarTracker::GetLiveNPObjectVarsForInstance(PP_Instance instance) const { |
95 DCHECK(CalledOnValidThread()); | 101 CHECK_VALID_THREAD(); |
96 | 102 |
97 InstanceMap::const_iterator found = instance_map_.find(instance); | 103 InstanceMap::const_iterator found = instance_map_.find(instance); |
98 if (found == instance_map_.end()) | 104 if (found == instance_map_.end()) |
99 return 0; | 105 return 0; |
100 return static_cast<int>(found->second->size()); | 106 return static_cast<int>(found->second->size()); |
101 } | 107 } |
102 | 108 |
103 void HostVarTracker::ForceFreeNPObjectsForInstance(PP_Instance instance) { | 109 void HostVarTracker::ForceFreeNPObjectsForInstance(PP_Instance instance) { |
104 DCHECK(CalledOnValidThread()); | 110 CHECK_VALID_THREAD(); |
105 | 111 |
106 InstanceMap::iterator found_instance = instance_map_.find(instance); | 112 InstanceMap::iterator found_instance = instance_map_.find(instance); |
107 if (found_instance == instance_map_.end()) | 113 if (found_instance == instance_map_.end()) |
108 return; // Nothing to do. | 114 return; // Nothing to do. |
109 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); | 115 NPObjectToNPObjectVarMap* np_object_map = found_instance->second.get(); |
110 | 116 |
111 // Force delete all var references. It's possible that deleting an object "A" | 117 // 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" | 118 // 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 | 119 // 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 | 120 // iterate safely. Furthermore, the maps contain WeakPtrs so that we can |
(...skipping 25 matching lines...) Expand all Loading... | |
140 NOTREACHED(); | 146 NOTREACHED(); |
141 return; | 147 return; |
142 } | 148 } |
143 iter->second.ref_count = 0; | 149 iter->second.ref_count = 0; |
144 DCHECK(iter->second.track_with_no_reference_count == 0); | 150 DCHECK(iter->second.track_with_no_reference_count == 0); |
145 DeleteObjectInfoIfNecessary(iter); | 151 DeleteObjectInfoIfNecessary(iter); |
146 } | 152 } |
147 | 153 |
148 } // namespace ppapi | 154 } // namespace ppapi |
149 } // namespace webkit | 155 } // namespace webkit |
OLD | NEW |