| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 MOJO_SERVICES_MEDIA_FRAMEWORK_UTIL_CALLBACK_JOINER_H_ | 5 #ifndef MOJO_SERVICES_MEDIA_FRAMEWORK_UTIL_CALLBACK_JOINER_H_ |
| 6 #define MOJO_SERVICES_MEDIA_FRAMEWORK_UTIL_CALLBACK_JOINER_H_ | 6 #define MOJO_SERVICES_MEDIA_FRAMEWORK_UTIL_CALLBACK_JOINER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 66 |
| 67 // Indicates the initiation of a child operation. Every call to Spawn should | 67 // Indicates the initiation of a child operation. Every call to Spawn should |
| 68 // be matched by a subsequent call to Complete. | 68 // be matched by a subsequent call to Complete. |
| 69 void Spawn() { ++counter_; } | 69 void Spawn() { ++counter_; } |
| 70 | 70 |
| 71 // Indicates the completion of a child operation. | 71 // Indicates the completion of a child operation. |
| 72 void Complete() { | 72 void Complete() { |
| 73 DCHECK(counter_ != 0); | 73 DCHECK(counter_ != 0); |
| 74 --counter_; | 74 --counter_; |
| 75 if (counter_ == 0) { | 75 if (counter_ == 0) { |
| 76 mojo::Callback<void()> join_callback; | 76 Callback<void()> join_callback; |
| 77 join_callback = join_callback_; | 77 join_callback = join_callback_; |
| 78 join_callback_.reset(); | 78 join_callback_.reset(); |
| 79 join_callback.Run(); | 79 join_callback.Run(); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Calls Spawn and returns a new callback, which calls Complete. THIS METHOD | 83 // Calls Spawn and returns a new callback, which calls Complete. THIS METHOD |
| 84 // WILL ONLY WORK IF THERE IS ALREADY A SHARED POINTER TO THIS OBJECT. | 84 // WILL ONLY WORK IF THERE IS ALREADY A SHARED POINTER TO THIS OBJECT. |
| 85 std::function<void()> NewCallback() { | 85 std::function<void()> NewCallback() { |
| 86 Spawn(); | 86 Spawn(); |
| 87 std::shared_ptr<CallbackJoiner> this_ptr = shared_from_this(); | 87 std::shared_ptr<CallbackJoiner> this_ptr = shared_from_this(); |
| 88 DCHECK(!this_ptr.unique()); | 88 DCHECK(!this_ptr.unique()); |
| 89 return [this_ptr]() { | 89 return [this_ptr]() { |
| 90 DCHECK(this_ptr); | 90 DCHECK(this_ptr); |
| 91 this_ptr->Complete(); | 91 this_ptr->Complete(); |
| 92 }; | 92 }; |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Specifies a callback to be called when all child operations have completed. | 95 // Specifies a callback to be called when all child operations have completed. |
| 96 // If no child operations are currently pending, the callback is called | 96 // If no child operations are currently pending, the callback is called |
| 97 // immediately. If child operations are pending, the callback is copied. The | 97 // immediately. If child operations are pending, the callback is copied. The |
| 98 // copy called later (and reset) when all child oeprations have completed. | 98 // copy called later (and reset) when all child oeprations have completed. |
| 99 // Only one callback at a time can be registered with WhenJoined. | 99 // Only one callback at a time can be registered with WhenJoined. |
| 100 void WhenJoined(const mojo::Callback<void()>& join_callback) { | 100 void WhenJoined(const Callback<void()>& join_callback) { |
| 101 DCHECK(join_callback_.is_null()); | 101 DCHECK(join_callback_.is_null()); |
| 102 if (counter_ == 0) { | 102 if (counter_ == 0) { |
| 103 join_callback.Run(); | 103 join_callback.Run(); |
| 104 } else { | 104 } else { |
| 105 join_callback_ = join_callback; | 105 join_callback_ = join_callback; |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 // Cancels a callback registered with WhenJoined if it hasn't run yet. The | 109 // Cancels a callback registered with WhenJoined if it hasn't run yet. The |
| 110 // return value indicates whether a callback was cancelled. | 110 // return value indicates whether a callback was cancelled. |
| 111 bool Cancel() { | 111 bool Cancel() { |
| 112 bool result = !join_callback_.is_null(); | 112 bool result = !join_callback_.is_null(); |
| 113 join_callback_.reset(); | 113 join_callback_.reset(); |
| 114 return result; | 114 return result; |
| 115 } | 115 } |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 size_t counter_ = 0; | 118 size_t counter_ = 0; |
| 119 mojo::Callback<void()> join_callback_; | 119 Callback<void()> join_callback_; |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 } // namespace media | 122 } // namespace media |
| 123 } // namespace mojo | 123 } // namespace mojo |
| 124 | 124 |
| 125 #endif // MOJO_SERVICES_MEDIA_FRAMEWORK_UTIL_CALLBACK_JOINER_H_ | 125 #endif // MOJO_SERVICES_MEDIA_FRAMEWORK_UTIL_CALLBACK_JOINER_H_ |
| OLD | NEW |