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

Unified Diff: mojo/public/cpp/bindings/strong_associated_binding.h

Issue 2004643002: Implement BroadcastChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: better tests 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: mojo/public/cpp/bindings/strong_associated_binding.h
diff --git a/mojo/public/cpp/bindings/strong_associated_binding.h b/mojo/public/cpp/bindings/strong_associated_binding.h
new file mode 100644
index 0000000000000000000000000000000000000000..39117317a487f267d7bd112b383e0c0f68371cc9
--- /dev/null
+++ b/mojo/public/cpp/bindings/strong_associated_binding.h
@@ -0,0 +1,88 @@
+// Copyright 2014 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 MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
+
+#include <utility>
+
+#include "base/logging.h"
+#include "mojo/public/cpp/bindings/associated_binding.h"
+#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
+#include "mojo/public/cpp/bindings/associated_interface_request.h"
+#include "mojo/public/cpp/bindings/callback.h"
+#include "mojo/public/cpp/system/core.h"
+
+namespace mojo {
+
+// This connects an interface implementation strongly to a pipe. When a
+// connection error is detected the implementation is deleted. Deleting the
+// connector also closes the pipe.
+//
+// This class is thread hostile once it is bound to a message pipe. Until it is
+// bound, it may be bound or destroyed on any thread.
+template <typename Interface>
+class StrongAssociatedBinding {
+ MOVE_ONLY_TYPE_FOR_CPP_03(StrongAssociatedBinding);
+
+ public:
+ explicit StrongAssociatedBinding(Interface* impl) : binding_(impl) {}
+
+ StrongAssociatedBinding(Interface* impl, ScopedMessagePipeHandle handle)
+ : StrongAssociatedBinding(impl) {
+ Bind(std::move(handle));
+ }
+
+ StrongAssociatedBinding(Interface* impl,
+ AssociatedInterfacePtrInfo<Interface>* ptr_info,
+ AssociatedGroup* associated_group)
+ : StrongAssociatedBinding(impl) {
+ Bind(ptr_info, associated_group);
+ }
+
+ StrongAssociatedBinding(Interface* impl,
+ AssociatedInterfaceRequest<Interface> request)
+ : StrongAssociatedBinding(impl) {
+ Bind(std::move(request));
+ }
+
+ ~StrongAssociatedBinding() {}
+
+ void Bind(AssociatedInterfacePtrInfo<Interface>* ptr_info,
+ AssociatedGroup* associated_group) {
+ DCHECK(!binding_.is_bound());
+ binding_.Bind(ptr_info, associated_group);
+ binding_.set_connection_error_handler([this]() { OnConnectionError(); });
+ }
+
+ void Bind(AssociatedInterfaceRequest<Interface> request) {
+ DCHECK(!binding_.is_bound());
+ binding_.Bind(std::move(request));
+ binding_.set_connection_error_handler([this]() { OnConnectionError(); });
+ }
+
+ // Note: The error handler must not delete the interface implementation.
+ //
+ // This method may only be called after this AssociatedStrongBinding has been
+ // bound to a message pipe.
+ void set_connection_error_handler(const Closure& error_handler) {
+ DCHECK(binding_.is_bound());
+ connection_error_handler_ = error_handler;
+ }
+
+ Interface* impl() { return binding_.impl(); }
+
+ void OnConnectionError() {
+ connection_error_handler_.Run();
+ delete binding_.impl();
+ }
+
+ private:
+ Closure connection_error_handler_;
+ AssociatedBinding<Interface> binding_;
+};
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_

Powered by Google App Engine
This is Rietveld 408576698