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

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

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 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_LIB_INTERFACE_PTR_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_
7
8 #include <algorithm> // For |std::swap()|.
9 #include <memory>
10 #include <utility>
11
12 #include "mojo/public/cpp/bindings/callback.h"
13 #include "mojo/public/cpp/bindings/interface_handle.h"
14 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
15 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
16 #include "mojo/public/cpp/bindings/lib/router.h"
17 #include "mojo/public/cpp/environment/logging.h"
18
19 struct MojoAsyncWaiter;
20
21 namespace mojo {
22 namespace internal {
23
24 template <typename Interface>
25 class InterfacePtrState {
26 public:
27 InterfacePtrState()
28 : proxy_(nullptr), router_(nullptr), waiter_(nullptr), version_(0u) {}
29
30 ~InterfacePtrState() {
31 // Destruction order matters here. We delete |proxy_| first, even though
32 // |router_| may have a reference to it, so that destructors for any request
33 // callbacks still pending can interact with the InterfacePtr.
34 delete proxy_;
35 delete router_;
36 }
37
38 Interface* instance() {
39 ConfigureProxyIfNecessary();
40
41 // This will be null if the object is not bound.
42 return proxy_;
43 }
44
45 uint32_t version() const { return version_; }
46
47 void QueryVersion(const Callback<void(uint32_t)>& callback) {
48 ConfigureProxyIfNecessary();
49
50 // It is safe to capture |this| because the callback won't be run after this
51 // object goes away.
52 auto callback_wrapper = [this, callback](uint32_t version) {
53 this->version_ = version;
54 callback.Run(version);
55 };
56
57 // Do a static cast in case the interface contains methods with the same
58 // name.
59 static_cast<ControlMessageProxy*>(proxy_)->QueryVersion(callback_wrapper);
60 }
61
62 void RequireVersion(uint32_t version) {
63 ConfigureProxyIfNecessary();
64
65 if (version <= version_)
66 return;
67
68 version_ = version;
69 // Do a static cast in case the interface contains methods with the same
70 // name.
71 static_cast<ControlMessageProxy*>(proxy_)->RequireVersion(version);
72 }
73
74 void Swap(InterfacePtrState* other) {
75 using std::swap;
76 swap(other->proxy_, proxy_);
77 swap(other->router_, router_);
78 handle_.swap(other->handle_);
79 swap(other->waiter_, waiter_);
80 swap(other->version_, version_);
81 }
82
83 void Bind(InterfaceHandle<Interface> info, const MojoAsyncWaiter* waiter) {
84 MOJO_DCHECK(!proxy_);
85 MOJO_DCHECK(!router_);
86 MOJO_DCHECK(!handle_.is_valid());
87 MOJO_DCHECK(!waiter_);
88 MOJO_DCHECK(version_ == 0u);
89 MOJO_DCHECK(info.is_valid());
90
91 handle_ = info.PassHandle();
92 waiter_ = waiter;
93 version_ = info.version();
94 }
95
96 bool WaitForIncomingResponse(
97 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) {
98 ConfigureProxyIfNecessary();
99
100 MOJO_DCHECK(router_);
101 return router_->WaitForIncomingMessage(deadline);
102 }
103
104 // After this method is called, the object is in an invalid state and
105 // shouldn't be reused.
106 InterfaceHandle<Interface> PassInterfaceHandle() {
107 return InterfaceHandle<Interface>(
108 router_ ? router_->PassMessagePipe() : handle_.Pass(), version_);
109 }
110
111 bool is_bound() const { return handle_.is_valid() || router_; }
112
113 bool encountered_error() const {
114 return router_ ? router_->encountered_error() : false;
115 }
116
117 void set_connection_error_handler(const Closure& error_handler) {
118 ConfigureProxyIfNecessary();
119
120 MOJO_DCHECK(router_);
121 router_->set_connection_error_handler(error_handler);
122 }
123
124 Router* router_for_testing() {
125 ConfigureProxyIfNecessary();
126 return router_;
127 }
128
129 private:
130 using Proxy = typename Interface::Proxy_;
131
132 void ConfigureProxyIfNecessary() {
133 // The proxy has been configured.
134 if (proxy_) {
135 MOJO_DCHECK(router_);
136 return;
137 }
138 // The object hasn't been bound.
139 if (!waiter_) {
140 MOJO_DCHECK(!handle_.is_valid());
141 return;
142 }
143
144 MessageValidatorList validators;
145 validators.push_back(
146 std::unique_ptr<MessageValidator>(new MessageHeaderValidator));
147 validators.push_back(std::unique_ptr<MessageValidator>(
148 new typename Interface::ResponseValidator_));
149
150 router_ = new Router(std::move(handle_), std::move(validators), waiter_);
151 waiter_ = nullptr;
152
153 proxy_ = new Proxy(router_);
154 }
155
156 Proxy* proxy_;
157 Router* router_;
158
159 // |proxy_| and |router_| are not initialized until read/write with the
160 // message pipe handle is needed. |handle_| and |waiter_| are valid between
161 // the Bind() call and the initialization of |proxy_| and |router_|.
162 ScopedMessagePipeHandle handle_;
163 const MojoAsyncWaiter* waiter_;
164
165 uint32_t version_;
166
167 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
168 };
169
170 } // namespace internal
171 } // namespace mojo
172
173 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/fixed_buffer.cc ('k') | mojo/public/cpp/bindings/lib/iterator_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698