Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / avatar_menu.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/profiles/avatar_menu.h"
6
7 #include "ash/ash_switches.h"
8 #include "base/bind.h"
9 #include "base/i18n/case_conversion.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/profiles/avatar_menu_actions.h"
15 #include "chrome/browser/profiles/avatar_menu_observer.h"
16 #include "chrome/browser/profiles/profile_list.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/profiles/profile_metrics.h"
19 #include "chrome/browser/profiles/profile_window.h"
20 #include "chrome/browser/profiles/profiles_state.h"
21 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_dialogs.h"
24 #include "chrome/browser/ui/host_desktop.h"
25 #include "chrome/browser/ui/startup/startup_browser_creator.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/profile_management_switches.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/notification_service.h"
30 #include "grit/generated_resources.h"
31 #include "grit/theme_resources.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/resource/resource_bundle.h"
34
35 #if defined(ENABLE_MANAGED_USERS)
36 #include "chrome/browser/managed_mode/managed_user_service.h"
37 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
38 #endif
39
40 using content::BrowserThread;
41
42 namespace {
43
44 // Constants for the show profile switcher experiment
45 const char kShowProfileSwitcherFieldTrialName[] = "ShowProfileSwitcher";
46 const char kAlwaysShowSwitcherGroupName[] = "AlwaysShow";
47
48 }  // namespace
49
50 AvatarMenu::AvatarMenu(ProfileInfoInterface* profile_cache,
51                        AvatarMenuObserver* observer,
52                        Browser* browser)
53     : profile_list_(ProfileList::Create(profile_cache)),
54       menu_actions_(AvatarMenuActions::Create()),
55       profile_info_(profile_cache),
56       observer_(observer),
57       browser_(browser) {
58   DCHECK(profile_info_);
59   // Don't DCHECK(browser_) so that unit tests can reuse this ctor.
60
61   ActiveBrowserChanged(browser_);
62
63   // Register this as an observer of the info cache.
64   registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
65       content::NotificationService::AllSources());
66 }
67
68 AvatarMenu::~AvatarMenu() {
69 }
70
71 AvatarMenu::Item::Item(size_t menu_index,
72                        size_t profile_index,
73                        const gfx::Image& icon)
74     : icon(icon),
75       active(false),
76       signed_in(false),
77       signin_required(false),
78       menu_index(menu_index),
79       profile_index(profile_index) {
80 }
81
82 AvatarMenu::Item::~Item() {
83 }
84
85 // static
86 bool AvatarMenu::ShouldShowAvatarMenu() {
87   if (base::FieldTrialList::FindFullName(kShowProfileSwitcherFieldTrialName) ==
88       kAlwaysShowSwitcherGroupName) {
89     // We should only be in this group when multi-profiles is enabled.
90     DCHECK(profiles::IsMultipleProfilesEnabled());
91     return true;
92   }
93
94   // TODO: Eliminate this ifdef. Add a delegate interface for the menu which
95   // would also help remove the Browser dependency in AvatarMenuActions
96   // implementations.
97   if (profiles::IsMultipleProfilesEnabled()) {
98 #if defined(OS_CHROMEOS)
99     // On ChromeOS the menu will not be shown.
100     return false;
101 #else
102     return switches::IsNewProfileManagement() ||
103            (g_browser_process->profile_manager() &&
104             g_browser_process->profile_manager()->GetNumberOfProfiles() > 1);
105 #endif
106   }
107   return false;
108 }
109
110 bool AvatarMenu::CompareItems(const Item* item1, const Item* item2) {
111   return base::i18n::ToLower(item1->name).compare(
112       base::i18n::ToLower(item2->name)) < 0;
113 }
114
115 void AvatarMenu::SwitchToProfile(size_t index, bool always_create) {
116   DCHECK(profiles::IsMultipleProfilesEnabled() ||
117          index == GetActiveProfileIndex());
118   const Item& item = GetItemAt(index);
119
120   if (switches::IsNewProfileManagement()) {
121     // Don't open a browser window for signed-out profiles.
122     if (item.signin_required) {
123       chrome::ShowUserManager(item.profile_path);
124       return;
125     }
126   }
127
128   base::FilePath path =
129       profile_info_->GetPathOfProfileAtIndex(item.profile_index);
130
131   chrome::HostDesktopType desktop_type = chrome::GetActiveDesktop();
132   if (browser_)
133     desktop_type = browser_->host_desktop_type();
134
135   profiles::SwitchToProfile(path, desktop_type, always_create,
136                             profiles::ProfileSwitchingDoneCallback());
137   ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::SWITCH_PROFILE_ICON);
138 }
139
140 void AvatarMenu::AddNewProfile(ProfileMetrics::ProfileAdd type) {
141   menu_actions_->AddNewProfile(type);
142 }
143
144 void AvatarMenu::EditProfile(size_t index) {
145   // Get the index in the profile cache from the menu index.
146   size_t profile_index = profile_list_->GetItemAt(index).profile_index;
147
148   Profile* profile = g_browser_process->profile_manager()->GetProfileByPath(
149         profile_info_->GetPathOfProfileAtIndex(profile_index));
150
151   menu_actions_->EditProfile(profile, profile_index);
152 }
153
154 void AvatarMenu::RebuildMenu() {
155   profile_list_->RebuildMenu();
156 }
157
158 size_t AvatarMenu::GetNumberOfItems() const {
159   return profile_list_->GetNumberOfItems();
160 }
161
162 const AvatarMenu::Item& AvatarMenu::GetItemAt(size_t index) const {
163   return profile_list_->GetItemAt(index);
164 }
165 size_t AvatarMenu::GetActiveProfileIndex() {
166
167   // During singleton profile deletion, this function can be called with no
168   // profiles in the model - crbug.com/102278 .
169   if (profile_list_->GetNumberOfItems() == 0)
170     return 0;
171
172   Profile* active_profile = NULL;
173   if (!browser_)
174     active_profile = ProfileManager::GetLastUsedProfile();
175   else
176     active_profile = browser_->profile();
177
178   size_t index =
179       profile_info_->GetIndexOfProfileWithPath(active_profile->GetPath());
180
181   index = profile_list_->MenuIndexFromProfileIndex(index);
182   DCHECK_LT(index, profile_list_->GetNumberOfItems());
183   return index;
184 }
185
186 base::string16 AvatarMenu::GetManagedUserInformation() const {
187   // |browser_| can be NULL in unit_tests.
188   if (browser_ && browser_->profile()->IsManaged()) {
189 #if defined(ENABLE_MANAGED_USERS)
190     ManagedUserService* service = ManagedUserServiceFactory::GetForProfile(
191         browser_->profile());
192     base::string16 custodian =
193         base::UTF8ToUTF16(service->GetCustodianEmailAddress());
194     return l10n_util::GetStringFUTF16(IDS_MANAGED_USER_INFO, custodian);
195 #endif
196   }
197   return base::string16();
198 }
199
200 const gfx::Image& AvatarMenu::GetManagedUserIcon() const {
201   return ResourceBundle::GetSharedInstance().GetNativeImageNamed(
202       IDR_MANAGED_USER_ICON);
203 }
204
205 void AvatarMenu::ActiveBrowserChanged(Browser* browser) {
206   browser_ = browser;
207   menu_actions_->ActiveBrowserChanged(browser);
208
209   // If browser is not NULL, get the path of its active profile.
210   base::FilePath path;
211   if (browser)
212     path = browser->profile()->GetPath();
213   profile_list_->ActiveProfilePathChanged(path);
214 }
215
216 bool AvatarMenu::ShouldShowAddNewProfileLink() const {
217   return menu_actions_->ShouldShowAddNewProfileLink();
218 }
219
220 bool AvatarMenu::ShouldShowEditProfileLink() const {
221   return menu_actions_->ShouldShowEditProfileLink();
222 }
223
224 void AvatarMenu::Observe(int type,
225                          const content::NotificationSource& source,
226                          const content::NotificationDetails& details) {
227   DCHECK_EQ(chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, type);
228   RebuildMenu();
229   if (observer_)
230     observer_->OnAvatarMenuChanged(this);
231 }