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/supervised_user/legacy/custodian_profile_downloader_service.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/signin/signin_manager_factory.h"
10 #include "chrome/common/pref_names.h"
11 #include "components/signin/core/browser/signin_manager.h"
12 #include "google_apis/gaia/gaia_auth_util.h"
14 CustodianProfileDownloaderService::CustodianProfileDownloaderService(
15 Profile
* custodian_profile
)
16 : custodian_profile_(custodian_profile
) {
19 CustodianProfileDownloaderService::~CustodianProfileDownloaderService() {}
21 void CustodianProfileDownloaderService::Shutdown() {
22 profile_downloader_
.reset();
25 void CustodianProfileDownloaderService::DownloadProfile(
26 const DownloadProfileCallback
& callback
) {
27 // The user must be logged in.
28 if (!SigninManagerFactory::GetForProfile(custodian_profile_
)
29 ->IsAuthenticated()) {
33 download_callback_
= callback
;
34 std::string current_email
= custodian_profile_
->GetProfileUserName();
35 if (gaia::AreEmailsSame(last_downloaded_profile_email_
, current_email
)) {
36 // Profile was previously downloaded successfully, use it as it is unlikely
37 // that we will need to download it again.
38 OnProfileDownloadSuccess(profile_downloader_
.get());
41 // If another profile download is in progress, drop it. It's not worth
42 // queueing them up, and more likely that the one that hasn't ended yet is
43 // failing somehow than that the new one won't succeed.
44 in_progress_profile_email_
= current_email
;
45 profile_downloader_
.reset(new ProfileDownloader(this));
46 profile_downloader_
->Start();
49 bool CustodianProfileDownloaderService::NeedsProfilePicture() const {
53 int CustodianProfileDownloaderService::GetDesiredImageSideLength() const {
57 std::string
CustodianProfileDownloaderService::GetCachedPictureURL() const {
61 Profile
* CustodianProfileDownloaderService::GetBrowserProfile() {
62 DCHECK(custodian_profile_
);
63 return custodian_profile_
;
66 bool CustodianProfileDownloaderService::IsPreSignin() const {
70 void CustodianProfileDownloaderService::OnProfileDownloadSuccess(
71 ProfileDownloader
* downloader
) {
72 download_callback_
.Run(downloader
->GetProfileFullName());
73 download_callback_
.Reset();
74 last_downloaded_profile_email_
= in_progress_profile_email_
;
77 void CustodianProfileDownloaderService::OnProfileDownloadFailure(
78 ProfileDownloader
* downloader
,
79 ProfileDownloaderDelegate::FailureReason reason
) {
80 // Ignore failures; proceed without the custodian's name.
81 download_callback_
.Reset();
82 profile_downloader_
.reset();