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