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

Side by Side Diff: mojo/public/cpp/bindings/strong_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
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_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_
7
8 #include "mojo/public/c/environment/async_waiter.h"
9 #include "mojo/public/cpp/bindings/binding.h"
10 #include "mojo/public/cpp/bindings/error_handler.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/system/core.h"
15
16 namespace mojo {
17
18 // This connects an interface implementation strongly to a pipe. When a
19 // connection error is detected the implementation is deleted. Deleting the
20 // connector also closes the pipe.
21 //
22 // Example of an implementation that is always bound strongly to a pipe
23 //
24 // class StronglyBound : public Foo {
25 // public:
26 // explicit StronglyBound(ScopedMessagePipeHandle handle)
27 // : binding_(this, handle.Pass()) {}
28 //
29 // // Foo implementation here
30 //
31 // private:
32 // StrongBinding<Foo> binding_;
33 // };
34 //
35 template <typename Interface>
36 class StrongBinding : public ErrorHandler {
37 public:
38 explicit StrongBinding(Interface* impl) : binding_(impl) {
39 binding_.set_error_handler(this);
40 }
41
42 StrongBinding(
43 Interface* impl,
44 ScopedMessagePipeHandle handle,
45 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
46 : StrongBinding(impl) {
47 binding_.Bind(handle.Pass(), waiter);
48 }
49
50 StrongBinding(
51 Interface* impl,
52 InterfacePtr<Interface>* ptr,
53 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter())
54 : StrongBinding(impl) {
55 binding_.Bind(ptr, waiter);
56 }
57
58 ~StrongBinding() override {}
59
60 bool WaitForIncomingMethodCall() { binding_.WaitForIncomingMethodCall(); }
61
62 void set_error_handler(ErrorHandler* error_handler) {
63 error_handler_ = error_handler;
64 }
65
66 typename Interface::Client* client() { return binding_.client(); }
67 // Exposed for testing, should not generally be used.
68 internal::Router* router() { return binding_.router(); }
69
70 // ErrorHandler implementation
71 void OnConnectionError() override {
72 if (error_handler_)
73 error_handler_->OnConnectionError();
74 delete binding_.impl();
75 }
76
77 private:
78 ErrorHandler* error_handler_ = nullptr;
79 Binding<Interface> binding_;
80 };
81
82 } // namespace mojo
83
84 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698