resource_tracker.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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_RESOURCE_TRACKER_H_
6#define PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_
7
8#include <set>
9
10#include "base/basictypes.h"
11#include "base/containers/hash_tables.h"
12#include "base/memory/linked_ptr.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/weak_ptr.h"
15#include "base/threading/thread_checker.h"
16#include "base/threading/thread_checker_impl.h"
17#include "ppapi/c/pp_instance.h"
18#include "ppapi/c/pp_resource.h"
19#include "ppapi/shared_impl/ppapi_shared_export.h"
20
21namespace ppapi {
22
23class Resource;
24
25class PPAPI_SHARED_EXPORT ResourceTracker {
26 public:
27  // A SINGLE_THREADED ResourceTracker will use a thread-checker to make sure
28  // it's always invoked on the same thread on which it was constructed. A
29  // THREAD_SAFE ResourceTracker will check that the ProxyLock is held. See
30  // CheckThreadingPreconditions() for more details.
31  enum ThreadMode { SINGLE_THREADED, THREAD_SAFE };
32  explicit ResourceTracker(ThreadMode thread_mode);
33  virtual ~ResourceTracker();
34
35  // The returned pointer will be NULL if there is no resource. The reference
36  // count of the resource is unaffected.
37  Resource* GetResource(PP_Resource res) const;
38
39  // Takes a reference on the given resource.
40  // Do not call this method on on the host side for resources backed by a
41  // ResourceHost.
42  void AddRefResource(PP_Resource res);
43
44  // Releases a reference on the given resource.
45  // Do not call this method on on the host side for resources backed by a
46  // ResourceHost.
47  void ReleaseResource(PP_Resource res);
48
49  // Releases a reference on the given resource once the message loop returns.
50  void ReleaseResourceSoon(PP_Resource res);
51
52  // Notifies the tracker that a new instance has been created. This must be
53  // called before creating any resources associated with the instance.
54  void DidCreateInstance(PP_Instance instance);
55
56  // Called when an instance is being deleted. All plugin refs for the
57  // associated resources will be force freed, and the resources (if they still
58  // exist) will be disassociated from the instance.
59  void DidDeleteInstance(PP_Instance instance);
60
61  // Returns the number of resources associated with the given instance.
62  // Returns 0 if the instance isn't known.
63  int GetLiveObjectsForInstance(PP_Instance instance) const;
64
65 protected:
66  // This calls AddResource and RemoveResource.
67  friend class Resource;
68
69  // On the host-side, make sure we are called on the right thread. On the
70  // plugin side, make sure we have the proxy lock.
71  void CheckThreadingPreconditions() const;
72
73  // This method is called by PluginResourceTracker's constructor so that in
74  // debug mode PP_Resources from the plugin process always have odd values
75  // (ignoring the type bits), while PP_Resources from the renderer process have
76  // even values.
77  // This allows us to check that resource refs aren't added or released on the
78  // wrong side.
79  void UseOddResourceValueInDebugMode();
80
81  // Adds the given resource to the tracker, associating it with the instance
82  // stored in the resource object. The new resource ID is returned, and the
83  // resource will have 0 plugin refcount. This is called by the resource
84  // constructor.
85  //
86  // Returns 0 if the resource could not be added.
87  virtual PP_Resource AddResource(Resource* object);
88
89  // The opposite of AddResource, this removes the tracking information for
90  // the given resource. It's called from the resource destructor.
91  virtual void RemoveResource(Resource* object);
92
93 private:
94  // Calls NotifyLastPluginRefWasDeleted on the given resource object and
95  // cancels pending callbacks for the resource.
96  void LastPluginRefWasDeleted(Resource* object);
97
98  int32 GetNextResourceValue();
99
100  // In debug mode, checks whether |res| comes from the same resource tracker.
101  bool CanOperateOnResource(PP_Resource res);
102
103  typedef std::set<PP_Resource> ResourceSet;
104
105  struct InstanceData {
106    // Lists all resources associated with the given instance as non-owning
107    // pointers. This allows us to notify those resources that the instance is
108    // going away (otherwise, they may crash if they outlive the instance).
109    ResourceSet resources;
110  };
111  typedef base::hash_map<PP_Instance, linked_ptr<InstanceData> > InstanceMap;
112
113  InstanceMap instance_map_;
114
115  // For each PP_Resource, keep the object pointer and a plugin use count.
116  // This use count is different then Resource object's RefCount, and is
117  // manipulated using this AddRefResource/UnrefResource. When the plugin use
118  // count is positive, we keep an extra ref on the Resource on
119  // behalf of the plugin. When it drops to 0, we free that ref, keeping
120  // the resource in the list.
121  //
122  // A resource will be in this list as long as the object is alive.
123  typedef std::pair<Resource*, int> ResourceAndRefCount;
124  typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap;
125  ResourceMap live_resources_;
126
127  int32 last_resource_value_;
128
129  // On the host side, we want to check that we are only called on the main
130  // thread. This is to protect us from accidentally using the tracker from
131  // other threads (especially the IO thread). On the plugin side, the tracker
132  // is protected by the proxy lock and is thread-safe, so this will be NULL.
133  scoped_ptr<base::ThreadChecker> thread_checker_;
134
135  base::WeakPtrFactory<ResourceTracker> weak_ptr_factory_;
136
137  DISALLOW_COPY_AND_ASSIGN(ResourceTracker);
138};
139
140}  // namespace ppapi
141
142#endif  // PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_
143