Chromium Code Reviews| Index: chrome/browser/ui/profile_menu_model.cc |
| diff --git a/chrome/browser/ui/profile_menu_model.cc b/chrome/browser/ui/profile_menu_model.cc |
| index 24ecd9a3bcd061ead5a0eda3c70c1bac35a2293f..ead119ce02aa73c49d32ab235b213c063c20ed03 100644 |
| --- a/chrome/browser/ui/profile_menu_model.cc |
| +++ b/chrome/browser/ui/profile_menu_model.cc |
| @@ -4,13 +4,134 @@ |
| #include "chrome/browser/ui/profile_menu_model.h" |
| +#include "base/path_service.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| #include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/profiles/profile_info_cache.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/common/chrome_paths.h" |
| #include "chrome/common/url_constants.h" |
| #include "grit/chromium_strings.h" |
| #include "grit/generated_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| +namespace { |
| + |
| +class SwitchProfileMenuModel : public ui::SimpleMenuModel, |
| + public ui::SimpleMenuModel::Delegate { |
| + public: |
| + SwitchProfileMenuModel(ui::SimpleMenuModel::Delegate* delegate, |
| + Browser* browser); |
| + |
|
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.
|
| + virtual void ExecuteCommand(int command_id) OVERRIDE; |
| + virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
| + virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
| + virtual bool GetAcceleratorForCommandId( |
| + int command_id, |
| + ui::Accelerator* accelerator) OVERRIDE; |
| + |
| + private: |
| + enum { |
| + // The profiles submenu contains a menu item for each profile. For |
| + // the i'th profile the command ID is COMMAND_SWITCH_TO_PROFILE + i. |
| + // If the profile matches the profile of the wrench button's browser |
| + // then the menu item is checked. |
| + COMMAND_SWITCH_TO_PROFILE, |
| + }; |
| + |
| + Browser* browser_; |
| + ui::SimpleMenuModel::Delegate* delegate_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SwitchProfileMenuModel); |
| +}; |
| + |
| +class ProfileSwitchObserver : public ProfileManagerObserver { |
| + virtual void OnProfileCreated(Profile* profile) OVERRIDE { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + Browser* browser = BrowserList::FindTabbedBrowser(profile, false); |
| + if (browser) |
| + browser->window()->Activate(); |
| + else |
| + Browser::NewWindowWithProfile(profile); |
| + } |
| + |
| + virtual bool DeleteAfterCreation() OVERRIDE { return true; } |
| +}; |
| + |
| +SwitchProfileMenuModel::SwitchProfileMenuModel( |
| + ui::SimpleMenuModel::Delegate* delegate, |
| + Browser* browser) |
| + : SimpleMenuModel(this), |
| + browser_(browser), |
| + delegate_(delegate) { |
| + ProfileInfoCache& cache = |
| + g_browser_process->profile_manager()->GetProfileInfoCache(); |
| + size_t count = cache.GetNumberOfProfiles(); |
| + for (size_t i = 0; i < count; ++i) { |
| + AddCheckItem(COMMAND_SWITCH_TO_PROFILE + i, |
| + cache.GetNameOfProfileAtIndex(i)); |
| + } |
| + |
| + AddSeparator(); |
| + |
| + const string16 short_product_name = |
| + l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| + AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( |
| + IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); |
| +} |
| + |
| +void SwitchProfileMenuModel::ExecuteCommand(int command_id) { |
| + ProfileInfoCache& cache = |
| + g_browser_process->profile_manager()->GetProfileInfoCache(); |
| + size_t index = command_id; |
| + if (index < cache.GetNumberOfProfiles()) { |
| + FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| + ProfileSwitchObserver* observer = new ProfileSwitchObserver; |
| + // The observer is deleted by the manager when profile creation is finished. |
| + g_browser_process->profile_manager()->CreateProfileAsync( |
| + profile_path, observer); |
| + } else { |
| + 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
|
| + } |
| +} |
| + |
| +bool SwitchProfileMenuModel::IsCommandIdChecked(int command_id) const { |
| + ProfileInfoCache& cache = |
| + g_browser_process->profile_manager()->GetProfileInfoCache(); |
| + size_t index = command_id; |
| + if (index < cache.GetNumberOfProfiles()) { |
| + FilePath userDataFolder; |
| + PathService::Get(chrome::DIR_USER_DATA, &userDataFolder); |
| + FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| + return browser_->GetProfile()->GetPath() == profile_path; |
| + } |
| + return delegate_->IsCommandIdChecked(command_id); |
| +} |
| + |
| +bool SwitchProfileMenuModel::IsCommandIdEnabled(int command_id) const { |
| + ProfileInfoCache& cache = |
| + g_browser_process->profile_manager()->GetProfileInfoCache(); |
| + size_t index = command_id; |
| + if (index < cache.GetNumberOfProfiles()) |
| + return true; |
| + return delegate_->IsCommandIdEnabled(command_id); |
| +} |
| + |
| +bool SwitchProfileMenuModel::GetAcceleratorForCommandId( |
| + int command_id, |
| + ui::Accelerator* accelerator) { |
| + ProfileInfoCache& cache = |
| + g_browser_process->profile_manager()->GetProfileInfoCache(); |
| + size_t index = command_id; |
| + if (index < cache.GetNumberOfProfiles()) |
| + return false; |
| + return delegate_->GetAcceleratorForCommandId(command_id, accelerator); |
| +} |
| + |
| +} // namespace |
| + |
| ProfileMenuModel::ProfileMenuModel(Browser* browser) |
| : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
| browser_(browser) { |
| @@ -20,8 +141,20 @@ ProfileMenuModel::ProfileMenuModel(Browser* browser) |
| const string16 short_product_name = |
| l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| - AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( |
| - IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); |
| + ProfileInfoCache& cache = |
| + g_browser_process->profile_manager()->GetProfileInfoCache(); |
| + if (cache.GetNumberOfProfiles() > 1) { |
| + switch_profiles_sub_menu_model_.reset( |
| + new SwitchProfileMenuModel(this, browser_)); |
| + AddSubMenu(IDC_PROFILE_MENU, l10n_util::GetStringFUTF16( |
| + IDS_PROFILES_MENU, short_product_name), |
| + switch_profiles_sub_menu_model_.get()); |
| + } else { |
| + switch_profiles_sub_menu_model_.reset(); |
| + AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( |
| + IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); |
| + } |
| + |
| AddItemWithStringId(COMMAND_DELETE_PROFILE, |
| IDS_PROFILES_DELETE_PROFILE); |
| } |
| @@ -48,7 +181,7 @@ void ProfileMenuModel::ExecuteCommand(int command_id) { |
| case COMMAND_CUSTOMIZE_PROFILE: |
| browser_->ShowOptionsTab(chrome::kPersonalOptionsSubPage); |
| break; |
| - case COMMAND_CREATE_NEW_PROFILE: |
| + case IDC_CREATE_NEW_PROFILE: |
| ProfileManager::CreateMultiProfileAsync(); |
| break; |
| case COMMAND_DELETE_PROFILE: |