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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/proximity_auth/connection.cc
diff --git a/components/proximity_auth/connection.cc b/components/proximity_auth/connection.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5a35e4dd21f05cf04c9358225ea1abc05fca0274
--- /dev/null
+++ b/components/proximity_auth/connection.cc
@@ -0,0 +1,101 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/proximity_auth/connection.h"
+
+#include "base/logging.h"
+#include "components/proximity_auth/connection_observer.h"
+#include "components/proximity_auth/wire_message.h"
+
+namespace proximity_auth {
+
+Connection::Connection(const RemoteDevice& remote_device)
+ : remote_device_(remote_device),
+ status_(DISCONNECTED),
+ is_sending_message_(false) {
+}
+
+Connection::~Connection() {
+}
+
+bool Connection::IsConnected() const {
+ return status_ == CONNECTED;
+}
+
+void Connection::SendMessage(scoped_ptr<WireMessage> message) {
+ if (!IsConnected()) {
+ VLOG(1) << "Cannot send message when disconnected.";
+ return;
+ }
+
+ if (is_sending_message_) {
+ VLOG(1) << "Another message is currently in progress.";
+ return;
+ }
+
+ is_sending_message_ = true;
+ SendMessageImpl(message.Pass());
+}
+
+void Connection::AddObserver(ConnectionObserver* observer) {
+ observers_.AddObserver(observer);
+}
+
+void Connection::RemoveObserver(ConnectionObserver* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+void Connection::SetStatus(Status status) {
+ if (status_ == status)
+ return;
+
+ received_bytes_.clear();
+
+ Status old_status = status_;
+ status_ = status;
+ FOR_EACH_OBSERVER(ConnectionObserver,
+ observers_,
+ OnConnectionStatusChanged(*this, old_status, status_));
+}
+
+void Connection::OnDidSendMessage(const WireMessage& message, bool success) {
+ if (!is_sending_message_) {
+ VLOG(1) << "Send completed, but no message in progress.";
+ return;
+ }
+
+ is_sending_message_ = false;
+ FOR_EACH_OBSERVER(
+ ConnectionObserver, observers_, OnSendCompleted(*this, message, success));
+}
+
+void Connection::OnBytesReceived(const std::string& bytes) {
+ if (!IsConnected()) {
+ VLOG(1) << "Received bytes, but not connected.";
+ return;
+ }
+
+ received_bytes_ += bytes;
+ if (HasReceivedCompleteMessage()) {
+ scoped_ptr<WireMessage> message = DeserializeWireMessage();
+ if (message) {
+ FOR_EACH_OBSERVER(
+ ConnectionObserver, observers_, OnMessageReceived(*this, *message));
+ }
+
+ // Whether the message was parsed successfully or not, clear the
+ // |received_bytes_| buffer.
+ received_bytes_.clear();
+ }
+}
+
+bool Connection::HasReceivedCompleteMessage() {
+ return WireMessage::IsCompleteMessage(received_bytes_);
+}
+
+scoped_ptr<WireMessage> Connection::DeserializeWireMessage() {
+ return WireMessage::Deserialize(received_bytes_);
+}
+
+} // namespace proximity_auth
« 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