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

Side by Side Diff: chrome/browser/profiles/profile_downloader.cc

Issue 566933005: Do not display lock for hosted domains (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ChromeOS test - part deux Created 6 years, 2 months 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/profiles/profile_downloader.h" 5 #include "chrome/browser/profiles/profile_downloader.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_downloader_delegate.h" 18 #include "chrome/browser/profiles/profile_downloader_delegate.h"
19 #include "chrome/browser/profiles/profile_manager.h" 19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
21 #include "chrome/browser/signin/signin_manager_factory.h" 21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "components/signin/core/browser/profile_oauth2_token_service.h" 22 #include "components/signin/core/browser/profile_oauth2_token_service.h"
23 #include "components/signin/core/browser/signin_manager.h" 23 #include "components/signin/core/browser/signin_manager.h"
24 #include "components/signin/core/common/profile_management_switches.h"
24 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
25 #include "google_apis/gaia/gaia_constants.h" 26 #include "google_apis/gaia/gaia_constants.h"
26 #include "google_apis/gaia/gaia_urls.h" 27 #include "google_apis/gaia/gaia_urls.h"
27 #include "net/base/load_flags.h" 28 #include "net/base/load_flags.h"
28 #include "net/url_request/url_fetcher.h" 29 #include "net/url_request/url_fetcher.h"
29 #include "net/url_request/url_request_status.h" 30 #include "net/url_request/url_request_status.h"
30 #include "skia/ext/image_operations.h" 31 #include "skia/ext/image_operations.h"
31 #include "url/gurl.h" 32 #include "url/gurl.h"
32 33
33 using content::BrowserThread; 34 using content::BrowserThread;
34 35
35 namespace { 36 namespace {
36 37
37 // Template for optional authorization header when using an OAuth access token. 38 // Template for optional authorization header when using an OAuth access token.
38 const char kAuthorizationHeader[] = 39 const char kAuthorizationHeader[] =
39 "Authorization: Bearer %s"; 40 "Authorization: Bearer %s";
40 41
41 // URL requesting user info.
42 const char kUserEntryURL[] =
43 "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
44
45 // OAuth scope for the user info API.
46 // For more info, see https://developers.google.com/accounts/docs/OAuth2LoginV1.
47 const char kAPIScope[] = "https://www.googleapis.com/auth/userinfo.profile";
48
49 // Path in JSON dictionary to user's photo thumbnail URL. 42 // Path in JSON dictionary to user's photo thumbnail URL.
50 const char kPhotoThumbnailURLPath[] = "picture"; 43 const char kPhotoThumbnailURLPath[] = "picture";
51 44
45 // Path in JSON dictionary to user's hosted domain.
46 const char kHostedDomainPath[] = "hd";
47
52 // From the user info API, this field corresponds to the full name of the user. 48 // From the user info API, this field corresponds to the full name of the user.
53 const char kFullNamePath[] = "name"; 49 const char kFullNamePath[] = "name";
54 50
55 const char kGivenNamePath[] = "given_name"; 51 const char kGivenNamePath[] = "given_name";
56 52
57 // Path in JSON dictionary to user's preferred locale. 53 // Path in JSON dictionary to user's preferred locale.
58 const char kLocalePath[] = "locale"; 54 const char kLocalePath[] = "locale";
59 55
60 // Path format for specifying thumbnail's size. 56 // Path format for specifying thumbnail's size.
61 const char kThumbnailSizeFormat[] = "s%d-c"; 57 const char kThumbnailSizeFormat[] = "s%d-c";
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // We can't set the image size, just use the default size. 122 // We can't set the image size, just use the default size.
127 *new_url = old_url; 123 *new_url = old_url;
128 return true; 124 return true;
129 } 125 }
130 126
131 } // namespace 127 } // namespace
132 128
133 // Parses the entry response and gets the name and profile image URL. 129 // Parses the entry response and gets the name and profile image URL.
134 // |data| should be the JSON formatted data return by the response. 130 // |data| should be the JSON formatted data return by the response.
135 // Returns false to indicate a parsing error. 131 // Returns false to indicate a parsing error.
136 bool ProfileDownloader::ParseProfileJSON(const std::string& data, 132 bool ProfileDownloader::ParseProfileJSON(base::DictionaryValue* root_dictionary,
137 base::string16* full_name, 133 base::string16* full_name,
138 base::string16* given_name, 134 base::string16* given_name,
139 std::string* url, 135 std::string* url,
140 int image_size, 136 int image_size,
141 std::string* profile_locale) { 137 std::string* profile_locale,
138 base::string16* hosted_domain) {
142 DCHECK(full_name); 139 DCHECK(full_name);
143 DCHECK(given_name); 140 DCHECK(given_name);
144 DCHECK(url); 141 DCHECK(url);
145 DCHECK(profile_locale); 142 DCHECK(profile_locale);
143 DCHECK(hosted_domain);
146 144
147 *full_name = base::string16(); 145 *full_name = base::string16();
148 *given_name = base::string16(); 146 *given_name = base::string16();
149 *url = std::string(); 147 *url = std::string();
150 *profile_locale = std::string(); 148 *profile_locale = std::string();
151 149 *hosted_domain = base::string16();
152 int error_code = -1;
153 std::string error_message;
154 scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError(
155 data, base::JSON_PARSE_RFC, &error_code, &error_message));
156 if (!root_value) {
157 LOG(ERROR) << "Error while parsing user entry response: "
158 << error_message;
159 return false;
160 }
161 if (!root_value->IsType(base::Value::TYPE_DICTIONARY)) {
162 LOG(ERROR) << "JSON root is not a dictionary: "
163 << root_value->GetType();
164 return false;
165 }
166 base::DictionaryValue* root_dictionary =
167 static_cast<base::DictionaryValue*>(root_value.get());
168 150
169 root_dictionary->GetString(kFullNamePath, full_name); 151 root_dictionary->GetString(kFullNamePath, full_name);
170 root_dictionary->GetString(kGivenNamePath, given_name); 152 root_dictionary->GetString(kGivenNamePath, given_name);
171 root_dictionary->GetString(kLocalePath, profile_locale); 153 root_dictionary->GetString(kLocalePath, profile_locale);
154 root_dictionary->GetString(kHostedDomainPath, hosted_domain);
172 155
173 std::string url_string; 156 std::string url_string;
174 if (root_dictionary->GetString(kPhotoThumbnailURLPath, &url_string)) { 157 if (root_dictionary->GetString(kPhotoThumbnailURLPath, &url_string)) {
175 GURL new_url; 158 GURL new_url;
176 if (!GetImageURLWithSize(GURL(url_string), image_size, &new_url)) { 159 if (!GetImageURLWithSize(GURL(url_string), image_size, &new_url)) {
177 LOG(ERROR) << "GetImageURLWithSize failed for url: " << url_string; 160 LOG(ERROR) << "GetImageURLWithSize failed for url: " << url_string;
178 return false; 161 return false;
179 } 162 }
180 *url = new_url.spec(); 163 *url = new_url.spec();
181 } 164 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 account_id_ = 223 account_id_ =
241 account_id.empty() ? 224 account_id.empty() ?
242 signin_manager->GetAuthenticatedAccountId() : account_id; 225 signin_manager->GetAuthenticatedAccountId() : account_id;
243 if (service->RefreshTokenIsAvailable(account_id_)) { 226 if (service->RefreshTokenIsAvailable(account_id_)) {
244 StartFetchingOAuth2AccessToken(); 227 StartFetchingOAuth2AccessToken();
245 } else { 228 } else {
246 service->AddObserver(this); 229 service->AddObserver(this);
247 } 230 }
248 } 231 }
249 232
233 base::string16 ProfileDownloader::GetProfileHostedDomain() const {
234 return profile_hosted_domain_;
235 }
236
250 base::string16 ProfileDownloader::GetProfileFullName() const { 237 base::string16 ProfileDownloader::GetProfileFullName() const {
251 return profile_full_name_; 238 return profile_full_name_;
252 } 239 }
253 240
254 base::string16 ProfileDownloader::GetProfileGivenName() const { 241 base::string16 ProfileDownloader::GetProfileGivenName() const {
255 return profile_given_name_; 242 return profile_given_name_;
256 } 243 }
257 244
258 std::string ProfileDownloader::GetProfileLocale() const { 245 std::string ProfileDownloader::GetProfileLocale() const {
259 return profile_locale_; 246 return profile_locale_;
260 } 247 }
261 248
262 SkBitmap ProfileDownloader::GetProfilePicture() const { 249 SkBitmap ProfileDownloader::GetProfilePicture() const {
263 return profile_picture_; 250 return profile_picture_;
264 } 251 }
265 252
266 ProfileDownloader::PictureStatus ProfileDownloader::GetProfilePictureStatus() 253 ProfileDownloader::PictureStatus ProfileDownloader::GetProfilePictureStatus()
267 const { 254 const {
268 return picture_status_; 255 return picture_status_;
269 } 256 }
270 257
271 std::string ProfileDownloader::GetProfilePictureURL() const { 258 std::string ProfileDownloader::GetProfilePictureURL() const {
272 return picture_url_; 259 return picture_url_;
273 } 260 }
274 261
275 void ProfileDownloader::StartFetchingImage() { 262 void ProfileDownloader::StartFetchingImage() {
276 VLOG(1) << "Fetching user entry with token: " << auth_token_; 263 VLOG(1) << "Fetching user entry with token: " << auth_token_;
277 user_entry_fetcher_.reset(net::URLFetcher::Create( 264 gaia_client_.reset(new gaia::GaiaOAuthClient(
278 GURL(kUserEntryURL), net::URLFetcher::GET, this)); 265 delegate_->GetBrowserProfile()->GetRequestContext()));
279 user_entry_fetcher_->SetRequestContext( 266 gaia_client_->GetUserInfo(auth_token_, 0, this);
280 delegate_->GetBrowserProfile()->GetRequestContext());
281 user_entry_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
282 net::LOAD_DO_NOT_SAVE_COOKIES);
283 if (!auth_token_.empty()) {
284 user_entry_fetcher_->SetExtraRequestHeaders(
285 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str()));
286 }
287 user_entry_fetcher_->Start();
288 } 267 }
289 268
290 void ProfileDownloader::StartFetchingOAuth2AccessToken() { 269 void ProfileDownloader::StartFetchingOAuth2AccessToken() {
291 Profile* profile = delegate_->GetBrowserProfile(); 270 Profile* profile = delegate_->GetBrowserProfile();
292 OAuth2TokenService::ScopeSet scopes; 271 OAuth2TokenService::ScopeSet scopes;
293 scopes.insert(kAPIScope); 272 scopes.insert(GaiaConstants::kGoogleUserInfoProfile);
273 // Increase scope to get hd attribute to determine if lock should be enabled.
274 if (switches::IsNewProfileManagement())
275 scopes.insert(GaiaConstants::kGoogleUserInfoEmail);
294 ProfileOAuth2TokenService* token_service = 276 ProfileOAuth2TokenService* token_service =
295 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 277 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
296 oauth2_access_token_request_ = token_service->StartRequest( 278 oauth2_access_token_request_ = token_service->StartRequest(
297 account_id_, scopes, this); 279 account_id_, scopes, this);
298 } 280 }
299 281
300 ProfileDownloader::~ProfileDownloader() { 282 ProfileDownloader::~ProfileDownloader() {
301 // Ensures PO2TS observation is cleared when ProfileDownloader is destructed 283 // Ensures PO2TS observation is cleared when ProfileDownloader is destructed
302 // before refresh token is available. 284 // before refresh token is available.
303 ProfileOAuth2TokenService* service = 285 ProfileOAuth2TokenService* service =
304 ProfileOAuth2TokenServiceFactory::GetForProfile( 286 ProfileOAuth2TokenServiceFactory::GetForProfile(
305 delegate_->GetBrowserProfile()); 287 delegate_->GetBrowserProfile());
306 if (service) 288 if (service)
307 service->RemoveObserver(this); 289 service->RemoveObserver(this);
308 } 290 }
309 291
292 void ProfileDownloader::OnGetUserInfoResponse(
293 scoped_ptr<base::DictionaryValue> user_info) {
294 std::string image_url;
295 if (!ParseProfileJSON(user_info.get(),
296 &profile_full_name_,
297 &profile_given_name_,
298 &image_url,
299 delegate_->GetDesiredImageSideLength(),
300 &profile_locale_,
301 &profile_hosted_domain_)) {
302 delegate_->OnProfileDownloadFailure(
303 this, ProfileDownloaderDelegate::SERVICE_ERROR);
304 return;
305 }
306 if (!delegate_->NeedsProfilePicture()) {
307 VLOG(1) << "Skipping profile picture download";
308 delegate_->OnProfileDownloadSuccess(this);
309 return;
310 }
311 if (IsDefaultProfileImageURL(image_url)) {
312 VLOG(1) << "User has default profile picture";
313 picture_status_ = PICTURE_DEFAULT;
314 delegate_->OnProfileDownloadSuccess(this);
315 return;
316 }
317 if (!image_url.empty() && image_url == delegate_->GetCachedPictureURL()) {
318 VLOG(1) << "Picture URL matches cached picture URL";
319 picture_status_ = PICTURE_CACHED;
320 delegate_->OnProfileDownloadSuccess(this);
321 return;
322 }
323 VLOG(1) << "Fetching profile image from " << image_url;
324 picture_url_ = image_url;
325 profile_image_fetcher_.reset(net::URLFetcher::Create(
326 GURL(image_url), net::URLFetcher::GET, this));
327 profile_image_fetcher_->SetRequestContext(
328 delegate_->GetBrowserProfile()->GetRequestContext());
329 profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
330 net::LOAD_DO_NOT_SAVE_COOKIES);
331 if (!auth_token_.empty()) {
332 profile_image_fetcher_->SetExtraRequestHeaders(
333 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str()));
334 }
335 profile_image_fetcher_->Start();
336 }
337
338 void ProfileDownloader::OnOAuthError() {
339 LOG(WARNING) << "OnOAuthError: Fetching profile data failed";
340 delegate_->OnProfileDownloadFailure(
341 this, ProfileDownloaderDelegate::SERVICE_ERROR);
342 }
343
344 void ProfileDownloader::OnNetworkError(int response_code) {
345 LOG(WARNING) << "OnNetworkError: Fetching profile data failed";
346 DVLOG(1) << " Response code: " << response_code;
347 delegate_->OnProfileDownloadFailure(
348 this, ProfileDownloaderDelegate::NETWORK_ERROR);
349 }
350
310 void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { 351 void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) {
311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
312 std::string data; 353 std::string data;
313 source->GetResponseAsString(&data); 354 source->GetResponseAsString(&data);
314 bool network_error = 355 bool network_error =
315 source->GetStatus().status() != net::URLRequestStatus::SUCCESS; 356 source->GetStatus().status() != net::URLRequestStatus::SUCCESS;
316 if (network_error || source->GetResponseCode() != 200) { 357 if (network_error || source->GetResponseCode() != 200) {
317 LOG(WARNING) << "Fetching profile data failed"; 358 LOG(WARNING) << "Fetching profile data failed";
318 DVLOG(1) << " Status: " << source->GetStatus().status(); 359 DVLOG(1) << " Status: " << source->GetStatus().status();
319 DVLOG(1) << " Error: " << source->GetStatus().error(); 360 DVLOG(1) << " Error: " << source->GetStatus().error();
320 DVLOG(1) << " Response code: " << source->GetResponseCode(); 361 DVLOG(1) << " Response code: " << source->GetResponseCode();
321 DVLOG(1) << " Url: " << source->GetURL().spec(); 362 DVLOG(1) << " Url: " << source->GetURL().spec();
322 delegate_->OnProfileDownloadFailure(this, network_error ? 363 delegate_->OnProfileDownloadFailure(this, network_error ?
323 ProfileDownloaderDelegate::NETWORK_ERROR : 364 ProfileDownloaderDelegate::NETWORK_ERROR :
324 ProfileDownloaderDelegate::SERVICE_ERROR); 365 ProfileDownloaderDelegate::SERVICE_ERROR);
325 return; 366 return;
326 } 367 }
327 368
328 if (source == user_entry_fetcher_.get()) { 369 VLOG(1) << "Decoding the image...";
329 std::string image_url; 370 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder(
330 if (!ParseProfileJSON(data, 371 this, data, ImageDecoder::DEFAULT_CODEC);
331 &profile_full_name_, 372 scoped_refptr<base::MessageLoopProxy> task_runner =
332 &profile_given_name_, 373 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
333 &image_url, 374 image_decoder->Start(task_runner);
334 delegate_->GetDesiredImageSideLength(),
335 &profile_locale_)) {
336 delegate_->OnProfileDownloadFailure(
337 this, ProfileDownloaderDelegate::SERVICE_ERROR);
338 return;
339 }
340 if (!delegate_->NeedsProfilePicture()) {
341 VLOG(1) << "Skipping profile picture download";
342 delegate_->OnProfileDownloadSuccess(this);
343 return;
344 }
345 if (IsDefaultProfileImageURL(image_url)) {
346 VLOG(1) << "User has default profile picture";
347 picture_status_ = PICTURE_DEFAULT;
348 delegate_->OnProfileDownloadSuccess(this);
349 return;
350 }
351 if (!image_url.empty() && image_url == delegate_->GetCachedPictureURL()) {
352 VLOG(1) << "Picture URL matches cached picture URL";
353 picture_status_ = PICTURE_CACHED;
354 delegate_->OnProfileDownloadSuccess(this);
355 return;
356 }
357 VLOG(1) << "Fetching profile image from " << image_url;
358 picture_url_ = image_url;
359 profile_image_fetcher_.reset(net::URLFetcher::Create(
360 GURL(image_url), net::URLFetcher::GET, this));
361 profile_image_fetcher_->SetRequestContext(
362 delegate_->GetBrowserProfile()->GetRequestContext());
363 profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
364 net::LOAD_DO_NOT_SAVE_COOKIES);
365 if (!auth_token_.empty()) {
366 profile_image_fetcher_->SetExtraRequestHeaders(
367 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str()));
368 }
369 profile_image_fetcher_->Start();
370 } else if (source == profile_image_fetcher_.get()) {
371 VLOG(1) << "Decoding the image...";
372 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder(
373 this, data, ImageDecoder::DEFAULT_CODEC);
374 scoped_refptr<base::MessageLoopProxy> task_runner =
375 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
376 image_decoder->Start(task_runner);
377 }
378 } 375 }
379 376
380 void ProfileDownloader::OnImageDecoded(const ImageDecoder* decoder, 377 void ProfileDownloader::OnImageDecoded(const ImageDecoder* decoder,
381 const SkBitmap& decoded_image) { 378 const SkBitmap& decoded_image) {
382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
383 int image_size = delegate_->GetDesiredImageSideLength(); 380 int image_size = delegate_->GetDesiredImageSideLength();
384 profile_picture_ = skia::ImageOperations::Resize( 381 profile_picture_ = skia::ImageOperations::Resize(
385 decoded_image, 382 decoded_image,
386 skia::ImageOperations::RESIZE_BEST, 383 skia::ImageOperations::RESIZE_BEST,
387 image_size, 384 image_size,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 void ProfileDownloader::OnGetTokenFailure( 420 void ProfileDownloader::OnGetTokenFailure(
424 const OAuth2TokenService::Request* request, 421 const OAuth2TokenService::Request* request,
425 const GoogleServiceAuthError& error) { 422 const GoogleServiceAuthError& error) {
426 DCHECK_EQ(request, oauth2_access_token_request_.get()); 423 DCHECK_EQ(request, oauth2_access_token_request_.get());
427 oauth2_access_token_request_.reset(); 424 oauth2_access_token_request_.reset();
428 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed:" 425 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed:"
429 << error.ToString(); 426 << error.ToString();
430 delegate_->OnProfileDownloadFailure( 427 delegate_->OnProfileDownloadFailure(
431 this, ProfileDownloaderDelegate::TOKEN_ERROR); 428 this, ProfileDownloaderDelegate::TOKEN_ERROR);
432 } 429 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_downloader.h ('k') | chrome/browser/profiles/profile_downloader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698