Skip to content

Commit

Permalink
base::Bind: Convert most of net/http.
Browse files Browse the repository at this point in the history
BUG=none
TEST=none
R=csilv

Review URL: http://codereview.chromium.org/8990001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@115220 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jhawkins@chromium.org committed Dec 20, 2011
1 parent 5fc2429 commit 49639fa
Show file tree
Hide file tree
Showing 86 changed files with 1,519 additions and 1,467 deletions.
2 changes: 1 addition & 1 deletion net/http/http_auth_cache_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MockAuthHandler : public HttpAuthHandler {

virtual int GenerateAuthTokenImpl(const AuthCredentials*,
const HttpRequestInfo*,
OldCompletionCallback* callback,
const CompletionCallback& callback,
std::string* auth_token) {
*auth_token = "mock-credentials";
return OK;
Expand Down
34 changes: 16 additions & 18 deletions net/http/http_auth_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "net/http/http_auth_controller.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
#include "base/threading/platform_thread.h"
Expand Down Expand Up @@ -161,20 +163,16 @@ HttpAuthController::HttpAuthController(
embedded_identity_used_(false),
default_credentials_used_(false),
http_auth_cache_(http_auth_cache),
http_auth_handler_factory_(http_auth_handler_factory),
ALLOW_THIS_IN_INITIALIZER_LIST(
io_callback_(this, &HttpAuthController::OnIOComplete)),
user_callback_(NULL) {
http_auth_handler_factory_(http_auth_handler_factory) {
}

HttpAuthController::~HttpAuthController() {
DCHECK(CalledOnValidThread());
user_callback_ = NULL;
}

int HttpAuthController::MaybeGenerateAuthToken(const HttpRequestInfo* request,
OldCompletionCallback* callback,
const BoundNetLog& net_log) {
int HttpAuthController::MaybeGenerateAuthToken(
const HttpRequestInfo* request, const CompletionCallback& callback,
const BoundNetLog& net_log) {
DCHECK(CalledOnValidThread());
bool needs_auth = HaveAuth() || SelectPreemptiveAuth(net_log);
if (!needs_auth)
Expand All @@ -183,15 +181,15 @@ int HttpAuthController::MaybeGenerateAuthToken(const HttpRequestInfo* request,
if (identity_.source != HttpAuth::IDENT_SRC_DEFAULT_CREDENTIALS)
credentials = &identity_.credentials;
DCHECK(auth_token_.empty());
DCHECK(NULL == user_callback_);
int rv = handler_->GenerateAuthToken(credentials,
request,
&io_callback_,
&auth_token_);
DCHECK(callback_.is_null());
int rv = handler_->GenerateAuthToken(
credentials, request,
base::Bind(&HttpAuthController::OnIOComplete, base::Unretained(this)),
&auth_token_);
if (DisableOnAuthHandlerResult(rv))
rv = OK;
if (rv == ERR_IO_PENDING)
user_callback_ = callback;
callback_ = callback;
else
OnIOComplete(rv);
return rv;
Expand Down Expand Up @@ -543,10 +541,10 @@ void HttpAuthController::OnIOComplete(int result) {
DCHECK(CalledOnValidThread());
if (DisableOnAuthHandlerResult(result))
result = OK;
if (user_callback_) {
OldCompletionCallback* c = user_callback_;
user_callback_ = NULL;
c->Run(result);
if (!callback_.is_null()) {
CompletionCallback c = callback_;
callback_.Reset();
c.Run(result);
}
}

Expand Down
5 changes: 2 additions & 3 deletions net/http/http_auth_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class NET_EXPORT_PRIVATE HttpAuthController
// a token is correctly generated synchronously, as well as when no tokens
// were necessary.
virtual int MaybeGenerateAuthToken(const HttpRequestInfo* request,
OldCompletionCallback* callback,
const CompletionCallback& callback,
const BoundNetLog& net_log);

// Adds either the proxy auth header, or the origin server auth header,
Expand Down Expand Up @@ -163,8 +163,7 @@ class NET_EXPORT_PRIVATE HttpAuthController

std::set<HttpAuth::Scheme> disabled_schemes_;

OldCompletionCallbackImpl<HttpAuthController> io_callback_;
OldCompletionCallback* user_callback_;
CompletionCallback callback_;
};

} // namespace net
Expand Down
12 changes: 7 additions & 5 deletions net/http/http_auth_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ void RunSingleRoundAuthTest(HandlerRunMode run_mode,
controller->ResetAuth(AuthCredentials());
EXPECT_TRUE(controller->HaveAuth());

TestOldCompletionCallback callback;
TestCompletionCallback callback;
EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING:
expected_controller_rv,
controller->MaybeGenerateAuthToken(&request, &callback,
controller->MaybeGenerateAuthToken(&request, callback.callback(),
dummy_log));
if (run_mode == RUN_HANDLER_ASYNC)
EXPECT_EQ(expected_controller_rv, callback.WaitForResult());
Expand Down Expand Up @@ -144,7 +144,7 @@ TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) {

virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials,
const HttpRequestInfo* request,
OldCompletionCallback* callback,
const CompletionCallback& callback,
std::string* auth_token) OVERRIDE {
int result =
HttpAuthHandlerMock::GenerateAuthTokenImpl(credentials,
Expand Down Expand Up @@ -215,7 +215,8 @@ TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) {
EXPECT_TRUE(controller->HaveAuth());

// Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler.
EXPECT_EQ(OK, controller->MaybeGenerateAuthToken(&request, NULL, dummy_log));
EXPECT_EQ(OK, controller->MaybeGenerateAuthToken(
&request, CompletionCallback(), dummy_log));
controller->AddAuthorizationHeader(&request_headers);

// Once a token is generated, simulate the receipt of a server response
Expand All @@ -229,7 +230,8 @@ TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) {
EXPECT_FALSE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_BASIC));

// Should only succeed if we are using the AUTH_SCHEME_BASIC MockHandler.
EXPECT_EQ(OK, controller->MaybeGenerateAuthToken(&request, NULL, dummy_log));
EXPECT_EQ(OK, controller->MaybeGenerateAuthToken(
&request, CompletionCallback(), dummy_log));
}

} // namespace net
34 changes: 17 additions & 17 deletions net/http/http_auth_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "net/http/http_auth_handler.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "net/base/net_errors.h"

Expand All @@ -13,11 +15,7 @@ HttpAuthHandler::HttpAuthHandler()
: auth_scheme_(HttpAuth::AUTH_SCHEME_MAX),
score_(-1),
target_(HttpAuth::AUTH_NONE),
properties_(-1),
original_callback_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(
wrapper_callback_(
this, &HttpAuthHandler::OnGenerateAuthTokenComplete)) {
properties_(-1) {
}

HttpAuthHandler::~HttpAuthHandler() {
Expand Down Expand Up @@ -62,19 +60,21 @@ NetLog::EventType EventTypeFromAuthTarget(HttpAuth::Target target) {

} // namespace

int HttpAuthHandler::GenerateAuthToken(const AuthCredentials* credentials,
const HttpRequestInfo* request,
OldCompletionCallback* callback,
std::string* auth_token) {
int HttpAuthHandler::GenerateAuthToken(
const AuthCredentials* credentials, const HttpRequestInfo* request,
const CompletionCallback& callback, std::string* auth_token) {
// TODO(cbentzel): Enforce non-NULL callback after cleaning up SocketStream.
DCHECK(request);
DCHECK(credentials != NULL || AllowsDefaultCredentials());
DCHECK(auth_token != NULL);
DCHECK(original_callback_ == NULL);
original_callback_ = callback;
DCHECK(callback_.is_null());
callback_ = callback;
net_log_.BeginEvent(EventTypeFromAuthTarget(target_), NULL);
int rv = GenerateAuthTokenImpl(credentials, request,
&wrapper_callback_, auth_token);
int rv = GenerateAuthTokenImpl(
credentials, request,
base::Bind(&HttpAuthHandler::OnGenerateAuthTokenComplete,
base::Unretained(this)),
auth_token);
if (rv != ERR_IO_PENDING)
FinishGenerateAuthToken();
return rv;
Expand All @@ -93,16 +93,16 @@ bool HttpAuthHandler::AllowsExplicitCredentials() {
}

void HttpAuthHandler::OnGenerateAuthTokenComplete(int rv) {
OldCompletionCallback* callback = original_callback_;
CompletionCallback callback = callback_;
FinishGenerateAuthToken();
if (callback)
callback->Run(rv);
if (!callback.is_null())
callback.Run(rv);
}

void HttpAuthHandler::FinishGenerateAuthToken() {
// TOOD(cbentzel): Should this be done in OK case only?
net_log_.EndEvent(EventTypeFromAuthTarget(target_), NULL);
original_callback_ = NULL;
callback_.Reset();
}

} // namespace net
17 changes: 8 additions & 9 deletions net/http/http_auth_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class NET_EXPORT_PRIVATE HttpAuthHandler {

// Initializes the handler using a challenge issued by a server.
// |challenge| must be non-NULL and have already tokenized the
// authentication scheme, but none of the tokens occuring after the
// authentication scheme, but none of the tokens occurring after the
// authentication scheme. |target| and |origin| are both stored
// for later use, and are not part of the initial challenge.
bool InitFromChallenge(HttpAuth::ChallengeTokenizer* challenge,
Expand All @@ -46,7 +46,7 @@ class NET_EXPORT_PRIVATE HttpAuthHandler {
// be made with a different nonce provided in the challenge.
//
// |challenge| must be non-NULL and have already tokenized the
// authentication scheme, but none of the tokens occuring after the
// authentication scheme, but none of the tokens occurring after the
// authentication scheme.
virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
HttpAuth::ChallengeTokenizer* challenge) = 0;
Expand All @@ -73,7 +73,7 @@ class NET_EXPORT_PRIVATE HttpAuthHandler {
// token, and the value of |*auth_token| is unspecified.
int GenerateAuthToken(const AuthCredentials* credentials,
const HttpRequestInfo* request,
OldCompletionCallback* callback,
const CompletionCallback& callback,
std::string* auth_token);

// The authentication scheme as an enumerated value.
Expand Down Expand Up @@ -148,18 +148,18 @@ class NET_EXPORT_PRIVATE HttpAuthHandler {

// Initializes the handler using a challenge issued by a server.
// |challenge| must be non-NULL and have already tokenized the
// authentication scheme, but none of the tokens occuring after the
// authentication scheme, but none of the tokens occurring after the
// authentication scheme.
// Implementations are expcted to initialize the following members:
// Implementations are expected to initialize the following members:
// scheme_, realm_, score_, properties_
virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) = 0;

// |GenerateAuthTokenImpl()} is the auth-scheme specific implementation
// of generating the next auth token. Callers sohuld use |GenerateAuthToken()|
// of generating the next auth token. Callers should use |GenerateAuthToken()|
// which will in turn call |GenerateAuthTokenImpl()|
virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials,
const HttpRequestInfo* request,
OldCompletionCallback* callback,
const CompletionCallback& callback,
std::string* auth_token) = 0;

// The auth-scheme as an enumerated value.
Expand Down Expand Up @@ -191,8 +191,7 @@ class NET_EXPORT_PRIVATE HttpAuthHandler {
void OnGenerateAuthTokenComplete(int rv);
void FinishGenerateAuthToken();

OldCompletionCallback* original_callback_;
OldCompletionCallbackImpl<HttpAuthHandler> wrapper_callback_;
CompletionCallback callback_;
};

} // namespace net
Expand Down
6 changes: 2 additions & 4 deletions net/http/http_auth_handler_basic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ HttpAuth::AuthorizationResult HttpAuthHandlerBasic::HandleAnotherChallenge(
}

int HttpAuthHandlerBasic::GenerateAuthTokenImpl(
const AuthCredentials* credentials,
const HttpRequestInfo*,
OldCompletionCallback*,
std::string* auth_token) {
const AuthCredentials* credentials, const HttpRequestInfo*,
const CompletionCallback&, std::string* auth_token) {
DCHECK(credentials);
// TODO(eroman): is this the right encoding of username/password?
std::string base64_username_password;
Expand Down
2 changes: 1 addition & 1 deletion net/http/http_auth_handler_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class NET_EXPORT_PRIVATE HttpAuthHandlerBasic : public HttpAuthHandler {

virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials,
const HttpRequestInfo* request,
OldCompletionCallback* callback,
const CompletionCallback& callback,
std::string* auth_token) OVERRIDE;

private:
Expand Down
2 changes: 1 addition & 1 deletion net/http/http_auth_handler_basic_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) {
HttpRequestInfo request_info;
std::string auth_token;
int rv = basic->GenerateAuthToken(&credentials, &request_info,
NULL, &auth_token);
CompletionCallback(), &auth_token);
EXPECT_EQ(OK, rv);
EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str());
}
Expand Down
6 changes: 2 additions & 4 deletions net/http/http_auth_handler_digest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ bool HttpAuthHandlerDigest::Init(HttpAuth::ChallengeTokenizer* challenge) {
}

int HttpAuthHandlerDigest::GenerateAuthTokenImpl(
const AuthCredentials* credentials,
const HttpRequestInfo* request,
OldCompletionCallback* callback,
std::string* auth_token) {
const AuthCredentials* credentials, const HttpRequestInfo* request,
const CompletionCallback& callback, std::string* auth_token) {
// Generate a random client nonce.
std::string cnonce = nonce_generator_->GenerateNonce();

Expand Down
2 changes: 1 addition & 1 deletion net/http/http_auth_handler_digest.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class NET_EXPORT_PRIVATE HttpAuthHandlerDigest : public HttpAuthHandler {

virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials,
const HttpRequestInfo* request,
OldCompletionCallback* callback,
const CompletionCallback& callback,
std::string* auth_token) OVERRIDE;

private:
Expand Down
6 changes: 3 additions & 3 deletions net/http/http_auth_handler_digest_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ bool RespondToChallenge(HttpAuth::Target target,
// Create a token in response to the challenge.
// NOTE: HttpAuthHandlerDigest's implementation of GenerateAuthToken always
// completes synchronously. That's why this test can get away with a
// TestOldCompletionCallback without an IO thread.
TestOldCompletionCallback callback;
// TestCompletionCallback without an IO thread.
TestCompletionCallback callback;
scoped_ptr<HttpRequestInfo> request(new HttpRequestInfo());
request->url = GURL(request_url);
AuthCredentials credentials(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"));
int rv_generate = handler->GenerateAuthToken(
&credentials, request.get(), &callback, token);
&credentials, request.get(), callback.callback(), token);
if (rv_generate != OK) {
ADD_FAILURE() << "Problems generating auth token";
return false;
Expand Down
Loading

0 comments on commit 49639fa

Please sign in to comment.