| 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 <stdint.h> |
| 9 |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/shared_memory.h" | 16 #include "base/memory/shared_memory.h" |
| 15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 16 #include "ppapi/c/pp_instance.h" | 18 #include "ppapi/c/pp_instance.h" |
| 17 #include "ppapi/c/pp_module.h" | 19 #include "ppapi/c/pp_module.h" |
| 18 #include "ppapi/c/pp_resource.h" | 20 #include "ppapi/c/pp_resource.h" |
| 19 #include "ppapi/c/pp_var.h" | 21 #include "ppapi/c/pp_var.h" |
| 20 #include "ppapi/shared_impl/host_resource.h" | 22 #include "ppapi/shared_impl/host_resource.h" |
| 21 #include "ppapi/shared_impl/ppapi_shared_export.h" | 23 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 45 public: | 47 public: |
| 46 // A SINGLE_THREADED VarTracker will use a thread-checker to make sure it's | 48 // A SINGLE_THREADED VarTracker will use a thread-checker to make sure it's |
| 47 // always invoked on the same thread on which it was constructed. A | 49 // always invoked on the same thread on which it was constructed. A |
| 48 // THREAD_SAFE VarTracker will check that the ProxyLock is held. See | 50 // THREAD_SAFE VarTracker will check that the ProxyLock is held. See |
| 49 // CheckThreadingPreconditions() for more details. | 51 // CheckThreadingPreconditions() for more details. |
| 50 enum ThreadMode { SINGLE_THREADED, THREAD_SAFE }; | 52 enum ThreadMode { SINGLE_THREADED, THREAD_SAFE }; |
| 51 explicit VarTracker(ThreadMode thread_mode); | 53 explicit VarTracker(ThreadMode thread_mode); |
| 52 virtual ~VarTracker(); | 54 virtual ~VarTracker(); |
| 53 | 55 |
| 54 // Called by the Var object to add a new var to the tracker. | 56 // Called by the Var object to add a new var to the tracker. |
| 55 int32 AddVar(Var* var); | 57 int32_t AddVar(Var* var); |
| 56 | 58 |
| 57 // Looks up a given var and returns a reference to the Var if it exists. | 59 // Looks up a given var and returns a reference to the Var if it exists. |
| 58 // Returns NULL if the var type is not an object we track (POD) or is | 60 // Returns NULL if the var type is not an object we track (POD) or is |
| 59 // invalid. | 61 // invalid. |
| 60 Var* GetVar(int32 var_id) const; | 62 Var* GetVar(int32_t var_id) const; |
| 61 Var* GetVar(const PP_Var& var) const; | 63 Var* GetVar(const PP_Var& var) const; |
| 62 | 64 |
| 63 // Increases a previously-known Var ID's refcount, returning true on success, | 65 // Increases a previously-known Var ID's refcount, returning true on success, |
| 64 // false if the ID is invalid. The PP_Var version returns true and does | 66 // false if the ID is invalid. The PP_Var version returns true and does |
| 65 // nothing for non-refcounted type vars. | 67 // nothing for non-refcounted type vars. |
| 66 bool AddRefVar(int32 var_id); | 68 bool AddRefVar(int32_t var_id); |
| 67 bool AddRefVar(const PP_Var& var); | 69 bool AddRefVar(const PP_Var& var); |
| 68 | 70 |
| 69 // Decreases the given Var ID's refcount, returning true on success, false if | 71 // Decreases the given Var ID's refcount, returning true on success, false if |
| 70 // the ID is invalid or if the refcount was already 0. The PP_Var version | 72 // the ID is invalid or if the refcount was already 0. The PP_Var version |
| 71 // returns true and does nothing for non-refcounted type vars. The var will | 73 // returns true and does nothing for non-refcounted type vars. The var will |
| 72 // be deleted if there are no more refs to it. | 74 // be deleted if there are no more refs to it. |
| 73 bool ReleaseVar(int32 var_id); | 75 bool ReleaseVar(int32_t var_id); |
| 74 bool ReleaseVar(const PP_Var& var); | 76 bool ReleaseVar(const PP_Var& var); |
| 75 | 77 |
| 76 // Create a new array buffer of size |size_in_bytes|. Return a PP_Var that | 78 // Create a new array buffer of size |size_in_bytes|. Return a PP_Var that |
| 77 // that references it and has an initial reference-count of 1. | 79 // that references it and has an initial reference-count of 1. |
| 78 PP_Var MakeArrayBufferPPVar(uint32 size_in_bytes); | 80 PP_Var MakeArrayBufferPPVar(uint32_t size_in_bytes); |
| 79 // Same as above, but copy the contents of |data| in to the new array buffer. | 81 // Same as above, but copy the contents of |data| in to the new array buffer. |
| 80 PP_Var MakeArrayBufferPPVar(uint32 size_in_bytes, const void* data); | 82 PP_Var MakeArrayBufferPPVar(uint32_t size_in_bytes, const void* data); |
| 81 // Same as above, but copy the contents of the shared memory in |h| | 83 // Same as above, but copy the contents of the shared memory in |h| |
| 82 // into the new array buffer. | 84 // into the new array buffer. |
| 83 PP_Var MakeArrayBufferPPVar(uint32 size_in_bytes, base::SharedMemoryHandle h); | 85 PP_Var MakeArrayBufferPPVar(uint32_t size_in_bytes, |
| 86 base::SharedMemoryHandle h); |
| 84 | 87 |
| 85 // Create an ArrayBuffer and copy the contents of |data| in to it. The | 88 // Create an ArrayBuffer and copy the contents of |data| in to it. The |
| 86 // returned object has 0 reference count in the tracker, and like all | 89 // returned object has 0 reference count in the tracker, and like all |
| 87 // RefCounted objects, has a 0 initial internal reference count. (You should | 90 // RefCounted objects, has a 0 initial internal reference count. (You should |
| 88 // usually immediately put this in a scoped_refptr). | 91 // usually immediately put this in a scoped_refptr). |
| 89 ArrayBufferVar* MakeArrayBufferVar(uint32 size_in_bytes, const void* data); | 92 ArrayBufferVar* MakeArrayBufferVar(uint32_t size_in_bytes, const void* data); |
| 90 | 93 |
| 91 // Creates a new resource var from a resource creation message. Returns a | 94 // Creates a new resource var from a resource creation message. Returns a |
| 92 // PP_Var that references a new PP_Resource, both with an initial reference | 95 // PP_Var that references a new PP_Resource, both with an initial reference |
| 93 // count of 1. On the host side, |creation_message| is ignored, and an empty | 96 // count of 1. On the host side, |creation_message| is ignored, and an empty |
| 94 // resource var is always returned. | 97 // resource var is always returned. |
| 95 virtual PP_Var MakeResourcePPVarFromMessage( | 98 virtual PP_Var MakeResourcePPVarFromMessage( |
| 96 PP_Instance instance, | 99 PP_Instance instance, |
| 97 const IPC::Message& creation_message, | 100 const IPC::Message& creation_message, |
| 98 int pending_renderer_id, | 101 int pending_renderer_id, |
| 99 int pending_browser_id) = 0; | 102 int pending_browser_id) = 0; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 130 static bool IsVarTypeRefcounted(PP_VarType type); | 133 static bool IsVarTypeRefcounted(PP_VarType type); |
| 131 | 134 |
| 132 // Called after an instance is deleted to do var cleanup. | 135 // Called after an instance is deleted to do var cleanup. |
| 133 virtual void DidDeleteInstance(PP_Instance instance) = 0; | 136 virtual void DidDeleteInstance(PP_Instance instance) = 0; |
| 134 | 137 |
| 135 // Returns an "id" for a shared memory handle that can be safely sent between | 138 // Returns an "id" for a shared memory handle that can be safely sent between |
| 136 // the host and plugin, and resolved back into the original handle on the | 139 // the host and plugin, and resolved back into the original handle on the |
| 137 // host. Not implemented on the plugin side. | 140 // host. Not implemented on the plugin side. |
| 138 virtual int TrackSharedMemoryHandle(PP_Instance instance, | 141 virtual int TrackSharedMemoryHandle(PP_Instance instance, |
| 139 base::SharedMemoryHandle handle, | 142 base::SharedMemoryHandle handle, |
| 140 uint32 size_in_bytes) = 0; | 143 uint32_t size_in_bytes) = 0; |
| 141 | 144 |
| 142 // Resolves an "id" generated by TrackSharedMemoryHandle back into | 145 // Resolves an "id" generated by TrackSharedMemoryHandle back into |
| 143 // a SharedMemory handle and its size on the host. | 146 // a SharedMemory handle and its size on the host. |
| 144 // Not implemented on the plugin side. | 147 // Not implemented on the plugin side. |
| 145 virtual bool StopTrackingSharedMemoryHandle(int id, | 148 virtual bool StopTrackingSharedMemoryHandle(int id, |
| 146 PP_Instance instance, | 149 PP_Instance instance, |
| 147 base::SharedMemoryHandle* handle, | 150 base::SharedMemoryHandle* handle, |
| 148 uint32* size_in_bytes) = 0; | 151 uint32_t* size_in_bytes) = 0; |
| 149 | 152 |
| 150 protected: | 153 protected: |
| 151 struct PPAPI_SHARED_EXPORT VarInfo { | 154 struct PPAPI_SHARED_EXPORT VarInfo { |
| 152 VarInfo(); | 155 VarInfo(); |
| 153 VarInfo(Var* v, int input_ref_count); | 156 VarInfo(Var* v, int input_ref_count); |
| 154 | 157 |
| 155 scoped_refptr<Var> var; | 158 scoped_refptr<Var> var; |
| 156 | 159 |
| 157 // Explicit reference count. This value is affected by the renderer calling | 160 // Explicit reference count. This value is affected by the renderer calling |
| 158 // AddRef and Release. A nonzero value here is represented by a single | 161 // AddRef and Release. A nonzero value here is represented by a single |
| 159 // reference in the host on our behalf (this reduces IPC traffic). | 162 // reference in the host on our behalf (this reduces IPC traffic). |
| 160 int ref_count; | 163 int ref_count; |
| 161 | 164 |
| 162 // Tracked object count (see class comment above). | 165 // Tracked object count (see class comment above). |
| 163 // | 166 // |
| 164 // "TrackObjectWithNoReference" might be called recursively in rare cases. | 167 // "TrackObjectWithNoReference" might be called recursively in rare cases. |
| 165 // For example, say the host calls a plugin function with an object as an | 168 // For example, say the host calls a plugin function with an object as an |
| 166 // argument, and in response, the plugin calls a host function that then | 169 // argument, and in response, the plugin calls a host function that then |
| 167 // calls another (or the same) plugin function with the same object. | 170 // calls another (or the same) plugin function with the same object. |
| 168 // | 171 // |
| 169 // This value tracks the number of calls to TrackObjectWithNoReference so | 172 // This value tracks the number of calls to TrackObjectWithNoReference so |
| 170 // we know when we can stop tracking this object. | 173 // we know when we can stop tracking this object. |
| 171 int track_with_no_reference_count; | 174 int track_with_no_reference_count; |
| 172 }; | 175 }; |
| 173 typedef base::hash_map<int32, VarInfo> VarMap; | 176 typedef base::hash_map<int32_t, VarInfo> VarMap; |
| 174 | 177 |
| 175 // Specifies what should happen with the refcount when calling AddVarInternal. | 178 // Specifies what should happen with the refcount when calling AddVarInternal. |
| 176 enum AddVarRefMode { | 179 enum AddVarRefMode { |
| 177 ADD_VAR_TAKE_ONE_REFERENCE, | 180 ADD_VAR_TAKE_ONE_REFERENCE, |
| 178 ADD_VAR_CREATE_WITH_NO_REFERENCE | 181 ADD_VAR_CREATE_WITH_NO_REFERENCE |
| 179 }; | 182 }; |
| 180 | 183 |
| 181 // On the host-side, make sure we are called on the right thread. On the | 184 // On the host-side, make sure we are called on the right thread. On the |
| 182 // plugin side, make sure we have the proxy lock. | 185 // plugin side, make sure we have the proxy lock. |
| 183 void CheckThreadingPreconditions() const; | 186 void CheckThreadingPreconditions() const; |
| 184 | 187 |
| 185 // Implementation of AddVar that allows the caller to specify whether the | 188 // Implementation of AddVar that allows the caller to specify whether the |
| 186 // initial refcount of the added object will be 0 or 1. | 189 // initial refcount of the added object will be 0 or 1. |
| 187 // | 190 // |
| 188 // Overridden in the plugin proxy to do additional object tracking. | 191 // Overridden in the plugin proxy to do additional object tracking. |
| 189 virtual int32 AddVarInternal(Var* var, AddVarRefMode mode); | 192 virtual int32_t AddVarInternal(Var* var, AddVarRefMode mode); |
| 190 | 193 |
| 191 // Convenience functions for doing lookups into the live_vars_ map. | 194 // Convenience functions for doing lookups into the live_vars_ map. |
| 192 VarMap::iterator GetLiveVar(int32 id); | 195 VarMap::iterator GetLiveVar(int32_t id); |
| 193 VarMap::iterator GetLiveVar(const PP_Var& var); | 196 VarMap::iterator GetLiveVar(const PP_Var& var); |
| 194 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; | 197 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; |
| 195 | 198 |
| 196 // Called when AddRefVar increases a "tracked" ProxyObject's refcount from | 199 // Called when AddRefVar increases a "tracked" ProxyObject's refcount from |
| 197 // zero to one. In the plugin side of the proxy, we need to send some | 200 // zero to one. In the plugin side of the proxy, we need to send some |
| 198 // messages to the host. In the host side, this should never be called since | 201 // messages to the host. In the host side, this should never be called since |
| 199 // there are no proxy objects. | 202 // there are no proxy objects. |
| 200 virtual void TrackedObjectGettingOneRef(VarMap::const_iterator iter); | 203 virtual void TrackedObjectGettingOneRef(VarMap::const_iterator iter); |
| 201 | 204 |
| 202 // Called when ReleaseVar decreases a object's refcount from one to zero. It | 205 // Called when ReleaseVar decreases a object's refcount from one to zero. It |
| 203 // may still be "tracked" (has a "track_with_no_reference_count") value. In | 206 // may still be "tracked" (has a "track_with_no_reference_count") value. In |
| 204 // the plugin side of the proxy, we need to tell the host that we no longer | 207 // the plugin side of the proxy, we need to tell the host that we no longer |
| 205 // have a reference. In the host side, this should never be called since | 208 // have a reference. In the host side, this should never be called since |
| 206 // there are no proxy objects. | 209 // there are no proxy objects. |
| 207 virtual void ObjectGettingZeroRef(VarMap::iterator iter); | 210 virtual void ObjectGettingZeroRef(VarMap::iterator iter); |
| 208 | 211 |
| 209 // Called when an object may have had its refcount or | 212 // Called when an object may have had its refcount or |
| 210 // track_with_no_reference_count value decreased. If the object has neither | 213 // track_with_no_reference_count value decreased. If the object has neither |
| 211 // refs anymore, this will remove it and return true. Returns false if it's | 214 // refs anymore, this will remove it and return true. Returns false if it's |
| 212 // still alive. | 215 // still alive. |
| 213 // | 216 // |
| 214 // Overridden by the PluginVarTracker to also clean up the host info map. | 217 // Overridden by the PluginVarTracker to also clean up the host info map. |
| 215 virtual bool DeleteObjectInfoIfNecessary(VarMap::iterator iter); | 218 virtual bool DeleteObjectInfoIfNecessary(VarMap::iterator iter); |
| 216 | 219 |
| 217 VarMap live_vars_; | 220 VarMap live_vars_; |
| 218 | 221 |
| 219 // Last assigned var ID. | 222 // Last assigned var ID. |
| 220 int32 last_var_id_; | 223 int32_t last_var_id_; |
| 221 | 224 |
| 222 private: | 225 private: |
| 223 // Create and return a new ArrayBufferVar size_in_bytes bytes long. This is | 226 // Create and return a new ArrayBufferVar size_in_bytes bytes long. This is |
| 224 // implemented by the Host and Plugin tracker separately, so that it can be | 227 // implemented by the Host and Plugin tracker separately, so that it can be |
| 225 // a real WebKit ArrayBuffer on the host side. | 228 // a real WebKit ArrayBuffer on the host side. |
| 226 virtual ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) = 0; | 229 virtual ArrayBufferVar* CreateArrayBuffer(uint32_t size_in_bytes) = 0; |
| 227 virtual ArrayBufferVar* CreateShmArrayBuffer( | 230 virtual ArrayBufferVar* CreateShmArrayBuffer( |
| 228 uint32 size_in_bytes, | 231 uint32_t size_in_bytes, |
| 229 base::SharedMemoryHandle handle) = 0; | 232 base::SharedMemoryHandle handle) = 0; |
| 230 | 233 |
| 231 // On the host side, we want to check that we are only called on the main | 234 // On the host side, we want to check that we are only called on the main |
| 232 // thread. This is to protect us from accidentally using the tracker from | 235 // thread. This is to protect us from accidentally using the tracker from |
| 233 // other threads (especially the IO thread). On the plugin side, the tracker | 236 // other threads (especially the IO thread). On the plugin side, the tracker |
| 234 // is protected by the proxy lock and is thread-safe, so this will be NULL. | 237 // is protected by the proxy lock and is thread-safe, so this will be NULL. |
| 235 scoped_ptr<base::ThreadChecker> thread_checker_; | 238 scoped_ptr<base::ThreadChecker> thread_checker_; |
| 236 | 239 |
| 237 DISALLOW_COPY_AND_ASSIGN(VarTracker); | 240 DISALLOW_COPY_AND_ASSIGN(VarTracker); |
| 238 }; | 241 }; |
| 239 | 242 |
| 240 } // namespace ppapi | 243 } // namespace ppapi |
| 241 | 244 |
| 242 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | 245 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ |
| OLD | NEW |