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

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

Issue 2005753006: ImageCapture: ScopedResultCallback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rockot@s nit 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_result_callback.h
diff --git a/media/capture/video/scoped_result_callback.h b/media/capture/video/scoped_result_callback.h
new file mode 100644
index 0000000000000000000000000000000000000000..9c937e8a2815b2ecbd883f3397d2a40344ff9e4d
--- /dev/null
+++ b/media/capture/video/scoped_result_callback.h
@@ -0,0 +1,58 @@
+// 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_RESULT_CALLBACK_H_
+#define MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_
+
+#include "base/callback_forward.h"
+#include "mojo/public/cpp/bindings/callback.h"
+
+namespace media {
+
+// This class guarantees that |callback_| has either been called or will pass it
+// to |on_error_callback_| on destruction. Inspired by ScopedWebCallbacks<>.
+template <typename CallbackType>
+class ScopedResultCallback {
+ MOVE_ONLY_TYPE_FOR_CPP_03(ScopedResultCallback);
+
+ public:
+ using OnErrorCallback = base::Callback<void(const CallbackType&)>;
+ ScopedResultCallback(const CallbackType& callback,
+ const OnErrorCallback& on_error_callback)
+ : callback_(callback), on_error_callback_(on_error_callback) {}
+
+ ~ScopedResultCallback() {
+ if (!callback_.is_null())
+ on_error_callback_.Run(callback_);
+ }
+
+ ScopedResultCallback(ScopedResultCallback&& other) {
+ *this = std::move(other);
+ }
+
+ ScopedResultCallback& operator=(ScopedResultCallback&& other) {
+ callback_ = other.callback_;
+ other.callback_.reset();
+ on_error_callback_ = other.on_error_callback_;
+ other.on_error_callback_.Reset();
+ return *this;
+ }
+
+ template <typename... Args>
+ void Run(Args... args) {
+ on_error_callback_.Reset();
+ // TODO(mcasas): Use base::ResetAndReturn() when mojo::Callback<> is
+ // compatible with base::Callback<>, see https://crbug.com/596521.
+ callback_.Run(std::forward<Args>(args)...);
+ callback_.reset();
+ }
+
+ private:
+ CallbackType callback_;
+ OnErrorCallback on_error_callback_;
+};
+
+} // namespace media
+
+#endif // MEDIA_CAPTURE_VIDEO_SCOPED_RESULT_CALLBACK_H_
« no previous file with comments | « media/capture/video/fake_video_capture_device_unittest.cc ('k') | media/capture/video/video_capture_device.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698