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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "chrome/browser/chromeos/arc/arc_auth_code_fetcher.h" 5 #include "chrome/browser/chromeos/arc/arc_auth_code_fetcher.h"
6 6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
7 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_string_value_serializer.h"
8 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/logging.h"
9 #include "base/values.h" 12 #include "base/values.h"
10 #include "chrome/browser/chromeos/arc/arc_auth_code_fetcher_delegate.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/browser/signin/signin_manager_factory.h" 14 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" 15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
14 #include "components/signin/core/account_id/account_id.h" 16 #include "components/signin/core/account_id/account_id.h"
15 #include "components/signin/core/browser/profile_oauth2_token_service.h" 17 #include "components/signin/core/browser/profile_oauth2_token_service.h"
16 #include "components/signin/core/browser/signin_manager_base.h" 18 #include "components/signin/core/browser/signin_manager_base.h"
17 #include "components/user_manager/known_user.h" 19 #include "components/user_manager/known_user.h"
18 #include "content/public/browser/browser_context.h" 20 #include "content/public/browser/browser_context.h"
19 #include "content/public/common/url_constants.h" 21 #include "content/public/common/url_constants.h"
20 #include "google_apis/gaia/gaia_auth_fetcher.h" 22 #include "google_apis/gaia/gaia_auth_fetcher.h"
(...skipping 13 matching lines...) Expand all
34 constexpr char kDeviceId[] = "device_id"; 36 constexpr char kDeviceId[] = "device_id";
35 constexpr char kDeviceType[] = "device_type"; 37 constexpr char kDeviceType[] = "device_type";
36 constexpr char kDeviceTypeArc[] = "arc_plus_plus"; 38 constexpr char kDeviceTypeArc[] = "arc_plus_plus";
37 constexpr char kLoginScopedToken[] = "login_scoped_token"; 39 constexpr char kLoginScopedToken[] = "login_scoped_token";
38 constexpr char kGetAuthCodeHeaders[] = 40 constexpr char kGetAuthCodeHeaders[] =
39 "Content-Type: application/json; charset=utf-8"; 41 "Content-Type: application/json; charset=utf-8";
40 constexpr char kContentTypeJSON[] = "application/json"; 42 constexpr char kContentTypeJSON[] = "application/json";
41 43
42 } // namespace 44 } // namespace
43 45
44 ArcAuthCodeFetcher::ArcAuthCodeFetcher( 46 ArcAuthCodeFetcher::ArcAuthCodeFetcher(Profile* profile,
45 ArcAuthCodeFetcherDelegate* delegate, 47 ArcAuthContext* context,
46 net::URLRequestContextGetter* request_context_getter, 48 const std::string& auth_endpoint)
47 Profile* profile,
48 const std::string& auth_endpoint)
49 : OAuth2TokenService::Consumer(kConsumerName), 49 : OAuth2TokenService::Consumer(kConsumerName),
50 delegate_(delegate),
51 request_context_getter_(request_context_getter),
52 profile_(profile), 50 profile_(profile),
53 auth_endpoint_(auth_endpoint) { 51 context_(context),
52 auth_endpoint_(auth_endpoint),
53 weak_ptr_factory_(this) {}
54
55 ArcAuthCodeFetcher::~ArcAuthCodeFetcher() = default;
56
57 void ArcAuthCodeFetcher::Fetch(const FetchCallback& callback) {
58 DCHECK(callback_.is_null());
59 callback_ = callback;
60
61 context_->Prepare(base::Bind(&ArcAuthCodeFetcher::OnPrepared,
62 weak_ptr_factory_.GetWeakPtr()));
63 }
64
65 void ArcAuthCodeFetcher::OnPrepared(
66 net::URLRequestContextGetter* request_context_getter) {
67 if (!request_context_getter) {
68 base::ResetAndReturn(&callback_).Run(std::string());
69 return;
70 }
71
72 DCHECK(!request_context_getter_);
73 request_context_getter_ = request_context_getter;
74
54 // Get token service and account ID to fetch auth tokens. 75 // Get token service and account ID to fetch auth tokens.
55 ProfileOAuth2TokenService* const token_service = 76 ProfileOAuth2TokenService* const token_service = context_->token_service();
56 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); 77 const std::string& account_id = context_->account_id();
57 const SigninManagerBase* const signin_manager =
58 SigninManagerFactory::GetForProfile(profile_);
59 CHECK(token_service && signin_manager);
60 const std::string& account_id = signin_manager->GetAuthenticatedAccountId();
61 DCHECK(!account_id.empty());
62 DCHECK(token_service->RefreshTokenIsAvailable(account_id)); 78 DCHECK(token_service->RefreshTokenIsAvailable(account_id));
63 79
64 OAuth2TokenService::ScopeSet scopes; 80 OAuth2TokenService::ScopeSet scopes;
65 scopes.insert(GaiaConstants::kOAuth1LoginScope); 81 scopes.insert(GaiaConstants::kOAuth1LoginScope);
66 login_token_request_.reset( 82 login_token_request_ = token_service->StartRequest(account_id, scopes, this);
67 token_service->StartRequest(account_id, scopes, this).release());
68 } 83 }
69 84
70 ArcAuthCodeFetcher::~ArcAuthCodeFetcher() {}
71
72 void ArcAuthCodeFetcher::OnGetTokenSuccess( 85 void ArcAuthCodeFetcher::OnGetTokenSuccess(
73 const OAuth2TokenService::Request* request, 86 const OAuth2TokenService::Request* request,
74 const std::string& access_token, 87 const std::string& access_token,
75 const base::Time& expiration_time) { 88 const base::Time& expiration_time) {
76 ResetFetchers(); 89 ResetFetchers();
77 90
78 const std::string device_id = user_manager::known_user::GetDeviceId( 91 const std::string device_id = user_manager::known_user::GetDeviceId(
79 multi_user_util::GetAccountIdFromProfile(profile_)); 92 multi_user_util::GetAccountIdFromProfile(profile_));
80 DCHECK(!device_id.empty()); 93 DCHECK(!device_id.empty());
81 94
(...skipping 15 matching lines...) Expand all
97 kGetAuthCodeNetworkRetry); 110 kGetAuthCodeNetworkRetry);
98 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders); 111 auth_code_fetcher_->SetExtraRequestHeaders(kGetAuthCodeHeaders);
99 auth_code_fetcher_->Start(); 112 auth_code_fetcher_->Start();
100 } 113 }
101 114
102 void ArcAuthCodeFetcher::OnGetTokenFailure( 115 void ArcAuthCodeFetcher::OnGetTokenFailure(
103 const OAuth2TokenService::Request* request, 116 const OAuth2TokenService::Request* request,
104 const GoogleServiceAuthError& error) { 117 const GoogleServiceAuthError& error) {
105 VLOG(2) << "Failed to get LST " << error.ToString() << "."; 118 VLOG(2) << "Failed to get LST " << error.ToString() << ".";
106 ResetFetchers(); 119 ResetFetchers();
107 120 base::ResetAndReturn(&callback_).Run(std::string());
108 delegate_->OnAuthCodeFailed();
109 } 121 }
110 122
111 void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) { 123 void ArcAuthCodeFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
112 const int response_code = source->GetResponseCode(); 124 const int response_code = source->GetResponseCode();
113 std::string json_string; 125 std::string json_string;
114 source->GetResponseAsString(&json_string); 126 source->GetResponseAsString(&json_string);
115 127
116 ResetFetchers(); 128 ResetFetchers();
117 129
118 if (response_code != net::HTTP_OK) { 130 if (response_code != net::HTTP_OK) {
119 VLOG(2) << "Server returned wrong response code: " << response_code << "."; 131 VLOG(2) << "Server returned wrong response code: " << response_code << ".";
120 delegate_->OnAuthCodeFailed(); 132 base::ResetAndReturn(&callback_).Run(std::string());
121 return; 133 return;
122 } 134 }
123 135
124 JSONStringValueDeserializer deserializer(json_string); 136 JSONStringValueDeserializer deserializer(json_string);
125 std::string error_msg; 137 std::string error_msg;
126 std::unique_ptr<base::Value> auth_code_info = 138 std::unique_ptr<base::Value> auth_code_info =
127 deserializer.Deserialize(nullptr, &error_msg); 139 deserializer.Deserialize(nullptr, &error_msg);
128 if (!auth_code_info) { 140 if (!auth_code_info) {
129 VLOG(2) << "Unable to deserialize auth code json data: " << error_msg 141 VLOG(2) << "Unable to deserialize auth code json data: " << error_msg
130 << "."; 142 << ".";
131 delegate_->OnAuthCodeFailed(); 143 base::ResetAndReturn(&callback_).Run(std::string());
132 return; 144 return;
133 } 145 }
134 146
135 std::unique_ptr<base::DictionaryValue> auth_code_dictionary = 147 std::unique_ptr<base::DictionaryValue> auth_code_dictionary =
136 base::DictionaryValue::From(std::move(auth_code_info)); 148 base::DictionaryValue::From(std::move(auth_code_info));
137 if (!auth_code_dictionary) { 149 if (!auth_code_dictionary) {
138 NOTREACHED(); 150 NOTREACHED();
139 delegate_->OnAuthCodeFailed(); 151 base::ResetAndReturn(&callback_).Run(std::string());
140 return; 152 return;
141 } 153 }
142 154
143 std::string auth_code; 155 std::string auth_code;
144 if (!auth_code_dictionary->GetString(kToken, &auth_code) || 156 if (!auth_code_dictionary->GetString(kToken, &auth_code) ||
145 auth_code.empty()) { 157 auth_code.empty()) {
146 VLOG(2) << "Response does not contain auth code."; 158 VLOG(2) << "Response does not contain auth code.";
147 delegate_->OnAuthCodeFailed(); 159 base::ResetAndReturn(&callback_).Run(std::string());
148 return; 160 return;
149 } 161 }
150 162
151 delegate_->OnAuthCodeSuccess(auth_code); 163 base::ResetAndReturn(&callback_).Run(auth_code);
152 } 164 }
153 165
154 void ArcAuthCodeFetcher::ResetFetchers() { 166 void ArcAuthCodeFetcher::ResetFetchers() {
155 login_token_request_.reset(); 167 login_token_request_.reset();
156 auth_code_fetcher_.reset(); 168 auth_code_fetcher_.reset();
157 } 169 }
158 170
159 } // namespace arc 171 } // namespace arc
OLDNEW
« 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