| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/user_image_downloader.h" | 5 #include "chrome/browser/chromeos/login/user_image_downloader.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/chrome_thread.h" | 13 #include "chrome/browser/chrome_thread.h" |
| 14 #include "chrome/browser/chromeos/login/google_authenticator.h" | 14 #include "chrome/browser/chromeos/login/google_authenticator.h" |
| 15 #include "chrome/browser/chromeos/login/image_downloader.h" |
| 15 #include "chrome/browser/chromeos/login/user_manager.h" | 16 #include "chrome/browser/chromeos/login/user_manager.h" |
| 16 #include "chrome/browser/profile_manager.h" | 17 #include "chrome/browser/profile_manager.h" |
| 17 #include "chrome/common/net/url_fetcher.h" | 18 #include "chrome/common/net/url_fetcher.h" |
| 18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 19 | 20 |
| 20 namespace chromeos { | 21 namespace chromeos { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // Contacts API URL that returns all user info. | 25 // Contacts API URL that returns all user info. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return; | 71 return; |
| 71 } | 72 } |
| 72 | 73 |
| 73 if (source == profile_fetcher_.get()) { | 74 if (source == profile_fetcher_.get()) { |
| 74 GURL image_url; | 75 GURL image_url; |
| 75 if (!GetImageURL(data, &image_url)) { | 76 if (!GetImageURL(data, &image_url)) { |
| 76 LOG(ERROR) << "Didn't find image url in " << data; | 77 LOG(ERROR) << "Didn't find image url in " << data; |
| 77 return; | 78 return; |
| 78 } | 79 } |
| 79 LOG(INFO) << "Sending request to " << image_url; | 80 LOG(INFO) << "Sending request to " << image_url; |
| 80 picture_fetcher_.reset( | 81 new ImageDownloader(this, GURL(image_url), auth_token_); |
| 81 new URLFetcher(GURL(image_url), URLFetcher::GET, this)); | |
| 82 picture_fetcher_->set_request_context( | |
| 83 ProfileManager::GetDefaultProfile()->GetRequestContext()); | |
| 84 picture_fetcher_->set_extra_request_headers( | |
| 85 StringPrintf(kAuthorizationHeader, auth_token_.c_str())); | |
| 86 picture_fetcher_->Start(); | |
| 87 } else if (source == picture_fetcher_.get()) { | |
| 88 LOG(INFO) << "Decoding the image..."; | |
| 89 std::vector<unsigned char> image_data(data.begin(), data.end()); | |
| 90 ChromeThread::PostTask( | |
| 91 ChromeThread::IO, FROM_HERE, | |
| 92 NewRunnableMethod( | |
| 93 this, &UserImageDownloader::DecodeImageInSandbox, | |
| 94 g_browser_process->resource_dispatcher_host(), | |
| 95 image_data)); | |
| 96 } | 82 } |
| 97 } | 83 } |
| 98 | 84 |
| 99 void UserImageDownloader::OnDecodeImageSucceeded( | 85 void UserImageDownloader::OnImageDecoded(const SkBitmap& decoded_image) { |
| 100 const SkBitmap& decoded_image) { | |
| 101 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 102 | |
| 103 // Save the image to file and its path to preferences. | 86 // Save the image to file and its path to preferences. |
| 104 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); | 87 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); |
| 105 if (user_manager) | 88 if (user_manager) |
| 106 user_manager->SaveUserImage(username_, decoded_image); | 89 user_manager->SaveUserImage(username_, decoded_image); |
| 107 } | 90 } |
| 108 | 91 |
| 109 void UserImageDownloader::DecodeImageInSandbox( | |
| 110 ResourceDispatcherHost* rdh, | |
| 111 const std::vector<unsigned char>& image_data) { | |
| 112 UtilityProcessHost* utility_process_host = | |
| 113 new UtilityProcessHost(rdh, | |
| 114 this, | |
| 115 ChromeThread::UI); | |
| 116 utility_process_host->StartImageDecoding(image_data); | |
| 117 } | |
| 118 | |
| 119 bool UserImageDownloader::GetImageURL(const std::string& json_data, | 92 bool UserImageDownloader::GetImageURL(const std::string& json_data, |
| 120 GURL* image_url) const { | 93 GURL* image_url) const { |
| 121 if (!image_url) { | 94 if (!image_url) { |
| 122 NOTREACHED(); | 95 NOTREACHED(); |
| 123 return false; | 96 return false; |
| 124 } | 97 } |
| 125 | 98 |
| 126 // Data is in JSON format with image url located at the following path: | 99 // Data is in JSON format with image url located at the following path: |
| 127 // root > feed > entry > dictionary > link > dictionary > href. | 100 // root > feed > entry > dictionary > link > dictionary > href. |
| 128 scoped_ptr<Value> root(base::JSONReader::Read(json_data, true)); | 101 scoped_ptr<Value> root(base::JSONReader::Read(json_data, true)); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 if (!link_dictionary->GetStringASCII("href", &url)) | 179 if (!link_dictionary->GetStringASCII("href", &url)) |
| 207 continue; | 180 continue; |
| 208 | 181 |
| 209 *image_url = GURL(url); | 182 *image_url = GURL(url); |
| 210 return true; | 183 return true; |
| 211 } | 184 } |
| 212 return false; | 185 return false; |
| 213 } | 186 } |
| 214 | 187 |
| 215 } // namespace chromeos | 188 } // namespace chromeos |
| 216 | |
| OLD | NEW |