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

Side by Side 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, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
7
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "mojo/public/cpp/bindings/associated_binding.h"
12 #include "mojo/public/cpp/bindings/associated_interface_ptr.h"
13 #include "mojo/public/cpp/bindings/associated_interface_request.h"
14 #include "mojo/public/cpp/bindings/callback.h"
15 #include "mojo/public/cpp/system/core.h"
16
17 namespace mojo {
18
19 // This connects an interface implementation strongly to a pipe. When a
20 // connection error is detected the implementation is deleted. Deleting the
21 // connector also closes the pipe.
22 //
23 // This class is thread hostile once it is bound to a message pipe. Until it is
24 // bound, it may be bound or destroyed on any thread.
25 template <typename Interface>
26 class StrongAssociatedBinding {
27 MOVE_ONLY_TYPE_FOR_CPP_03(StrongAssociatedBinding);
28
29 public:
30 explicit StrongAssociatedBinding(Interface* impl) : binding_(impl) {}
31
32 StrongAssociatedBinding(Interface* impl, ScopedMessagePipeHandle handle)
33 : StrongAssociatedBinding(impl) {
34 Bind(std::move(handle));
35 }
36
37 StrongAssociatedBinding(Interface* impl,
38 AssociatedInterfacePtrInfo<Interface>* ptr_info,
39 AssociatedGroup* associated_group)
40 : StrongAssociatedBinding(impl) {
41 Bind(ptr_info, associated_group);
42 }
43
44 StrongAssociatedBinding(Interface* impl,
45 AssociatedInterfaceRequest<Interface> request)
46 : StrongAssociatedBinding(impl) {
47 Bind(std::move(request));
48 }
49
50 ~StrongAssociatedBinding() {}
51
52 void Bind(AssociatedInterfacePtrInfo<Interface>* ptr_info,
53 AssociatedGroup* associated_group) {
54 DCHECK(!binding_.is_bound());
55 binding_.Bind(ptr_info, associated_group);
56 binding_.set_connection_error_handler([this]() { OnConnectionError(); });
57 }
58
59 void Bind(AssociatedInterfaceRequest<Interface> request) {
60 DCHECK(!binding_.is_bound());
61 binding_.Bind(std::move(request));
62 binding_.set_connection_error_handler([this]() { OnConnectionError(); });
63 }
64
65 // Note: The error handler must not delete the interface implementation.
66 //
67 // This method may only be called after this AssociatedStrongBinding has been
68 // bound to a message pipe.
69 void set_connection_error_handler(const Closure& error_handler) {
70 DCHECK(binding_.is_bound());
71 connection_error_handler_ = error_handler;
72 }
73
74 Interface* impl() { return binding_.impl(); }
75
76 void OnConnectionError() {
77 connection_error_handler_.Run();
78 delete binding_.impl();
79 }
80
81 private:
82 Closure connection_error_handler_;
83 AssociatedBinding<Interface> binding_;
84 };
85
86 } // namespace mojo
87
88 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698