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