| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ | |
| 6 #define MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/callback_helpers.h" | |
| 10 #include "base/macros.h" | |
| 11 | |
| 12 namespace media { | |
| 13 | |
| 14 // This class guarantees that |callback_| has either been called or will pass it | |
| 15 // to |on_error_callback_| on destruction. Inspired by ScopedWebCallbacks<>. | |
| 16 template <typename CallbackType> | |
| 17 class ScopedResultCallback { | |
| 18 public: | |
| 19 using OnErrorCallback = base::Callback<void(const CallbackType&)>; | |
| 20 ScopedResultCallback(const CallbackType& callback, | |
| 21 const OnErrorCallback& on_error_callback) | |
| 22 : callback_(callback), on_error_callback_(on_error_callback) {} | |
| 23 | |
| 24 ~ScopedResultCallback() { | |
| 25 if (!callback_.is_null()) | |
| 26 on_error_callback_.Run(callback_); | |
| 27 } | |
| 28 | |
| 29 ScopedResultCallback(ScopedResultCallback&& other) { | |
| 30 *this = std::move(other); | |
| 31 } | |
| 32 | |
| 33 ScopedResultCallback& operator=(ScopedResultCallback&& other) { | |
| 34 callback_ = other.callback_; | |
| 35 other.callback_.Reset(); | |
| 36 on_error_callback_ = other.on_error_callback_; | |
| 37 other.on_error_callback_.Reset(); | |
| 38 return *this; | |
| 39 } | |
| 40 | |
| 41 template <typename... Args> | |
| 42 void Run(Args... args) { | |
| 43 on_error_callback_.Reset(); | |
| 44 base::ResetAndReturn(&callback_).Run(std::forward<Args>(args)...); | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 CallbackType callback_; | |
| 49 OnErrorCallback on_error_callback_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(ScopedResultCallback); | |
| 52 }; | |
| 53 | |
| 54 } // namespace media | |
| 55 | |
| 56 #endif // MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ | |
| OLD | NEW |