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

Side by Side Diff: mojo/public/cpp/bindings/lib/interface_impl_internal.h

Issue 686883005: Provides a way to use bindings that doesn't require subclassing (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: review feedback 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/interface_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_
7 7
8 #include "mojo/public/cpp/bindings/error_handler.h" 8 #include "mojo/public/cpp/bindings/error_handler.h"
9 #include "mojo/public/cpp/bindings/interface_binding_delegate.h"
9 #include "mojo/public/cpp/bindings/interface_ptr.h" 10 #include "mojo/public/cpp/bindings/interface_ptr.h"
10 #include "mojo/public/cpp/bindings/lib/filter_chain.h" 11 #include "mojo/public/cpp/bindings/lib/filter_chain.h"
11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" 12 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
12 #include "mojo/public/cpp/environment/environment.h" 13 #include "mojo/public/cpp/environment/environment.h"
13 #include "mojo/public/cpp/environment/logging.h" 14 #include "mojo/public/cpp/environment/logging.h"
14 #include "mojo/public/cpp/system/macros.h" 15 #include "mojo/public/cpp/system/macros.h"
15 16
16 namespace mojo { 17 namespace mojo {
18
19 class InterfaceBindingDelegate;
20
17 namespace internal { 21 namespace internal {
18 22
19 template <typename Interface> 23 template <typename Interface>
20 class InterfaceImplBase : public Interface {
21 public:
22 virtual ~InterfaceImplBase() {}
23 virtual void OnConnectionEstablished() = 0;
24 virtual void OnConnectionError() = 0;
25 };
26
27 template <typename Interface>
28 class InterfaceImplState : public ErrorHandler { 24 class InterfaceImplState : public ErrorHandler {
29 public: 25 public:
30 typedef typename Interface::Client Client; 26 typedef typename Interface::Client Client;
31 27
32 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) 28 explicit InterfaceImplState(Interface* instance,
33 : router_(nullptr), 29 InterfaceBindingDelegate* delegate)
30 : delegate_(delegate),
31 router_(nullptr),
34 proxy_(nullptr), 32 proxy_(nullptr),
35 instance_bound_to_pipe_(false) 33 instance_bound_to_pipe_(false) {
36 #ifndef NDEBUG
37 ,
38 deleting_instance_due_to_error_(false)
39 #endif
40 {
41 MOJO_DCHECK(instance); 34 MOJO_DCHECK(instance);
42 stub_.set_sink(instance); 35 stub_.set_sink(instance);
43 } 36 }
44 37
45 virtual ~InterfaceImplState() { 38 virtual ~InterfaceImplState() {
46 #ifndef NDEBUG
47 MOJO_DCHECK(!instance_bound_to_pipe_ || deleting_instance_due_to_error_);
48 #endif
49 delete proxy_; 39 delete proxy_;
50 if (router_) { 40 if (router_) {
51 router_->set_error_handler(nullptr); 41 router_->set_error_handler(nullptr);
52 delete router_; 42 delete router_;
53 } 43 }
54 } 44 }
55 45
56 void BindProxy( 46 void BindProxy(
57 InterfacePtr<Interface>* ptr, 47 InterfacePtr<Interface>* ptr,
58 bool instance_bound_to_pipe, 48 bool instance_bound_to_pipe,
(...skipping 14 matching lines...) Expand all
73 filters.Append<typename Interface::Client::ResponseValidator_>(); 63 filters.Append<typename Interface::Client::ResponseValidator_>();
74 64
75 router_ = new Router(handle.Pass(), filters.Pass(), waiter); 65 router_ = new Router(handle.Pass(), filters.Pass(), waiter);
76 router_->set_incoming_receiver(&stub_); 66 router_->set_incoming_receiver(&stub_);
77 router_->set_error_handler(this); 67 router_->set_error_handler(this);
78 68
79 proxy_ = new typename Client::Proxy_(router_); 69 proxy_ = new typename Client::Proxy_(router_);
80 70
81 instance_bound_to_pipe_ = instance_bound_to_pipe; 71 instance_bound_to_pipe_ = instance_bound_to_pipe;
82 72
83 instance()->OnConnectionEstablished(); 73 if (delegate_)
74 delegate_->OnConnectionEstablished();
84 } 75 }
85 76
86 bool WaitForIncomingMethodCall() { 77 bool WaitForIncomingMethodCall() {
87 MOJO_DCHECK(router_); 78 MOJO_DCHECK(router_);
88 return router_->WaitForIncomingMessage(); 79 return router_->WaitForIncomingMessage();
89 } 80 }
90 81
91 Router* router() { return router_; } 82 Router* router() { return router_; }
92 Client* client() { return proxy_; } 83 Client* client() { return proxy_; }
84 Interface* instance() { return stub_.sink(); }
85
86 bool instance_bound_to_pipe() const { return instance_bound_to_pipe_; }
93 87
94 private: 88 private:
95 InterfaceImplBase<Interface>* instance() { 89 virtual void OnConnectionError() override { delegate_->OnConnectionError(); }
96 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink());
97 }
98 90
99 virtual void OnConnectionError() override { 91 InterfaceBindingDelegate* delegate_;
100 // If the the instance is not bound to the pipe, the instance might choose
101 // to delete itself in the OnConnectionError handler, which would in turn
102 // delete this. Save the error behavior before invoking the error handler
103 // so we can correctly decide what to do.
104 bool bound = instance_bound_to_pipe_;
105 instance()->OnConnectionError();
106 if (!bound)
107 return;
108 #ifndef NDEBUG
109 deleting_instance_due_to_error_ = true;
110 #endif
111 delete instance();
112 }
113
114 Router* router_; 92 Router* router_;
115 typename Client::Proxy_* proxy_; 93 typename Client::Proxy_* proxy_;
116 typename Interface::Stub_ stub_; 94 typename Interface::Stub_ stub_;
117 bool instance_bound_to_pipe_; 95 bool instance_bound_to_pipe_;
118 #ifndef NDEBUG
119 bool deleting_instance_due_to_error_;
120 #endif
121 96
122 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); 97 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState);
123 }; 98 };
124 99
125 } // namespace internal 100 } // namespace internal
126 } // namespace mojo 101 } // namespace mojo
127 102
128 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ 103 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/interface_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698