var_tracker.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
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 <vector>
9
10#include "base/basictypes.h"
11#include "base/hash_tables.h"
12#include "base/memory/ref_counted.h"
13#include "base/threading/non_thread_safe.h"
14#include "ppapi/c/pp_instance.h"
15#include "ppapi/c/pp_module.h"
16#include "ppapi/c/pp_var.h"
17#include "ppapi/shared_impl/ppapi_shared_export.h"
18
19namespace ppapi {
20
21class ArrayBufferVar;
22class Var;
23
24// Tracks non-POD (refcounted) var objects held by a plugin.
25//
26// The tricky part is the concept of a "tracked object". These are only
27// necessary in the plugin side of the proxy when running out of process. A
28// tracked object is one that the plugin is aware of, but doesn't hold a
29// reference to. This will happen when the plugin is passed an object as an
30// argument from the host (renderer) as an input argument to a sync function,
31// but where ownership is not passed.
32//
33// This class maintains the "track_with_no_reference_count" but doesn't do
34// anything with it other than call virtual functions. The interesting parts
35// are added by the PluginObjectVar derived from this class.
36class 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
44 public:
45  VarTracker();
46  virtual ~VarTracker();
47
48  // Called by the Var object to add a new var to the tracker.
49  int32 AddVar(Var* var);
50
51  // Looks up a given var and returns a reference to the Var if it exists.
52  // Returns NULL if the var type is not an object we track (POD) or is
53  // invalid.
54  Var* GetVar(int32 var_id) const;
55  Var* GetVar(const PP_Var& var) const;
56
57  // Increases a previously-known Var ID's refcount, returning true on success,
58  // false if the ID is invalid. The PP_Var version returns true and does
59  // nothing for non-refcounted type vars.
60  bool AddRefVar(int32 var_id);
61  bool AddRefVar(const PP_Var& var);
62
63  // Decreases the given Var ID's refcount, returning true on success, false if
64  // the ID is invalid or if the refcount was already 0. The PP_Var version
65  // returns true and does nothing for non-refcounted type vars. The var will
66  // be deleted if there are no more refs to it.
67  bool ReleaseVar(int32 var_id);
68  bool ReleaseVar(const PP_Var& var);
69
70  // Create a new array buffer of size |size_in_bytes|. Return a PP_Var that
71  // that references it and has an initial reference-count of 1.
72  PP_Var MakeArrayBufferPPVar(uint32 size_in_bytes);
73  // Same as above, but copy the contents of |data| in to the new array buffer.
74  PP_Var MakeArrayBufferPPVar(uint32 size_in_bytes, const void* data);
75
76  // Return a vector containing all PP_Vars that are in the tracker. This is
77  // to help implement PPB_Testing_Dev.GetLiveVars and should generally not be
78  // used in production code. The PP_Vars are returned in no particular order,
79  // and their reference counts are unaffected.
80  std::vector<PP_Var> GetLiveVars();
81
82  // Retrieves the internal reference counts for testing. Returns 0 if we
83  // know about the object but the corresponding value is 0, or -1 if the
84  // given object ID isn't in our map.
85  int GetRefCountForObject(const PP_Var& object);
86  int GetTrackedWithNoReferenceCountForObject(const PP_Var& object);
87
88  // Called after an instance is deleted to do var cleanup.
89  virtual void DidDeleteInstance(PP_Instance instance) = 0;
90
91 protected:
92  struct VarInfo {
93    VarInfo();
94    VarInfo(Var* v, int input_ref_count);
95
96    scoped_refptr<Var> var;
97
98    // Explicit reference count. This value is affected by the renderer calling
99    // AddRef and Release. A nonzero value here is represented by a single
100    // reference in the host on our behalf (this reduces IPC traffic).
101    int ref_count;
102
103    // Tracked object count (see class comment above).
104    //
105    // "TrackObjectWithNoReference" might be called recursively in rare cases.
106    // For example, say the host calls a plugin function with an object as an
107    // argument, and in response, the plugin calls a host function that then
108    // calls another (or the same) plugin function with the same object.
109    //
110    // This value tracks the number of calls to TrackObjectWithNoReference so
111    // we know when we can stop tracking this object.
112    int track_with_no_reference_count;
113  };
114  typedef base::hash_map<int32, VarInfo> VarMap;
115
116  // Specifies what should happen with the refcount when calling AddVarInternal.
117  enum AddVarRefMode {
118    ADD_VAR_TAKE_ONE_REFERENCE,
119    ADD_VAR_CREATE_WITH_NO_REFERENCE
120  };
121
122  // Implementation of AddVar that allows the caller to specify whether the
123  // initial refcount of the added object will be 0 or 1.
124  //
125  // Overridden in the plugin proxy to do additional object tracking.
126  virtual int32 AddVarInternal(Var* var, AddVarRefMode mode);
127
128  // Convenience functions for doing lookups into the live_vars_ map.
129  VarMap::iterator GetLiveVar(int32 id);
130  VarMap::iterator GetLiveVar(const PP_Var& var);
131  VarMap::const_iterator GetLiveVar(const PP_Var& var) const;
132
133  // Returns true if the given vartype is refcounted and has associated objects
134  // (it's not POD).
135  bool IsVarTypeRefcounted(PP_VarType type) const;
136
137  // Called when AddRefVar increases a "tracked" ProxyObject's refcount from
138  // zero to one. In the plugin side of the proxy, we need to send some
139  // messages to the host. In the host side, this should never be called since
140  // there are no proxy objects.
141  virtual void TrackedObjectGettingOneRef(VarMap::const_iterator iter);
142
143  // Called when ReleaseVar decreases a object's refcount from one to zero. It
144  // may still be "tracked" (has a "track_with_no_reference_count") value. In
145  // the plugin side of the proxy, we need to tell the host that we no longer
146  // have a reference. In the host side, this should never be called since
147  // there are no proxy objects.
148  virtual void ObjectGettingZeroRef(VarMap::iterator iter);
149
150  // Called when an object may have had its refcount or
151  // track_with_no_reference_count value decreased. If the object has neither
152  // refs anymore, this will remove it and return true. Returns false if it's
153  // still alive.
154  //
155  // Overridden by the PluginVarTracker to also clean up the host info map.
156  virtual bool DeleteObjectInfoIfNecessary(VarMap::iterator iter);
157
158  VarMap live_vars_;
159
160  // Last assigned var ID.
161  int32 last_var_id_;
162
163 private:
164  // Create and return a new ArrayBufferVar size_in_bytes bytes long. This is
165  // implemented by the Host and Plugin tracker separately, so that it can be
166  // a real WebKit ArrayBuffer on the host side.
167  virtual ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) = 0;
168
169  DISALLOW_COPY_AND_ASSIGN(VarTracker);
170};
171
172}  // namespace ppapi
173
174#endif  // PPAPI_SHARED_IMPL_VAR_TRACKER_H_
175