OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_THUNK_ENTER_H_ | 5 #ifndef PPAPI_THUNK_ENTER_H_ |
6 #define PPAPI_THUNK_ENTER_H_ | 6 #define PPAPI_THUNK_ENTER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "ppapi/c/pp_resource.h" | 9 #include "ppapi/c/pp_resource.h" |
10 #include "ppapi/proxy/interface_id.h" | 10 #include "ppapi/proxy/interface_id.h" |
11 #include "ppapi/shared_impl/function_group_base.h" | 11 #include "ppapi/shared_impl/function_group_base.h" |
| 12 #include "ppapi/shared_impl/proxy_lock.h" |
12 #include "ppapi/shared_impl/resource.h" | 13 #include "ppapi/shared_impl/resource.h" |
13 #include "ppapi/shared_impl/tracker_base.h" | 14 #include "ppapi/shared_impl/tracker_base.h" |
14 #include "ppapi/shared_impl/resource_tracker.h" | 15 #include "ppapi/shared_impl/resource_tracker.h" |
15 #include "ppapi/thunk/ppapi_thunk_export.h" | 16 #include "ppapi/thunk/ppapi_thunk_export.h" |
16 #include "ppapi/thunk/ppb_instance_api.h" | 17 #include "ppapi/thunk/ppb_instance_api.h" |
17 #include "ppapi/thunk/resource_creation_api.h" | 18 #include "ppapi/thunk/resource_creation_api.h" |
18 | 19 |
19 namespace ppapi { | 20 namespace ppapi { |
20 namespace thunk { | 21 namespace thunk { |
21 | 22 |
22 // EnterHost* helper objects: These objects wrap a call from the C PPAPI into | 23 // Enter* helper objects: These objects wrap a call from the C PPAPI into |
23 // the internal implementation. They make sure the lock is acquired and will | 24 // the internal implementation. They make sure the lock is acquired and will |
24 // automatically set up some stuff for you. | 25 // automatically set up some stuff for you. |
25 // | 26 // |
26 // You should always check whether the enter succeeded before using the object. | 27 // You should always check whether the enter succeeded before using the object. |
27 // If this fails, then the instance or resource ID supplied was invalid. | 28 // If this fails, then the instance or resource ID supplied was invalid. |
28 // | 29 // |
29 // The |report_error| arguments to the constructor should indicate if errors | 30 // The |report_error| arguments to the constructor should indicate if errors |
30 // should be logged to the console. If the calling function expects that the | 31 // should be logged to the console. If the calling function expects that the |
31 // input values are correct (the normal case), this should be set to true. In | 32 // input values are correct (the normal case), this should be set to true. In |
32 // some case like |IsFoo(PP_Resource)| the caller is quersioning whether their | 33 // some case like |IsFoo(PP_Resource)| the caller is questioning whether their |
33 // handle is this type, and we don't want to report an error if it's not. | 34 // handle is this type, and we don't want to report an error if it's not. |
34 // | 35 // |
35 // Standalone functions: EnterHostFunction | 36 // Standalone functions: EnterFunction |
36 // Automatically gets the implementation for the function API for the | 37 // Automatically gets the implementation for the function API for the |
37 // supplied PP_Instance. | 38 // supplied PP_Instance. |
38 // | 39 // |
39 // Resource member functions: EnterHostResource | 40 // Resource member functions: EnterResource |
40 // Automatically interprets the given PP_Resource as a resource ID and sets | 41 // Automatically interprets the given PP_Resource as a resource ID and sets |
41 // up the resource object for you. | 42 // up the resource object for you. |
42 | 43 |
43 template<typename FunctionsT> | 44 namespace subtle { |
44 class EnterFunction { | 45 |
| 46 // This helps us define our RAII Enter classes easily. To make an RAII class |
| 47 // which locks the proxy lock on construction and unlocks on destruction, |
| 48 // inherit from |LockOnEntry<true>|. For cases where you don't want to lock, |
| 49 // inherit from |LockOnEntry<false>|. This allows us to share more code between |
| 50 // Enter* and Enter*NoLock classes. |
| 51 template <bool lock_on_entry> |
| 52 struct LockOnEntry; |
| 53 |
| 54 template <> |
| 55 struct LockOnEntry<false> { |
| 56 // TODO(dmichael) assert the lock is held. |
| 57 }; |
| 58 |
| 59 template <> |
| 60 struct LockOnEntry<true> { |
| 61 LockOnEntry() { |
| 62 ppapi::ProxyLock::Acquire(); |
| 63 } |
| 64 ~LockOnEntry() { |
| 65 ppapi::ProxyLock::Release(); |
| 66 } |
| 67 }; |
| 68 |
| 69 } // namespace subtle |
| 70 |
| 71 |
| 72 template<typename FunctionsT, bool lock_on_entry = true> |
| 73 class EnterFunction : subtle::LockOnEntry<lock_on_entry> { |
45 public: | 74 public: |
46 EnterFunction(PP_Instance instance, bool report_error) | 75 EnterFunction(PP_Instance instance, bool report_error) |
47 : functions_(NULL) { | 76 : functions_(NULL) { |
48 FunctionGroupBase* base = TrackerBase::Get()->GetFunctionAPI( | 77 FunctionGroupBase* base = TrackerBase::Get()->GetFunctionAPI( |
49 instance, FunctionsT::interface_id); | 78 instance, FunctionsT::interface_id); |
50 if (base) | 79 if (base) |
51 functions_ = base->GetAs<FunctionsT>(); | 80 functions_ = base->GetAs<FunctionsT>(); |
52 // TODO(brettw) check error and if report_error is set, do something. | 81 // TODO(brettw) check error and if report_error is set, do something. |
53 } | 82 } |
| 83 |
54 ~EnterFunction() {} | 84 ~EnterFunction() {} |
55 | 85 |
56 bool succeeded() const { return !!functions_; } | 86 bool succeeded() const { return !!functions_; } |
57 bool failed() const { return !functions_; } | 87 bool failed() const { return !functions_; } |
58 | 88 |
59 FunctionsT* functions() { return functions_; } | 89 FunctionsT* functions() { return functions_; } |
60 | 90 |
61 private: | 91 private: |
62 FunctionsT* functions_; | 92 FunctionsT* functions_; |
63 | 93 |
64 DISALLOW_COPY_AND_ASSIGN(EnterFunction); | 94 DISALLOW_COPY_AND_ASSIGN(EnterFunction); |
65 }; | 95 }; |
66 | 96 |
67 // Like EnterResource but assumes the lock is already held. | 97 // Like EnterResource but assumes the lock is already held. |
68 // TODO(brettw) actually implement locks, this is just a placeholder for now. | |
69 template<typename FunctionsT> | 98 template<typename FunctionsT> |
70 class EnterFunctionNoLock : public EnterFunction<FunctionsT> { | 99 class EnterFunctionNoLock : public EnterFunction<FunctionsT, false> { |
71 public: | 100 public: |
72 EnterFunctionNoLock(PP_Instance instance, bool report_error) | 101 EnterFunctionNoLock(PP_Instance instance, bool report_error) |
73 : EnterFunction<FunctionsT>(instance, report_error) { | 102 : EnterFunction<FunctionsT, false>(instance, report_error) { |
74 // TODO(brettw) assert the lock is held. | |
75 } | 103 } |
76 }; | 104 }; |
77 | 105 |
78 // Used when a caller has a resource, and wants to do EnterFunction for the | 106 // Used when a caller has a resource, and wants to do EnterFunction for the |
79 // instance corresponding to that resource. | 107 // instance corresponding to that resource. |
80 template<typename FunctionsT> | 108 template<typename FunctionsT> |
81 class EnterFunctionGivenResource : public EnterFunction<FunctionsT> { | 109 class EnterFunctionGivenResource : public EnterFunction<FunctionsT> { |
82 public: | 110 public: |
83 EnterFunctionGivenResource(PP_Resource resource, bool report_error) | 111 EnterFunctionGivenResource(PP_Resource resource, bool report_error) |
84 : EnterFunction<FunctionsT>(GetInstanceForResource(resource), | 112 : EnterFunction<FunctionsT>(GetInstanceForResource(resource), |
85 report_error) { | 113 report_error) { |
86 } | 114 } |
87 | 115 |
88 private: | 116 private: |
89 static PP_Instance GetInstanceForResource(PP_Resource resource) { | 117 static PP_Instance GetInstanceForResource(PP_Resource resource) { |
90 Resource* object = | 118 Resource* object = |
91 TrackerBase::Get()->GetResourceTracker()->GetResource(resource); | 119 TrackerBase::Get()->GetResourceTracker()->GetResource(resource); |
92 return object ? object->pp_instance() : 0; | 120 return object ? object->pp_instance() : 0; |
93 } | 121 } |
94 }; | 122 }; |
95 | 123 |
96 // EnterResource --------------------------------------------------------------- | 124 // EnterResource --------------------------------------------------------------- |
97 | 125 |
98 template<typename ResourceT> | 126 template<typename ResourceT, bool lock_on_entry = true> |
99 class EnterResource { | 127 class EnterResource : subtle::LockOnEntry<lock_on_entry> { |
100 public: | 128 public: |
101 EnterResource(PP_Resource resource, bool report_error) | 129 EnterResource(PP_Resource resource, bool report_error) |
102 : object_(NULL) { | 130 : object_(NULL) { |
103 resource_ = TrackerBase::Get()->GetResourceTracker()->GetResource(resource); | 131 resource_ = TrackerBase::Get()->GetResourceTracker()->GetResource(resource); |
104 if (resource_) | 132 if (resource_) |
105 object_ = resource_->GetAs<ResourceT>(); | 133 object_ = resource_->GetAs<ResourceT>(); |
106 // TODO(brettw) check error and if report_error is set, do something. | 134 // TODO(brettw) check error and if report_error is set, do something. |
107 } | 135 } |
108 ~EnterResource() {} | 136 ~EnterResource() {} |
109 | 137 |
110 bool succeeded() const { return !!object_; } | 138 bool succeeded() const { return !!object_; } |
111 bool failed() const { return !object_; } | 139 bool failed() const { return !object_; } |
112 | 140 |
113 ResourceT* object() { return object_; } | 141 ResourceT* object() { return object_; } |
114 Resource* resource() { return resource_; } | 142 Resource* resource() { return resource_; } |
115 | 143 |
116 private: | 144 private: |
117 Resource* resource_; | 145 Resource* resource_; |
118 ResourceT* object_; | 146 ResourceT* object_; |
119 | 147 |
120 DISALLOW_COPY_AND_ASSIGN(EnterResource); | 148 DISALLOW_COPY_AND_ASSIGN(EnterResource); |
121 }; | 149 }; |
122 | 150 |
123 // Like EnterResource but assumes the lock is already held. | 151 // Like EnterResource but assumes the lock is already held. |
124 // TODO(brettw) actually implement locks, this is just a placeholder for now. | |
125 template<typename ResourceT> | 152 template<typename ResourceT> |
126 class EnterResourceNoLock : public EnterResource<ResourceT> { | 153 class EnterResourceNoLock : public EnterResource<ResourceT, false> { |
127 public: | 154 public: |
128 EnterResourceNoLock(PP_Resource resource, bool report_error) | 155 EnterResourceNoLock(PP_Resource resource, bool report_error) |
129 : EnterResource<ResourceT>(resource, report_error) { | 156 : EnterResource<ResourceT, false>(resource, report_error) { |
130 // TODO(brettw) assert the lock is held. | |
131 } | 157 } |
132 }; | 158 }; |
133 | 159 |
134 // Simpler wrapper to enter the resource creation API. This is used for every | 160 // Simpler wrapper to enter the resource creation API. This is used for every |
135 // class so we have this helper function to save template instantiations and | 161 // class so we have this helper function to save template instantiations and |
136 // typing. | 162 // typing. |
137 class PPAPI_THUNK_EXPORT EnterResourceCreation | 163 class PPAPI_THUNK_EXPORT EnterResourceCreation |
138 : public EnterFunctionNoLock<ResourceCreationAPI> { | 164 : public EnterFunctionNoLock<ResourceCreationAPI> { |
139 public: | 165 public: |
140 EnterResourceCreation(PP_Instance instance); | 166 EnterResourceCreation(PP_Instance instance); |
141 ~EnterResourceCreation(); | 167 ~EnterResourceCreation(); |
142 }; | 168 }; |
143 | 169 |
144 // Simpler wrapper to enter the instance API from proxy code. This is used for | 170 // Simpler wrapper to enter the instance API from proxy code. This is used for |
145 // many interfaces so we have this helper function to save template | 171 // many interfaces so we have this helper function to save template |
146 // instantiations and typing. | 172 // instantiations and typing. |
147 class PPAPI_THUNK_EXPORT EnterInstance | 173 class PPAPI_THUNK_EXPORT EnterInstance |
148 : public EnterFunctionNoLock<PPB_Instance_FunctionAPI> { | 174 : public EnterFunctionNoLock<PPB_Instance_FunctionAPI> { |
149 public: | 175 public: |
150 EnterInstance(PP_Instance instance); | 176 EnterInstance(PP_Instance instance); |
151 ~EnterInstance(); | 177 ~EnterInstance(); |
152 }; | 178 }; |
153 | 179 |
154 } // namespace thunk | 180 } // namespace thunk |
155 } // namespace ppapi | 181 } // namespace ppapi |
156 | 182 |
157 #endif // PPAPI_THUNK_ENTER_H_ | 183 #endif // PPAPI_THUNK_ENTER_H_ |
OLD | NEW |