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

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

Issue 1473273003: Mojo C++ bindings: InterfacePtr<T> and Binding<T> use MultiplexRouter when T passes associated inte… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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 2015 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_ASSOCIATED_INTERFACE_PTR_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_INTERNAL_H_
7
8 #include <algorithm> // For |std::swap()|.
9
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "mojo/public/cpp/bindings/associated_group.h"
13 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h"
14 #include "mojo/public/cpp/bindings/callback.h"
15 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
16 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h"
17 #include "mojo/public/cpp/bindings/lib/interface_id.h"
18 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
19 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h"
20
21 namespace mojo {
22 namespace internal {
23
24 template <typename Interface>
25 class AssociatedInterfacePtrState {
26 public:
27 AssociatedInterfacePtrState() : version_(0u) {}
28
29 ~AssociatedInterfacePtrState() {
30 endpoint_client_.reset();
31 proxy_.reset();
32 }
33
34 Interface* instance() {
35 // This will be null if the object is not bound.
36 return proxy_.get();
37 }
38
39 uint32_t version() const { return version_; }
40
41 void QueryVersion(const Callback<void(uint32_t)>& callback) {
42 // It is safe to capture |this| because the callback won't be run after this
43 // object goes away.
44 auto callback_wrapper = [this, callback](uint32_t version) {
45 this->version_ = version;
46 callback.Run(version);
47 };
48
49 // Do a static cast in case the interface contains methods with the same
50 // name.
51 static_cast<ControlMessageProxy*>(proxy_.get())
52 ->QueryVersion(callback_wrapper);
53 }
54
55 void RequireVersion(uint32_t version) {
56 if (version <= version_)
57 return;
58
59 version_ = version;
60 // Do a static cast in case the interface contains methods with the same
61 // name.
62 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version);
63 }
64
65 void Swap(AssociatedInterfacePtrState* other) {
66 using std::swap;
67 swap(other->endpoint_client_, endpoint_client_);
68 swap(other->proxy_, proxy_);
69 swap(other->version_, version_);
70 }
71
72 void Bind(AssociatedInterfacePtrInfo<Interface> info) {
73 DCHECK(!endpoint_client_);
74 DCHECK(!proxy_);
75 DCHECK_EQ(0u, version_);
76 DCHECK(info.is_valid());
77
78 version_ = info.version();
79 endpoint_client_.reset(new InterfaceEndpointClient(
80 AssociatedInterfacePtrInfoHelper::PassHandle(&info), nullptr,
81 make_scoped_ptr(new typename Interface::ResponseValidator_())));
82 proxy_.reset(new Proxy(endpoint_client_.get()));
83 }
84
85 // After this method is called, the object is in an invalid state and
86 // shouldn't be reused.
87 AssociatedInterfacePtrInfo<Interface> PassInterface() {
88 ScopedInterfaceEndpointHandle handle = endpoint_client_->PassHandle();
89 endpoint_client_.reset();
90 proxy_.reset();
91
92 AssociatedInterfacePtrInfo<Interface> result;
93 result.set_version(version_);
94 AssociatedInterfacePtrInfoHelper::SetHandle(&result, handle.Pass());
95 return result.Pass();
96 }
97
98 bool is_bound() const { return !!endpoint_client_; }
99
100 bool encountered_error() const {
101 return endpoint_client_ ? endpoint_client_->encountered_error() : false;
102 }
103
104 void set_connection_error_handler(const Closure& error_handler) {
105 DCHECK(endpoint_client_);
106 endpoint_client_->set_connection_error_handler(error_handler);
107 }
108
109 // Returns true if bound and awaiting a response to a message.
110 bool has_pending_callbacks() const {
111 return endpoint_client_ && endpoint_client_->has_pending_responders();
112 }
113
114 AssociatedGroup* associated_group() {
115 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr;
116 }
117
118 private:
119 using Proxy = typename Interface::Proxy_;
120
121 scoped_ptr<InterfaceEndpointClient> endpoint_client_;
122 scoped_ptr<Proxy> proxy_;
123
124 uint32_t version_;
125
126 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState);
127 };
128
129 } // namespace internal
130 } // namespace mojo
131
132 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_INTERNAL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/interface_ptr.h ('k') | mojo/public/cpp/bindings/lib/associated_interface_ptr_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698