1 // Copyright 2018 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.
4
5 #include "chrome/browser/signin/signin_profile_attributes_updater.h"
6
7 #include <string>
8
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/profiles/profile_attributes_storage.h"
12 #include "chrome/browser/signin/signin_util.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/signin/public/identity_manager/account_info.h"
15 #include "components/signin/public/identity_manager/consent_level.h"
16 #include "google_apis/gaia/gaia_auth_util.h"
17
SigninProfileAttributesUpdater(signin::IdentityManager * identity_manager,SigninErrorController * signin_error_controller,ProfileAttributesStorage * profile_attributes_storage,const base::FilePath & profile_path,PrefService * prefs)18 SigninProfileAttributesUpdater::SigninProfileAttributesUpdater(
19 signin::IdentityManager* identity_manager,
20 SigninErrorController* signin_error_controller,
21 ProfileAttributesStorage* profile_attributes_storage,
22 const base::FilePath& profile_path,
23 PrefService* prefs)
24 : identity_manager_(identity_manager),
25 signin_error_controller_(signin_error_controller),
26 profile_attributes_storage_(profile_attributes_storage),
27 profile_path_(profile_path),
28 prefs_(prefs) {
29 DCHECK(identity_manager_);
30 DCHECK(signin_error_controller_);
31 DCHECK(profile_attributes_storage_);
32 identity_manager_observer_.Add(identity_manager_);
33 signin_error_controller_observer_.Add(signin_error_controller);
34
35 UpdateProfileAttributes();
36 // TODO(crbug.com/908457): Call OnErrorChanged() here, to catch any change
37 // that happened since the construction of SigninErrorController. Profile
38 // metrics depend on this bug and must be fixed first.
39 }
40
41 SigninProfileAttributesUpdater::~SigninProfileAttributesUpdater() = default;
42
Shutdown()43 void SigninProfileAttributesUpdater::Shutdown() {
44 identity_manager_observer_.RemoveAll();
45 signin_error_controller_observer_.RemoveAll();
46 }
47
UpdateProfileAttributes()48 void SigninProfileAttributesUpdater::UpdateProfileAttributes() {
49 ProfileAttributesEntry* entry;
50 if (!profile_attributes_storage_->GetProfileAttributesWithPath(profile_path_,
51 &entry)) {
52 return;
53 }
54
55 CoreAccountInfo account_info = identity_manager_->GetPrimaryAccountInfo(
56 signin::ConsentLevel::kNotRequired);
57
58 bool clear_profile = account_info.IsEmpty();
59
60 if (account_info.gaia != entry->GetGAIAId() ||
61 !gaia::AreEmailsSame(account_info.email,
62 base::UTF16ToUTF8(entry->GetUserName()))) {
63 // Reset prefs. Note: this will also update the |ProfileAttributesEntry|.
64 prefs_->ClearPref(prefs::kProfileUsingDefaultAvatar);
65 prefs_->ClearPref(prefs::kProfileUsingGAIAAvatar);
66 }
67
68 if (clear_profile) {
69 entry->SetLocalAuthCredentials(std::string());
70 entry->SetAuthInfo(std::string(), base::string16(), false);
71 if (!signin_util::IsForceSigninEnabled())
72 entry->SetIsSigninRequired(false);
73 } else {
74 entry->SetAuthInfo(account_info.gaia, base::UTF8ToUTF16(account_info.email),
75 identity_manager_->HasPrimaryAccount());
76 }
77 }
78
OnErrorChanged()79 void SigninProfileAttributesUpdater::OnErrorChanged() {
80 ProfileAttributesEntry* entry;
81 if (!profile_attributes_storage_->GetProfileAttributesWithPath(profile_path_,
82 &entry)) {
83 return;
84 }
85
86 entry->SetIsAuthError(signin_error_controller_->HasError());
87 }
88
OnPrimaryAccountSet(const CoreAccountInfo & primary_account_info)89 void SigninProfileAttributesUpdater::OnPrimaryAccountSet(
90 const CoreAccountInfo& primary_account_info) {
91 UpdateProfileAttributes();
92 }
93
OnPrimaryAccountCleared(const CoreAccountInfo & previous_primary_account_info)94 void SigninProfileAttributesUpdater::OnPrimaryAccountCleared(
95 const CoreAccountInfo& previous_primary_account_info) {
96 UpdateProfileAttributes();
97 }
98
OnUnconsentedPrimaryAccountChanged(const CoreAccountInfo & unconsented_primary_account_info)99 void SigninProfileAttributesUpdater::OnUnconsentedPrimaryAccountChanged(
100 const CoreAccountInfo& unconsented_primary_account_info) {
101 if (identity_manager_->HasPrimaryAccount())
102 return;
103
104 UpdateProfileAttributes();
105 }
106