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 #ifndef PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | 5 #ifndef PPAPI_SHARED_IMPL_VAR_TRACKER_H_ |
6 #define PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | 6 #define PPAPI_SHARED_IMPL_VAR_TRACKER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/shared_memory.h" | 13 #include "base/shared_memory.h" |
15 #include "base/threading/thread_checker.h" | 14 #include "base/threading/non_thread_safe.h" |
16 #include "ppapi/c/pp_instance.h" | 15 #include "ppapi/c/pp_instance.h" |
17 #include "ppapi/c/pp_module.h" | 16 #include "ppapi/c/pp_module.h" |
18 #include "ppapi/c/pp_resource.h" | 17 #include "ppapi/c/pp_resource.h" |
19 #include "ppapi/c/pp_var.h" | 18 #include "ppapi/c/pp_var.h" |
20 #include "ppapi/shared_impl/host_resource.h" | 19 #include "ppapi/shared_impl/host_resource.h" |
21 #include "ppapi/shared_impl/ppapi_shared_export.h" | 20 #include "ppapi/shared_impl/ppapi_shared_export.h" |
22 | 21 |
23 namespace ppapi { | 22 namespace ppapi { |
24 | 23 |
25 class ArrayBufferVar; | 24 class ArrayBufferVar; |
26 class Var; | 25 class Var; |
27 | 26 |
28 // Tracks non-POD (refcounted) var objects held by a plugin. | 27 // Tracks non-POD (refcounted) var objects held by a plugin. |
29 // | 28 // |
30 // The tricky part is the concept of a "tracked object". These are only | 29 // The tricky part is the concept of a "tracked object". These are only |
31 // necessary in the plugin side of the proxy when running out of process. A | 30 // necessary in the plugin side of the proxy when running out of process. A |
32 // tracked object is one that the plugin is aware of, but doesn't hold a | 31 // tracked object is one that the plugin is aware of, but doesn't hold a |
33 // reference to. This will happen when the plugin is passed an object as an | 32 // reference to. This will happen when the plugin is passed an object as an |
34 // argument from the host (renderer) as an input argument to a sync function, | 33 // argument from the host (renderer) as an input argument to a sync function, |
35 // but where ownership is not passed. | 34 // but where ownership is not passed. |
36 // | 35 // |
37 // This class maintains the "track_with_no_reference_count" but doesn't do | 36 // This class maintains the "track_with_no_reference_count" but doesn't do |
38 // anything with it other than call virtual functions. The interesting parts | 37 // anything with it other than call virtual functions. The interesting parts |
39 // are added by the PluginObjectVar derived from this class. | 38 // are added by the PluginObjectVar derived from this class. |
40 class PPAPI_SHARED_EXPORT VarTracker { | 39 class PPAPI_SHARED_EXPORT VarTracker |
| 40 #ifdef ENABLE_PEPPER_THREADING |
| 41 : NON_EXPORTED_BASE(public base::NonThreadSafeDoNothing) { |
| 42 #else |
| 43 // TODO(dmichael): Remove the thread checking when calls are allowed off the |
| 44 // main thread (crbug.com/92909). |
| 45 : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 46 #endif |
41 public: | 47 public: |
42 // A SINGLE_THREADED VarTracker will use a thread-checker to make sure it's | 48 VarTracker(); |
43 // always invoked on the same thread on which it was constructed. A | |
44 // THREAD_SAFE VarTracker will check that the ProxyLock is held. See | |
45 // CheckThreadingPreconditions() for more details. | |
46 enum ThreadMode { SINGLE_THREADED, THREAD_SAFE }; | |
47 explicit VarTracker(ThreadMode thread_mode); | |
48 virtual ~VarTracker(); | 49 virtual ~VarTracker(); |
49 | 50 |
50 // Called by the Var object to add a new var to the tracker. | 51 // Called by the Var object to add a new var to the tracker. |
51 int32 AddVar(Var* var); | 52 int32 AddVar(Var* var); |
52 | 53 |
53 // Looks up a given var and returns a reference to the Var if it exists. | 54 // Looks up a given var and returns a reference to the Var if it exists. |
54 // Returns NULL if the var type is not an object we track (POD) or is | 55 // Returns NULL if the var type is not an object we track (POD) or is |
55 // invalid. | 56 // invalid. |
56 Var* GetVar(int32 var_id) const; | 57 Var* GetVar(int32 var_id) const; |
57 Var* GetVar(const PP_Var& var) const; | 58 Var* GetVar(const PP_Var& var) const; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 int track_with_no_reference_count; | 141 int track_with_no_reference_count; |
141 }; | 142 }; |
142 typedef base::hash_map<int32, VarInfo> VarMap; | 143 typedef base::hash_map<int32, VarInfo> VarMap; |
143 | 144 |
144 // Specifies what should happen with the refcount when calling AddVarInternal. | 145 // Specifies what should happen with the refcount when calling AddVarInternal. |
145 enum AddVarRefMode { | 146 enum AddVarRefMode { |
146 ADD_VAR_TAKE_ONE_REFERENCE, | 147 ADD_VAR_TAKE_ONE_REFERENCE, |
147 ADD_VAR_CREATE_WITH_NO_REFERENCE | 148 ADD_VAR_CREATE_WITH_NO_REFERENCE |
148 }; | 149 }; |
149 | 150 |
150 // On the host-side, make sure we are called on the right thread. On the | |
151 // plugin side, make sure we have the proxy lock. | |
152 void CheckThreadingPreconditions() const; | |
153 | |
154 // Implementation of AddVar that allows the caller to specify whether the | 151 // Implementation of AddVar that allows the caller to specify whether the |
155 // initial refcount of the added object will be 0 or 1. | 152 // initial refcount of the added object will be 0 or 1. |
156 // | 153 // |
157 // Overridden in the plugin proxy to do additional object tracking. | 154 // Overridden in the plugin proxy to do additional object tracking. |
158 virtual int32 AddVarInternal(Var* var, AddVarRefMode mode); | 155 virtual int32 AddVarInternal(Var* var, AddVarRefMode mode); |
159 | 156 |
160 // Convenience functions for doing lookups into the live_vars_ map. | 157 // Convenience functions for doing lookups into the live_vars_ map. |
161 VarMap::iterator GetLiveVar(int32 id); | 158 VarMap::iterator GetLiveVar(int32 id); |
162 VarMap::iterator GetLiveVar(const PP_Var& var); | 159 VarMap::iterator GetLiveVar(const PP_Var& var); |
163 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; | 160 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; |
(...skipping 30 matching lines...) Expand all Loading... |
194 | 191 |
195 private: | 192 private: |
196 // Create and return a new ArrayBufferVar size_in_bytes bytes long. This is | 193 // Create and return a new ArrayBufferVar size_in_bytes bytes long. This is |
197 // implemented by the Host and Plugin tracker separately, so that it can be | 194 // implemented by the Host and Plugin tracker separately, so that it can be |
198 // a real WebKit ArrayBuffer on the host side. | 195 // a real WebKit ArrayBuffer on the host side. |
199 virtual ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) = 0; | 196 virtual ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) = 0; |
200 virtual ArrayBufferVar* CreateShmArrayBuffer( | 197 virtual ArrayBufferVar* CreateShmArrayBuffer( |
201 uint32 size_in_bytes, | 198 uint32 size_in_bytes, |
202 base::SharedMemoryHandle handle) = 0; | 199 base::SharedMemoryHandle handle) = 0; |
203 | 200 |
204 // On the host side, we want to check that we are only called on the main | |
205 // thread. This is to protect us from accidentally using the tracker from | |
206 // other threads (especially the IO thread). On the plugin side, the tracker | |
207 // is protected by the proxy lock and is thread-safe, so this will be NULL. | |
208 scoped_ptr<base::ThreadChecker> thread_checker_; | |
209 | |
210 DISALLOW_COPY_AND_ASSIGN(VarTracker); | 201 DISALLOW_COPY_AND_ASSIGN(VarTracker); |
211 }; | 202 }; |
212 | 203 |
213 } // namespace ppapi | 204 } // namespace ppapi |
214 | 205 |
215 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | 206 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ |
OLD | NEW |