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

Unified Diff: chrome/browser/chromeos/arc/arc_auth_code_fetcher.cc

Issue 2498363002: Remove delegates from ArcAuthCodeFetcher and ArcAuthContext. (Closed)
Patch Set: rebase Created 4 years, 1 month 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
Index: chrome/browser/chromeos/arc/arc_auth_code_fetcher.cc
diff --git a/chrome/browser/chromeos/arc/arc_auth_code_fetcher.cc b/chrome/browser/chromeos/arc/arc_auth_code_fetcher.cc
index 2ab0774eb64249fbbff2465a10a15646eadcb63e..9d4be8b8e490e985020fa7b328f14ea8b640c242 100644
--- a/chrome/browser/chromeos/arc/arc_auth_code_fetcher.cc
+++ b/chrome/browser/chromeos/arc/arc_auth_code_fetcher.cc
@@ -4,10 +4,12 @@
#include "chrome/browser/chromeos/arc/arc_auth_code_fetcher.h"
+#include "base/bind.h"
+#include "base/callback_helpers.h"
#include "base/json/json_string_value_serializer.h"
#include "base/json/json_writer.h"
+#include "base/logging.h"
#include "base/values.h"
-#include "chrome/browser/chromeos/arc/arc_auth_code_fetcher_delegate.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
@@ -41,34 +43,45 @@ constexpr char kContentTypeJSON[] = "application/json";
} // namespace
-ArcAuthCodeFetcher::ArcAuthCodeFetcher(
- ArcAuthCodeFetcherDelegate* delegate,
- net::URLRequestContextGetter* request_context_getter,
- Profile* profile,
- const std::string& auth_endpoint)
+ArcAuthCodeFetcher::ArcAuthCodeFetcher(Profile* profile,
+ ArcAuthContext* context,
+ const std::string& auth_endpoint)
: OAuth2TokenService::Consumer(kConsumerName),
- delegate_(delegate),
- request_context_getter_(request_context_getter),
profile_(profile),
- auth_endpoint_(auth_endpoint) {
+ context_(context),
+ auth_endpoint_(auth_endpoint),
+ weak_ptr_factory_(this) {}
+
+ArcAuthCodeFetcher::~ArcAuthCodeFetcher() = default;
+
+void ArcAuthCodeFetcher::Fetch(const FetchCallback& callback) {
+ DCHECK(callback_.is_null());
+ callback_ = callback;
+
+ context_->Prepare(base::Bind(&ArcAuthCodeFetcher::OnPrepared,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void ArcAuthCodeFetcher::OnPrepared(
+ net::URLRequestContextGetter* request_context_getter) {
+ if (!request_context_getter) {
+ base::ResetAndReturn(&callback_).Run(std::string());
+ return;
+ }
+
+ DCHECK(!request_context_getter_);
+ request_context_getter_ = request_context_getter;
+
// Get token service and account ID to fetch auth tokens.
- ProfileOAuth2TokenService* const token_service =
- ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
- const SigninManagerBase* const signin_manager =
- SigninManagerFactory::GetForProfile(profile_);
- CHECK(token_service && signin_manager);
- const std::string& account_id = signin_manager->GetAuthenticatedAccountId();
- DCHECK(!account_id.empty());
+ ProfileOAuth2TokenService* const token_service = context_->token_service();
+ const std::string& account_id = context_->account_id();
DCHECK(token_service->RefreshTokenIsAvailable(account_id));
OAuth2TokenService::ScopeSet scopes;
scopes.insert(GaiaConstants::kOAuth1LoginScope);
- login_token_request_.reset(
- token_service->StartRequest(account_id, scopes, this).release());
+ login_token_request_ = token_service->StartRequest(account_id, scopes, this);
}
-ArcAuthCodeFetcher::~ArcAuthCodeFetcher() {}
-
void ArcAuthCodeFetcher::OnGetTokenSuccess(
const OAuth2TokenService::Request* request,
const std::string& access_token,
@@ -104,8 +117,7 @@ void ArcAuthCodeFetcher::OnGetTokenFailure(
const GoogleServiceAuthError& error) {
VLOG(2) << "Failed to get LST " << error.ToString() << ".";
ResetFetchers();
-
- delegate_->OnAuthCodeFailed();
+ base::ResetAndReturn(&callback_).Run(std::string());
}
void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
@@ -117,7 +129,7 @@ void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
if (response_code != net::HTTP_OK) {
VLOG(2) << "Server returned wrong response code: " << response_code << ".";
- delegate_->OnAuthCodeFailed();
+ base::ResetAndReturn(&callback_).Run(std::string());
return;
}
@@ -128,7 +140,7 @@ void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
if (!auth_code_info) {
VLOG(2) << "Unable to deserialize auth code json data: " << error_msg
<< ".";
- delegate_->OnAuthCodeFailed();
+ base::ResetAndReturn(&callback_).Run(std::string());
return;
}
@@ -136,7 +148,7 @@ void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
base::DictionaryValue::From(std::move(auth_code_info));
if (!auth_code_dictionary) {
NOTREACHED();
- delegate_->OnAuthCodeFailed();
+ base::ResetAndReturn(&callback_).Run(std::string());
return;
}
@@ -144,11 +156,11 @@ void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
if (!auth_code_dictionary->GetString(kToken, &auth_code) ||
auth_code.empty()) {
VLOG(2) << "Response does not contain auth code.";
- delegate_->OnAuthCodeFailed();
+ base::ResetAndReturn(&callback_).Run(std::string());
return;
}
- delegate_->OnAuthCodeSuccess(auth_code);
+ base::ResetAndReturn(&callback_).Run(auth_code);
}
void ArcAuthCodeFetcher::ResetFetchers() {
« no previous file with comments | « chrome/browser/chromeos/arc/arc_auth_code_fetcher.h ('k') | chrome/browser/chromeos/arc/arc_auth_code_fetcher_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698