Chromium Code Reviews| 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" | |
| 8 #include "chrome/app/chrome_command_ids.h" | |
| 7 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 8 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/ui/browser_window.h" | |
| 13 #include "chrome/common/chrome_paths.h" | |
| 9 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 10 #include "grit/chromium_strings.h" | 15 #include "grit/chromium_strings.h" |
| 11 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 13 | 18 |
| 19 namespace { | |
| 20 | |
| 21 class SwitchProfileMenuModel : public ui::SimpleMenuModel, | |
| 22 public ui::SimpleMenuModel::Delegate { | |
| 23 public: | |
| 24 SwitchProfileMenuModel(ui::SimpleMenuModel::Delegate* delegate, | |
| 25 Browser* browser); | |
| 26 | |
|
Miranda Callahan
2011/07/08 14:39:25
could you add a comment here that these are overri
sail
2011/07/08 15:52:06
Done.
| |
| 27 virtual void ExecuteCommand(int command_id) OVERRIDE; | |
| 28 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | |
| 29 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | |
| 30 virtual bool GetAcceleratorForCommandId( | |
| 31 int command_id, | |
| 32 ui::Accelerator* accelerator) OVERRIDE; | |
| 33 | |
| 34 private: | |
| 35 enum { | |
| 36 // The profiles submenu contains a menu item for each profile. For | |
| 37 // the i'th profile the command ID is COMMAND_SWITCH_TO_PROFILE + i. | |
| 38 // If the profile matches the profile of the wrench button's browser | |
| 39 // then the menu item is checked. | |
| 40 COMMAND_SWITCH_TO_PROFILE, | |
| 41 }; | |
| 42 | |
| 43 Browser* browser_; | |
| 44 ui::SimpleMenuModel::Delegate* delegate_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(SwitchProfileMenuModel); | |
| 47 }; | |
| 48 | |
| 49 class ProfileSwitchObserver : public ProfileManagerObserver { | |
| 50 virtual void OnProfileCreated(Profile* profile) OVERRIDE { | |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 52 | |
| 53 Browser* browser = BrowserList::FindTabbedBrowser(profile, false); | |
| 54 if (browser) | |
| 55 browser->window()->Activate(); | |
| 56 else | |
| 57 Browser::NewWindowWithProfile(profile); | |
| 58 } | |
| 59 | |
| 60 virtual bool DeleteAfterCreation() OVERRIDE { return true; } | |
| 61 }; | |
| 62 | |
| 63 SwitchProfileMenuModel::SwitchProfileMenuModel( | |
| 64 ui::SimpleMenuModel::Delegate* delegate, | |
| 65 Browser* browser) | |
| 66 : SimpleMenuModel(this), | |
| 67 browser_(browser), | |
| 68 delegate_(delegate) { | |
| 69 ProfileInfoCache& cache = | |
| 70 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 71 size_t count = cache.GetNumberOfProfiles(); | |
| 72 for (size_t i = 0; i < count; ++i) { | |
| 73 AddCheckItem(COMMAND_SWITCH_TO_PROFILE + i, | |
| 74 cache.GetNameOfProfileAtIndex(i)); | |
| 75 } | |
| 76 | |
| 77 AddSeparator(); | |
| 78 | |
| 79 const string16 short_product_name = | |
| 80 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | |
| 81 AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( | |
| 82 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); | |
| 83 } | |
| 84 | |
| 85 void SwitchProfileMenuModel::ExecuteCommand(int command_id) { | |
| 86 ProfileInfoCache& cache = | |
| 87 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 88 size_t index = command_id; | |
| 89 if (index < cache.GetNumberOfProfiles()) { | |
| 90 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); | |
| 91 ProfileSwitchObserver* observer = new ProfileSwitchObserver; | |
| 92 // The observer is deleted by the manager when profile creation is finished. | |
| 93 g_browser_process->profile_manager()->CreateProfileAsync( | |
| 94 profile_path, observer); | |
| 95 } else { | |
| 96 delegate_->ExecuteCommand(command_id); | |
|
Miranda Callahan
2011/07/08 14:39:25
I wonder if you should make explicit that this onl
sail
2011/07/08 15:52:06
Good point. I fixed this by getting rid of IDC_CRE
| |
| 97 } | |
| 98 } | |
| 99 | |
| 100 bool SwitchProfileMenuModel::IsCommandIdChecked(int command_id) const { | |
| 101 ProfileInfoCache& cache = | |
| 102 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 103 size_t index = command_id; | |
| 104 if (index < cache.GetNumberOfProfiles()) { | |
| 105 FilePath userDataFolder; | |
| 106 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder); | |
| 107 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); | |
| 108 return browser_->GetProfile()->GetPath() == profile_path; | |
| 109 } | |
| 110 return delegate_->IsCommandIdChecked(command_id); | |
| 111 } | |
| 112 | |
| 113 bool SwitchProfileMenuModel::IsCommandIdEnabled(int command_id) const { | |
| 114 ProfileInfoCache& cache = | |
| 115 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 116 size_t index = command_id; | |
| 117 if (index < cache.GetNumberOfProfiles()) | |
| 118 return true; | |
| 119 return delegate_->IsCommandIdEnabled(command_id); | |
| 120 } | |
| 121 | |
| 122 bool SwitchProfileMenuModel::GetAcceleratorForCommandId( | |
| 123 int command_id, | |
| 124 ui::Accelerator* accelerator) { | |
| 125 ProfileInfoCache& cache = | |
| 126 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 127 size_t index = command_id; | |
| 128 if (index < cache.GetNumberOfProfiles()) | |
| 129 return false; | |
| 130 return delegate_->GetAcceleratorForCommandId(command_id, accelerator); | |
| 131 } | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 14 ProfileMenuModel::ProfileMenuModel(Browser* browser) | 135 ProfileMenuModel::ProfileMenuModel(Browser* browser) |
| 15 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 136 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
| 16 browser_(browser) { | 137 browser_(browser) { |
| 17 AddItemWithStringId(COMMAND_CUSTOMIZE_PROFILE, | 138 AddItemWithStringId(COMMAND_CUSTOMIZE_PROFILE, |
| 18 IDS_PROFILES_CUSTOMIZE_PROFILE); | 139 IDS_PROFILES_CUSTOMIZE_PROFILE); |
| 19 AddSeparator(); | 140 AddSeparator(); |
| 20 | 141 |
| 21 const string16 short_product_name = | 142 const string16 short_product_name = |
| 22 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | 143 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| 23 AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( | 144 ProfileInfoCache& cache = |
| 24 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); | 145 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 146 if (cache.GetNumberOfProfiles() > 1) { | |
| 147 switch_profiles_sub_menu_model_.reset( | |
| 148 new SwitchProfileMenuModel(this, browser_)); | |
| 149 AddSubMenu(IDC_PROFILE_MENU, l10n_util::GetStringFUTF16( | |
| 150 IDS_PROFILES_MENU, short_product_name), | |
| 151 switch_profiles_sub_menu_model_.get()); | |
| 152 } else { | |
| 153 switch_profiles_sub_menu_model_.reset(); | |
| 154 AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( | |
| 155 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); | |
| 156 } | |
| 157 | |
| 25 AddItemWithStringId(COMMAND_DELETE_PROFILE, | 158 AddItemWithStringId(COMMAND_DELETE_PROFILE, |
| 26 IDS_PROFILES_DELETE_PROFILE); | 159 IDS_PROFILES_DELETE_PROFILE); |
| 27 } | 160 } |
| 28 | 161 |
| 29 ProfileMenuModel::~ProfileMenuModel() { | 162 ProfileMenuModel::~ProfileMenuModel() { |
| 30 } | 163 } |
| 31 | 164 |
| 32 // ui::SimpleMenuModel::Delegate implementation | 165 // ui::SimpleMenuModel::Delegate implementation |
| 33 bool ProfileMenuModel::IsCommandIdChecked(int command_id) const { | 166 bool ProfileMenuModel::IsCommandIdChecked(int command_id) const { |
| 34 return false; | 167 return false; |
| 35 } | 168 } |
| 36 | 169 |
| 37 bool ProfileMenuModel::IsCommandIdEnabled(int command_id) const { | 170 bool ProfileMenuModel::IsCommandIdEnabled(int command_id) const { |
| 38 return true; | 171 return true; |
| 39 } | 172 } |
| 40 | 173 |
| 41 bool ProfileMenuModel::GetAcceleratorForCommandId(int command_id, | 174 bool ProfileMenuModel::GetAcceleratorForCommandId(int command_id, |
| 42 ui::Accelerator* accelerator) { | 175 ui::Accelerator* accelerator) { |
| 43 return false; | 176 return false; |
| 44 } | 177 } |
| 45 | 178 |
| 46 void ProfileMenuModel::ExecuteCommand(int command_id) { | 179 void ProfileMenuModel::ExecuteCommand(int command_id) { |
| 47 switch (command_id) { | 180 switch (command_id) { |
| 48 case COMMAND_CUSTOMIZE_PROFILE: | 181 case COMMAND_CUSTOMIZE_PROFILE: |
| 49 browser_->ShowOptionsTab(chrome::kPersonalOptionsSubPage); | 182 browser_->ShowOptionsTab(chrome::kPersonalOptionsSubPage); |
| 50 break; | 183 break; |
| 51 case COMMAND_CREATE_NEW_PROFILE: | 184 case IDC_CREATE_NEW_PROFILE: |
| 52 ProfileManager::CreateMultiProfileAsync(); | 185 ProfileManager::CreateMultiProfileAsync(); |
| 53 break; | 186 break; |
| 54 case COMMAND_DELETE_PROFILE: | 187 case COMMAND_DELETE_PROFILE: |
| 55 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | 188 g_browser_process->profile_manager()->ScheduleProfileForDeletion( |
| 56 browser_->profile()->GetPath()); | 189 browser_->profile()->GetPath()); |
| 57 break; | 190 break; |
| 58 default: | 191 default: |
| 59 NOTREACHED(); | 192 NOTREACHED(); |
| 60 break; | 193 break; |
| 61 } | 194 } |
| 62 } | 195 } |
| OLD | NEW |