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. The var will |
| 52 // be deleted if there are no more refs to it. |
| 53 bool ReleaseVar(int32 var_id); |
| 54 bool ReleaseVar(const PP_Var& var); |
| 55 |
| 56 protected: |
| 57 struct VarInfo { |
| 58 VarInfo(); |
| 59 VarInfo(Var* v, int input_ref_count); |
| 60 |
| 61 scoped_refptr<Var> var; |
| 62 |
| 63 // Explicit reference count. This value is affected by the renderer calling |
| 64 // AddRef and Release. A nonzero value here is represented by a single |
| 65 // reference in the host on our behalf (this reduces IPC traffic). |
| 66 int ref_count; |
| 67 |
| 68 // Tracked object count (see class comment above). |
| 69 // |
| 70 // "TrackObjectWithNoReference" might be called recursively in rare cases. |
| 71 // For example, say the host calls a plugin function with an object as an |
| 72 // argument, and in response, the plugin calls a host function that then |
| 73 // calls another (or the same) plugin function with the same object. |
| 74 // |
| 75 // This value tracks the number of calls to TrackObjectWithNoReference so |
| 76 // we know when we can stop tracking this object. |
| 77 int track_with_no_reference_count; |
| 78 }; |
| 79 typedef base::hash_map<int32, VarInfo> VarMap; |
| 80 |
| 81 // Specifies what should happen with the refcount when calling AddVarInternal. |
| 82 enum AddVarRefMode { |
| 83 ADD_VAR_TAKE_ONE_REFERENCE, |
| 84 ADD_VAR_CREATE_WITH_NO_REFERENCE |
| 85 }; |
| 86 |
| 87 // Implementation of AddVar that allows the caller to specify whether the |
| 88 // initial refcount of the added object will be 0 or 1. |
| 89 // |
| 90 // Overridden in the plugin proxy to do additional object tracking. |
| 91 virtual int32 AddVarInternal(Var* var, AddVarRefMode mode); |
| 92 |
| 93 // Convenience functions for doing lookups into the live_vars_ map. |
| 94 VarMap::iterator GetLiveVar(int32 id); |
| 95 VarMap::iterator GetLiveVar(const PP_Var& var); |
| 96 VarMap::const_iterator GetLiveVar(const PP_Var& var) const; |
| 97 |
| 98 // Returns true if the given vartype is refcounted and has associated objects |
| 99 // (it's not POD). |
| 100 bool IsVarTypeRefcounted(PP_VarType type) const; |
| 101 |
| 102 // Called when AddRefVar increases a "tracked" ProxyObject's refcount from |
| 103 // zero to one. In the plugin side of the proxy, we need to send some |
| 104 // messages to the host. In the host side, this should never be called since |
| 105 // there are no proxy objects. |
| 106 virtual void TrackedObjectGettingOneRef(VarMap::const_iterator iter); |
| 107 |
| 108 // Called when ReleaseVar decreases a object's refcount from one to zero. It |
| 109 // may still be "tracked" (has a "track_with_no_reference_count") value. In |
| 110 // the plugin side of the proxy, we need to tell the host that we no longer |
| 111 // have a reference. In the host side, this should never be called since |
| 112 // there are no proxy objects. |
| 113 virtual void ObjectGettingZeroRef(VarMap::iterator iter); |
| 114 |
| 115 // Called when an object may have had its refcount or |
| 116 // track_with_no_reference_count value decreased. If the object has neither |
| 117 // refs anymore, this will remove it and return true. Returns false if it's |
| 118 // still alive. |
| 119 // |
| 120 // Overridden by the PluginVarTracker to also clean up the host info map. |
| 121 virtual bool DeleteObjectInfoIfNecessary(VarMap::iterator iter); |
| 122 |
| 123 VarMap live_vars_; |
| 124 |
| 125 // Last assigned var ID. |
| 126 int32 last_var_id_; |
| 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(VarTracker); |
| 129 }; |
| 130 |
| 131 } // namespace ppapi |
| 132 |
| 133 #endif // PPAPI_SHARED_IMPL_VAR_TRACKER_H_ |
OLD | NEW |