Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | |
| 6 #define PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/hash_tables.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "ppapi/c/pp_var.h" | |
| 12 | |
| 13 namespace ppapi { | |
| 14 | |
| 15 class Var; | |
| 16 | |
| 17 // Tracks non-POD (refcounted) var objects held by a plugin. | |
| 18 // | |
| 19 // The tricky part is the concept of a "tracked object". These are only | |
| 20 // necessary in the plugin side of the proxy when running out of process. A | |
| 21 // tracked object is one that the plugin is aware of, but doesn't hold a | |
| 22 // reference to. This will happen when the plugin is passed an object as an | |
| 23 // argument from the host (renderer) as an input argument to a sync function, | |
| 24 // but where ownership is not passed. | |
| 25 // | |
| 26 // This class maintains the "track_with_no_reference_count" but doesn't do | |
| 27 // anything with it other than call virtual functions. The interesting parts | |
| 28 // are added by the PluginObjectVar derived from this class. | |
| 29 class VarTracker { | |
| 30 public: | |
| 31 VarTracker(); | |
| 32 virtual ~VarTracker(); | |
| 33 | |
| 34 // Called by the Var object to add a new var to the tracker. | |
| 35 int32 AddVar(Var* var); | |
| 36 | |
| 37 // Looks up a given var and returns a reference to the Var if it exists. | |
| 38 // Returns NULL if the var type is not an object we track (POD) or is | |
| 39 // invalid. | |
| 40 Var* GetVar(int32 var_id) const; | |
| 41 Var* GetVar(const PP_Var& var) const; | |
| 42 | |
| 43 // Increases a previously-known Var ID's refcount, returning true on success, | |
| 44 // false if the ID is invalid. The PP_Var version returns true and does | |
| 45 // nothing for non-refcounted type vars. | |
| 46 bool AddRefVar(int32 var_id); | |
| 47 bool AddRefVar(const PP_Var& var); | |
| 48 | |
| 49 // Decreases the given Var ID's refcount, returning true on success, false if | |
| 50 // the ID is invalid or if the refcount was already 0. The PP_Var version | |
| 51 // returns true and does nothing for non-refcounted type vars. | |
|
dmichael (off chromium)
2011/08/08 22:13:59
Maybe mention the Var is deleted if ref count goes
| |
| 52 bool ReleaseVar(int32 var_id); | |
| 53 bool ReleaseVar(const PP_Var& var); | |
| 54 | |
| 55 protected: | |
| 56 struct VarInfo { | |
| 57 VarInfo(); | |
| 58 VarInfo(Var* v, int input_ref_count); | |
| 59 | |
| 60 scoped_refptr<Var> var; | |
| 61 | |
| 62 // Explicit reference count. This value is affected by the renderer calling | |
| 63 // AddRef and Release. A nonzero value here is represented by a single | |
| 64 // reference in the host on our behalf (this reduces IPC traffic). | |
| 65 int ref_count; | |
| 66 | |
| 67 // Tracked object count (see class comment above). | |
| 68 // | |
| 69 // "TrackObjectWithNoReference" might be called recursively in rare cases. | |
| 70 // For example, say the host calls a plugin function with an object as an | |
| 71 // argument, and in response, the plugin calls a host function that then | |
| 72 // calls another (or the same) plugin function with the same object. | |
| 73 // | |
| 74 // This value tracks the number of calls to TrackObjectWithNoReference so | |
| 75 // we know when we can stop tracking this object. | |
| 76 int track_with_no_reference_count; | |
| 77 }; | |
| 78 typedef base::hash_map<int32, VarInfo> VarMap; | |
| 79 | |
| 80 // Implementation of AddVar that allows the caller to specify whether the | |
| 81 // initial refcount of the added object will be 0 or 1. | |
| 82 // | |
| 83 // Overridden in the plugin proxy to do additional object tracking. | |
| 84 virtual int32 AddVarInternal(Var* var, bool take_ref); | |
| 85 | |
| 86 // Convenience functions for doing lookups into the live_vars_ map. | |
| 87 VarMap::iterator GetLiveVar(int32 id); | |
| 88 VarMap::iterator GetLiveVar(const PP_Var& var); | |
| 89 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; | |
| 90 | |
| 91 // Returns true if the given vartype is refcounted and has associated objects | |
| 92 // (it's not POD). | |
| 93 bool IsVarTypeRefcounted(PP_VarType type) const; | |
| 94 | |
| 95 // Called when AddRefVar increases a "tracked" ProxyObject's refcount from | |
| 96 // zero to one. In the plugin side of the proxy, we need to send some | |
| 97 // messages to the host. In the host side, this should never be called since | |
| 98 // there are no proxy objects. | |
|
dmichael (off chromium)
2011/08/08 22:13:59
optional suggest: You could have a member on this
brettw
2011/08/09 17:08:09
The implementation of this function is actually NO
dmichael (off chromium)
2011/08/09 19:16:48
Missed that; perfect, thanks.
| |
| 99 virtual void TrackedObjectGettingOneRef(VarMap::const_iterator iter); | |
| 100 | |
| 101 // Called when ReleaseVar decreases a object's refcount from one to zero. It | |
| 102 // may still be "tracked" (has a "track_with_no_reference_count") value. In | |
| 103 // the plugin side of the proxy, we need to tell the host that we no longer | |
| 104 // have a reference. In the host side, this should never be called since | |
| 105 // there are no proxy objects. | |
| 106 virtual void ObjectGettingZeroRef(VarMap::iterator iter); | |
| 107 | |
| 108 // Called when an object may have had its refcount or | |
| 109 // track_with_no_reference_count value decreased. If the object has neither | |
| 110 // refs anymore, this will remove it and return true. Returns false if it's | |
| 111 // still alive. | |
| 112 // | |
| 113 // Overridden by the PluginVarTracker to also clean up the host info map. | |
| 114 virtual bool DeleteObjectInfoIfNecessary(VarMap::iterator iter); | |
| 115 | |
| 116 VarMap live_vars_; | |
| 117 | |
| 118 // Last assigned var ID. | |
| 119 int32 last_var_id_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(VarTracker); | |
| 122 }; | |
| 123 | |
| 124 } // namespace ppapi | |
| 125 | |
| 126 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ | |
| OLD | NEW |