| OLD | NEW | 
|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 // | 4 // | 
| 5 // CancelableCallback is a wrapper around base::Callback that allows | 5 // CancelableCallback is a wrapper around base::Callback that allows | 
| 6 // cancellation of a callback. CancelableCallback takes a reference on the | 6 // cancellation of a callback. CancelableCallback takes a reference on the | 
| 7 // wrapped callback until this object is destroyed or Reset()/Cancel() are | 7 // wrapped callback until this object is destroyed or Reset()/Cancel() are | 
| 8 // called. | 8 // called. | 
| 9 // | 9 // | 
| 10 // NOTE: | 10 // NOTE: | 
| 11 // | 11 // | 
| 12 // Calling CancelableCallback::Cancel() brings the object back to its natural, | 12 // Calling CancelableCallback::Cancel() brings the object back to its natural, | 
| 13 // default-constructed state, i.e., CancelableCallback::callback() will return | 13 // default-constructed state, i.e., CancelableCallback::callback() will return | 
| 14 // a null callback. | 14 // a null callback. | 
| 15 // | 15 // | 
| 16 // THREAD-SAFETY: | 16 // THREAD-SAFETY: | 
| 17 // | 17 // | 
| 18 // CancelableCallback objects must be created on, posted to, cancelled on, and | 18 // CancelableCallback objects must be created on, posted to, cancelled on, and | 
| 19 // destroyed on the same thread. | 19 // destroyed on the same thread. | 
| 20 // | 20 // | 
| 21 // | 21 // | 
| 22 // EXAMPLE USAGE: | 22 // EXAMPLE USAGE: | 
| 23 // | 23 // | 
| 24 // In the following example, the test is verifying that RunIntensiveTest() | 24 // In the following example, the test is verifying that RunIntensiveTest() | 
| 25 // Quit()s the message loop within 4 seconds. The cancelable callback is posted | 25 // Quit()s the message loop within 4 seconds. The cancelable callback is posted | 
| 26 // to the message loop, the intensive test runs, the message loop is run, | 26 // to the message loop, the intensive test runs, the message loop is run, | 
| 27 // then the callback is cancelled. | 27 // then the callback is cancelled. | 
| 28 // | 28 // | 
|  | 29 // RunLoop run_loop; | 
|  | 30 // | 
| 29 // void TimeoutCallback(const std::string& timeout_message) { | 31 // void TimeoutCallback(const std::string& timeout_message) { | 
| 30 //   FAIL() << timeout_message; | 32 //   FAIL() << timeout_message; | 
| 31 //   MessageLoop::current()->QuitWhenIdle(); | 33 //   run_loop.QuitWhenIdle(); | 
| 32 // } | 34 // } | 
| 33 // | 35 // | 
| 34 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out.")); | 36 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out.")); | 
| 35 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(), | 37 // ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, timeout.callback(), | 
| 36 //                                         4000)  // 4 seconds to run. | 38 //                                                TimeDelta::FromSeconds(4)); | 
| 37 // RunIntensiveTest(); | 39 // RunIntensiveTest(); | 
| 38 // MessageLoop::current()->Run(); | 40 // run_loop.Run(); | 
| 39 // timeout.Cancel();  // Hopefully this is hit before the timeout callback runs. | 41 // timeout.Cancel();  // Hopefully this is hit before the timeout callback runs. | 
| 40 // | 42 // | 
| 41 | 43 | 
| 42 #ifndef BASE_CANCELABLE_CALLBACK_H_ | 44 #ifndef BASE_CANCELABLE_CALLBACK_H_ | 
| 43 #define BASE_CANCELABLE_CALLBACK_H_ | 45 #define BASE_CANCELABLE_CALLBACK_H_ | 
| 44 | 46 | 
| 45 #include <utility> | 47 #include <utility> | 
| 46 | 48 | 
| 47 #include "base/base_export.h" | 49 #include "base/base_export.h" | 
| 48 #include "base/bind.h" | 50 #include "base/bind.h" | 
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 125   base::WeakPtrFactory<CancelableCallback<void(A...)>> weak_factory_; | 127   base::WeakPtrFactory<CancelableCallback<void(A...)>> weak_factory_; | 
| 126 | 128 | 
| 127   DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 129   DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 
| 128 }; | 130 }; | 
| 129 | 131 | 
| 130 typedef CancelableCallback<void(void)> CancelableClosure; | 132 typedef CancelableCallback<void(void)> CancelableClosure; | 
| 131 | 133 | 
| 132 }  // namespace base | 134 }  // namespace base | 
| 133 | 135 | 
| 134 #endif  // BASE_CANCELABLE_CALLBACK_H_ | 136 #endif  // BASE_CANCELABLE_CALLBACK_H_ | 
| OLD | NEW | 
|---|