| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 net::CompletionCallback process_callback = | 98 net::CompletionCallback process_callback = |
| 99 base::ResetAndReturn(&pending_process_msg_callback_); | 99 base::ResetAndReturn(&pending_process_msg_callback_); |
| 100 | 100 |
| 101 if (result != net::OK) { | 101 if (result != net::OK) { |
| 102 error_observer_->OnConnectionError(result); | 102 error_observer_->OnConnectionError(result); |
| 103 } | 103 } |
| 104 | 104 |
| 105 process_callback.Run(result); | 105 process_callback.Run(result); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // MessageProcessor filter used to route EndConnection messages through to |
| 109 // OnConnectionError notifications on the owning BlimpConnection. |
| 110 class BlimpConnection::EndConnectionFilter : public BlimpMessageProcessor { |
| 111 public: |
| 112 explicit EndConnectionFilter(BlimpConnection* connection); |
| 113 |
| 114 void set_message_handler(BlimpMessageProcessor* message_handler) { |
| 115 message_handler_ = message_handler; |
| 116 } |
| 117 |
| 118 // BlimpMessageProcessor implementation. |
| 119 void ProcessMessage(std::unique_ptr<BlimpMessage> message, |
| 120 const net::CompletionCallback& callback) override; |
| 121 |
| 122 private: |
| 123 // Owning BlimpConnection, on which to call OnConnectionError. |
| 124 BlimpConnection* connection_; |
| 125 |
| 126 // Caller-provided message handler to forward non-EndConnection messages to. |
| 127 BlimpMessageProcessor* message_handler_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(EndConnectionFilter); |
| 130 }; |
| 131 |
| 132 BlimpConnection::EndConnectionFilter::EndConnectionFilter( |
| 133 BlimpConnection* connection) |
| 134 : connection_(connection), message_handler_(nullptr) {} |
| 135 |
| 136 void BlimpConnection::EndConnectionFilter::ProcessMessage( |
| 137 std::unique_ptr<BlimpMessage> message, |
| 138 const net::CompletionCallback& callback) { |
| 139 if (message->has_protocol_control() && |
| 140 message->protocol_control().has_end_connection()) { |
| 141 // Report the EndConnection reason to connection error observers. |
| 142 connection_->OnConnectionError( |
| 143 message->protocol_control().end_connection().reason()); |
| 144 |
| 145 // Caller must ensure |callback| safe to call after OnConnectionError. |
| 146 callback.Run(message->protocol_control().end_connection().reason()); |
| 147 return; |
| 148 } |
| 149 |
| 150 message_handler_->ProcessMessage(std::move(message), callback); |
| 151 } |
| 152 |
| 108 BlimpConnection::BlimpConnection(std::unique_ptr<PacketReader> reader, | 153 BlimpConnection::BlimpConnection(std::unique_ptr<PacketReader> reader, |
| 109 std::unique_ptr<PacketWriter> writer) | 154 std::unique_ptr<PacketWriter> writer) |
| 110 : reader_(std::move(reader)), | 155 : reader_(std::move(reader)), |
| 111 message_pump_(new BlimpMessagePump(reader_.get())), | 156 message_pump_(new BlimpMessagePump(reader_.get())), |
| 112 writer_(std::move(writer)), | 157 writer_(std::move(writer)), |
| 113 outgoing_msg_processor_(new BlimpMessageSender(writer_.get())) { | 158 outgoing_msg_processor_(new BlimpMessageSender(writer_.get())), |
| 159 end_connection_filter_(new EndConnectionFilter(this)) { |
| 114 DCHECK(writer_); | 160 DCHECK(writer_); |
| 115 DCHECK(reader_); | 161 DCHECK(reader_); |
| 116 | 162 |
| 117 message_pump_->set_error_observer(this); | 163 message_pump_->set_error_observer(this); |
| 118 outgoing_msg_processor_->set_error_observer(this); | 164 outgoing_msg_processor_->set_error_observer(this); |
| 119 } | 165 } |
| 120 | 166 |
| 121 BlimpConnection::BlimpConnection() {} | 167 BlimpConnection::BlimpConnection() {} |
| 122 | 168 |
| 123 BlimpConnection::~BlimpConnection() { | 169 BlimpConnection::~BlimpConnection() { |
| 124 VLOG(1) << "BlimpConnection destroyed."; | 170 VLOG(1) << "BlimpConnection destroyed."; |
| 125 } | 171 } |
| 126 | 172 |
| 127 void BlimpConnection::AddConnectionErrorObserver( | 173 void BlimpConnection::AddConnectionErrorObserver( |
| 128 ConnectionErrorObserver* observer) { | 174 ConnectionErrorObserver* observer) { |
| 129 error_observers_.AddObserver(observer); | 175 error_observers_.AddObserver(observer); |
| 130 } | 176 } |
| 131 | 177 |
| 132 void BlimpConnection::RemoveConnectionErrorObserver( | 178 void BlimpConnection::RemoveConnectionErrorObserver( |
| 133 ConnectionErrorObserver* observer) { | 179 ConnectionErrorObserver* observer) { |
| 134 error_observers_.RemoveObserver(observer); | 180 error_observers_.RemoveObserver(observer); |
| 135 } | 181 } |
| 136 | 182 |
| 137 void BlimpConnection::SetIncomingMessageProcessor( | 183 void BlimpConnection::SetIncomingMessageProcessor( |
| 138 BlimpMessageProcessor* processor) { | 184 BlimpMessageProcessor* processor) { |
| 139 message_pump_->SetMessageProcessor(processor); | 185 end_connection_filter_->set_message_handler(processor); |
| 186 message_pump_->SetMessageProcessor(processor ? end_connection_filter_.get() |
| 187 : nullptr); |
| 140 } | 188 } |
| 141 | 189 |
| 142 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { | 190 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { |
| 143 return outgoing_msg_processor_.get(); | 191 return outgoing_msg_processor_.get(); |
| 144 } | 192 } |
| 145 | 193 |
| 146 void BlimpConnection::OnConnectionError(int error) { | 194 void BlimpConnection::OnConnectionError(int error) { |
| 147 VLOG(1) << "OnConnectionError, error=" << error; | 195 VLOG(1) << "OnConnectionError, error=" << error; |
| 148 | 196 |
| 149 // Propagate the error to all observers. | 197 // Propagate the error to all observers. |
| 150 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, | 198 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, |
| 151 OnConnectionError(error)); | 199 OnConnectionError(error)); |
| 152 } | 200 } |
| 153 | 201 |
| 154 } // namespace blimp | 202 } // namespace blimp |
| OLD | NEW |