OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/cancelable_callback.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/logging.h" | |
10 | |
11 namespace base { | |
12 | |
13 CancelableCallback::CancelableCallback() | |
14 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
15 } | |
16 | |
17 CancelableCallback::CancelableCallback(const base::Closure& callback) | |
18 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
19 callback_(callback) { | |
20 DCHECK(!callback.is_null()); | |
21 InitializeForwarder(); | |
22 } | |
23 | |
24 CancelableCallback::~CancelableCallback() {} | |
25 | |
26 void CancelableCallback::Cancel() { | |
27 weak_factory_.InvalidateWeakPtrs(); | |
28 callback_.Reset(); | |
29 } | |
30 | |
31 bool CancelableCallback::IsCancelled() const { | |
32 return callback_.is_null(); | |
33 } | |
34 | |
35 void CancelableCallback::Reset(const base::Closure& callback) { | |
36 DCHECK(!callback.is_null()); | |
37 | |
38 // Outstanding tasks (e.g., posted to a message loop) must not be called. | |
39 Cancel(); | |
40 | |
41 // |forwarder_| is no longer valid after Cancel(), so re-bind. | |
42 InitializeForwarder(); | |
43 | |
44 callback_ = callback; | |
45 } | |
46 | |
47 const base::Closure& CancelableCallback::callback() const { | |
48 return forwarder_; | |
49 } | |
50 | |
51 void CancelableCallback::RunCallback() { | |
52 callback_.Run(); | |
53 } | |
54 | |
55 void CancelableCallback::InitializeForwarder() { | |
56 forwarder_ = base::Bind(&CancelableCallback::RunCallback, | |
57 weak_factory_.GetWeakPtr()); | |
58 } | |
59 | |
60 } // namespace bind | |
OLD | NEW |