OLD | NEW |
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 #include "mojo/public/bindings/lib/router.h" | 5 #include "mojo/public/bindings/lib/router.h" |
6 | 6 |
7 namespace mojo { | 7 namespace mojo { |
8 namespace internal { | 8 namespace internal { |
9 | 9 |
10 // ---------------------------------------------------------------------------- | 10 // ---------------------------------------------------------------------------- |
11 | 11 |
12 class ResponderThunk : public MessageReceiver { | 12 class ResponderThunk : public MessageReceiver { |
13 public: | 13 public: |
14 explicit ResponderThunk(const SharedData<Router*>& router) | 14 explicit ResponderThunk(const SharedData<Router*>& router) |
15 : router_(router) { | 15 : router_(router) { |
16 } | 16 } |
17 virtual ~ResponderThunk() { | 17 virtual ~ResponderThunk() { |
18 } | 18 } |
19 | 19 |
20 // MessageReceiver implementation: | 20 // MessageReceiver implementation: |
21 virtual bool Accept(Message* message) MOJO_OVERRIDE { | 21 virtual bool Accept(Message* message) MOJO_OVERRIDE { |
22 assert(message->has_flag(kMessageIsResponse)); | 22 assert(message->has_flag(kMessageIsResponse)); |
23 | 23 |
24 bool result = false; | 24 bool result = false; |
25 | 25 |
26 Router* router = router_.value(); | 26 Router* router = router_.value(); |
27 if (router) | 27 if (router) |
28 result = router->Accept(message); | 28 result = router->Accept(message); |
29 | 29 |
30 delete this; | |
31 return result; | 30 return result; |
32 } | 31 } |
33 | 32 |
34 virtual bool AcceptWithResponder(Message* message, | 33 virtual bool AcceptWithResponder(Message* message, |
35 MessageReceiver* responder) MOJO_OVERRIDE { | 34 MessageReceiver* responder) MOJO_OVERRIDE { |
36 assert(false); // not reached! | 35 assert(false); // not reached! |
37 return false; | 36 return false; |
38 } | 37 } |
39 | 38 |
40 private: | 39 private: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 86 |
88 // Reserve 0 in case we want it to convey special meaning in the future. | 87 // Reserve 0 in case we want it to convey special meaning in the future. |
89 uint64_t request_id = next_request_id_++; | 88 uint64_t request_id = next_request_id_++; |
90 if (request_id == 0) | 89 if (request_id == 0) |
91 request_id = next_request_id_++; | 90 request_id = next_request_id_++; |
92 | 91 |
93 message->set_request_id(request_id); | 92 message->set_request_id(request_id); |
94 if (!connector_.Accept(message)) | 93 if (!connector_.Accept(message)) |
95 return false; | 94 return false; |
96 | 95 |
| 96 // We assume ownership of |responder|. |
97 responders_[request_id] = responder; | 97 responders_[request_id] = responder; |
98 return true; | 98 return true; |
99 } | 99 } |
100 | 100 |
101 bool Router::HandleIncomingMessage(Message* message) { | 101 bool Router::HandleIncomingMessage(Message* message) { |
102 if (message->has_flag(kMessageExpectsResponse)) { | 102 if (message->has_flag(kMessageExpectsResponse)) { |
103 if (incoming_receiver_) { | 103 if (incoming_receiver_) { |
104 return incoming_receiver_->AcceptWithResponder( | 104 MessageReceiver* responder = new ResponderThunk(weak_self_); |
105 message, | 105 bool ok = incoming_receiver_->AcceptWithResponder(message, responder); |
106 new ResponderThunk(weak_self_)); | 106 if (!ok) |
| 107 delete responder; |
| 108 return ok; |
107 } | 109 } |
108 | 110 |
109 // If we receive a request expecting a response when the client is not | 111 // If we receive a request expecting a response when the client is not |
110 // listening, then we have no choice but to tear down the pipe. | 112 // listening, then we have no choice but to tear down the pipe. |
111 connector_.CloseMessagePipe(); | 113 connector_.CloseMessagePipe(); |
112 } else if (message->has_flag(kMessageIsResponse)) { | 114 } else if (message->has_flag(kMessageIsResponse)) { |
113 uint64_t request_id = message->request_id(); | 115 uint64_t request_id = message->request_id(); |
114 ResponderMap::iterator it = responders_.find(request_id); | 116 ResponderMap::iterator it = responders_.find(request_id); |
115 if (it == responders_.end()) { | 117 if (it == responders_.end()) { |
116 assert(false); | 118 assert(false); |
117 return false; | 119 return false; |
118 } | 120 } |
119 MessageReceiver* responder = it->second; | 121 MessageReceiver* responder = it->second; |
120 responders_.erase(it); | 122 responders_.erase(it); |
121 responder->Accept(message); | 123 responder->Accept(message); |
| 124 delete responder; |
122 } else { | 125 } else { |
123 if (incoming_receiver_) | 126 if (incoming_receiver_) |
124 return incoming_receiver_->Accept(message); | 127 return incoming_receiver_->Accept(message); |
125 // OK to drop message on the floor. | 128 // OK to drop message on the floor. |
126 } | 129 } |
127 | 130 |
128 return false; | 131 return false; |
129 } | 132 } |
130 | 133 |
131 // ---------------------------------------------------------------------------- | 134 // ---------------------------------------------------------------------------- |
132 | 135 |
133 } // namespace internal | 136 } // namespace internal |
134 } // namespace mojo | 137 } // namespace mojo |
OLD | NEW |