OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/profile_menu_model.h" | 5 #include "chrome/browser/ui/profile_menu_model.h" |
6 | 6 |
| 7 #include "base/path_service.h" |
7 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/profile_info_cache.h" |
8 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "chrome/common/chrome_paths.h" |
9 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
10 #include "grit/chromium_strings.h" | 14 #include "grit/chromium_strings.h" |
11 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
12 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
13 | 17 |
| 18 namespace { |
| 19 |
| 20 enum { |
| 21 COMMAND_CUSTOMIZE_PROFILE, |
| 22 COMMAND_DELETE_PROFILE, |
| 23 COMMAND_CREATE_NEW_PROFILE, |
| 24 COMMAND_SWITCH_PROFILE_MENU, |
| 25 // The profiles submenu contains a menu item for each profile. For |
| 26 // the i'th profile the command ID is COMMAND_SWITCH_TO_PROFILE + i. |
| 27 // Since there can be any number of profiles this must be the last command id. |
| 28 COMMAND_SWITCH_TO_PROFILE, |
| 29 }; |
| 30 |
| 31 class SwitchProfileMenuModel : public ui::SimpleMenuModel, |
| 32 public ui::SimpleMenuModel::Delegate { |
| 33 public: |
| 34 SwitchProfileMenuModel(ui::SimpleMenuModel::Delegate* delegate, |
| 35 Browser* browser); |
| 36 |
| 37 // Overridden from ui::SimpleMenuModel::Delegate. |
| 38 virtual void ExecuteCommand(int command_id) OVERRIDE; |
| 39 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
| 40 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
| 41 virtual bool GetAcceleratorForCommandId( |
| 42 int command_id, |
| 43 ui::Accelerator* accelerator) OVERRIDE; |
| 44 |
| 45 private: |
| 46 bool IsSwitchProfileCommand(int command_id) const; |
| 47 size_t GetProfileIndexFromSwitchProfileCommand(int command_id) const; |
| 48 |
| 49 Browser* browser_; |
| 50 ui::SimpleMenuModel::Delegate* delegate_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(SwitchProfileMenuModel); |
| 53 }; |
| 54 |
| 55 class ProfileSwitchObserver : public ProfileManagerObserver { |
| 56 virtual void OnProfileCreated(Profile* profile) OVERRIDE { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 |
| 59 Browser* browser = BrowserList::FindTabbedBrowser(profile, false); |
| 60 if (browser) |
| 61 browser->window()->Activate(); |
| 62 else |
| 63 Browser::NewWindowWithProfile(profile); |
| 64 } |
| 65 |
| 66 virtual bool DeleteAfterCreation() OVERRIDE { return true; } |
| 67 }; |
| 68 |
| 69 SwitchProfileMenuModel::SwitchProfileMenuModel( |
| 70 ui::SimpleMenuModel::Delegate* delegate, |
| 71 Browser* browser) |
| 72 : SimpleMenuModel(this), |
| 73 browser_(browser), |
| 74 delegate_(delegate) { |
| 75 ProfileInfoCache& cache = |
| 76 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 77 size_t count = cache.GetNumberOfProfiles(); |
| 78 for (size_t i = 0; i < count; ++i) { |
| 79 AddCheckItem(COMMAND_SWITCH_TO_PROFILE + i, |
| 80 cache.GetNameOfProfileAtIndex(i)); |
| 81 } |
| 82 |
| 83 AddSeparator(); |
| 84 |
| 85 const string16 short_product_name = |
| 86 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| 87 AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( |
| 88 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); |
| 89 } |
| 90 |
| 91 void SwitchProfileMenuModel::ExecuteCommand(int command_id) { |
| 92 ProfileInfoCache& cache = |
| 93 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 94 if (IsSwitchProfileCommand(command_id)) { |
| 95 size_t index = GetProfileIndexFromSwitchProfileCommand(command_id); |
| 96 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| 97 ProfileSwitchObserver* observer = new ProfileSwitchObserver; |
| 98 // The observer is deleted by the manager when profile creation is finished. |
| 99 g_browser_process->profile_manager()->CreateProfileAsync( |
| 100 profile_path, observer); |
| 101 } else { |
| 102 delegate_->ExecuteCommand(command_id); |
| 103 } |
| 104 } |
| 105 |
| 106 bool SwitchProfileMenuModel::IsCommandIdChecked(int command_id) const { |
| 107 ProfileInfoCache& cache = |
| 108 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 109 if (IsSwitchProfileCommand(command_id)) { |
| 110 size_t index = GetProfileIndexFromSwitchProfileCommand(command_id); |
| 111 FilePath userDataFolder; |
| 112 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder); |
| 113 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| 114 return browser_->GetProfile()->GetPath() == profile_path; |
| 115 } |
| 116 return delegate_->IsCommandIdChecked(command_id); |
| 117 } |
| 118 |
| 119 bool SwitchProfileMenuModel::IsCommandIdEnabled(int command_id) const { |
| 120 if (IsSwitchProfileCommand(command_id)) |
| 121 return true; |
| 122 return delegate_->IsCommandIdEnabled(command_id); |
| 123 } |
| 124 |
| 125 bool SwitchProfileMenuModel::GetAcceleratorForCommandId( |
| 126 int command_id, |
| 127 ui::Accelerator* accelerator) { |
| 128 if (IsSwitchProfileCommand(command_id)) |
| 129 return false; |
| 130 return delegate_->GetAcceleratorForCommandId(command_id, accelerator); |
| 131 } |
| 132 |
| 133 bool SwitchProfileMenuModel::IsSwitchProfileCommand(int command_id) const { |
| 134 return command_id >= COMMAND_SWITCH_TO_PROFILE; |
| 135 } |
| 136 |
| 137 size_t SwitchProfileMenuModel::GetProfileIndexFromSwitchProfileCommand( |
| 138 int command_id) const { |
| 139 DCHECK(IsSwitchProfileCommand(command_id)); |
| 140 return command_id - COMMAND_SWITCH_TO_PROFILE; |
| 141 } |
| 142 |
| 143 } // namespace |
| 144 |
14 ProfileMenuModel::ProfileMenuModel(Browser* browser) | 145 ProfileMenuModel::ProfileMenuModel(Browser* browser) |
15 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 146 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
16 browser_(browser) { | 147 browser_(browser) { |
17 AddItemWithStringId(COMMAND_CUSTOMIZE_PROFILE, | 148 AddItemWithStringId(COMMAND_CUSTOMIZE_PROFILE, |
18 IDS_PROFILES_CUSTOMIZE_PROFILE); | 149 IDS_PROFILES_CUSTOMIZE_PROFILE); |
19 AddSeparator(); | 150 AddSeparator(); |
20 | 151 |
21 const string16 short_product_name = | 152 const string16 short_product_name = |
22 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | 153 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
23 AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( | 154 ProfileInfoCache& cache = |
24 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); | 155 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 156 if (cache.GetNumberOfProfiles() > 1) { |
| 157 switch_profiles_sub_menu_model_.reset( |
| 158 new SwitchProfileMenuModel(this, browser_)); |
| 159 AddSubMenu(COMMAND_SWITCH_PROFILE_MENU, l10n_util::GetStringFUTF16( |
| 160 IDS_PROFILES_MENU, short_product_name), |
| 161 switch_profiles_sub_menu_model_.get()); |
| 162 } else { |
| 163 switch_profiles_sub_menu_model_.reset(); |
| 164 AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( |
| 165 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); |
| 166 } |
| 167 |
25 AddItemWithStringId(COMMAND_DELETE_PROFILE, | 168 AddItemWithStringId(COMMAND_DELETE_PROFILE, |
26 IDS_PROFILES_DELETE_PROFILE); | 169 IDS_PROFILES_DELETE_PROFILE); |
27 } | 170 } |
28 | 171 |
29 ProfileMenuModel::~ProfileMenuModel() { | 172 ProfileMenuModel::~ProfileMenuModel() { |
30 } | 173 } |
31 | 174 |
32 // ui::SimpleMenuModel::Delegate implementation | 175 // ui::SimpleMenuModel::Delegate implementation |
33 bool ProfileMenuModel::IsCommandIdChecked(int command_id) const { | 176 bool ProfileMenuModel::IsCommandIdChecked(int command_id) const { |
34 return false; | 177 return false; |
(...skipping 18 matching lines...) Expand all Loading... |
53 break; | 196 break; |
54 case COMMAND_DELETE_PROFILE: | 197 case COMMAND_DELETE_PROFILE: |
55 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | 198 g_browser_process->profile_manager()->ScheduleProfileForDeletion( |
56 browser_->profile()->GetPath()); | 199 browser_->profile()->GetPath()); |
57 break; | 200 break; |
58 default: | 201 default: |
59 NOTREACHED(); | 202 NOTREACHED(); |
60 break; | 203 break; |
61 } | 204 } |
62 } | 205 } |
OLD | NEW |