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

Side by Side Diff: blimp/net/engine_authentication_handler.cc

Issue 1492643003: [Blimp Net] Add EngineAuthHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses comments and adds unit tests Created 5 years 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
OLDNEW
(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/engine_authentication_handler.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "base/timer/timer.h"
10 #include "blimp/common/proto/blimp_message.pb.h"
11 #include "blimp/net/blimp_connection.h"
12 #include "blimp/net/blimp_message_processor.h"
13 #include "blimp/net/blimp_transport.h"
14 #include "blimp/net/connection_error_observer.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_errors.h"
17
18 namespace blimp {
19
20 namespace {
21 // Expect authentication to be done in 10 seconds.
Wez 2015/12/08 00:31:41 nit: I think you can be more explicit "Expect Clie
haibinlu 2015/12/08 01:54:50 Done.
22 const double kAuthTimeoutDurationInSeconds = 10;
23
24 // Authenticates one connection. It deletes itself when
25 // * the connection is authenticated and passed to |connection_handler|.
26 // * the connection gets into error state.
Wez 2015/12/08 00:31:41 nit: into an error state
haibinlu 2015/12/08 01:54:50 Done.
27 // * timeout on waiting for auth message.
Wez 2015/12/08 00:31:41 nit: the auth message does not arrive within a rea
haibinlu 2015/12/08 01:54:50 Done.
28 class Authenticator : public ConnectionErrorObserver,
29 public BlimpMessageProcessor {
30 public:
31 explicit Authenticator(scoped_ptr<BlimpConnection> connection,
32 base::WeakPtr<ConnectionHandler> connection_handler);
33 ~Authenticator() override;
34
35 private:
36 // Processes authentication result and delete this object.
Wez 2015/12/08 00:31:41 nit: delete->deletes nit: You can also write "thi
haibinlu 2015/12/08 01:54:50 Done.
37 void OnConnectionAuthenticated(bool authenticated);
38
39 // Invoked on timeout while waiting for auth message.
Wez 2015/12/08 00:31:41 nit: Reword this similarly to the processes one, a
haibinlu 2015/12/08 01:54:50 Done.
40 void OnAuthenticationTimeout();
41
42 // ConnectionErrorObserver implementation.
43 void OnConnectionError(int error) override;
44
45 // BlimpMessageProcessor implementation.
46 void ProcessMessage(scoped_ptr<BlimpMessage> message,
47 const net::CompletionCallback& callback) override;
48
49 // The connection to be authenticated.
50 scoped_ptr<BlimpConnection> connection_;
51
52 // Handler for authenticated connections.
Wez 2015/12/08 00:31:41 nit: Handler to pass successfully authentication c
haibinlu 2015/12/08 01:54:50 Done.
53 base::WeakPtr<ConnectionHandler> connection_handler_;
54
55 // A timer to fail authentication on timeout.
56 base::OneShotTimer timer_;
Wez 2015/12/08 00:31:41 nit: timeout_ or timeout_timer_?
haibinlu 2015/12/08 01:54:50 Done.
57
58 DISALLOW_COPY_AND_ASSIGN(Authenticator);
59 };
60
61 Authenticator::Authenticator(
62 scoped_ptr<BlimpConnection> connection,
63 base::WeakPtr<ConnectionHandler> connection_handler)
64 : connection_(std::move(connection)),
65 connection_handler_(connection_handler) {
66 connection_->SetConnectionErrorObserver(this);
67 connection_->SetIncomingMessageProcessor(this);
68 timer_.Start(FROM_HERE,
69 base::TimeDelta::FromSecondsD(kAuthTimeoutDurationInSeconds),
Wez 2015/12/08 00:31:41 nit: Why are we using FromSecondsD() here, rather
haibinlu 2015/12/08 01:54:50 No, we do not right now. Kevin prefered FromSecond
70 this, &Authenticator::OnAuthenticationTimeout);
71 }
72
73 Authenticator::~Authenticator() {}
74
75 void Authenticator::OnConnectionAuthenticated(bool authenticated) {
76 timer_.Stop();
Wez 2015/12/08 00:31:41 nit: We are about to delete |this|, which will del
haibinlu 2015/12/08 01:54:50 right. no need to stop timer here.
77 connection_->SetIncomingMessageProcessor(nullptr);
78 connection_->SetConnectionErrorObserver(nullptr);
79
80 if (authenticated) {
81 connection_handler_->HandleConnection(std::move(connection_));
82 }
83
84 delete this;
85 }
86
87 void Authenticator::OnAuthenticationTimeout() {
88 DVLOG(1) << "Connection authentication timeout";
89 OnConnectionAuthenticated(false);
90 }
91
92 void Authenticator::OnConnectionError(int error) {
93 DVLOG(1) << "Connection error before authenticated";
Wez 2015/12/08 00:31:41 nit: Log the actual error code.
haibinlu 2015/12/08 01:54:50 Done.
94 OnConnectionAuthenticated(false);
95 }
96
97 void Authenticator::ProcessMessage(scoped_ptr<BlimpMessage> message,
98 const net::CompletionCallback& callback) {
99 if (message->type() == BlimpMessage::PROTOCOL_CONTROL) {
100 // TODO(haibinlu): check client token.
101 OnConnectionAuthenticated(true);
102 } else {
103 OnConnectionAuthenticated(false);
Wez 2015/12/08 00:31:41 nit: Perhaps a DVLOG(1) here, as you have above, w
haibinlu 2015/12/08 01:54:50 Done.
104 }
105
106 callback.Run(net::OK);
Wez 2015/12/08 00:31:41 nit: We should update MessageProcessor (separate C
haibinlu 2015/12/08 01:54:50 Acknowledged.
107 }
108
109 } // namespace
110
111 EngineAuthenticationHandler::EngineAuthenticationHandler(
112 ConnectionHandler* connection_handler)
113 : connection_handler_weak_factory_(connection_handler) {}
114
115 EngineAuthenticationHandler::~EngineAuthenticationHandler() {}
116
117 void EngineAuthenticationHandler::HandleConnection(
118 scoped_ptr<BlimpConnection> connection) {
119 // Authenticator manages its own lifetime.
120 new Authenticator(std::move(connection),
121 connection_handler_weak_factory_.GetWeakPtr());
122 }
123
124 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698