| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef MEDIA_BASE_CALLBACK_HOLDER_H_ | 5 #ifndef MEDIA_BASE_CALLBACK_HOLDER_H_ | 
| 6 #define MEDIA_BASE_CALLBACK_HOLDER_H_ | 6 #define MEDIA_BASE_CALLBACK_HOLDER_H_ | 
| 7 | 7 | 
| 8 #include "base/bind.h" | 8 #include "base/bind.h" | 
|  | 9 #include "base/bind_to_current_loop.h" | 
| 9 #include "base/callback.h" | 10 #include "base/callback.h" | 
| 10 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" | 
| 11 #include "media/base/bind_to_current_loop.h" |  | 
| 12 | 12 | 
| 13 namespace media { | 13 namespace media { | 
| 14 | 14 | 
| 15 // A helper class that can hold a callback from being fired. | 15 // A helper class that can hold a callback from being fired. | 
| 16 template <typename CB> class CallbackHolder { | 16 template <typename CB> class CallbackHolder { | 
| 17  public: | 17  public: | 
| 18   CallbackHolder() : hold_(false) {} | 18   CallbackHolder() : hold_(false) {} | 
| 19 | 19 | 
| 20   ~CallbackHolder() { | 20   ~CallbackHolder() { | 
| 21     // Make sure all callbacks are satisfied! | 21     // Make sure all callbacks are satisfied! | 
| (...skipping 23 matching lines...) Expand all  Loading... | 
| 45     if (hold_) | 45     if (hold_) | 
| 46       held_cb_ = base::ResetAndReturn(&original_cb_); | 46       held_cb_ = base::ResetAndReturn(&original_cb_); | 
| 47     else | 47     else | 
| 48       base::ResetAndReturn(&original_cb_).Run(); | 48       base::ResetAndReturn(&original_cb_).Run(); | 
| 49   } | 49   } | 
| 50 | 50 | 
| 51   template <typename A1> void RunOrHold(A1 a1) { | 51   template <typename A1> void RunOrHold(A1 a1) { | 
| 52     DCHECK(held_cb_.is_null()); | 52     DCHECK(held_cb_.is_null()); | 
| 53     if (hold_) { | 53     if (hold_) { | 
| 54       held_cb_ = base::Bind(base::ResetAndReturn(&original_cb_), | 54       held_cb_ = base::Bind(base::ResetAndReturn(&original_cb_), | 
| 55                             internal::TrampolineForward(a1)); | 55                             base::internal::TrampolineForward(a1)); | 
| 56     } else { | 56     } else { | 
| 57       base::ResetAndReturn(&original_cb_).Run(a1); | 57       base::ResetAndReturn(&original_cb_).Run(a1); | 
| 58     } | 58     } | 
| 59   } | 59   } | 
| 60 | 60 | 
| 61   template <typename A1, typename A2> void RunOrHold(A1 a1, A2 a2) { | 61   template <typename A1, typename A2> void RunOrHold(A1 a1, A2 a2) { | 
| 62     DCHECK(held_cb_.is_null()); | 62     DCHECK(held_cb_.is_null()); | 
| 63     if (hold_) { | 63     if (hold_) { | 
| 64       held_cb_ = base::Bind(base::ResetAndReturn(&original_cb_), | 64       held_cb_ = base::Bind(base::ResetAndReturn(&original_cb_), | 
| 65                             internal::TrampolineForward(a1), | 65                             base::internal::TrampolineForward(a1), | 
| 66                             internal::TrampolineForward(a2)); | 66                             base::internal::TrampolineForward(a2)); | 
| 67     } else { | 67     } else { | 
| 68       base::ResetAndReturn(&original_cb_).Run(a1, a2); | 68       base::ResetAndReturn(&original_cb_).Run(a1, a2); | 
| 69     } | 69     } | 
| 70   } | 70   } | 
| 71 | 71 | 
| 72   // Releases and runs the held callback. | 72   // Releases and runs the held callback. | 
| 73   void RunHeldCallback() { | 73   void RunHeldCallback() { | 
| 74     DCHECK(hold_); | 74     DCHECK(hold_); | 
| 75     DCHECK(!held_cb_.is_null()); | 75     DCHECK(!held_cb_.is_null()); | 
| 76     hold_ = false; | 76     hold_ = false; | 
| 77     base::ResetAndReturn(&held_cb_).Run(); | 77     base::ResetAndReturn(&held_cb_).Run(); | 
| 78   } | 78   } | 
| 79 | 79 | 
| 80  private: | 80  private: | 
| 81   bool hold_; | 81   bool hold_; | 
| 82   CB original_cb_; | 82   CB original_cb_; | 
| 83   base::Closure held_cb_; | 83   base::Closure held_cb_; | 
| 84 }; | 84 }; | 
| 85 | 85 | 
| 86 }  // namespace media | 86 }  // namespace media | 
| 87 | 87 | 
| 88 #endif  // MEDIA_BASE_CALLBACK_HOLDER_H_ | 88 #endif  // MEDIA_BASE_CALLBACK_HOLDER_H_ | 
| OLD | NEW | 
|---|