// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef GOOGLE_APIS_DRIVE_TASK_UTIL_H_ #define GOOGLE_APIS_DRIVE_TASK_UTIL_H_ #include "base/bind.h" #include "base/thread_task_runner_handle.h" namespace google_apis { // Runs the task with the task runner. void RunTaskWithTaskRunner(scoped_refptr task_runner, const base::Closure& task); namespace internal { // Implementation of the composed callback, whose signature is |Sig|. template struct ComposedCallback; // ComposedCallback with no argument. template<> struct ComposedCallback { static void Run( const base::Callback& runner, const base::Closure& callback) { runner.Run(callback); } }; // ComposedCallback with one argument. template struct ComposedCallback { static void Run( const base::Callback& runner, const base::Callback& callback, T1 arg1) { runner.Run(base::Bind(callback, arg1)); } }; // ComposedCallback with two arguments. template struct ComposedCallback { static void Run( const base::Callback& runner, const base::Callback& callback, T1 arg1, T2 arg2) { runner.Run(base::Bind(callback, arg1, arg2)); } }; // ComposedCallback with two arguments, and the last one is scoped_ptr. template struct ComposedCallback)> { static void Run( const base::Callback& runner, const base::Callback)>& callback, T1 arg1, scoped_ptr arg2) { runner.Run(base::Bind(callback, arg1, base::Passed(&arg2))); } }; // ComposedCallback with three arguments. template struct ComposedCallback { static void Run( const base::Callback& runner, const base::Callback& callback, T1 arg1, T2 arg2, T3 arg3) { runner.Run(base::Bind(callback, arg1, arg2, arg3)); } }; // ComposedCallback with three arguments, and the last one is scoped_ptr. template struct ComposedCallback)> { static void Run( const base::Callback& runner, const base::Callback)>& callback, T1 arg1, T2 arg2, scoped_ptr arg3) { runner.Run(base::Bind(callback, arg1, arg2, base::Passed(&arg3))); } }; // ComposedCallback with four arguments. template struct ComposedCallback { static void Run( const base::Callback& runner, const base::Callback& callback, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { runner.Run(base::Bind(callback, arg1, arg2, arg3, arg4)); } }; } // namespace internal // Returns callback that takes arguments (arg1, arg2, ...), create a closure // by binding them to |callback|, and runs |runner| with the closure. // I.e. the returned callback works as follows: // runner.Run(Bind(callback, arg1, arg2, ...)) template CallbackType CreateComposedCallback( const base::Callback& runner, const CallbackType& callback) { DCHECK(!runner.is_null()); DCHECK(!callback.is_null()); return base::Bind( &internal::ComposedCallback::Run, runner, callback); } // Returns callback which runs the given |callback| on the current thread. template CallbackType CreateRelayCallback(const CallbackType& callback) { return CreateComposedCallback( base::Bind(&RunTaskWithTaskRunner, base::ThreadTaskRunnerHandle::Get()), callback); } } // namespace google_apis #endif // GOOGLE_APIS_DRIVE_TASK_UTIL_H_