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
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile_avatar_downloader.h"
7 #include "base/files/file_path.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
11 #include "chrome/browser/profiles/profile_info_cache.h"
12 #include "net/base/load_flags.h"
15 const char kHighResAvatarDownloadUrlPrefix
[] =
16 "http://www.gstatic.com/chrome/profile_avatars/";
19 ProfileAvatarDownloader::ProfileAvatarDownloader(
21 const base::FilePath
& profile_path
,
22 ProfileInfoCache
* cache
)
23 : icon_index_(icon_index
),
24 profile_path_(profile_path
),
26 GURL
url(std::string(kHighResAvatarDownloadUrlPrefix
) +
27 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index
));
28 fetcher_
.reset(new chrome::BitmapFetcher(url
, this));
31 ProfileAvatarDownloader::~ProfileAvatarDownloader() {
34 void ProfileAvatarDownloader::Start() {
35 // In unit tests, the browser process can return a NULL request context.
36 net::URLRequestContextGetter
* request_context
=
37 g_browser_process
->system_request_context();
42 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
46 // BitmapFetcherDelegate overrides.
47 void ProfileAvatarDownloader::OnFetchComplete(const GURL url
,
48 const SkBitmap
* bitmap
) {
49 if (!bitmap
|| !cache_
)
52 // Decode the downloaded bitmap. Ownership of the image is taken by |cache_|.
53 gfx::Image image
= gfx::Image::CreateFrom1xBitmap(*bitmap
);
54 cache_
->SaveAvatarImageAtPath(&image
,
55 profiles::GetDefaultAvatarIconFileNameAtIndex(icon_index_
),
56 profiles::GetPathOfHighResAvatarAtIndex(icon_index_
),