| OLD | NEW |
| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 21 #include "chrome/browser/profiles/profile_downloader_delegate.h" | 22 #include "chrome/browser/profiles/profile_downloader_delegate.h" |
| 22 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
| 23 #include "chrome/browser/signin/account_fetcher_service_factory.h" | 24 #include "chrome/browser/signin/account_fetcher_service_factory.h" |
| 24 #include "chrome/browser/signin/account_tracker_service_factory.h" | 25 #include "chrome/browser/signin/account_tracker_service_factory.h" |
| 25 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 26 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 26 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 27 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 27 #include "chrome/browser/signin/signin_manager_factory.h" | 28 #include "chrome/browser/signin/signin_manager_factory.h" |
| 28 #include "components/data_use_measurement/core/data_use_user_data.h" | 29 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 29 #include "components/signin/core/browser/account_fetcher_service.h" | 30 #include "components/signin/core/browser/account_fetcher_service.h" |
| 30 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 31 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 31 #include "components/signin/core/browser/signin_client.h" | 32 #include "components/signin/core/browser/signin_client.h" |
| 32 #include "components/signin/core/browser/signin_manager.h" | 33 #include "components/signin/core/browser/signin_manager.h" |
| 33 #include "components/signin/core/common/profile_management_switches.h" | 34 #include "components/signin/core/common/profile_management_switches.h" |
| 34 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 35 #include "google_apis/gaia/gaia_constants.h" | 36 #include "google_apis/gaia/gaia_constants.h" |
| 36 #include "net/base/load_flags.h" | 37 #include "net/base/load_flags.h" |
| 37 #include "net/url_request/url_fetcher.h" | 38 #include "net/url_request/url_fetcher.h" |
| 38 #include "net/url_request/url_request_status.h" | 39 #include "net/url_request/url_request_status.h" |
| 39 #include "skia/ext/image_operations.h" | 40 #include "skia/ext/image_operations.h" |
| 40 #include "url/gurl.h" | 41 #include "url/gurl.h" |
| 41 | 42 |
| 42 using content::BrowserThread; | 43 using content::BrowserThread; |
| 43 | 44 |
| 44 namespace { | 45 namespace { |
| 45 | 46 |
| 46 // Template for optional authorization header when using an OAuth access token. | 47 // Template for optional authorization header when using an OAuth access token. |
| 47 const char kAuthorizationHeader[] = | 48 const char kAuthorizationHeader[] = |
| 48 "Authorization: Bearer %s"; | 49 "Authorization: Bearer %s"; |
| 49 | 50 |
| 50 // Path format for specifying thumbnail's size. | |
| 51 const char kThumbnailSizeFormat[] = "s%d-c"; | |
| 52 // Default thumbnail size. | |
| 53 const int kDefaultThumbnailSize = 64; | |
| 54 | |
| 55 // Separator of URL path components. | 51 // Separator of URL path components. |
| 56 const char kURLPathSeparator = '/'; | 52 const char kURLPathSeparator = '/'; |
| 57 | 53 |
| 58 // Photo ID of the Picasa Web Albums profile picture (base64 of 0). | 54 // Photo ID of the Picasa Web Albums profile picture (base64 of 0). |
| 59 const char kPicasaPhotoId[] = "AAAAAAAAAAA"; | 55 const char kPicasaPhotoId[] = "AAAAAAAAAAA"; |
| 60 | 56 |
| 61 // Photo version of the default PWA profile picture (base64 of 1). | 57 // Photo version of the default PWA profile picture (base64 of 1). |
| 62 const char kDefaultPicasaPhotoVersion[] = "AAAAAAAAAAE"; | 58 const char kDefaultPicasaPhotoVersion[] = "AAAAAAAAAAE"; |
| 63 | 59 |
| 64 // The minimum number of path components in profile picture URL. | 60 // The minimum number of path components in profile picture URL. |
| 65 const size_t kProfileImageURLPathComponentsCount = 6; | 61 const size_t kProfileImageURLPathComponentsCount = 6; |
| 66 | 62 |
| 67 // Index of path component with photo ID. | 63 // Index of path component with photo ID. |
| 68 const int kPhotoIdPathComponentIndex = 2; | 64 const int kPhotoIdPathComponentIndex = 2; |
| 69 | 65 |
| 70 // Index of path component with photo version. | 66 // Index of path component with photo version. |
| 71 const int kPhotoVersionPathComponentIndex = 3; | 67 const int kPhotoVersionPathComponentIndex = 3; |
| 72 | 68 |
| 73 // Given an image URL this function builds a new URL set to |size|. | |
| 74 // For example, if |size| was set to 256 and |old_url| was either: | |
| 75 // https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg | |
| 76 // or | |
| 77 // https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s64-c/photo.jpg | |
| 78 // then return value in |new_url| would be: | |
| 79 // https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s256-c/photo.jpg | |
| 80 bool GetImageURLWithSize(const GURL& old_url, int size, GURL* new_url) { | |
| 81 DCHECK(new_url); | |
| 82 std::vector<std::string> components = base::SplitString( | |
| 83 old_url.path(), std::string(1, kURLPathSeparator), | |
| 84 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 85 if (components.size() == 0) | |
| 86 return false; | |
| 87 | |
| 88 const std::string& old_spec = old_url.spec(); | |
| 89 std::string default_size_component( | |
| 90 base::StringPrintf(kThumbnailSizeFormat, kDefaultThumbnailSize)); | |
| 91 std::string new_size_component( | |
| 92 base::StringPrintf(kThumbnailSizeFormat, size)); | |
| 93 | |
| 94 size_t pos = old_spec.find(default_size_component); | |
| 95 size_t end = std::string::npos; | |
| 96 if (pos != std::string::npos) { | |
| 97 // The default size is already specified in the URL so it needs to be | |
| 98 // replaced with the new size. | |
| 99 end = pos + default_size_component.size(); | |
| 100 } else { | |
| 101 // The default size is not in the URL so try to insert it before the last | |
| 102 // component. | |
| 103 const std::string& file_name = old_url.ExtractFileName(); | |
| 104 if (!file_name.empty()) { | |
| 105 pos = old_spec.find(file_name); | |
| 106 end = pos - 1; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 if (pos != std::string::npos) { | |
| 111 std::string new_spec = old_spec.substr(0, pos) + new_size_component + | |
| 112 old_spec.substr(end); | |
| 113 *new_url = GURL(new_spec); | |
| 114 return new_url->is_valid(); | |
| 115 } | |
| 116 | |
| 117 // We can't set the image size, just use the default size. | |
| 118 *new_url = old_url; | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 } // namespace | 69 } // namespace |
| 123 | 70 |
| 124 // static | 71 // static |
| 125 bool ProfileDownloader::IsDefaultProfileImageURL(const std::string& url) { | 72 bool ProfileDownloader::IsDefaultProfileImageURL(const std::string& url) { |
| 126 if (url.empty()) | 73 if (url.empty()) |
| 127 return true; | 74 return true; |
| 128 | 75 |
| 129 GURL image_url_object(url); | 76 GURL image_url_object(url); |
| 130 DCHECK(image_url_object.is_valid()); | 77 DCHECK(image_url_object.is_valid()); |
| 131 VLOG(1) << "URL to check for default image: " << image_url_object.spec(); | 78 VLOG(1) << "URL to check for default image: " << image_url_object.spec(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 return profile_picture_; | 154 return profile_picture_; |
| 208 } | 155 } |
| 209 | 156 |
| 210 ProfileDownloader::PictureStatus ProfileDownloader::GetProfilePictureStatus() | 157 ProfileDownloader::PictureStatus ProfileDownloader::GetProfilePictureStatus() |
| 211 const { | 158 const { |
| 212 return picture_status_; | 159 return picture_status_; |
| 213 } | 160 } |
| 214 | 161 |
| 215 std::string ProfileDownloader::GetProfilePictureURL() const { | 162 std::string ProfileDownloader::GetProfilePictureURL() const { |
| 216 GURL url; | 163 GURL url; |
| 217 if (GetImageURLWithSize(GURL(account_info_.picture_url), | 164 if (profiles::GetImageURLWithThumbnailSize( |
| 218 delegate_->GetDesiredImageSideLength(), | 165 GURL(account_info_.picture_url), |
| 219 &url)) { | 166 delegate_->GetDesiredImageSideLength(), |
| 167 &url)) { |
| 220 return url.spec(); | 168 return url.spec(); |
| 221 } | 169 } |
| 222 return account_info_.picture_url; | 170 return account_info_.picture_url; |
| 223 } | 171 } |
| 224 | 172 |
| 225 void ProfileDownloader::StartFetchingImage() { | 173 void ProfileDownloader::StartFetchingImage() { |
| 226 VLOG(1) << "Fetching user entry with token: " << auth_token_; | 174 VLOG(1) << "Fetching user entry with token: " << auth_token_; |
| 227 account_info_ = account_tracker_service_->GetAccountInfo(account_id_); | 175 account_info_ = account_tracker_service_->GetAccountInfo(account_id_); |
| 228 | 176 |
| 229 if (delegate_->IsPreSignin()) { | 177 if (delegate_->IsPreSignin()) { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 // downloader has been waiting so we need to fetch the image data now. | 340 // downloader has been waiting so we need to fetch the image data now. |
| 393 if (waiting_for_account_info_) { | 341 if (waiting_for_account_info_) { |
| 394 waiting_for_account_info_ = false; | 342 waiting_for_account_info_ = false; |
| 395 // FetchImageData might call the delegate's OnProfileDownloadSuccess | 343 // FetchImageData might call the delegate's OnProfileDownloadSuccess |
| 396 // synchronously, causing |this| to be deleted so there should not be more | 344 // synchronously, causing |this| to be deleted so there should not be more |
| 397 // code after it. | 345 // code after it. |
| 398 FetchImageData(); | 346 FetchImageData(); |
| 399 } | 347 } |
| 400 } | 348 } |
| 401 } | 349 } |
| OLD | NEW |