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 #include "ppapi/thunk/enter.h" | 5 #include "ppapi/thunk/enter.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "ppapi/c/pp_errors.h" |
8 #include "ppapi/thunk/ppb_instance_api.h" | 11 #include "ppapi/thunk/ppb_instance_api.h" |
9 #include "ppapi/thunk/resource_creation_api.h" | 12 #include "ppapi/thunk/resource_creation_api.h" |
10 | 13 |
11 namespace ppapi { | 14 namespace ppapi { |
12 namespace thunk { | 15 namespace thunk { |
13 | 16 |
| 17 namespace subtle { |
| 18 |
| 19 bool CallbackIsRequired(const PP_CompletionCallback& callback) { |
| 20 return callback.func != NULL && |
| 21 (callback.flags & PP_COMPLETIONCALLBACK_FLAG_OPTIONAL) == 0; |
| 22 } |
| 23 |
| 24 EnterBase::EnterBase() |
| 25 : callback_(PP_BlockUntilComplete()), |
| 26 retval_(PP_OK) { |
| 27 // TODO(brettw) validate threads. |
| 28 } |
| 29 |
| 30 EnterBase::EnterBase(const PP_CompletionCallback& callback) |
| 31 : callback_(CallbackIsRequired(callback) ? callback |
| 32 : PP_BlockUntilComplete()), |
| 33 retval_(PP_OK) { |
| 34 // TODO(brettw) validate threads. |
| 35 } |
| 36 |
| 37 EnterBase::~EnterBase() { |
| 38 if (callback_.func) { |
| 39 // All async completions should have cleared the callback in SetResult(). |
| 40 DCHECK(retval_ != PP_OK_COMPLETIONPENDING && retval_ != PP_OK); |
| 41 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 42 callback_.func, callback_.user_data, retval_)); |
| 43 } |
| 44 } |
| 45 |
| 46 int32_t EnterBase::SetResult(int32_t result) { |
| 47 if (!callback_.func || result == PP_OK_COMPLETIONPENDING) { |
| 48 // Easy case, we don't need to issue the callback (either none is |
| 49 // required, or the implementation will asynchronously issue it |
| 50 // for us), just store the result. |
| 51 callback_ = PP_BlockUntilComplete(); |
| 52 retval_ = result; |
| 53 return retval_; |
| 54 } |
| 55 |
| 56 // This is a required callback, asynchronously issue it. |
| 57 // TODO(brettw) make this work on different threads, etc. |
| 58 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 59 callback_.func, callback_.user_data, result)); |
| 60 |
| 61 // Now that the callback will be issued in the future, we should return |
| 62 // "pending" to the caller, and not issue the callback again. |
| 63 callback_ = PP_BlockUntilComplete(); |
| 64 retval_ = PP_OK_COMPLETIONPENDING; |
| 65 return retval_; |
| 66 } |
| 67 |
| 68 FunctionGroupBase* EnterBase::GetFunctions(PP_Instance instance, |
| 69 ApiID id) const { |
| 70 return PpapiGlobals::Get()->GetFunctionAPI(instance, id); |
| 71 } |
| 72 |
| 73 Resource* EnterBase::GetResource(PP_Resource resource) const { |
| 74 return PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource); |
| 75 } |
| 76 |
| 77 void EnterBase::SetStateForResourceError(PP_Resource /* pp_resource */, |
| 78 Resource* /* resource_base */, |
| 79 void* object, |
| 80 bool /* report_error */) { |
| 81 if (object) |
| 82 return; // Everything worked. |
| 83 |
| 84 retval_ = PP_ERROR_BADRESOURCE; |
| 85 // TODO(brettw) log the error. |
| 86 } |
| 87 |
| 88 } // namespace subtle |
| 89 |
14 EnterResourceCreation::EnterResourceCreation(PP_Instance instance) | 90 EnterResourceCreation::EnterResourceCreation(PP_Instance instance) |
15 : EnterFunctionNoLock<ResourceCreationAPI>(instance, true) { | 91 : EnterFunctionNoLock<ResourceCreationAPI>(instance, true) { |
16 } | 92 } |
17 | 93 |
18 EnterResourceCreation::~EnterResourceCreation() { | 94 EnterResourceCreation::~EnterResourceCreation() { |
19 } | 95 } |
20 | 96 |
21 EnterInstance::EnterInstance(PP_Instance instance) | 97 EnterInstance::EnterInstance(PP_Instance instance) |
22 : EnterFunctionNoLock<PPB_Instance_FunctionAPI>(instance, true) { | 98 : EnterFunctionNoLock<PPB_Instance_FunctionAPI>(instance, true) { |
23 } | 99 } |
24 | 100 |
25 EnterInstance::~EnterInstance() { | 101 EnterInstance::~EnterInstance() { |
26 } | 102 } |
27 | 103 |
28 } // namespace thunk | 104 } // namespace thunk |
29 } // namespace ppapi | 105 } // namespace ppapi |
OLD | NEW |