OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ | 5 #ifndef PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
6 #define PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ | 6 #define PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
16 #include "base/threading/thread_checker_impl.h" | 15 #include "base/threading/thread_checker_impl.h" |
17 #include "ppapi/c/pp_instance.h" | 16 #include "ppapi/c/pp_instance.h" |
18 #include "ppapi/c/pp_resource.h" | 17 #include "ppapi/c/pp_resource.h" |
19 #include "ppapi/shared_impl/ppapi_shared_export.h" | 18 #include "ppapi/shared_impl/ppapi_shared_export.h" |
20 | 19 |
21 namespace ppapi { | 20 namespace ppapi { |
22 | 21 |
23 class Resource; | 22 class Resource; |
24 | 23 |
25 class PPAPI_SHARED_EXPORT ResourceTracker { | 24 class PPAPI_SHARED_EXPORT ResourceTracker { |
26 public: | 25 public: |
27 // A SINGLE_THREADED ResourceTracker will use a thread-checker to make sure | 26 ResourceTracker(); |
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(); | 27 virtual ~ResourceTracker(); |
34 | 28 |
35 // The returned pointer will be NULL if there is no resource. The reference | 29 // The returned pointer will be NULL if there is no resource. The reference |
36 // count of the resource is unaffected. | 30 // count of the resource is unaffected. |
37 Resource* GetResource(PP_Resource res) const; | 31 Resource* GetResource(PP_Resource res) const; |
38 | 32 |
39 void AddRefResource(PP_Resource res); | 33 void AddRefResource(PP_Resource res); |
40 void ReleaseResource(PP_Resource res); | 34 void ReleaseResource(PP_Resource res); |
41 | 35 |
42 // Releases a reference on the given resource once the message loop returns. | 36 // Releases a reference on the given resource once the message loop returns. |
43 void ReleaseResourceSoon(PP_Resource res); | 37 void ReleaseResourceSoon(PP_Resource res); |
44 | 38 |
45 // Notifies the tracker that a new instance has been created. This must be | 39 // Notifies the tracker that a new instance has been created. This must be |
46 // called before creating any resources associated with the instance. | 40 // called before creating any resources associated with the instance. |
47 void DidCreateInstance(PP_Instance instance); | 41 void DidCreateInstance(PP_Instance instance); |
48 | 42 |
49 // Called when an instance is being deleted. All plugin refs for the | 43 // Called when an instance is being deleted. All plugin refs for the |
50 // associated resources will be force freed, and the resources (if they still | 44 // associated resources will be force freed, and the resources (if they still |
51 // exist) will be disassociated from the instance. | 45 // exist) will be disassociated from the instance. |
52 void DidDeleteInstance(PP_Instance instance); | 46 void DidDeleteInstance(PP_Instance instance); |
53 | 47 |
54 // Returns the number of resources associated with the given instance. | 48 // Returns the number of resources associated with the given instance. |
55 // Returns 0 if the instance isn't known. | 49 // Returns 0 if the instance isn't known. |
56 int GetLiveObjectsForInstance(PP_Instance instance) const; | 50 int GetLiveObjectsForInstance(PP_Instance instance) const; |
57 | 51 |
58 protected: | 52 protected: |
59 // This calls AddResource and RemoveResource. | 53 // This calls AddResource and RemoveResource. |
60 friend class Resource; | 54 friend class Resource; |
61 | 55 |
62 // On the host-side, make sure we are called on the right thread. On the | |
63 // plugin side, make sure we have the proxy lock. | |
64 void CheckThreadingPreconditions() const; | |
65 | |
66 // Adds the given resource to the tracker, associating it with the instance | 56 // Adds the given resource to the tracker, associating it with the instance |
67 // stored in the resource object. The new resource ID is returned, and the | 57 // stored in the resource object. The new resource ID is returned, and the |
68 // resource will have 0 plugin refcount. This is called by the resource | 58 // resource will have 0 plugin refcount. This is called by the resource |
69 // constructor. | 59 // constructor. |
70 // | 60 // |
71 // Returns 0 if the resource could not be added. | 61 // Returns 0 if the resource could not be added. |
72 virtual PP_Resource AddResource(Resource* object); | 62 virtual PP_Resource AddResource(Resource* object); |
73 | 63 |
74 // The opposite of AddResource, this removes the tracking information for | 64 // The opposite of AddResource, this removes the tracking information for |
75 // the given resource. It's called from the resource destructor. | 65 // the given resource. It's called from the resource destructor. |
(...skipping 25 matching lines...) Expand all Loading... |
101 // | 91 // |
102 // A resource will be in this list as long as the object is alive. | 92 // A resource will be in this list as long as the object is alive. |
103 typedef std::pair<Resource*, int> ResourceAndRefCount; | 93 typedef std::pair<Resource*, int> ResourceAndRefCount; |
104 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; | 94 typedef base::hash_map<PP_Resource, ResourceAndRefCount> ResourceMap; |
105 ResourceMap live_resources_; | 95 ResourceMap live_resources_; |
106 | 96 |
107 int32 last_resource_value_; | 97 int32 last_resource_value_; |
108 | 98 |
109 base::WeakPtrFactory<ResourceTracker> weak_ptr_factory_; | 99 base::WeakPtrFactory<ResourceTracker> weak_ptr_factory_; |
110 | 100 |
111 // On the host side, we want to check that we are only called on the main | 101 // TODO(raymes): We won't need to do thread checks once pepper calls are |
112 // thread. This is to protect us from accidentally using the tracker from | 102 // allowed off of the main thread. |
113 // other threads (especially the IO thread). On the plugin side, the tracker | 103 // See http://code.google.com/p/chromium/issues/detail?id=92909. |
114 // is protected by the proxy lock and is thread-safe, so this will be NULL. | 104 #ifdef ENABLE_PEPPER_THREADING |
115 scoped_ptr<base::ThreadChecker> thread_checker_; | 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 |
116 | 114 |
117 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); | 115 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); |
118 }; | 116 }; |
119 | 117 |
120 } // namespace ppapi | 118 } // namespace ppapi |
121 | 119 |
122 #endif // PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ | 120 #endif // PPAPI_SHARED_IMPL_RESOURCE_TRACKER_H_ |
OLD | NEW |