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_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/shared_impl/api_id.h" | 10 #include "ppapi/shared_impl/api_id.h" |
11 #include "ppapi/shared_impl/function_group_base.h" | |
12 #include "ppapi/shared_impl/ppapi_globals.h" | 11 #include "ppapi/shared_impl/ppapi_globals.h" |
13 #include "ppapi/shared_impl/proxy_lock.h" | 12 #include "ppapi/shared_impl/proxy_lock.h" |
14 #include "ppapi/shared_impl/resource.h" | 13 #include "ppapi/shared_impl/resource.h" |
15 #include "ppapi/shared_impl/resource_tracker.h" | 14 #include "ppapi/shared_impl/resource_tracker.h" |
16 #include "ppapi/thunk/ppapi_thunk_export.h" | 15 #include "ppapi/thunk/ppapi_thunk_export.h" |
17 #include "ppapi/thunk/ppb_instance_api.h" | 16 #include "ppapi/thunk/ppb_instance_api.h" |
18 #include "ppapi/thunk/resource_creation_api.h" | 17 #include "ppapi/thunk/resource_creation_api.h" |
19 | 18 |
20 namespace ppapi { | 19 namespace ppapi { |
21 namespace thunk { | 20 namespace thunk { |
22 | 21 |
23 // Enter* helper objects: These objects wrap a call from the C PPAPI into | 22 // Enter* helper objects: These objects wrap a call from the C PPAPI into |
24 // the internal implementation. They make sure the lock is acquired and will | 23 // the internal implementation. They make sure the lock is acquired and will |
25 // automatically set up some stuff for you. | 24 // automatically set up some stuff for you. |
26 // | 25 // |
27 // You should always check whether the enter succeeded before using the object. | 26 // You should always check whether the enter succeeded before using the object. |
28 // If this fails, then the instance or resource ID supplied was invalid. | 27 // If this fails, then the instance or resource ID supplied was invalid. |
29 // | 28 // |
30 // The |report_error| arguments to the constructor should indicate if errors | 29 // The |report_error| arguments to the constructor should indicate if errors |
31 // should be logged to the console. If the calling function expects that the | 30 // should be logged to the console. If the calling function expects that the |
32 // input values are correct (the normal case), this should be set to true. In | 31 // input values are correct (the normal case), this should be set to true. In |
33 // some case like |IsFoo(PP_Resource)| the caller is questioning whether their | 32 // some case like |IsFoo(PP_Resource)| the caller is questioning whether their |
34 // handle is this type, and we don't want to report an error if it's not. | 33 // handle is this type, and we don't want to report an error if it's not. |
35 // | 34 // |
36 // Standalone functions: EnterFunction | |
37 // Automatically gets the implementation for the function API for the | |
38 // supplied PP_Instance. | |
39 // | |
40 // Resource member functions: EnterResource | 35 // Resource member functions: EnterResource |
41 // Automatically interprets the given PP_Resource as a resource ID and sets | 36 // Automatically interprets the given PP_Resource as a resource ID and sets |
42 // up the resource object for you. | 37 // up the resource object for you. |
43 | 38 |
44 namespace subtle { | 39 namespace subtle { |
45 | 40 |
46 // This helps us define our RAII Enter classes easily. To make an RAII class | 41 // 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, | 42 // 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, | 43 // 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 | 44 // inherit from |LockOnEntry<false>|. This allows us to share more code between |
(...skipping 27 matching lines...) Expand all Loading... | |
77 // | 72 // |
78 // Returns the "retval()". This is to support the typical usage of | 73 // Returns the "retval()". This is to support the typical usage of |
79 // return enter.SetResult(...); | 74 // return enter.SetResult(...); |
80 // without having to write a separate "return enter.retval();" line. | 75 // without having to write a separate "return enter.retval();" line. |
81 int32_t SetResult(int32_t result); | 76 int32_t SetResult(int32_t result); |
82 | 77 |
83 // Use this value as the return value for the function. | 78 // Use this value as the return value for the function. |
84 int32_t retval() const { return retval_; } | 79 int32_t retval() const { return retval_; } |
85 | 80 |
86 protected: | 81 protected: |
87 // Helper function to return a function group from a PP_Instance. Having this | |
88 // code be in the non-templatized base keeps us from having to instantiate | |
89 // it in every template. | |
90 FunctionGroupBase* GetFunctions(PP_Instance instance, ApiID id) const; | |
91 | |
92 // Helper function to return a Resource from a PP_Resource. Having this | 82 // Helper function to return a Resource from a PP_Resource. Having this |
93 // code be in the non-templatized base keeps us from having to instantiate | 83 // code be in the non-templatized base keeps us from having to instantiate |
94 // it in every template. | 84 // it in every template. |
95 Resource* GetResource(PP_Resource resource) const; | 85 Resource* GetResource(PP_Resource resource) const; |
96 | 86 |
97 // Does error handling associated with entering a resource. The resource_base | 87 // Does error handling associated with entering a resource. The resource_base |
98 // is the result of looking up the given pp_resource. The object is the | 88 // is the result of looking up the given pp_resource. The object is the |
99 // result of converting the base to the desired object (converted back to a | 89 // result of converting the base to the desired object (converted back to a |
100 // Resource* so this function doesn't have to be templatized). The reason for | 90 // Resource* so this function doesn't have to be templatized). The reason for |
101 // passing both resource_base and object is that we can differentiate "bad | 91 // passing both resource_base and object is that we can differentiate "bad |
(...skipping 15 matching lines...) Expand all Loading... | |
117 // Holds the callback. The function will only be non-NULL when the | 107 // Holds the callback. The function will only be non-NULL when the |
118 // callback is requried. Optional callbacks don't require any special | 108 // callback is requried. Optional callbacks don't require any special |
119 // handling from us at this layer. | 109 // handling from us at this layer. |
120 PP_CompletionCallback callback_; | 110 PP_CompletionCallback callback_; |
121 | 111 |
122 int32_t retval_; | 112 int32_t retval_; |
123 }; | 113 }; |
124 | 114 |
125 } // namespace subtle | 115 } // namespace subtle |
126 | 116 |
127 // EnterFunction -------------------------------------------------------------- | |
128 | |
129 template<typename FunctionsT, bool lock_on_entry = true> | |
130 class EnterFunction : public subtle::EnterBase, | |
131 public subtle::LockOnEntry<lock_on_entry> { | |
132 public: | |
133 EnterFunction(PP_Instance instance, bool report_error) | |
134 : EnterBase() { | |
135 Init(instance, report_error); | |
136 } | |
137 EnterFunction(PP_Instance instance, | |
138 const PP_CompletionCallback& callback, | |
139 bool report_error) | |
140 : EnterBase(callback) { | |
141 Init(instance, report_error); | |
142 } | |
143 | |
144 ~EnterFunction() {} | |
145 | |
146 bool succeeded() const { return !!functions_; } | |
147 bool failed() const { return !functions_; } | |
148 | |
149 FunctionsT* functions() { return functions_; } | |
150 | |
151 private: | |
152 void Init(PP_Instance instance, bool report_error) { | |
153 FunctionGroupBase* base = GetFunctions(instance, FunctionsT::kApiID); | |
154 if (base) | |
155 functions_ = base->GetAs<FunctionsT>(); | |
156 else | |
157 functions_ = NULL; | |
158 SetStateForFunctionError(instance, functions_, report_error); | |
159 } | |
160 | |
161 FunctionsT* functions_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(EnterFunction); | |
164 }; | |
165 | |
166 // Like EnterFunction but assumes the lock is already held. | |
167 template<typename FunctionsT> | |
168 class EnterFunctionNoLock : public EnterFunction<FunctionsT, false> { | |
169 public: | |
170 EnterFunctionNoLock(PP_Instance instance, bool report_error) | |
171 : EnterFunction<FunctionsT, false>(instance, report_error) { | |
172 } | |
173 }; | |
174 | |
175 // Used when a caller has a resource, and wants to do EnterFunction for the | |
176 // instance corresponding to that resource. | |
177 template<typename FunctionsT> | |
178 class EnterFunctionGivenResource : public EnterFunction<FunctionsT> { | |
179 public: | |
180 EnterFunctionGivenResource(PP_Resource resource, bool report_error) | |
181 : EnterFunction<FunctionsT>(GetInstanceForResource(resource), | |
182 report_error) { | |
183 } | |
184 | |
185 private: | |
186 static PP_Instance GetInstanceForResource(PP_Resource resource) { | |
187 Resource* object = | |
188 PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource); | |
189 return object ? object->pp_instance() : 0; | |
190 } | |
191 }; | |
192 | |
193 // EnterResource --------------------------------------------------------------- | 117 // EnterResource --------------------------------------------------------------- |
194 | 118 |
195 template<typename ResourceT, bool lock_on_entry = true> | 119 template<typename ResourceT, bool lock_on_entry = true> |
196 class EnterResource : public subtle::EnterBase, | 120 class EnterResource : public subtle::EnterBase, |
197 public subtle::LockOnEntry<lock_on_entry> { | 121 public subtle::LockOnEntry<lock_on_entry> { |
198 public: | 122 public: |
199 EnterResource(PP_Resource resource, bool report_error) | 123 EnterResource(PP_Resource resource, bool report_error) |
200 : EnterBase() { | 124 : EnterBase() { |
201 Init(resource, report_error); | 125 Init(resource, report_error); |
202 } | 126 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 | 158 |
235 // Like EnterResource but assumes the lock is already held. | 159 // Like EnterResource but assumes the lock is already held. |
236 template<typename ResourceT> | 160 template<typename ResourceT> |
237 class EnterResourceNoLock : public EnterResource<ResourceT, false> { | 161 class EnterResourceNoLock : public EnterResource<ResourceT, false> { |
238 public: | 162 public: |
239 EnterResourceNoLock(PP_Resource resource, bool report_error) | 163 EnterResourceNoLock(PP_Resource resource, bool report_error) |
240 : EnterResource<ResourceT, false>(resource, report_error) { | 164 : EnterResource<ResourceT, false>(resource, report_error) { |
241 } | 165 } |
242 }; | 166 }; |
243 | 167 |
244 // Simpler wrapper to enter the resource creation API. This is used for every | 168 // EnterInstance --------------------------------------------------------------- |
245 // class so we have this helper function to save template instantiations and | 169 |
246 // typing. | 170 class PPAPI_THUNK_EXPORT EnterInstance |
171 : public subtle::EnterBase, | |
172 public subtle::LockOnEntry<true> { | |
173 public: | |
174 EnterInstance(PP_Instance instance); | |
175 EnterInstance(PP_Instance instance, | |
176 const PP_CompletionCallback& callback); | |
177 ~EnterInstance(); | |
178 | |
179 bool succeeded() const { return !!functions_; } | |
180 bool failed() const { return !functions_; } | |
181 | |
182 PPB_Instance_API* functions() { return functions_; } | |
183 | |
184 private: | |
185 PPB_Instance_API* functions_; | |
186 }; | |
187 | |
188 class PPAPI_THUNK_EXPORT EnterInstanceNoLock | |
brettw
2012/04/21 19:50:01
I basically copied the code for the regular and "N
| |
189 : public subtle::EnterBase, | |
190 public subtle::LockOnEntry<false> { | |
191 public: | |
192 EnterInstanceNoLock(PP_Instance instance); | |
193 //EnterInstanceNoLock(PP_Instance instance, | |
194 // const PP_CompletionCallback& callback); | |
195 ~EnterInstanceNoLock(); | |
196 | |
197 bool succeeded() const { return !!functions_; } | |
198 bool failed() const { return !functions_; } | |
199 | |
200 PPB_Instance_API* functions() { return functions_; } | |
201 | |
202 private: | |
203 PPB_Instance_API* functions_; | |
204 }; | |
205 | |
206 // EnterResourceCreation ------------------------------------------------------- | |
207 | |
247 class PPAPI_THUNK_EXPORT EnterResourceCreation | 208 class PPAPI_THUNK_EXPORT EnterResourceCreation |
248 : public EnterFunction<ResourceCreationAPI> { | 209 : public subtle::EnterBase, |
210 public subtle::LockOnEntry<true> { | |
249 public: | 211 public: |
250 EnterResourceCreation(PP_Instance instance); | 212 EnterResourceCreation(PP_Instance instance); |
251 ~EnterResourceCreation(); | 213 ~EnterResourceCreation(); |
214 | |
215 bool succeeded() const { return !!functions_; } | |
216 bool failed() const { return !functions_; } | |
217 | |
218 ResourceCreationAPI* functions() { return functions_; } | |
219 | |
220 private: | |
221 ResourceCreationAPI* functions_; | |
252 }; | 222 }; |
253 | 223 |
254 // Simpler wrapper to enter the instance API from proxy code. This is used for | 224 class PPAPI_THUNK_EXPORT EnterResourceCreationNoLock |
255 // many interfaces so we have this helper function to save template | 225 : public subtle::EnterBase, |
256 // instantiations and typing. | 226 public subtle::LockOnEntry<false> { |
257 class PPAPI_THUNK_EXPORT EnterInstance | |
258 : public EnterFunction<PPB_Instance_FunctionAPI> { | |
259 public: | 227 public: |
260 EnterInstance(PP_Instance instance); | 228 EnterResourceCreationNoLock(PP_Instance instance); |
261 EnterInstance(PP_Instance instance, const PP_CompletionCallback& callback); | 229 ~EnterResourceCreationNoLock(); |
262 ~EnterInstance(); | |
263 }; | |
264 | 230 |
265 class PPAPI_THUNK_EXPORT EnterInstanceNoLock | 231 bool succeeded() const { return !!functions_; } |
266 : public EnterFunctionNoLock<PPB_Instance_FunctionAPI> { | 232 bool failed() const { return !functions_; } |
267 public: | 233 |
268 EnterInstanceNoLock(PP_Instance instance); | 234 ResourceCreationAPI* functions() { return functions_; } |
269 ~EnterInstanceNoLock(); | 235 |
236 private: | |
237 ResourceCreationAPI* functions_; | |
270 }; | 238 }; |
271 | 239 |
272 } // namespace thunk | 240 } // namespace thunk |
273 } // namespace ppapi | 241 } // namespace ppapi |
274 | 242 |
275 #endif // PPAPI_THUNK_ENTER_H_ | 243 #endif // PPAPI_THUNK_ENTER_H_ |
OLD | NEW |