OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_avatar_downloader.h" | 5 #include "chrome/browser/profiles/profile_avatar_downloader.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 10 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
11 #include "chrome/browser/profiles/profile_info_cache.h" | 11 #include "chrome/browser/profiles/profile_info_cache.h" |
12 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 const char kHighResAvatarDownloadUrlPrefix[] = | 15 const char kHighResAvatarDownloadUrlPrefix[] = |
16 "http://www.gstatic.com/chrome/profile_avatars/"; | 16 "http://www.gstatic.com/chrome/profile_avatars/"; |
17 } | 17 } |
18 | 18 |
19 ProfileAvatarDownloader::ProfileAvatarDownloader( | 19 ProfileAvatarDownloader::ProfileAvatarDownloader( |
20 size_t icon_index, | 20 size_t icon_index, |
21 const base::FilePath& profile_path, | 21 const base::FilePath& profile_path, |
22 ProfileInfoCache* cache) | 22 ProfileInfoCache* cache) |
23 : icon_index_(icon_index), | 23 : icon_index_(icon_index), |
24 profile_path_(profile_path), | 24 profile_path_(profile_path), |
25 // The downloader should only execute on desktop platforms. | |
26 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS) | |
27 downloader_active_(true), | |
28 #else | |
29 downloader_active_(false), | |
30 #endif | |
25 cache_(cache) { | 31 cache_(cache) { |
26 GURL url(std::string(kHighResAvatarDownloadUrlPrefix) + | 32 GURL url(std::string(kHighResAvatarDownloadUrlPrefix) + |
27 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index)); | 33 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index)); |
28 fetcher_.reset(new chrome::BitmapFetcher(url, this)); | 34 fetcher_.reset(new chrome::BitmapFetcher(url, this)); |
29 } | 35 } |
30 | 36 |
31 ProfileAvatarDownloader::~ProfileAvatarDownloader() { | 37 ProfileAvatarDownloader::~ProfileAvatarDownloader() { |
32 } | 38 } |
33 | 39 |
34 void ProfileAvatarDownloader::Start() { | 40 void ProfileAvatarDownloader::Start() { |
35 // In unit tests, the browser process can return a NULL request context. | 41 if (downloader_active_) { |
noms (inactive)
2015/03/17 14:26:30
nit: i would return early if !downloader_active
Mike Lerman
2015/03/18 19:29:48
Done.
| |
36 net::URLRequestContextGetter* request_context = | 42 // In unit tests, the browser process can return a NULL request context. |
37 g_browser_process->system_request_context(); | 43 net::URLRequestContextGetter* request_context = |
38 if (request_context) | 44 g_browser_process->system_request_context(); |
39 fetcher_->Start( | 45 if (request_context) |
40 request_context, | 46 fetcher_->Start( |
41 std::string(), | 47 request_context, |
42 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 48 std::string(), |
43 net::LOAD_NORMAL); | 49 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
50 net::LOAD_NORMAL); | |
51 } | |
44 } | 52 } |
45 | 53 |
46 // BitmapFetcherDelegate overrides. | 54 // BitmapFetcherDelegate overrides. |
47 void ProfileAvatarDownloader::OnFetchComplete(const GURL url, | 55 void ProfileAvatarDownloader::OnFetchComplete(const GURL url, |
48 const SkBitmap* bitmap) { | 56 const SkBitmap* bitmap) { |
49 if (!bitmap || !cache_) | 57 if (!bitmap || !cache_ || !downloader_active_) |
50 return; | 58 return; |
51 | 59 |
52 // Decode the downloaded bitmap. Ownership of the image is taken by |cache_|. | 60 // Decode the downloaded bitmap. Ownership of the image is taken by |cache_|. |
53 gfx::Image image = gfx::Image::CreateFrom1xBitmap(*bitmap); | 61 gfx::Image image = gfx::Image::CreateFrom1xBitmap(*bitmap); |
54 cache_->SaveAvatarImageAtPath(&image, | 62 cache_->SaveAvatarImageAtPath(&image, |
55 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index_), | 63 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index_), |
56 profiles::GetPathOfHighResAvatarAtIndex(icon_index_), | 64 profiles::GetPathOfHighResAvatarAtIndex(icon_index_), |
57 profile_path_); | 65 profile_path_); |
58 } | 66 } |
OLD | NEW |