Remove --enable-hidpi-pdf-plugin flags, enable by default
[chromium-blink-merge.git] / base / task_runner_util.h
blobd1b9f62fcd6b25abf788324e118979421c738e03
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
3 // found in the LICENSE file.
5 #ifndef BASE_TASK_RUNNER_UTIL_H_
6 #define BASE_TASK_RUNNER_UTIL_H_
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/callback_internal.h"
11 #include "base/logging.h"
12 #include "base/task_runner.h"
14 namespace base {
16 namespace internal {
18 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
19 template <typename ReturnType>
20 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func,
21 ReturnType* result) {
22 if (!func.is_null())
23 *result = func.Run();
26 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
27 template <typename ReturnType>
28 Closure ReturnAsParam(const Callback<ReturnType(void)>& func,
29 ReturnType* result) {
30 DCHECK(result);
31 return Bind(&ReturnAsParamAdapter<ReturnType>, func, result);
34 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
35 template <typename ReturnType>
36 void ReplyAdapter(const Callback<void(ReturnType)>& callback,
37 ReturnType* result) {
38 DCHECK(result);
39 if(!callback.is_null())
40 callback.Run(CallbackForward(*result));
43 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
44 template <typename ReturnType, typename OwnedType>
45 Closure ReplyHelper(const Callback<void(ReturnType)>& callback,
46 OwnedType result) {
47 return Bind(&ReplyAdapter<ReturnType>, callback, result);
50 } // namespace internal
52 // When you have these methods
54 // R DoWorkAndReturn();
55 // void Callback(const R& result);
57 // and want to call them in a PostTaskAndReply kind of fashion where the
58 // result of DoWorkAndReturn is passed to the Callback, you can use
59 // PostTaskAndReplyWithResult as in this example:
61 // PostTaskAndReplyWithResult(
62 // target_thread_.message_loop_proxy(),
63 // FROM_HERE,
64 // Bind(&DoWorkAndReturn),
65 // Bind(&Callback));
66 template <typename ReturnType>
67 bool PostTaskAndReplyWithResult(
68 TaskRunner* task_runner,
69 const tracked_objects::Location& from_here,
70 const Callback<ReturnType(void)>& task,
71 const Callback<void(ReturnType)>& reply) {
72 ReturnType* result = new ReturnType;
73 return task_runner->PostTaskAndReply(
74 from_here,
75 internal::ReturnAsParam<ReturnType>(task, result),
76 internal::ReplyHelper(reply, Owned(result)));
79 } // namespace base
81 #endif // BASE_TASK_RUNNER_UTIL_H_