Index: media/capture/video/scoped_callback.h |
diff --git a/media/capture/video/scoped_callback.h b/media/capture/video/scoped_callback.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..84a1a5a00d4902148624ec7244e80352aaf32b8d |
--- /dev/null |
+++ b/media/capture/video/scoped_callback.h |
@@ -0,0 +1,52 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ |
+#define MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ |
+ |
+#include "base/callback_forward.h" |
+ |
+namespace media { |
+ |
+// This class wraps a |callback_| and guarantees that it will be called on |
miu
2016/05/28 01:29:56
This is a little inaccurate. It will guarantee tha
mcasas
2016/05/31 20:19:04
Rewritten.
|
+// destruction and according to the provided OnErrorCallback if it hasn't been |
+// retrieved before (via PassCallback()). Inspired by ScopedWebCallbacks |
+// template. |
+template <typename CallbackType> |
+class ScopedCallback { |
miu
2016/05/28 01:29:56
For me, the name of this utility class was a bit c
mcasas
2016/05/31 20:19:04
If this class was to get traction, I think its bas
miu
2016/06/01 00:53:36
nit: ScopedResultCallback? (since this mechanism i
mcasas
2016/06/01 01:21:01
ScopedResultCallback sounds great.
|
+ MOVE_ONLY_TYPE_FOR_CPP_03(ScopedCallback); |
+ |
+ public: |
+ using OnErrorCallback = base::Callback<void(const CallbackType&)>; |
+ ScopedCallback(const CallbackType& callback, |
+ const OnErrorCallback& on_error_callback) |
+ : callback_(callback), on_error_callback_(on_error_callback) {} |
+ |
+ ~ScopedCallback() { |
+ if (!callback_.is_null()) |
+ on_error_callback_.Run(callback_); |
miu
2016/05/28 01:29:56
Just in case that |callback_| has a ref-counted po
mcasas
2016/05/31 20:19:04
Done.
(Note that base::ResetAndReturn() doesn't w
|
+ } |
+ |
+ ScopedCallback(ScopedCallback&& other) { *this = std::move(other); } |
+ |
+ ScopedCallback& operator=(ScopedCallback&& other) { |
+ callback_ = other.PassCallback(); |
+ on_error_callback_ = other.on_error_callback_; |
+ return *this; |
+ } |
+ |
+ CallbackType PassCallback() { |
miu
2016/05/28 01:29:56
A few things:
1. Instead of this method, would cl
mcasas
2016/05/31 20:19:04
Taken all in except the base::ResetAndReturn() for
miu
2016/06/01 00:53:36
Understood.
|
+ CallbackType callback = callback_; |
+ callback_.reset(); |
+ return callback; |
+ } |
+ |
+ private: |
+ CallbackType callback_; |
+ OnErrorCallback on_error_callback_; |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ |