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 3 // found in the LICENSE file. 4 5 #include "components/proximity_auth/connection.h" 6 7 #include "base/logging.h" 8 #include "components/proximity_auth/connection_observer.h" 9 #include "components/proximity_auth/wire_message.h" 10 11 namespace proximity_auth { 12 13 Connection::Connection(const RemoteDevice& remote_device) 14 : remote_device_(remote_device), 15 status_(DISCONNECTED), 16 is_sending_message_(false) { 17 } 18 19 Connection::~Connection() { 20 } 21 22 bool Connection::IsConnected() const { 23 return status_ == CONNECTED; 24 } 25 26 void Connection::SendMessage(scoped_ptr<WireMessage> message) { 27 if (!IsConnected()) { 28 VLOG(1) << "Cannot send message when disconnected."; 29 return; 30 } 31 32 if (is_sending_message_) { 33 VLOG(1) << "Another message is currently in progress."; 34 return; 35 } 36 37 is_sending_message_ = true; 38 SendMessageImpl(message.Pass()); 39 } 40 41 void Connection::AddObserver(ConnectionObserver* observer) { 42 observers_.AddObserver(observer); 43 } 44 45 void Connection::RemoveObserver(ConnectionObserver* observer) { 46 observers_.RemoveObserver(observer); 47 } 48 49 void Connection::SetStatus(Status status) { 50 if (status_ == status) 51 return; 52 53 received_bytes_.clear(); 54 55 Status old_status = status_; 56 status_ = status; 57 FOR_EACH_OBSERVER(ConnectionObserver, 58 observers_, 59 OnConnectionStatusChanged(*this, old_status, status_)); 60 } 61 62 void Connection::OnDidSendMessage(const WireMessage& message, bool success) { 63 if (!is_sending_message_) { 64 VLOG(1) << "Send completed, but no message in progress."; 65 return; 66 } 67 68 is_sending_message_ = false; 69 FOR_EACH_OBSERVER( 70 ConnectionObserver, observers_, OnSendCompleted(*this, message, success)); 71 } 72 73 void Connection::OnBytesReceived(const std::string& bytes) { 74 if (!IsConnected()) { 75 VLOG(1) << "Received bytes, but not connected."; 76 return; 77 } 78 79 received_bytes_ += bytes; 80 81 bool is_incomplete_message; 82 scoped_ptr<WireMessage> message = 83 DeserializeWireMessage(&is_incomplete_message); 84 if (is_incomplete_message) 85 return; 86 87 if (message) { 88 FOR_EACH_OBSERVER( 89 ConnectionObserver, observers_, OnMessageReceived(*this, *message)); 90 } 91 92 // Whether the message was parsed successfully or not, clear the 93 // |received_bytes_| buffer. 94 received_bytes_.clear(); 95 } 96 97 scoped_ptr<WireMessage> Connection::DeserializeWireMessage( 98 bool* is_incomplete_message) { 99 return WireMessage::Deserialize(received_bytes_, is_incomplete_message); 100 } 101 102 } // namespace proximity_auth 103