| 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" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "blimp/common/logging.h" | 12 #include "blimp/common/logging.h" |
| 13 #include "blimp/common/proto/blimp_message.pb.h" | 13 #include "blimp/common/proto/blimp_message.pb.h" |
| 14 #include "blimp/net/blimp_connection_details.h" |
| 14 #include "blimp/net/blimp_message_processor.h" | 15 #include "blimp/net/blimp_message_processor.h" |
| 15 #include "blimp/net/blimp_message_pump.h" | 16 #include "blimp/net/blimp_message_pump.h" |
| 16 #include "blimp/net/common.h" | 17 #include "blimp/net/common.h" |
| 17 #include "blimp/net/connection_error_observer.h" | 18 #include "blimp/net/connection_error_observer.h" |
| 18 #include "blimp/net/packet_reader.h" | 19 #include "blimp/net/packet_reader.h" |
| 19 #include "blimp/net/packet_writer.h" | 20 #include "blimp/net/packet_writer.h" |
| 20 #include "net/base/completion_callback.h" | 21 #include "net/base/completion_callback.h" |
| 21 | 22 |
| 22 namespace blimp { | 23 namespace blimp { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Forwards incoming blimp messages to PacketWriter. | 26 // Forwards incoming blimp messages to PacketWriter. |
| 26 class BlimpMessageSender : public BlimpMessageProcessor { | 27 class BlimpMessageSender : public BlimpMessageProcessor { |
| 27 public: | 28 public: |
| 28 explicit BlimpMessageSender(PacketWriter* writer); | 29 explicit BlimpMessageSender(PacketWriter* writer); |
| 29 ~BlimpMessageSender() override; | 30 ~BlimpMessageSender() override; |
| 30 | 31 |
| 31 void set_error_observer(ConnectionErrorObserver* observer) { | 32 void set_error_observer(ConnectionErrorObserver* observer) { |
| 32 error_observer_ = observer; | 33 error_observer_ = observer; |
| 33 } | 34 } |
| 34 | 35 |
| 35 // BlimpMessageProcessor implementation. | 36 // BlimpMessageProcessor implementation. |
| 36 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | 37 void ProcessMessage(std::unique_ptr<BlimpMessage> message, |
| 37 const net::CompletionCallback& callback) override; | 38 const net::CompletionCallback& callback) override; |
| 38 | 39 |
| 40 void SetBlimpConnectionDetails(BlimpConnectionDetails* details) { |
| 41 DCHECK(writer_); |
| 42 writer_->SetBlimpConnectionDetails(details); |
| 43 } |
| 44 |
| 39 private: | 45 private: |
| 40 void OnWritePacketComplete(int result); | 46 void OnWritePacketComplete(int result); |
| 41 | 47 |
| 42 PacketWriter* writer_; | 48 PacketWriter* writer_; |
| 43 ConnectionErrorObserver* error_observer_ = nullptr; | 49 ConnectionErrorObserver* error_observer_ = nullptr; |
| 44 scoped_refptr<net::IOBuffer> buffer_; | 50 scoped_refptr<net::IOBuffer> buffer_; |
| 45 net::CompletionCallback pending_process_msg_callback_; | 51 net::CompletionCallback pending_process_msg_callback_; |
| 46 base::WeakPtrFactory<BlimpMessageSender> weak_factory_; | 52 base::WeakPtrFactory<BlimpMessageSender> weak_factory_; |
| 47 | 53 |
| 48 DISALLOW_COPY_AND_ASSIGN(BlimpMessageSender); | 54 DISALLOW_COPY_AND_ASSIGN(BlimpMessageSender); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 void BlimpConnection::RemoveConnectionErrorObserver( | 135 void BlimpConnection::RemoveConnectionErrorObserver( |
| 130 ConnectionErrorObserver* observer) { | 136 ConnectionErrorObserver* observer) { |
| 131 error_observers_.RemoveObserver(observer); | 137 error_observers_.RemoveObserver(observer); |
| 132 } | 138 } |
| 133 | 139 |
| 134 void BlimpConnection::SetIncomingMessageProcessor( | 140 void BlimpConnection::SetIncomingMessageProcessor( |
| 135 BlimpMessageProcessor* processor) { | 141 BlimpMessageProcessor* processor) { |
| 136 message_pump_->SetMessageProcessor(processor); | 142 message_pump_->SetMessageProcessor(processor); |
| 137 } | 143 } |
| 138 | 144 |
| 145 void BlimpConnection::SetBlimpConnectionDetails( |
| 146 BlimpConnectionDetails* details) { |
| 147 if (message_pump_.get()) { |
| 148 message_pump_->SetBlimpConnectionDetails(details); |
| 149 } |
| 150 BlimpMessageSender* sender = |
| 151 static_cast<BlimpMessageSender*>(outgoing_msg_processor_.get()); |
| 152 if (sender) { |
| 153 sender->SetBlimpConnectionDetails(details); |
| 154 } |
| 155 } |
| 156 |
| 139 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { | 157 BlimpMessageProcessor* BlimpConnection::GetOutgoingMessageProcessor() { |
| 140 return outgoing_msg_processor_.get(); | 158 return outgoing_msg_processor_.get(); |
| 141 } | 159 } |
| 142 | 160 |
| 143 void BlimpConnection::OnConnectionError(int error) { | 161 void BlimpConnection::OnConnectionError(int error) { |
| 144 VLOG(1) << "OnConnectionError, error=" << error; | 162 VLOG(1) << "OnConnectionError, error=" << error; |
| 145 | 163 |
| 146 // Propagate the error to all observers. | 164 // Propagate the error to all observers. |
| 147 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, | 165 FOR_EACH_OBSERVER(ConnectionErrorObserver, error_observers_, |
| 148 OnConnectionError(error)); | 166 OnConnectionError(error)); |
| 149 } | 167 } |
| 150 | 168 |
| 151 } // namespace blimp | 169 } // namespace blimp |
| OLD | NEW |