| 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" | 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "base/threading/thread_checker.h" | |
| 15 #include "ppapi/c/pp_instance.h" | 14 #include "ppapi/c/pp_instance.h" |
| 16 #include "ppapi/c/pp_module.h" | 15 #include "ppapi/c/pp_module.h" |
| 17 #include "ppapi/c/pp_var.h" | 16 #include "ppapi/c/pp_var.h" |
| 18 #include "ppapi/shared_impl/ppapi_shared_export.h" | 17 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 19 | 18 |
| 20 namespace ppapi { | 19 namespace ppapi { |
| 21 | 20 |
| 22 class ArrayBufferVar; | 21 class ArrayBufferVar; |
| 23 class Var; | 22 class Var; |
| 24 | 23 |
| 25 // Tracks non-POD (refcounted) var objects held by a plugin. | 24 // Tracks non-POD (refcounted) var objects held by a plugin. |
| 26 // | 25 // |
| 27 // The tricky part is the concept of a "tracked object". These are only | 26 // The tricky part is the concept of a "tracked object". These are only |
| 28 // necessary in the plugin side of the proxy when running out of process. A | 27 // necessary in the plugin side of the proxy when running out of process. A |
| 29 // tracked object is one that the plugin is aware of, but doesn't hold a | 28 // tracked object is one that the plugin is aware of, but doesn't hold a |
| 30 // reference to. This will happen when the plugin is passed an object as an | 29 // reference to. This will happen when the plugin is passed an object as an |
| 31 // argument from the host (renderer) as an input argument to a sync function, | 30 // argument from the host (renderer) as an input argument to a sync function, |
| 32 // but where ownership is not passed. | 31 // but where ownership is not passed. |
| 33 // | 32 // |
| 34 // This class maintains the "track_with_no_reference_count" but doesn't do | 33 // This class maintains the "track_with_no_reference_count" but doesn't do |
| 35 // anything with it other than call virtual functions. The interesting parts | 34 // anything with it other than call virtual functions. The interesting parts |
| 36 // are added by the PluginObjectVar derived from this class. | 35 // are added by the PluginObjectVar derived from this class. |
| 37 class PPAPI_SHARED_EXPORT VarTracker { | 36 class PPAPI_SHARED_EXPORT VarTracker |
| 37 #ifdef ENABLE_PEPPER_THREADING |
| 38 : NON_EXPORTED_BASE(public base::NonThreadSafeDoNothing) { |
| 39 #else |
| 40 // TODO(dmichael): Remove the thread checking when calls are allowed off the |
| 41 // main thread (crbug.com/92909). |
| 42 : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 43 #endif |
| 38 public: | 44 public: |
| 39 // A SINGLE_THREADED VarTracker will use a thread-checker to make sure it's | 45 VarTracker(); |
| 40 // always invoked on the same thread on which it was constructed. A | |
| 41 // THREAD_SAFE VarTracker will check that the ProxyLock is held. See | |
| 42 // CheckThreadingPreconditions() for more details. | |
| 43 enum ThreadMode { SINGLE_THREADED, THREAD_SAFE }; | |
| 44 explicit VarTracker(ThreadMode thread_mode); | |
| 45 virtual ~VarTracker(); | 46 virtual ~VarTracker(); |
| 46 | 47 |
| 47 // Called by the Var object to add a new var to the tracker. | 48 // Called by the Var object to add a new var to the tracker. |
| 48 int32 AddVar(Var* var); | 49 int32 AddVar(Var* var); |
| 49 | 50 |
| 50 // Looks up a given var and returns a reference to the Var if it exists. | 51 // Looks up a given var and returns a reference to the Var if it exists. |
| 51 // Returns NULL if the var type is not an object we track (POD) or is | 52 // Returns NULL if the var type is not an object we track (POD) or is |
| 52 // invalid. | 53 // invalid. |
| 53 Var* GetVar(int32 var_id) const; | 54 Var* GetVar(int32 var_id) const; |
| 54 Var* GetVar(const PP_Var& var) const; | 55 Var* GetVar(const PP_Var& var) const; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 int track_with_no_reference_count; | 118 int track_with_no_reference_count; |
| 118 }; | 119 }; |
| 119 typedef base::hash_map<int32, VarInfo> VarMap; | 120 typedef base::hash_map<int32, VarInfo> VarMap; |
| 120 | 121 |
| 121 // Specifies what should happen with the refcount when calling AddVarInternal. | 122 // Specifies what should happen with the refcount when calling AddVarInternal. |
| 122 enum AddVarRefMode { | 123 enum AddVarRefMode { |
| 123 ADD_VAR_TAKE_ONE_REFERENCE, | 124 ADD_VAR_TAKE_ONE_REFERENCE, |
| 124 ADD_VAR_CREATE_WITH_NO_REFERENCE | 125 ADD_VAR_CREATE_WITH_NO_REFERENCE |
| 125 }; | 126 }; |
| 126 | 127 |
| 127 // On the host-side, make sure we are called on the right thread. On the | |
| 128 // plugin side, make sure we have the proxy lock. | |
| 129 void CheckThreadingPreconditions() const; | |
| 130 | |
| 131 // Implementation of AddVar that allows the caller to specify whether the | 128 // Implementation of AddVar that allows the caller to specify whether the |
| 132 // initial refcount of the added object will be 0 or 1. | 129 // initial refcount of the added object will be 0 or 1. |
| 133 // | 130 // |
| 134 // Overridden in the plugin proxy to do additional object tracking. | 131 // Overridden in the plugin proxy to do additional object tracking. |
| 135 virtual int32 AddVarInternal(Var* var, AddVarRefMode mode); | 132 virtual int32 AddVarInternal(Var* var, AddVarRefMode mode); |
| 136 | 133 |
| 137 // Convenience functions for doing lookups into the live_vars_ map. | 134 // Convenience functions for doing lookups into the live_vars_ map. |
| 138 VarMap::iterator GetLiveVar(int32 id); | 135 VarMap::iterator GetLiveVar(int32 id); |
| 139 VarMap::iterator GetLiveVar(const PP_Var& var); | 136 VarMap::iterator GetLiveVar(const PP_Var& var); |
| 140 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; | 137 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 168 | 165 |
| 169 // Last assigned var ID. | 166 // Last assigned var ID. |
| 170 int32 last_var_id_; | 167 int32 last_var_id_; |
| 171 | 168 |
| 172 private: | 169 private: |
| 173 // Create and return a new ArrayBufferVar size_in_bytes bytes long. This is | 170 // Create and return a new ArrayBufferVar size_in_bytes bytes long. This is |
| 174 // implemented by the Host and Plugin tracker separately, so that it can be | 171 // implemented by the Host and Plugin tracker separately, so that it can be |
| 175 // a real WebKit ArrayBuffer on the host side. | 172 // a real WebKit ArrayBuffer on the host side. |
| 176 virtual ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) = 0; | 173 virtual ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) = 0; |
| 177 | 174 |
| 178 // On the host side, we want to check that we are only called on the main | |
| 179 // thread. This is to protect us from accidentally using the tracker from | |
| 180 // other threads (especially the IO thread). On the plugin side, the tracker | |
| 181 // is protected by the proxy lock and is thread-safe, so this will be NULL. | |
| 182 scoped_ptr<base::ThreadChecker> thread_checker_; | |
| 183 | |
| 184 DISALLOW_COPY_AND_ASSIGN(VarTracker); | 175 DISALLOW_COPY_AND_ASSIGN(VarTracker); |
| 185 }; | 176 }; |
| 186 | 177 |
| 187 } // namespace ppapi | 178 } // namespace ppapi |
| 188 | 179 |
| 189 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | 180 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ |
| OLD | NEW |