Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / google_apis / drive / task_util.h
blobabc0d8f3b89775c5acec79baff550f82b4a27dac
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 GOOGLE_APIS_DRIVE_TASK_UTIL_H_
6 #define GOOGLE_APIS_DRIVE_TASK_UTIL_H_
8 #include "base/bind.h"
9 #include "base/thread_task_runner_handle.h"
11 namespace google_apis {
13 // Runs the task with the task runner.
14 void RunTaskWithTaskRunner(scoped_refptr<base::TaskRunner> task_runner,
15 const base::Closure& task);
17 namespace internal {
19 // Implementation of the composed callback, whose signature is |Sig|.
20 template<typename Sig> struct ComposedCallback;
22 // ComposedCallback with no argument.
23 template<>
24 struct ComposedCallback<void()> {
25 static void Run(
26 const base::Callback<void(const base::Closure&)>& runner,
27 const base::Closure& callback) {
28 runner.Run(callback);
32 // ComposedCallback with one argument.
33 template<typename T1>
34 struct ComposedCallback<void(T1)> {
35 static void Run(
36 const base::Callback<void(const base::Closure&)>& runner,
37 const base::Callback<void(T1)>& callback,
38 T1 arg1) {
39 runner.Run(base::Bind(callback, arg1));
43 // ComposedCallback with two arguments.
44 template<typename T1, typename T2>
45 struct ComposedCallback<void(T1, T2)> {
46 static void Run(
47 const base::Callback<void(const base::Closure&)>& runner,
48 const base::Callback<void(T1, T2)>& callback,
49 T1 arg1, T2 arg2) {
50 runner.Run(base::Bind(callback, arg1, arg2));
54 // ComposedCallback with two arguments, and the last one is scoped_ptr.
55 template<typename T1, typename T2, typename D2>
56 struct ComposedCallback<void(T1, scoped_ptr<T2, D2>)> {
57 static void Run(
58 const base::Callback<void(const base::Closure&)>& runner,
59 const base::Callback<void(T1, scoped_ptr<T2, D2>)>& callback,
60 T1 arg1, scoped_ptr<T2, D2> arg2) {
61 runner.Run(base::Bind(callback, arg1, base::Passed(&arg2)));
65 // ComposedCallback with three arguments.
66 template<typename T1, typename T2, typename T3>
67 struct ComposedCallback<void(T1, T2, T3)> {
68 static void Run(
69 const base::Callback<void(const base::Closure&)>& runner,
70 const base::Callback<void(T1, T2, T3)>& callback,
71 T1 arg1, T2 arg2, T3 arg3) {
72 runner.Run(base::Bind(callback, arg1, arg2, arg3));
76 // ComposedCallback with three arguments, and the last one is scoped_ptr.
77 template<typename T1, typename T2, typename T3, typename D3>
78 struct ComposedCallback<void(T1, T2, scoped_ptr<T3, D3>)> {
79 static void Run(
80 const base::Callback<void(const base::Closure&)>& runner,
81 const base::Callback<void(T1, T2, scoped_ptr<T3, D3>)>& callback,
82 T1 arg1, T2 arg2, scoped_ptr<T3, D3> arg3) {
83 runner.Run(base::Bind(callback, arg1, arg2, base::Passed(&arg3)));
87 // ComposedCallback with four arguments.
88 template<typename T1, typename T2, typename T3, typename T4>
89 struct ComposedCallback<void(T1, T2, T3, T4)> {
90 static void Run(
91 const base::Callback<void(const base::Closure&)>& runner,
92 const base::Callback<void(T1, T2, T3, T4)>& callback,
93 T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
94 runner.Run(base::Bind(callback, arg1, arg2, arg3, arg4));
98 } // namespace internal
100 // Returns callback that takes arguments (arg1, arg2, ...), create a closure
101 // by binding them to |callback|, and runs |runner| with the closure.
102 // I.e. the returned callback works as follows:
103 // runner.Run(Bind(callback, arg1, arg2, ...))
104 template<typename CallbackType>
105 CallbackType CreateComposedCallback(
106 const base::Callback<void(const base::Closure&)>& runner,
107 const CallbackType& callback) {
108 DCHECK(!runner.is_null());
109 DCHECK(!callback.is_null());
110 return base::Bind(
111 &internal::ComposedCallback<typename CallbackType::RunType>::Run,
112 runner, callback);
115 // Returns callback which runs the given |callback| on the current thread.
116 template<typename CallbackType>
117 CallbackType CreateRelayCallback(const CallbackType& callback) {
118 return CreateComposedCallback(
119 base::Bind(&RunTaskWithTaskRunner, base::ThreadTaskRunnerHandle::Get()),
120 callback);
123 } // namespace google_apis
125 #endif // GOOGLE_APIS_DRIVE_TASK_UTIL_H_