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 |
+// 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/27 22:36:38
I think this exists already (in base/callback_help
mcasas
2016/05/27 23:36:06
Partially. There is ScopedClosureRunner(), which w
|
+ 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_); |
+ } |
+ |
+ 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() { |
+ CallbackType callback = callback_; |
+ callback_.reset(); |
+ return callback; |
+ } |
+ |
+ private: |
+ CallbackType callback_; |
+ OnErrorCallback on_error_callback_; |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ |