| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 ~LockOnEntry() { | 65 ~LockOnEntry() { |
| 66 // You must not release the lock before leaving the scope of the | 66 // You must not release the lock before leaving the scope of the |
| 67 // Enter*NoLock. | 67 // Enter*NoLock. |
| 68 ProxyLock::AssertAcquired(); | 68 ProxyLock::AssertAcquired(); |
| 69 } | 69 } |
| 70 #endif | 70 #endif |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 template <> | 73 template <> |
| 74 struct LockOnEntry<true> { | 74 struct LockOnEntry<true> { |
| 75 LockOnEntry() { | 75 LockOnEntry() { ppapi::ProxyLock::Acquire(); } |
| 76 ppapi::ProxyLock::Acquire(); | 76 ~LockOnEntry() { ppapi::ProxyLock::Release(); } |
| 77 } | |
| 78 ~LockOnEntry() { | |
| 79 ppapi::ProxyLock::Release(); | |
| 80 } | |
| 81 }; | 77 }; |
| 82 | 78 |
| 83 // Keep non-templatized since we need non-inline functions here. | 79 // Keep non-templatized since we need non-inline functions here. |
| 84 class PPAPI_THUNK_EXPORT EnterBase { | 80 class PPAPI_THUNK_EXPORT EnterBase { |
| 85 public: | 81 public: |
| 86 EnterBase(); | 82 EnterBase(); |
| 87 explicit EnterBase(PP_Resource resource); | 83 explicit EnterBase(PP_Resource resource); |
| 88 EnterBase(PP_Instance instance, SingletonResourceID resource_id); | 84 EnterBase(PP_Instance instance, SingletonResourceID resource_id); |
| 89 EnterBase(PP_Resource resource, const PP_CompletionCallback& callback); | 85 EnterBase(PP_Resource resource, const PP_CompletionCallback& callback); |
| 90 EnterBase(PP_Instance instance, SingletonResourceID resource_id, | 86 EnterBase(PP_Instance instance, |
| 87 SingletonResourceID resource_id, |
| 91 const PP_CompletionCallback& callback); | 88 const PP_CompletionCallback& callback); |
| 92 virtual ~EnterBase(); | 89 virtual ~EnterBase(); |
| 93 | 90 |
| 94 // Sets the result for calls that use a completion callback. It handles making | 91 // Sets the result for calls that use a completion callback. It handles making |
| 95 // sure that "Required" callbacks are scheduled to run asynchronously and | 92 // sure that "Required" callbacks are scheduled to run asynchronously and |
| 96 // "Blocking" callbacks cause the caller to block. (Interface implementations, | 93 // "Blocking" callbacks cause the caller to block. (Interface implementations, |
| 97 // therefore, should not do any special casing based on the type of the | 94 // therefore, should not do any special casing based on the type of the |
| 98 // callback.) | 95 // callback.) |
| 99 // | 96 // |
| 100 // Returns the "retval()". This is to support the typical usage of | 97 // Returns the "retval()". This is to support the typical usage of |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 // will be NULL. | 159 // will be NULL. |
| 163 scoped_refptr<TrackedCallback> callback_; | 160 scoped_refptr<TrackedCallback> callback_; |
| 164 | 161 |
| 165 int32_t retval_; | 162 int32_t retval_; |
| 166 }; | 163 }; |
| 167 | 164 |
| 168 } // namespace subtle | 165 } // namespace subtle |
| 169 | 166 |
| 170 // EnterResource --------------------------------------------------------------- | 167 // EnterResource --------------------------------------------------------------- |
| 171 | 168 |
| 172 template<typename ResourceT, bool lock_on_entry = true> | 169 template <typename ResourceT, bool lock_on_entry = true> |
| 173 class EnterResource | 170 class EnterResource |
| 174 : public subtle::LockOnEntry<lock_on_entry>, // Must be first; see above. | 171 : public subtle::LockOnEntry<lock_on_entry>, // Must be first; see above. |
| 175 public subtle::EnterBase { | 172 public subtle::EnterBase { |
| 176 public: | 173 public: |
| 177 EnterResource(PP_Resource resource, bool report_error) | 174 EnterResource(PP_Resource resource, bool report_error) : EnterBase(resource) { |
| 178 : EnterBase(resource) { | |
| 179 Init(resource, report_error); | 175 Init(resource, report_error); |
| 180 } | 176 } |
| 181 EnterResource(PP_Resource resource, const PP_CompletionCallback& callback, | 177 EnterResource(PP_Resource resource, |
| 178 const PP_CompletionCallback& callback, |
| 182 bool report_error) | 179 bool report_error) |
| 183 : EnterBase(resource, callback) { | 180 : EnterBase(resource, callback) { |
| 184 Init(resource, report_error); | 181 Init(resource, report_error); |
| 185 } | 182 } |
| 186 ~EnterResource() {} | 183 ~EnterResource() {} |
| 187 | 184 |
| 188 ResourceT* object() { return object_; } | 185 ResourceT* object() { return object_; } |
| 189 Resource* resource() { return resource_; } | 186 Resource* resource() { return resource_; } |
| 190 | 187 |
| 191 private: | 188 private: |
| 192 void Init(PP_Resource resource, bool report_error) { | 189 void Init(PP_Resource resource, bool report_error) { |
| 193 if (resource_) | 190 if (resource_) |
| 194 object_ = resource_->GetAs<ResourceT>(); | 191 object_ = resource_->GetAs<ResourceT>(); |
| 195 else | 192 else |
| 196 object_ = NULL; | 193 object_ = NULL; |
| 197 // Validate the resource (note, if both are wrong, we will return | 194 // Validate the resource (note, if both are wrong, we will return |
| 198 // PP_ERROR_BADRESOURCE; last in wins). | 195 // PP_ERROR_BADRESOURCE; last in wins). |
| 199 SetStateForResourceError(resource, resource_, object_, report_error); | 196 SetStateForResourceError(resource, resource_, object_, report_error); |
| 200 } | 197 } |
| 201 | 198 |
| 202 ResourceT* object_; | 199 ResourceT* object_; |
| 203 | 200 |
| 204 DISALLOW_COPY_AND_ASSIGN(EnterResource); | 201 DISALLOW_COPY_AND_ASSIGN(EnterResource); |
| 205 }; | 202 }; |
| 206 | 203 |
| 207 // ---------------------------------------------------------------------------- | 204 // ---------------------------------------------------------------------------- |
| 208 | 205 |
| 209 // Like EnterResource but assumes the lock is already held. | 206 // Like EnterResource but assumes the lock is already held. |
| 210 template<typename ResourceT> | 207 template <typename ResourceT> |
| 211 class EnterResourceNoLock : public EnterResource<ResourceT, false> { | 208 class EnterResourceNoLock : public EnterResource<ResourceT, false> { |
| 212 public: | 209 public: |
| 213 EnterResourceNoLock(PP_Resource resource, bool report_error) | 210 EnterResourceNoLock(PP_Resource resource, bool report_error) |
| 214 : EnterResource<ResourceT, false>(resource, report_error) { | 211 : EnterResource<ResourceT, false>(resource, report_error) {} |
| 215 } | |
| 216 EnterResourceNoLock(PP_Resource resource, | 212 EnterResourceNoLock(PP_Resource resource, |
| 217 const PP_CompletionCallback& callback, | 213 const PP_CompletionCallback& callback, |
| 218 bool report_error) | 214 bool report_error) |
| 219 : EnterResource<ResourceT, false>(resource, callback, report_error) { | 215 : EnterResource<ResourceT, false>(resource, callback, report_error) {} |
| 220 } | |
| 221 }; | 216 }; |
| 222 | 217 |
| 223 // EnterInstance --------------------------------------------------------------- | 218 // EnterInstance --------------------------------------------------------------- |
| 224 | 219 |
| 225 class PPAPI_THUNK_EXPORT EnterInstance | 220 class PPAPI_THUNK_EXPORT EnterInstance |
| 226 : public subtle::LockOnEntry<true>, // Must be first; see above. | 221 : public subtle::LockOnEntry<true>, // Must be first; see above. |
| 227 public subtle::EnterBase { | 222 public subtle::EnterBase { |
| 228 public: | 223 public: |
| 229 explicit EnterInstance(PP_Instance instance); | 224 explicit EnterInstance(PP_Instance instance); |
| 230 EnterInstance(PP_Instance instance, | 225 EnterInstance(PP_Instance instance, const PP_CompletionCallback& callback); |
| 231 const PP_CompletionCallback& callback); | |
| 232 ~EnterInstance(); | 226 ~EnterInstance(); |
| 233 | 227 |
| 234 bool succeeded() const { return !!functions_; } | 228 bool succeeded() const { return !!functions_; } |
| 235 bool failed() const { return !functions_; } | 229 bool failed() const { return !functions_; } |
| 236 | 230 |
| 237 PPB_Instance_API* functions() const { return functions_; } | 231 PPB_Instance_API* functions() const { return functions_; } |
| 238 | 232 |
| 239 private: | 233 private: |
| 240 PPB_Instance_API* functions_; | 234 PPB_Instance_API* functions_; |
| 241 }; | 235 }; |
| 242 | 236 |
| 243 class PPAPI_THUNK_EXPORT EnterInstanceNoLock | 237 class PPAPI_THUNK_EXPORT EnterInstanceNoLock |
| 244 : public subtle::LockOnEntry<false>, // Must be first; see above. | 238 : public subtle::LockOnEntry<false>, // Must be first; see above. |
| 245 public subtle::EnterBase { | 239 public subtle::EnterBase { |
| 246 public: | 240 public: |
| 247 explicit EnterInstanceNoLock(PP_Instance instance); | 241 explicit EnterInstanceNoLock(PP_Instance instance); |
| 248 EnterInstanceNoLock(PP_Instance instance, | 242 EnterInstanceNoLock(PP_Instance instance, |
| 249 const PP_CompletionCallback& callback); | 243 const PP_CompletionCallback& callback); |
| 250 ~EnterInstanceNoLock(); | 244 ~EnterInstanceNoLock(); |
| 251 | 245 |
| 252 PPB_Instance_API* functions() { return functions_; } | 246 PPB_Instance_API* functions() { return functions_; } |
| 253 | 247 |
| 254 private: | 248 private: |
| 255 PPB_Instance_API* functions_; | 249 PPB_Instance_API* functions_; |
| 256 }; | 250 }; |
| 257 | 251 |
| 258 // EnterInstanceAPI ------------------------------------------------------------ | 252 // EnterInstanceAPI ------------------------------------------------------------ |
| 259 | 253 |
| 260 template<typename ApiT, bool lock_on_entry = true> | 254 template <typename ApiT, bool lock_on_entry = true> |
| 261 class EnterInstanceAPI | 255 class EnterInstanceAPI |
| 262 : public subtle::LockOnEntry<lock_on_entry>, // Must be first; see above | 256 : public subtle::LockOnEntry<lock_on_entry>, // Must be first; see above |
| 263 public subtle::EnterBase { | 257 public subtle::EnterBase { |
| 264 public: | 258 public: |
| 265 explicit EnterInstanceAPI(PP_Instance instance) | 259 explicit EnterInstanceAPI(PP_Instance instance) |
| 266 : EnterBase(instance, ApiT::kSingletonResourceID), | 260 : EnterBase(instance, ApiT::kSingletonResourceID), functions_(NULL) { |
| 267 functions_(NULL) { | |
| 268 if (resource_) | 261 if (resource_) |
| 269 functions_ = resource_->GetAs<ApiT>(); | 262 functions_ = resource_->GetAs<ApiT>(); |
| 270 SetStateForFunctionError(instance, functions_, true); | 263 SetStateForFunctionError(instance, functions_, true); |
| 271 } | 264 } |
| 272 EnterInstanceAPI(PP_Instance instance, | 265 EnterInstanceAPI(PP_Instance instance, const PP_CompletionCallback& callback) |
| 273 const PP_CompletionCallback& callback) | |
| 274 : EnterBase(instance, ApiT::kSingletonResourceID, callback), | 266 : EnterBase(instance, ApiT::kSingletonResourceID, callback), |
| 275 functions_(NULL) { | 267 functions_(NULL) { |
| 276 if (resource_) | 268 if (resource_) |
| 277 functions_ = resource_->GetAs<ApiT>(); | 269 functions_ = resource_->GetAs<ApiT>(); |
| 278 SetStateForFunctionError(instance, functions_, true); | 270 SetStateForFunctionError(instance, functions_, true); |
| 279 } | 271 } |
| 280 ~EnterInstanceAPI() {} | 272 ~EnterInstanceAPI() {} |
| 281 | 273 |
| 282 bool succeeded() const { return !!functions_; } | 274 bool succeeded() const { return !!functions_; } |
| 283 bool failed() const { return !functions_; } | 275 bool failed() const { return !functions_; } |
| 284 | 276 |
| 285 ApiT* functions() const { return functions_; } | 277 ApiT* functions() const { return functions_; } |
| 286 | 278 |
| 287 private: | 279 private: |
| 288 ApiT* functions_; | 280 ApiT* functions_; |
| 289 }; | 281 }; |
| 290 | 282 |
| 291 template<typename ApiT> | 283 template <typename ApiT> |
| 292 class EnterInstanceAPINoLock : public EnterInstanceAPI<ApiT, false> { | 284 class EnterInstanceAPINoLock : public EnterInstanceAPI<ApiT, false> { |
| 293 public: | 285 public: |
| 294 explicit EnterInstanceAPINoLock(PP_Instance instance) | 286 explicit EnterInstanceAPINoLock(PP_Instance instance) |
| 295 : EnterInstanceAPI<ApiT, false>(instance) { | 287 : EnterInstanceAPI<ApiT, false>(instance) {} |
| 296 } | |
| 297 }; | 288 }; |
| 298 | 289 |
| 299 // EnterResourceCreation ------------------------------------------------------- | 290 // EnterResourceCreation ------------------------------------------------------- |
| 300 | 291 |
| 301 class PPAPI_THUNK_EXPORT EnterResourceCreation | 292 class PPAPI_THUNK_EXPORT EnterResourceCreation |
| 302 : public subtle::LockOnEntry<true>, // Must be first; see above. | 293 : public subtle::LockOnEntry<true>, // Must be first; see above. |
| 303 public subtle::EnterBase { | 294 public subtle::EnterBase { |
| 304 public: | 295 public: |
| 305 explicit EnterResourceCreation(PP_Instance instance); | 296 explicit EnterResourceCreation(PP_Instance instance); |
| 306 ~EnterResourceCreation(); | 297 ~EnterResourceCreation(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 321 ResourceCreationAPI* functions() { return functions_; } | 312 ResourceCreationAPI* functions() { return functions_; } |
| 322 | 313 |
| 323 private: | 314 private: |
| 324 ResourceCreationAPI* functions_; | 315 ResourceCreationAPI* functions_; |
| 325 }; | 316 }; |
| 326 | 317 |
| 327 } // namespace thunk | 318 } // namespace thunk |
| 328 } // namespace ppapi | 319 } // namespace ppapi |
| 329 | 320 |
| 330 #endif // PPAPI_THUNK_ENTER_H_ | 321 #endif // PPAPI_THUNK_ENTER_H_ |
| OLD | NEW |