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

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

Issue 1492643003: [Blimp Net] Add EngineAuthHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses Kevin's comments. 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_auth_handler.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "blimp/common/proto/blimp_message.pb.h"
10 #include "blimp/net/blimp_connection.h"
11 #include "blimp/net/blimp_transport.h"
12 #include "net/base/net_errors.h"
13
14 namespace blimp {
15
16 namespace {
17 // Expect authentication to be done in 10 seconds.
18 const double kAuthTimeoutDurationInSeconds = 10;
19 } // namespace
20
21 EngineAuthHandler::EngineAuthHandler(ConnectionHandler* connection_handler)
22 : connection_handler_(connection_handler) {
23 DCHECK(connection_handler_);
24 }
25
26 EngineAuthHandler::~EngineAuthHandler() {}
27
28 void EngineAuthHandler::HandleConnection(
29 scoped_ptr<BlimpConnection> connection) {
30 scoped_ptr<Authenticator> authenticator(
31 new Authenticator(std::move(connection)));
32 authenticator->Start(base::Bind(&EngineAuthHandler::OnAuthResult,
33 base::Unretained(this),
34 base::Unretained(authenticator.get())));
35 pending_auths_.push_back(std::move(authenticator));
36 }
37
38 void EngineAuthHandler::OnAuthResult(Authenticator* authenticator,
39 scoped_ptr<BlimpConnection> connection,
40 bool authenticated) {
41 if (authenticated) {
42 connection_handler_->HandleConnection(std::move(connection));
43 }
44
45 RemovePendingAuthenticator(authenticator);
46 }
47
48 void EngineAuthHandler::RemovePendingAuthenticator(
49 Authenticator* authenticator) {
50 auto it = pending_auths_.begin();
51 for (; it != pending_auths_.end(); ++it) {
52 if (it->get() == authenticator)
53 break;
54 }
55 DCHECK(it != pending_auths_.end());
56 pending_auths_.erase(it);
57 }
58
59 EngineAuthHandler::Authenticator::Authenticator(
60 scoped_ptr<BlimpConnection> connection)
61 : connection_(std::move(connection)) {
62 DCHECK(connection_);
63 }
64
65 EngineAuthHandler::Authenticator::~Authenticator() {}
66
67 void EngineAuthHandler::Authenticator::Start(
68 const AuthCallback& auth_callback) {
69 DCHECK(connection_);
Kevin M 2015/12/03 23:12:05 Not needed if connection_ is const
haibinlu 2015/12/03 23:42:21 Acknowledged.
70 auth_callback_ = auth_callback;
71 connection_->SetConnectionErrorObserver(this);
72 connection_->SetIncomingMessageProcessor(this);
73 timer_.Start(FROM_HERE,
74 base::TimeDelta::FromSecondsD(kAuthTimeoutDurationInSeconds),
75 this, &EngineAuthHandler::Authenticator::FailAuthetication);
76 }
77
78 void EngineAuthHandler::Authenticator::OnConnectionError(int error) {
79 DCHECK(connection_);
Kevin M 2015/12/03 23:12:05 Not needed
haibinlu 2015/12/03 23:42:21 Done.
80 InvokeAuthCallback(false);
Kevin M 2015/12/03 23:12:05 Log |error|? Anything we can do to interpret it or
haibinlu 2015/12/03 23:42:21 added log. but I donot think we need to propagate
81 }
82
83 void EngineAuthHandler::Authenticator::ProcessMessage(
84 scoped_ptr<BlimpMessage> message,
85 const net::CompletionCallback& callback) {
86 DCHECK(connection_);
Kevin M 2015/12/03 23:12:05 Not needed
haibinlu 2015/12/03 23:42:21 Done.
87 if (message->type() == BlimpMessage::PROTOCOL_CONTROL) {
88 // TODO(haibinlu): check client token.
89 InvokeAuthCallback(true);
90 callback.Run(net::OK);
91 } else {
92 InvokeAuthCallback(false);
93 }
94 }
95
96 void EngineAuthHandler::Authenticator::InvokeAuthCallback(bool authenticated) {
97 timer_.Stop();
98
99 if (auth_callback_.is_null()) {
Kevin M 2015/12/03 23:12:05 DCHECK for this case, there aren't any cases where
haibinlu 2015/12/03 23:42:21 Done.
100 return;
101 }
102
103 base::ResetAndReturn(&auth_callback_)
104 .Run(std::move(connection_), authenticated);
105 }
106
107 void EngineAuthHandler::Authenticator::FailAuthetication() {
108 InvokeAuthCallback(false);
109 }
110
111 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698