Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: components/proximity_auth/connection.cc

Issue 538843002: [EasyUnlock] Port Connection class to native code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Android test compile failures Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/proximity_auth/connection.h ('k') | components/proximity_auth/connection_observer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "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 if (HasReceivedCompleteMessage()) {
81 scoped_ptr<WireMessage> message = DeserializeWireMessage();
82 if (message) {
83 FOR_EACH_OBSERVER(
84 ConnectionObserver, observers_, OnMessageReceived(*this, *message));
85 }
86
87 // Whether the message was parsed successfully or not, clear the
88 // |received_bytes_| buffer.
89 received_bytes_.clear();
90 }
91 }
92
93 bool Connection::HasReceivedCompleteMessage() {
94 return WireMessage::IsCompleteMessage(received_bytes_);
95 }
96
97 scoped_ptr<WireMessage> Connection::DeserializeWireMessage() {
98 return WireMessage::Deserialize(received_bytes_);
99 }
100
101 } // namespace proximity_auth
OLDNEW
« no previous file with comments | « components/proximity_auth/connection.h ('k') | components/proximity_auth/connection_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698