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

Side by Side Diff: mojo/public/cpp/bindings/binding.h

Issue 718473003: Add mojo::Binding<Interface> for more flexible pipe<->impl binding (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/interface_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
7
8 #include "mojo/public/c/environment/async_waiter.h"
9 #include "mojo/public/cpp/bindings/error_handler.h"
10 #include "mojo/public/cpp/bindings/interface_ptr.h"
11 #include "mojo/public/cpp/bindings/lib/filter_chain.h"
12 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
13 #include "mojo/public/cpp/bindings/lib/router.h"
14 #include "mojo/public/cpp/environment/logging.h"
15 #include "mojo/public/cpp/system/core.h"
16
17 namespace mojo {
18
19 // This binds an interface implementation a pipe. Deleting the binding closes
20 // the pipe.
21 //
22 // Example:
23 //
24 // #include "foo.mojom.h"
25 //
26 // class FooImpl : public Foo {
27 // public:
28 // explicit FooImpl(ScopedMessagePipeHandle handle)
29 // : binding_(this, handle.Pass()) {}
30 //
31 // // Foo implementation here.
32 //
33 // private:
34 // Binding<Foo> binding_;
35 // };
36 //
37 template <typename Interface>
38 class Binding : public ErrorHandler {
39 public:
40 using Client = typename Interface::Client;
41
42 explicit Binding(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); }
43
44 Binding(Interface* impl,
45 ScopedMessagePipeHandle handle,
46 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
47 : Binding(impl) {
48 Bind(handle.Pass(), waiter);
49 }
50
51 Binding(Interface* impl,
52 InterfacePtr<Interface>* ptr,
53 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
54 : Binding(impl) {
55 MessagePipe pipe;
56 ptr->Bind(pipe.handle0.Pass(), waiter);
57 Bind(pipe.handle1.Pass(), waiter);
58 }
59
60 virtual ~Binding() {
61 delete proxy_;
62 if (router_) {
63 router_->set_error_handler(nullptr);
64 delete router_;
65 }
66 }
67
68 void Bind(
69 ScopedMessagePipeHandle handle,
70 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
71 internal::FilterChain filters;
72 filters.Append<internal::MessageHeaderValidator>();
73 filters.Append<typename Interface::RequestValidator_>();
74 filters.Append<typename Client::ResponseValidator_>();
75
76 router_ = new internal::Router(handle.Pass(), filters.Pass(), waiter);
77 router_->set_incoming_receiver(&stub_);
78 router_->set_error_handler(this);
79
80 proxy_ = new typename Client::Proxy_(router_);
81 }
82
83 void Bind(
84 InterfacePtr<Interface>* ptr,
85 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
86 MessagePipe pipe;
87 ptr->Bind(pipe.handle0.Pass(), waiter);
88 Bind(pipe.handle1.Pass(), waiter);
89 }
DaveMoore 2014/11/11 21:16:41 Maybe add void Bind(InterfaceRequest<Interface>*
90
91 bool WaitForIncomingMethodCall() {
92 MOJO_DCHECK(router_);
93 return router_->WaitForIncomingMessage();
94 }
95
96 void set_error_handler(ErrorHandler* error_handler) {
97 error_handler_ = error_handler;
98 }
99
100 // ErrorHandler implementation
101 void OnConnectionError() override {
102 if (error_handler_)
103 error_handler_->OnConnectionError();
104 }
105
106 Interface* impl() { return impl_; }
107 Client* client() { return proxy_; }
108 // Exposed for testing, should not generally be used.
109 internal::Router* router() { return router_; }
110
111 private:
112 internal::Router* router_ = nullptr;
113 typename Client::Proxy_* proxy_ = nullptr;
114 typename Interface::Stub_ stub_;
115 Interface* impl_;
116 ErrorHandler* error_handler_ = nullptr;
117
118 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding);
119 };
120
121 } // namespace mojo
122
123 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/interface_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698