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 MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ | 5 #ifndef MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ |
6 #define MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ | 6 #define MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
| 9 #include "base/macros.h" |
9 #include "mojo/public/cpp/bindings/callback.h" | 10 #include "mojo/public/cpp/bindings/callback.h" |
10 | 11 |
11 namespace media { | 12 namespace media { |
12 | 13 |
13 // This class guarantees that |callback_| has either been called or will pass it | 14 // This class guarantees that |callback_| has either been called or will pass it |
14 // to |on_error_callback_| on destruction. Inspired by ScopedWebCallbacks<>. | 15 // to |on_error_callback_| on destruction. Inspired by ScopedWebCallbacks<>. |
15 template <typename CallbackType> | 16 template <typename CallbackType> |
16 class ScopedResultCallback { | 17 class ScopedResultCallback { |
17 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedResultCallback); | |
18 | |
19 public: | 18 public: |
20 using OnErrorCallback = base::Callback<void(const CallbackType&)>; | 19 using OnErrorCallback = base::Callback<void(const CallbackType&)>; |
21 ScopedResultCallback(const CallbackType& callback, | 20 ScopedResultCallback(const CallbackType& callback, |
22 const OnErrorCallback& on_error_callback) | 21 const OnErrorCallback& on_error_callback) |
23 : callback_(callback), on_error_callback_(on_error_callback) {} | 22 : callback_(callback), on_error_callback_(on_error_callback) {} |
24 | 23 |
25 ~ScopedResultCallback() { | 24 ~ScopedResultCallback() { |
26 if (!callback_.is_null()) | 25 if (!callback_.is_null()) |
27 on_error_callback_.Run(callback_); | 26 on_error_callback_.Run(callback_); |
28 } | 27 } |
(...skipping 15 matching lines...) Expand all Loading... |
44 on_error_callback_.Reset(); | 43 on_error_callback_.Reset(); |
45 // TODO(mcasas): Use base::ResetAndReturn() when mojo::Callback<> is | 44 // TODO(mcasas): Use base::ResetAndReturn() when mojo::Callback<> is |
46 // compatible with base::Callback<>, see https://crbug.com/596521. | 45 // compatible with base::Callback<>, see https://crbug.com/596521. |
47 callback_.Run(std::forward<Args>(args)...); | 46 callback_.Run(std::forward<Args>(args)...); |
48 callback_.reset(); | 47 callback_.reset(); |
49 } | 48 } |
50 | 49 |
51 private: | 50 private: |
52 CallbackType callback_; | 51 CallbackType callback_; |
53 OnErrorCallback on_error_callback_; | 52 OnErrorCallback on_error_callback_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(ScopedResultCallback); |
54 }; | 55 }; |
55 | 56 |
56 } // namespace media | 57 } // namespace media |
57 | 58 |
58 #endif // MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ | 59 #endif // MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_ |
OLD | NEW |