Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/fake_authenticator.h" | 5 #include "remoting/protocol/fake_authenticator.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "net/base/io_buffer.h" | |
| 9 #include "net/socket/stream_socket.h" | 10 #include "net/socket/stream_socket.h" |
| 10 #include "remoting/base/constants.h" | 11 #include "remoting/base/constants.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 13 | 14 |
| 14 namespace remoting { | 15 namespace remoting { |
| 15 namespace protocol { | 16 namespace protocol { |
| 16 | 17 |
| 17 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) | 18 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) |
| 18 : accept_(accept), | 19 : accept_(accept), |
| 19 async_(async), | 20 async_(async), |
| 21 error_(net::OK), | |
| 22 read_(false), | |
| 23 written_(false), | |
| 20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 24 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 21 } | 25 } |
| 22 | 26 |
| 23 FakeChannelAuthenticator::~FakeChannelAuthenticator() { | 27 FakeChannelAuthenticator::~FakeChannelAuthenticator() { |
| 24 } | 28 } |
| 25 | 29 |
| 26 void FakeChannelAuthenticator::SecureAndAuthenticate( | 30 void FakeChannelAuthenticator::SecureAndAuthenticate( |
| 27 scoped_ptr<net::StreamSocket> socket, | 31 scoped_ptr<net::StreamSocket> socket, |
| 28 const DoneCallback& done_callback) { | 32 const DoneCallback& done_callback) { |
| 29 net::Error error; | 33 socket_ = socket.Pass(); |
| 30 | 34 |
| 31 if (accept_) { | 35 if (accept_) { |
| 32 error = net::OK; | 36 error_ = net::OK; |
| 33 } else { | 37 } else { |
| 34 error = net::ERR_FAILED; | 38 error_ = net::ERR_FAILED; |
|
Wez
2012/02/22 22:51:38
nit: Now that |error_| is a member, you could rena
Sergey Ulanov
2012/02/22 23:36:32
Done.
| |
| 35 socket.reset(); | |
| 36 } | 39 } |
| 37 | 40 |
| 38 if (async_) { | 41 if (async_) { |
| 39 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 42 done_callback_ = done_callback; |
| 40 &FakeChannelAuthenticator::CallCallback, weak_factory_.GetWeakPtr(), | 43 |
| 41 done_callback, error, base::Passed(&socket))); | 44 scoped_refptr<net::IOBuffer> write_buf = new net::IOBuffer(1); |
| 45 write_buf->data()[0] = 0; | |
| 46 int result = socket_->Write( | |
| 47 write_buf, 1, base::Bind(&FakeChannelAuthenticator::OnAuthBytesWritten, | |
| 48 base::Unretained(this))); | |
|
Wez
2012/02/22 22:51:38
Why not continue to use |weak_factory_| here?
Sergey Ulanov
2012/02/22 23:36:32
It is not necessary because socket_ will be delete
Wez
2012/02/23 19:33:42
The old code was using a |weak_factory_| member, s
Sergey Ulanov
2012/02/23 22:10:06
You are right, changed it to use weak_factory_
| |
| 49 if (result != net::ERR_IO_PENDING) | |
| 50 OnAuthBytesWritten(result); | |
|
Wez
2012/02/22 22:51:38
nit: At this point |read_| is never true, so we ca
Sergey Ulanov
2012/02/22 23:36:32
Added comment.
| |
| 51 | |
| 52 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(1); | |
| 53 result = socket_->Read( | |
| 54 read_buf, 1, base::Bind(&FakeChannelAuthenticator::OnAuthBytesRead, | |
| 55 base::Unretained(this))); | |
| 56 if (result != net::ERR_IO_PENDING) | |
| 57 OnAuthBytesRead(result); | |
| 42 } else { | 58 } else { |
| 43 done_callback.Run(error, socket.Pass()); | 59 if (error_ != net::OK) |
| 60 socket_.reset(); | |
| 61 done_callback.Run(error_, socket_.Pass()); | |
| 44 } | 62 } |
| 45 } | 63 } |
| 46 | 64 |
| 47 void FakeChannelAuthenticator::CallCallback( | 65 void FakeChannelAuthenticator::OnAuthBytesWritten(int result) { |
| 48 const DoneCallback& done_callback, | 66 EXPECT_EQ(1, result); |
| 49 net::Error error, | 67 EXPECT_FALSE(written_); |
| 50 scoped_ptr<net::StreamSocket> socket) { | 68 written_ = true; |
| 51 done_callback.Run(error, socket.Pass()); | 69 if (read_) |
| 70 done_callback_.Run(error_, socket_.Pass()); | |
| 71 } | |
| 72 | |
| 73 void FakeChannelAuthenticator::OnAuthBytesRead(int result) { | |
| 74 EXPECT_EQ(1, result); | |
| 75 EXPECT_FALSE(read_); | |
| 76 read_ = true; | |
| 77 if (written_) | |
| 78 done_callback_.Run(error_, socket_.Pass()); | |
| 52 } | 79 } |
| 53 | 80 |
| 54 FakeAuthenticator::FakeAuthenticator( | 81 FakeAuthenticator::FakeAuthenticator( |
| 55 Type type, int round_trips, Action action, bool async) | 82 Type type, int round_trips, Action action, bool async) |
| 56 : type_(type), | 83 : type_(type), |
| 57 round_trips_(round_trips), | 84 round_trips_(round_trips), |
| 58 action_(action), | 85 action_(action), |
| 59 async_(async), | 86 async_(async), |
| 60 messages_(0) { | 87 messages_(0) { |
| 61 } | 88 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 | 161 |
| 135 scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator( | 162 scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator( |
| 136 const std::string& remote_jid, | 163 const std::string& remote_jid, |
| 137 const buzz::XmlElement* first_message) { | 164 const buzz::XmlElement* first_message) { |
| 138 return scoped_ptr<Authenticator>(new FakeAuthenticator( | 165 return scoped_ptr<Authenticator>(new FakeAuthenticator( |
| 139 FakeAuthenticator::HOST, round_trips_, action_, async_)); | 166 FakeAuthenticator::HOST, round_trips_, action_, async_)); |
| 140 } | 167 } |
| 141 | 168 |
| 142 } // namespace protocol | 169 } // namespace protocol |
| 143 } // namespace remoting | 170 } // namespace remoting |
| OLD | NEW |