Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/login/profile_image_downloader.h" | 5 #include "chrome/browser/chromeos/login/profile_image_downloader.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 // URL requesting Picasa API for user info. | 37 // URL requesting Picasa API for user info. |
| 38 const char kUserEntryURL[] = | 38 const char kUserEntryURL[] = |
| 39 "http://picasaweb.google.com/data/entry/api/user/default?alt=json"; | 39 "http://picasaweb.google.com/data/entry/api/user/default?alt=json"; |
| 40 // Path in JSON dictionary to user's photo thumbnail URL. | 40 // Path in JSON dictionary to user's photo thumbnail URL. |
| 41 const char kPhotoThumbnailURLPath[] = "entry.gphoto$thumbnail.$t"; | 41 const char kPhotoThumbnailURLPath[] = "entry.gphoto$thumbnail.$t"; |
| 42 // Path format for specifying thumbnail's size. | 42 // Path format for specifying thumbnail's size. |
| 43 const char kThumbnailSizeFormat[] = "s%d-c"; | 43 const char kThumbnailSizeFormat[] = "s%d-c"; |
| 44 // Default Picasa thumbnail size. | 44 // Default Picasa thumbnail size. |
| 45 const int kDefaultThumbnailSize = 64; | 45 const int kDefaultThumbnailSize = 64; |
| 46 | 46 |
| 47 // Path components that identify the default profile picture. | |
| 48 const char* kDefaultProfileImagePathComponents[] = { | |
| 49 "/AAAAAAAAAAI/AAAAAAAAAAA/", | |
| 50 "/AAAAAAAAAA/" | |
| 51 }; | |
| 52 | |
| 47 } // namespace | 53 } // namespace |
| 48 | 54 |
| 49 std::string ProfileImageDownloader::GetProfileImageURL( | 55 std::string ProfileImageDownloader::GetProfileImageURL( |
| 50 const std::string& data) const { | 56 const std::string& data) const { |
| 51 int error_code = -1; | 57 int error_code = -1; |
| 52 std::string error_message; | 58 std::string error_message; |
| 53 scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError( | 59 scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError( |
| 54 data, false, &error_code, &error_message)); | 60 data, false, &error_code, &error_message)); |
| 55 if (!root_value.get()) { | 61 if (!root_value.get()) { |
| 56 LOG(ERROR) << "Error while parsing Picasa user entry response: " | 62 LOG(ERROR) << "Error while parsing Picasa user entry response: " |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 } | 104 } |
| 99 | 105 |
| 100 GURL thumbnail_url(thumbnail_url_string); | 106 GURL thumbnail_url(thumbnail_url_string); |
| 101 if (!thumbnail_url.is_valid()) { | 107 if (!thumbnail_url.is_valid()) { |
| 102 LOG(ERROR) << "Thumbnail URL is not valid: " << thumbnail_url_string; | 108 LOG(ERROR) << "Thumbnail URL is not valid: " << thumbnail_url_string; |
| 103 return std::string(); | 109 return std::string(); |
| 104 } | 110 } |
| 105 return thumbnail_url.spec(); | 111 return thumbnail_url.spec(); |
| 106 } | 112 } |
| 107 | 113 |
| 114 bool ProfileImageDownloader::IsDefaultProfileImageURL( | |
| 115 const std::string& url) const { | |
| 116 GURL image_url_object(url); | |
| 117 DCHECK(image_url_object.is_valid()); | |
| 118 for (size_t i = 0; | |
| 119 i < arraysize(kDefaultProfileImagePathComponents); | |
| 120 ++i) { | |
| 121 const char* component = | |
| 122 kDefaultProfileImagePathComponents[i]; | |
| 123 if (image_url_object.path().find(component) != std::string::npos) | |
|
Ivan Korotkov
2011/10/26 20:43:57
This will match /AAAAAAAAAA/ anywhere in the path.
whywhat
2011/10/27 13:28:10
Done.
| |
| 124 return true; | |
| 125 } | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 108 ProfileImageDownloader::ProfileImageDownloader(Delegate* delegate) | 129 ProfileImageDownloader::ProfileImageDownloader(Delegate* delegate) |
| 109 : delegate_(delegate) { | 130 : delegate_(delegate) { |
| 110 } | 131 } |
| 111 | 132 |
| 112 void ProfileImageDownloader::Start() { | 133 void ProfileImageDownloader::Start() { |
| 113 VLOG(1) << "Starting profile image downloader..."; | 134 VLOG(1) << "Starting profile image downloader..."; |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 115 | 136 |
| 116 TokenService* service = | 137 TokenService* service = |
| 117 ProfileManager::GetDefaultProfile()->GetTokenService(); | 138 ProfileManager::GetDefaultProfile()->GetTokenService(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 return; | 189 return; |
| 169 } | 190 } |
| 170 | 191 |
| 171 if (source == user_entry_fetcher_.get()) { | 192 if (source == user_entry_fetcher_.get()) { |
| 172 std::string image_url = GetProfileImageURL(data); | 193 std::string image_url = GetProfileImageURL(data); |
| 173 if (image_url.empty()) { | 194 if (image_url.empty()) { |
| 174 if (delegate_) | 195 if (delegate_) |
| 175 delegate_->OnDownloadFailure(); | 196 delegate_->OnDownloadFailure(); |
| 176 return; | 197 return; |
| 177 } | 198 } |
| 199 if (IsDefaultProfileImageURL(image_url)) { | |
| 200 if (delegate_) | |
| 201 delegate_->OnDownloadDefaultImage(); | |
| 202 return; | |
| 203 } | |
| 178 VLOG(1) << "Fetching profile image from " << image_url; | 204 VLOG(1) << "Fetching profile image from " << image_url; |
| 179 profile_image_fetcher_.reset(content::URLFetcher::Create( | 205 profile_image_fetcher_.reset(content::URLFetcher::Create( |
| 180 GURL(image_url), content::URLFetcher::GET, this)); | 206 GURL(image_url), content::URLFetcher::GET, this)); |
| 181 profile_image_fetcher_->SetRequestContext( | 207 profile_image_fetcher_->SetRequestContext( |
| 182 ProfileManager::GetDefaultProfile()->GetRequestContext()); | 208 ProfileManager::GetDefaultProfile()->GetRequestContext()); |
| 183 if (!auth_token_.empty()) { | 209 if (!auth_token_.empty()) { |
| 184 profile_image_fetcher_->SetExtraRequestHeaders( | 210 profile_image_fetcher_->SetExtraRequestHeaders( |
| 185 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); | 211 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); |
| 186 } | 212 } |
| 187 profile_image_fetcher_->Start(); | 213 profile_image_fetcher_->Start(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 } else { | 255 } else { |
| 230 if (token_details->service() == GaiaConstants::kPicasaService) { | 256 if (token_details->service() == GaiaConstants::kPicasaService) { |
| 231 LOG(WARNING) << "ProfileImageDownloader: token request failed"; | 257 LOG(WARNING) << "ProfileImageDownloader: token request failed"; |
| 232 if (delegate_) | 258 if (delegate_) |
| 233 delegate_->OnDownloadFailure(); | 259 delegate_->OnDownloadFailure(); |
| 234 } | 260 } |
| 235 } | 261 } |
| 236 } | 262 } |
| 237 | 263 |
| 238 } // namespace chromeos | 264 } // namespace chromeos |
| OLD | NEW |