| OLD | NEW |
| (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 #include "blimp/net/blimp_connection.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/callback_helpers.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "blimp/common/create_blimp_message.h" | |
| 15 #include "blimp/common/logging.h" | |
| 16 #include "blimp/common/proto/blimp_message.pb.h" | |
| 17 #include "blimp/net/blimp_message_processor.h" | |
| 18 #include "blimp/net/blimp_message_pump.h" | |
| 19 #include "blimp/net/common.h" | |
| 20 #include "blimp/net/connection_error_observer.h" | |
| 21 #include "blimp/net/message_port.h" | |
| 22 #include "blimp/net/packet_writer.h" | |
| 23 #include "net/base/completion_callback.h" | |
| 24 | |
| 25 namespace blimp { | |
| 26 | |
| 27 // MessageProcessor filter used to route EndConnection messages through to | |
| 28 // OnConnectionError notifications on the owning BlimpConnection. | |
| 29 class BlimpConnection::EndConnectionFilter : public BlimpMessageProcessor { | |
| 30 public: | |
| 31 explicit EndConnectionFilter(BlimpConnection* connection); | |
| 32 | |
| 33 void set_message_handler(BlimpMessageProcessor* message_handler) { | |
| 34 message_handler_ = message_handler; | |
| 35 } | |
| 36 | |
| 37 // BlimpMessageProcessor implementation. | |
| 38 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
| 39 const net::CompletionCallback& callback) override; | |
| 40 | |
| 41 private: | |
| 42 // Owning BlimpConnection, on which to call OnConnectionError. | |
| 43 BlimpConnection* connection_; | |
| 44 | |
| 45 // Caller-provided message handler to forward non-EndConnection messages to. | |
| 46 BlimpMessageProcessor* message_handler_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(EndConnectionFilter); | |
| 49 }; | |
| 50 | |
| 51 BlimpConnection::EndConnectionFilter::EndConnectionFilter( | |
| 52 BlimpConnection* connection) | |
| 53 : connection_(connection), message_handler_(nullptr) {} | |
| 54 | |
| 55 void BlimpConnection::EndConnectionFilter::ProcessMessage( | |
| 56 std::unique_ptr<BlimpMessage> message, | |
| 57 const net::CompletionCallback& callback) { | |
| 58 if (message->has_protocol_control() && | |
| 59 message->protocol_control().has_end_connection()) { | |
| 60 // Report the EndConnection reason to connection error observers. | |
| 61 connection_->OnConnectionError( | |
| 62 message->protocol_control().end_connection().reason()); | |
| 63 | |
| 64 // Caller must ensure |callback| safe to call after OnConnectionError. | |
| 65 callback.Run(message->protocol_control().end_connection().reason()); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 message_handler_->ProcessMessage(std::move(message), callback); | |
| 70 } | |
| 71 | |
| 72 BlimpConnection::BlimpConnection() | |
| 73 : end_connection_filter_(new EndConnectionFilter(this)) {} | |
| 74 | |
| 75 BlimpConnection::~BlimpConnection() { | |
| 76 VLOG(1) << "BlimpConnection destroyed."; | |
| 77 } | |
| 78 | |
| 79 void BlimpConnection::AddConnectionErrorObserver( | |
| 80 ConnectionErrorObserver* observer) { | |
| 81 error_observers_.AddObserver(observer); | |
| 82 } | |
| 83 | |
| 84 void BlimpConnection::RemoveConnectionErrorObserver( | |
| 85 ConnectionErrorObserver* observer) { | |
| 86 error_observers_.RemoveObserver(observer); | |
| 87 } | |
| 88 | |
| 89 void BlimpConnection::AddEndConnectionProcessor( | |
| 90 BlimpMessageProcessor* processor) { | |
| 91 end_connection_filter_->set_message_handler(processor); | |
| 92 } | |
| 93 | |
| 94 BlimpMessageProcessor* BlimpConnection::GetEndConnectionProcessor() const { | |
| 95 return end_connection_filter_.get(); | |
| 96 } | |
| 97 | |
| 98 void BlimpConnection::OnConnectionError(int error) { | |
| 99 VLOG(1) << "OnConnectionError, error=" << error; | |
| 100 | |
| 101 // Propagate the error to all observers. | |
| 102 for (auto& observer : error_observers_) { | |
| 103 observer.OnConnectionError(error); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 } // namespace blimp | |
| OLD | NEW |