Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: chrome/browser/profiles/avatar_menu.cc

Issue 1242793005: Refactor most c/b/profiles calls to ProfileInfoCache. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Windows unit test and ChromeOS build Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/avatar_menu.h" 5 #include "chrome/browser/profiles/avatar_menu.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/i18n/case_conversion.h" 8 #include "base/i18n/case_conversion.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/profiler/scoped_tracker.h" 10 #include "base/profiler/scoped_tracker.h"
(...skipping 29 matching lines...) Expand all
40 using content::BrowserThread; 40 using content::BrowserThread;
41 41
42 namespace { 42 namespace {
43 43
44 // Constants for the show profile switcher experiment 44 // Constants for the show profile switcher experiment
45 const char kShowProfileSwitcherFieldTrialName[] = "ShowProfileSwitcher"; 45 const char kShowProfileSwitcherFieldTrialName[] = "ShowProfileSwitcher";
46 const char kAlwaysShowSwitcherGroupName[] = "AlwaysShow"; 46 const char kAlwaysShowSwitcherGroupName[] = "AlwaysShow";
47 47
48 } // namespace 48 } // namespace
49 49
50 AvatarMenu::AvatarMenu(ProfileInfoInterface* profile_cache, 50 AvatarMenu::AvatarMenu(ProfileAttributesStorage* profile_storage,
51 AvatarMenuObserver* observer, 51 AvatarMenuObserver* observer,
52 Browser* browser) 52 Browser* browser)
53 : profile_list_(ProfileList::Create(profile_cache)), 53 : profile_list_(ProfileList::Create(profile_storage)),
54 menu_actions_(AvatarMenuActions::Create()), 54 menu_actions_(AvatarMenuActions::Create()),
55 #if defined(ENABLE_SUPERVISED_USERS) 55 #if defined(ENABLE_SUPERVISED_USERS)
56 supervised_user_observer_(this), 56 supervised_user_observer_(this),
57 #endif 57 #endif
58 profile_info_(profile_cache),
59 observer_(observer), 58 observer_(observer),
60 browser_(browser) { 59 browser_(browser) {
61 DCHECK(profile_info_);
62 // Don't DCHECK(browser_) so that unit tests can reuse this ctor. 60 // Don't DCHECK(browser_) so that unit tests can reuse this ctor.
63 61
64 ActiveBrowserChanged(browser_); 62 ActiveBrowserChanged(browser_);
65 63
66 // Register this as an observer of the info cache. 64 // Register this as an observer of the info cache.
67 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); 65 g_browser_process->profile_manager()->
Mike Lerman 2015/08/06 16:06:19 The ctor was passed a ProfileAttributesStorage; wh
66 GetProfileAttributesStorage().AddObserver(this);
68 67
69 #if defined(ENABLE_SUPERVISED_USERS) 68 #if defined(ENABLE_SUPERVISED_USERS)
70 // Register this as an observer of the SupervisedUserService to be notified 69 // Register this as an observer of the SupervisedUserService to be notified
71 // of changes to the custodian info. 70 // of changes to the custodian info.
72 if (browser_) { 71 if (browser_) {
73 supervised_user_observer_.Add( 72 supervised_user_observer_.Add(
74 SupervisedUserServiceFactory::GetForProfile(browser_->profile())); 73 SupervisedUserServiceFactory::GetForProfile(browser_->profile()));
75 } 74 }
76 #endif 75 #endif
77 } 76 }
78 77
79 AvatarMenu::~AvatarMenu() { 78 AvatarMenu::~AvatarMenu() {
80 g_browser_process->profile_manager()-> 79 g_browser_process->profile_manager()->
81 GetProfileInfoCache().RemoveObserver(this); 80 GetProfileAttributesStorage().RemoveObserver(this);
82 } 81 }
83 82
84 AvatarMenu::Item::Item(size_t menu_index, 83 AvatarMenu::Item::Item(size_t menu_index,
85 size_t profile_index,
86 const gfx::Image& icon) 84 const gfx::Image& icon)
87 : icon(icon), 85 : icon(icon),
88 active(false), 86 active(false),
89 signed_in(false), 87 signed_in(false),
90 signin_required(false), 88 signin_required(false),
91 menu_index(menu_index), 89 menu_index(menu_index) {
92 profile_index(profile_index) {
93 } 90 }
94 91
95 AvatarMenu::Item::~Item() { 92 AvatarMenu::Item::~Item() {
96 } 93 }
97 94
98 // static 95 // static
99 bool AvatarMenu::ShouldShowAvatarMenu() { 96 bool AvatarMenu::ShouldShowAvatarMenu() {
100 if (base::FieldTrialList::FindFullName(kShowProfileSwitcherFieldTrialName) == 97 if (base::FieldTrialList::FindFullName(kShowProfileSwitcherFieldTrialName) ==
101 kAlwaysShowSwitcherGroupName) { 98 kAlwaysShowSwitcherGroupName) {
102 // We should only be in this group when multi-profiles is enabled. 99 // We should only be in this group when multi-profiles is enabled.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 if (switches::IsNewAvatarMenu()) { 132 if (switches::IsNewAvatarMenu()) {
136 // Don't open a browser window for signed-out profiles. 133 // Don't open a browser window for signed-out profiles.
137 if (item.signin_required) { 134 if (item.signin_required) {
138 UserManager::Show(item.profile_path, 135 UserManager::Show(item.profile_path,
139 profiles::USER_MANAGER_NO_TUTORIAL, 136 profiles::USER_MANAGER_NO_TUTORIAL,
140 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); 137 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
141 return; 138 return;
142 } 139 }
143 } 140 }
144 141
145 base::FilePath path =
146 profile_info_->GetPathOfProfileAtIndex(item.profile_index);
147
148 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop(); 142 chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop();
149 if (browser_) 143 if (browser_)
150 desktop_type = browser_->host_desktop_type(); 144 desktop_type = browser_->host_desktop_type();
151 145
152 profiles::SwitchToProfile(path, desktop_type, always_create, 146 profiles::SwitchToProfile(item.profile_path, desktop_type, always_create,
153 ProfileManager::CreateCallback(), 147 ProfileManager::CreateCallback(),
154 metric); 148 metric);
155 } 149 }
156 150
157 void AvatarMenu::AddNewProfile(ProfileMetrics::ProfileAdd type) { 151 void AvatarMenu::AddNewProfile(ProfileMetrics::ProfileAdd type) {
158 menu_actions_->AddNewProfile(type); 152 menu_actions_->AddNewProfile(type);
159 } 153 }
160 154
161 void AvatarMenu::EditProfile(size_t index) { 155 void AvatarMenu::EditProfile(size_t index) {
162 // Get the index in the profile cache from the menu index. 156 base::FilePath profile_path = profile_list_->GetItemAt(index).profile_path;
163 size_t profile_index = profile_list_->GetItemAt(index).profile_index;
164
165 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( 157 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath(
166 profile_info_->GetPathOfProfileAtIndex(profile_index)); 158 profile_path);
Mike Lerman 2015/08/06 16:06:19 Don't need to declare a FilePath, you can just inl
167 159 menu_actions_->EditProfile(profile);
168 menu_actions_->EditProfile(profile, profile_index);
169 } 160 }
170 161
171 void AvatarMenu::RebuildMenu() { 162 void AvatarMenu::RebuildMenu() {
172 profile_list_->RebuildMenu(); 163 profile_list_->RebuildMenu();
173 } 164 }
174 165
175 size_t AvatarMenu::GetNumberOfItems() const { 166 size_t AvatarMenu::GetNumberOfItems() const {
176 return profile_list_->GetNumberOfItems(); 167 return profile_list_->GetNumberOfItems();
177 } 168 }
178 169
179 const AvatarMenu::Item& AvatarMenu::GetItemAt(size_t index) const { 170 const AvatarMenu::Item& AvatarMenu::GetItemAt(size_t index) const {
180 return profile_list_->GetItemAt(index); 171 return profile_list_->GetItemAt(index);
181 } 172 }
173
174 size_t AvatarMenu::GetIndexOfItemWithProfilePath(const base::FilePath& path) {
175 return profile_list_->MenuIndexFromProfilePath(path);
176 }
177
182 size_t AvatarMenu::GetActiveProfileIndex() { 178 size_t AvatarMenu::GetActiveProfileIndex() {
183 179
184 // During singleton profile deletion, this function can be called with no 180 // During singleton profile deletion, this function can be called with no
185 // profiles in the model - crbug.com/102278 . 181 // profiles in the model - crbug.com/102278 .
186 if (profile_list_->GetNumberOfItems() == 0) 182 if (profile_list_->GetNumberOfItems() == 0)
187 return 0; 183 return 0;
188 184
189 Profile* active_profile = NULL; 185 Profile* active_profile = NULL;
190 if (!browser_) 186 if (!browser_)
191 active_profile = ProfileManager::GetLastUsedProfile(); 187 active_profile = ProfileManager::GetLastUsedProfile();
192 else 188 else
193 active_profile = browser_->profile(); 189 active_profile = browser_->profile();
194 190
195 size_t index = 191 size_t index =
196 profile_info_->GetIndexOfProfileWithPath(active_profile->GetPath()); 192 profile_list_->MenuIndexFromProfilePath(active_profile->GetPath());
197
198 index = profile_list_->MenuIndexFromProfileIndex(index);
199 DCHECK_LT(index, profile_list_->GetNumberOfItems()); 193 DCHECK_LT(index, profile_list_->GetNumberOfItems());
200 return index; 194 return index;
201 } 195 }
202 196
203 base::string16 AvatarMenu::GetSupervisedUserInformation() const { 197 base::string16 AvatarMenu::GetSupervisedUserInformation() const {
204 // |browser_| can be NULL in unit_tests. 198 // |browser_| can be NULL in unit_tests.
205 if (browser_ && browser_->profile()->IsSupervised()) { 199 if (browser_ && browser_->profile()->IsSupervised()) {
206 #if defined(ENABLE_SUPERVISED_USERS) 200 #if defined(ENABLE_SUPERVISED_USERS)
207 SupervisedUserService* service = 201 SupervisedUserService* service =
208 SupervisedUserServiceFactory::GetForProfile(browser_->profile()); 202 SupervisedUserServiceFactory::GetForProfile(browser_->profile());
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 if (observer_) 293 if (observer_)
300 observer_->OnAvatarMenuChanged(this); 294 observer_->OnAvatarMenuChanged(this);
301 } 295 }
302 #endif 296 #endif
303 297
304 void AvatarMenu::Update() { 298 void AvatarMenu::Update() {
305 RebuildMenu(); 299 RebuildMenu();
306 if (observer_) 300 if (observer_)
307 observer_->OnAvatarMenuChanged(this); 301 observer_->OnAvatarMenuChanged(this);
308 } 302 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698