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_PROXY_ENTER_PROXY_H_ | 5 #ifndef PPAPI_PROXY_ENTER_PROXY_H_ |
6 #define PPAPI_PROXY_ENTER_PROXY_H_ | 6 #define PPAPI_PROXY_ENTER_PROXY_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "ppapi/cpp/completion_callback.h" | 9 #include "ppapi/cpp/completion_callback.h" |
10 #include "ppapi/proxy/host_dispatcher.h" | 10 #include "ppapi/proxy/host_dispatcher.h" |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 void RunCallback(int32_t result) { | 159 void RunCallback(int32_t result) { |
160 DCHECK(needs_running_); | 160 DCHECK(needs_running_); |
161 needs_running_ = false; | 161 needs_running_ = false; |
162 callback_.Run(result); | 162 callback_.Run(result); |
163 } | 163 } |
164 | 164 |
165 bool needs_running_; | 165 bool needs_running_; |
166 pp::CompletionCallback callback_; | 166 pp::CompletionCallback callback_; |
167 }; | 167 }; |
168 | 168 |
| 169 // Like EnterHostFromHostResourceForceCallback but for Function APIs. It takes |
| 170 // an instance instead of a resource ID. |
| 171 template<typename FunctionT> |
| 172 class EnterHostFunctionForceCallback |
| 173 : public thunk::EnterFunctionNoLock<FunctionT> { |
| 174 public: |
| 175 // For callbacks that take no parameters except the "int32_t result". Most |
| 176 // implementations will use the 1-extra-argument constructor below. |
| 177 template<class CallbackFactory, typename Method> |
| 178 EnterHostFunctionForceCallback(PP_Instance instance, |
| 179 CallbackFactory& factory, |
| 180 Method method) |
| 181 : thunk::EnterFunctionNoLock<FunctionT>(instance, false), |
| 182 needs_running_(true), |
| 183 callback_(factory.NewOptionalCallback(method)) { |
| 184 if (this->failed()) |
| 185 RunCallback(PP_ERROR_BADARGUMENT); |
| 186 } |
| 187 |
| 188 // For callbacks that take an extra parameter as a closure. |
| 189 template<class CallbackFactory, typename Method, typename A> |
| 190 EnterHostFunctionForceCallback(PP_Instance instance, |
| 191 CallbackFactory& factory, |
| 192 Method method, |
| 193 const A& a) |
| 194 : thunk::EnterFunctionNoLock<FunctionT>(instance, false), |
| 195 needs_running_(true), |
| 196 callback_(factory.NewOptionalCallback(method, a)) { |
| 197 if (this->failed()) |
| 198 RunCallback(PP_ERROR_BADARGUMENT); |
| 199 } |
| 200 |
| 201 // For callbacks that take two extra parameters as a closure. |
| 202 template<class CallbackFactory, typename Method, typename A, typename B> |
| 203 EnterHostFunctionForceCallback(PP_Instance instance, |
| 204 CallbackFactory& factory, |
| 205 Method method, |
| 206 const A& a, |
| 207 const B& b) |
| 208 : thunk::EnterFunctionNoLock<FunctionT>(instance), |
| 209 needs_running_(true), |
| 210 callback_(factory.NewOptionalCallback(method, a, b)) { |
| 211 if (this->failed()) |
| 212 RunCallback(PP_ERROR_BADARGUMENT); |
| 213 } |
| 214 |
| 215 ~EnterHostFunctionForceCallback() { |
| 216 if (needs_running_) { |
| 217 NOTREACHED() << "Should always call SetResult except in the " |
| 218 "initialization failed case."; |
| 219 RunCallback(PP_ERROR_FAILED); |
| 220 } |
| 221 } |
| 222 |
| 223 void SetResult(int32_t result) { |
| 224 DCHECK(needs_running_) << "Don't call SetResult when there already is one."; |
| 225 needs_running_ = false; |
| 226 if (result != PP_OK_COMPLETIONPENDING) |
| 227 callback_.Run(result); |
| 228 } |
| 229 |
| 230 PP_CompletionCallback callback() { |
| 231 return callback_.pp_completion_callback(); |
| 232 } |
| 233 |
| 234 private: |
| 235 void RunCallback(int32_t result) { |
| 236 DCHECK(needs_running_); |
| 237 needs_running_ = false; |
| 238 callback_.Run(result); |
| 239 } |
| 240 |
| 241 bool needs_running_; |
| 242 pp::CompletionCallback callback_; |
| 243 }; |
| 244 |
169 } // namespace proxy | 245 } // namespace proxy |
170 } // namespace ppapi | 246 } // namespace ppapi |
171 | 247 |
172 #endif // PPAPI_PROXY_ENTER_PROXY_H_ | 248 #endif // PPAPI_PROXY_ENTER_PROXY_H_ |
OLD | NEW |