OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "blimp/net/blimp_connection.h" | 5 #include "blimp/net/blimp_connection.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 DVLOG(2) << "OnWritePacketComplete, result=" << result; | 92 DVLOG(2) << "OnWritePacketComplete, result=" << result; |
93 DCHECK_NE(net::ERR_IO_PENDING, result); | 93 DCHECK_NE(net::ERR_IO_PENDING, result); |
94 base::ResetAndReturn(&pending_process_msg_callback_).Run(result); | 94 base::ResetAndReturn(&pending_process_msg_callback_).Run(result); |
95 if (result != net::OK) { | 95 if (result != net::OK) { |
96 error_observer_->OnConnectionError(result); | 96 error_observer_->OnConnectionError(result); |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 } // namespace | 100 } // namespace |
101 | 101 |
102 class BlimpConnection::EndConnectionHandler | |
Kevin M
2016/05/25 21:19:19
Comment on the class' purpose?
Wez
2016/05/25 23:11:36
Done.
| |
103 : public BlimpMessageProcessor { | |
104 public: | |
105 explicit EndConnectionHandler(BlimpConnection* connection) | |
106 : connection_(connection), message_handler_(nullptr) {} | |
107 | |
108 void set_message_handler(BlimpMessageProcessor* message_handler) { | |
109 message_handler_ = message_handler; | |
110 } | |
111 | |
112 // BlimpMessageProcessor implementation. | |
113 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
114 const net::CompletionCallback& callback) override { | |
115 if (message->has_protocol_control() && | |
116 message->protocol_control().has_end_connection()) { | |
117 // Report the EndConnection reason to connection error observers. | |
118 connection_->OnConnectionError( | |
119 message->protocol_control().end_connection().reason()); | |
120 | |
121 // Caller is responsible for ensuring |callback| is still safe to call. | |
122 callback.Run(message->protocol_control().end_connection().reason()); | |
123 return; | |
124 } | |
125 | |
126 message_handler_->ProcessMessage(std::move(message), callback); | |
127 } | |
128 | |
129 private: | |
130 BlimpConnection* connection_; | |
131 BlimpMessageProcessor* message_handler_; | |
Kevin M
2016/05/25 21:19:19
Document the role of |message_handler_|?
Wez
2016/05/25 23:11:37
Done.
| |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(EndConnectionHandler); | |
134 }; | |
135 | |
102 BlimpConnection::BlimpConnection(std::unique_ptr<PacketReader> reader, | 136 BlimpConnection::BlimpConnection(std::unique_ptr<PacketReader> reader, |
103 std::unique_ptr<PacketWriter> writer) | 137 std::unique_ptr<PacketWriter> writer) |
104 : reader_(std::move(reader)), | 138 : reader_(std::move(reader)), |
105 message_pump_(new BlimpMessagePump(reader_.get())), | 139 message_pump_(new BlimpMessagePump(reader_.get())), |
106 writer_(std::move(writer)), | 140 writer_(std::move(writer)), |
107 outgoing_msg_processor_(new BlimpMessageSender(writer_.get())) { | 141 outgoing_msg_processor_(new BlimpMessageSender(writer_.get())), |
142 end_connection_handler_(new EndConnectionHandler(this)) { | |
108 DCHECK(writer_); | 143 DCHECK(writer_); |
109 | 144 |
110 // Observe the connection errors received by any of this connection's network | 145 // Observe the connection errors received by any of this connection's network |
111 // objects. | 146 // objects. |
112 message_pump_->set_error_observer(this); | 147 message_pump_->set_error_observer(this); |
113 BlimpMessageSender* sender = | 148 BlimpMessageSender* sender = |
114 static_cast<BlimpMessageSender*>(outgoing_msg_processor_.get()); | 149 static_cast<BlimpMessageSender*>(outgoing_msg_processor_.get()); |
115 sender->set_error_observer(this); | 150 sender->set_error_observer(this); |
116 } | 151 } |
117 | 152 |
118 BlimpConnection::BlimpConnection() {} | 153 BlimpConnection::BlimpConnection() {} |
119 | 154 |
120 BlimpConnection::~BlimpConnection() { | 155 BlimpConnection::~BlimpConnection() { |
121 VLOG(1) << "BlimpConnection destroyed."; | 156 VLOG(1) << "BlimpConnection destroyed."; |
122 } | 157 } |
123 | 158 |
124 void BlimpConnection::AddConnectionErrorObserver( | 159 void BlimpConnection::AddConnectionErrorObserver( |
125 ConnectionErrorObserver* observer) { | 160 ConnectionErrorObserver* observer) { |
126 error_observers_.AddObserver(observer); | 161 error_observers_.AddObserver(observer); |
127 } | 162 } |
128 | 163 |
129 void BlimpConnection::RemoveConnectionErrorObserver( | 164 void BlimpConnection::RemoveConnectionErrorObserver( |
130 ConnectionErrorObserver* observer) { | 165 ConnectionErrorObserver* observer) { |
131 error_observers_.RemoveObserver(observer); | 166 error_observers_.RemoveObserver(observer); |
132 } | 167 } |
133 | 168 |
134 void BlimpConnection::SetIncomingMessageProcessor( | 169 void BlimpConnection::SetIncomingMessageProcessor( |
135 BlimpMessageProcessor* processor) { | 170 BlimpMessageProcessor* processor) { |
136 message_pump_->SetMessageProcessor(processor); | 171 end_connection_handler_->set_message_handler(processor); |
172 message_pump_->SetMessageProcessor(processor ? end_connection_handler_.get() | |
173 : nullptr); | |
137 } | 174 } |
138 | 175 |
139 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { | 176 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { |
140 return outgoing_msg_processor_.get(); | 177 return outgoing_msg_processor_.get(); |
141 } | 178 } |
142 | 179 |
143 void BlimpConnection::OnConnectionError(int error) { | 180 void BlimpConnection::OnConnectionError(int error) { |
144 VLOG(1) << "OnConnectionError, error=" << error; | 181 VLOG(1) << "OnConnectionError, error=" << error; |
145 | 182 |
146 // Propagate the error to all observers. | 183 // Propagate the error to all observers. |
147 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, | 184 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, |
148 OnConnectionError(error)); | 185 OnConnectionError(error)); |
149 } | 186 } |
150 | 187 |
151 } // namespace blimp | 188 } // namespace blimp |
OLD | NEW |