Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1108)

Unified Diff: media/capture/video/scoped_callback.h

Issue 2005753006: ImageCapture: ScopedResultCallback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reillyg@ second round of comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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_

Powered by Google App Engine
This is Rietveld 408576698