resource_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_RESOURCE_TRACKER_H_
6#define PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_
7
8#include <set>
9
10#include "base/basictypes.h"
11#include "base/hash_tables.h"
12#include "base/memory/linked_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/threading/thread_checker.h"
15#include "base/threading/thread_checker_impl.h"
16#include "ppapi/c/pp_instance.h"
17#include "ppapi/c/pp_resource.h"
18#include "ppapi/shared_impl/ppapi_shared_export.h"
19
20namespace ppapi {
21
22class Resource;
23
24class PPAPI_SHARED_EXPORT ResourceTracker {
25 public:
26  ResourceTracker();
27  virtual ~ResourceTracker();
28
29  // The returned pointer will be NULL if there is no resource. The reference
30  // count of the resource is unaffected.
31  Resource* GetResource(PP_Resource res) const;
32
33  void AddRefResource(PP_Resource res);
34  void ReleaseResource(PP_Resource res);
35
36  // Releases a reference on the given resource once the message loop returns.
37  void ReleaseResourceSoon(PP_Resource res);
38
39  // Notifies the tracker that a new instance has been created. This must be
40  // called before creating any resources associated with the instance.
41  void DidCreateInstance(PP_Instance instance);
42
43  // Called when an instance is being deleted. All plugin refs for the
44  // associated resources will be force freed, and the resources (if they still
45  // exist) will be disassociated from the instance.
46  void DidDeleteInstance(PP_Instance instance);
47
48  // Returns the number of resources associated with the given instance.
49  // Returns 0 if the instance isn't known.
50  int GetLiveObjectsForInstance(PP_Instance instance) const;
51
52 protected:
53  // This calls AddResource and RemoveResource.
54  friend class Resource;
55
56  // Adds the given resource to the tracker, associating it with the instance
57  // stored in the resource object. The new resource ID is returned, and the
58  // resource will have 0 plugin refcount. This is called by the resource
59  // constructor.
60  //
61  // Returns 0 if the resource could not be added.
62  virtual PP_Resource AddResource(Resource* object);
63
64  // The opposite of AddResource, this removes the tracking information for
65  // the given resource. It's called from the resource destructor.
66  virtual void RemoveResource(Resource* object);
67
68 private:
69  // Calls NotifyLastPluginRefWasDeleted on the given resource object and
70  // cancels pending callbacks for the resource.
71  void LastPluginRefWasDeleted(Resource* object);
72
73  typedef std::set<PP_Resource> ResourceSet;
74
75  struct InstanceData {
76    // Lists all resources associated with the given instance as non-owning
77    // pointers. This allows us to notify those resources that the instance is
78    // going away (otherwise, they may crash if they outlive the instance).
79    ResourceSet resources;
80  };
81  typedef base::hash_map<PP_Instance, linked_ptr<InstanceData> > InstanceMap;
82
83  InstanceMap instance_map_;
84
85  // For each PP_Resource, keep the object pointer and a plugin use count.
86  // This use count is different then Resource object's RefCount, and is
87  // manipulated using this AddRefResource/UnrefResource. When the plugin use
88  // count is positive, we keep an extra ref on the Resource on
89  // behalf of the plugin. When it drops to 0, we free that ref, keeping
90  // the resource in the list.
91  //
92  // A resource will be in this list as long as the object is alive.
93  typedef std::pair<Resource*, int> ResourceAndRefCount;
94  typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap;
95  ResourceMap live_resources_;
96
97  int32 last_resource_value_;
98
99  base::WeakPtrFactory<ResourceTracker> weak_ptr_factory_;
100
101  // TODO(raymes): We won't need to do thread checks once pepper calls are
102  // allowed off of the main thread.
103  // See http://code.google.com/p/chromium/issues/detail?id=92909.
104#ifdef ENABLE_PEPPER_THREADING
105  base::ThreadCheckerDoNothing thread_checker_;
106#else
107  // TODO(raymes): We've seen plugins (Flash) creating resources from random
108  // threads. Let's always crash for now in this case. Once we have a handle
109  // over how common this is, we can change ThreadCheckerImpl->ThreadChecker
110  // so that we only crash in debug mode. See
111  // https://code.google.com/p/chromium/issues/detail?id=146415.
112  base::ThreadCheckerImpl thread_checker_;
113#endif
114
115  DISALLOW_COPY_AND_ASSIGN(ResourceTracker);
116};
117
118}  // namespace ppapi
119
120#endif  // PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_
121