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

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

Issue 708053005: Add simple strong and weak connectors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.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 | « no previous file | mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc » ('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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRONG_CONNECTOR_H_
2 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_CONNECTOR_H_
3
4 #include "mojo/public/c/environment/async_waiter.h"
5 #include "mojo/public/cpp/bindings/error_handler.h"
6 #include "mojo/public/cpp/bindings/lib/filter_chain.h"
7 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
8 #include "mojo/public/cpp/bindings/lib/router.h"
9 #include "mojo/public/cpp/system/core.h"
10
11 namespace mojo {
12
13 // This connects an interface implementation strongly to a pipe. When a
14 // connection error is detected the implementation is deleted. Deleting the
15 // connector also closes the pipe.
16 //
17 // Example of an implementation that is always bound strongly to a pipe
18 //
19 // class StronglyBound : public Foo {
20 // public:
21 // explicit StronglyBound(ScopedMessagePipeHandle handle)
22 // : connector_(this, handle.Pass()) {}
23 //
24 // // Foo implementation here
25 //
26 // private:
27 // StrongConnector<Foo> connector_;
28 // };
29 //
30 template <typename Interface>
31 class StrongConnector : public ErrorHandler {
32 typedef typename Interface::Client Client;
33
34 public:
35 explicit StrongConnector(Interface* impl) : impl_(impl) {
36 stub_.set_sink(impl_);
37 }
38
39 StrongConnector(
40 Interface* impl,
41 ScopedMessagePipeHandle handle,
42 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
43 : StrongConnector(impl) {
44 stub_.set_sink(impl_);
45 Bind(handle.Pass(), waiter);
46 }
47
48 virtual ~StrongConnector() {
49 delete proxy_;
50 if (router_) {
51 router_->set_error_handler(nullptr);
52 delete router_;
53 }
54 }
55
56 void Bind(
57 ScopedMessagePipeHandle handle,
58 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
59 internal::FilterChain filters;
60 filters.Append<internal::MessageHeaderValidator>();
61 filters.Append<typename Interface::RequestValidator_>();
62 filters.Append<typename Interface::Client::ResponseValidator_>();
63
64 router_ = new internal::Router(handle.Pass(), filters.Pass(), waiter);
65 router_->set_incoming_receiver(&stub_);
66 router_->set_error_handler(this);
67
68 proxy_ = new typename Client::Proxy_(router_);
69 }
70
71 void set_error_handler(ErrorHandler* error_handler) {
72 error_handler_ = error_handler;
73 }
74
75 // ErrorHandler implementation
76 void OnConnectionError() override {
77 if (error_handler_)
78 error_handler_->OnConnectionError();
79 delete impl_;
80 }
81
82 typename Interface::Client* client() { return proxy_; }
83
84 private:
85 internal::Router* router_ = nullptr;
86 typename Client::Proxy_* proxy_ = nullptr;
87 typename Interface::Stub_ stub_;
88 Interface* impl_;
89 ErrorHandler* error_handler_ = nullptr;
90 };
91
92 } // namespace mojo
93
94 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_CONNECTOR_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698