Class base::Bind and base::Callback in chrome are just like bind/function in boost, which are used to bind and store functions.

Binding a bare function

int Return5() { return 5; }
base::Callback<int(void)> func_cb = base::Bind(&Return5);
func_cb.Run();

Binding a member function

The first argument to bind is the member function to call, the second is the object on which to call it.

class Ref : public base::RefCountedThreadSafe<Ref> {
public:
    int Foo() { return 3; }
    void PrintBye() { LOG(INFO) << "bye."; }
};

Passing parameters and run

// unbound parameters
void MyFunc(int i, const std::string& str) {}
base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc);
cb.Run(23, "hello, world");

// Closure
void MyFunc(int i, const std::string& str) {}
base::Closure cb = base::Bind(&MyFunc, 23, "hello world");
cb.Run();

Advanced binding usage

As base::Bind can bind base::Callback, so nested base::Bind is available. For example:

#include "base/memory/ref_counted.h"
#include "base/bind.h"
#include "base/callback.h"

class MyClass : public base::RefCounted<MyClass>
{
public:
    void foo( int n, std::string str ) {
        // 5 "hello world"
    }
};

void test(){
    scoped_refptr<MyClass> cls = make_scoped_refptr(new MyClass);
    
    base::Callback<void(int, std::string)> c1 = base::Bind( &MyClass::foo, cls );
    // base::Callback<void(std::string)> c2 = base::Bind( c1, 5 );
    base::Closure c3 = base::Bind( base::Bind( c1, 5 ), "hello world" );
    c3.Run();
}
base::Bind(&MyClass::Foo, base::Unretained(this));

This disables all lifetime management on the object. You’re responsible for making sure the object is alive at the time of the call. You break it, you own it!

MyClass* myclass = new MyClass;
base::Bind(&MyClass::Foo, base::Owned(myclass));

The object will be deleted when the callback is destroyed, even if it’s not run (like if you post a task during shutdown). Potentially useful for “fire and forget” cases.

int DoSomething(int arg) { cout << arg << endl; }
base::Callback<void<int>) cb =
   base::Bind(base::IgnoreResult(&DoSomething));

Ownership of the parameter will be with the callback until the it is run, when ownership is passed to the callback function. This means the callback can only be run once. If the callback is never run, it will delete the object when it’s destroyed.

void TakesOwnership(scoped_ptr<Foo> arg) {}
scoped_ptr<Foo> f(new Foo);
// f becomes null during the following call.
base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f));
void TakesOneRef(scoped_refptr<Foo> arg) {}
scoped_refptr<Foo> f(new Foo)
base::Closure cb = base::Bind(&TakesOneRef, f);

The closure will take a reference as long as it is alive, and another reference will be taken for the called function.

template<typename Func>
inline base::Closure LambdaClosure( Func const& func )
{
   struct Inner : public base::RefCountedThreadSafe<Inner>
   {
      Inner( Func const& func ) : func_(func) {}
      void Invoke() { func_(); }
      Func func_;
   };

   scoped_refptr<Inner> spInner( new Inner(func) );
   return base::Bind( &Inner::Invoke, spInner );
}

// usage:
base::Closure cb = LambdaClosure( [](){
    // ...
} );
void foo(int arg) { cout << arg << endl }
int n = 1;
base::Closure has_ref = base::Bind(&foo, base::ConstRef(n));
n = 2;
has_ref.Run();  // Prints "2"

Normally parameters are copied in the closure. DANGER: ConstRef stores a const reference instead, referencing the original parameter. This means that you must ensure the object outlives the callback!

base::Bind(&MyClass::Foo, GetWeakPtr());

The callback will not be issued if the object is destroyed at the time it’s issued. DANGER: weak pointers are not threadsafe, so don’t use this when passing between threads!