Chromium Code Reviews| Index: chrome/browser/ui/toolbar/wrench_menu_model.cc |
| diff --git a/chrome/browser/ui/toolbar/wrench_menu_model.cc b/chrome/browser/ui/toolbar/wrench_menu_model.cc |
| index 6d97478615b78cb8e533a0621c5fe6e46cda60a5..8f9260b4aeb27af6999a25d2917272883fb2c007 100644 |
| --- a/chrome/browser/ui/toolbar/wrench_menu_model.cc |
| +++ b/chrome/browser/ui/toolbar/wrench_menu_model.cc |
| @@ -9,6 +9,7 @@ |
| #include "base/command_line.h" |
| #include "base/i18n/number_formatting.h" |
| +#include "base/path_service.h" |
| #include "base/string_number_conversions.h" |
| #include "base/string_util.h" |
| #include "base/utf_string_conversions.h" |
| @@ -17,6 +18,7 @@ |
| #include "chrome/browser/defaults.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| #include "chrome/browser/sync/profile_sync_service.h" |
| #include "chrome/browser/sync/sync_ui_util.h" |
| #include "chrome/browser/tabs/tab_strip_model.h" |
| @@ -24,6 +26,7 @@ |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/toolbar/encoding_menu_controller.h" |
| #include "chrome/browser/upgrade_detector.h" |
| +#include "chrome/common/chrome_paths.h" |
| #include "chrome/common/chrome_switches.h" |
| #include "chrome/common/pref_names.h" |
| #include "chrome/common/profiling.h" |
| @@ -210,6 +213,95 @@ void BookmarkSubMenuModel::Build(Browser* browser) { |
| //////////////////////////////////////////////////////////////////////////////// |
| +// ProfilesSubMenuModel |
| + |
| +ProfilesSubMenuModel::ProfilesSubMenuModel( |
| + ui::SimpleMenuModel::Delegate* delegate, Browser* browser) |
| + : SimpleMenuModel(this), |
| + browser_(browser), |
| + delegate_(delegate) { |
| + Build(); |
| +} |
| + |
| +void ProfilesSubMenuModel::Build() { |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + size_t count = profile_manager->GetNumberOfProfiles(); |
| + for (size_t i = 0; i < count; ++i) { |
| + AddCheckItem(COMMAND_SWITCH_TO_PROFILE + i, |
| + profile_manager->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)); |
| +} |
| + |
| +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; } |
| +}; |
| + |
| +void ProfilesSubMenuModel::ExecuteCommand(int command_id) { |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + size_t index = command_id; |
| + if (index < profile_manager->GetNumberOfProfiles()) { |
| + FilePath userDataFolder; |
| + PathService::Get(chrome::DIR_USER_DATA, &userDataFolder); |
| + FilePath profile_path = |
| + profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder); |
| + |
| + ProfileSwitchObserver* observer = new ProfileSwitchObserver; |
| + // The observer is deleted by the manager when profile creation is finished. |
| + profile_manager->CreateProfileAsync(profile_path, observer); |
| + } else { |
| + delegate_->ExecuteCommand(command_id); |
| + } |
| +} |
| + |
| +bool ProfilesSubMenuModel::IsCommandIdChecked(int command_id) const { |
|
Miranda Callahan
2011/06/13 22:08:11
Are these really going to be checkboxed menu items
sail
2011/06/13 23:08:46
I found that the checkmark helped when switching b
|
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + size_t index = command_id; |
| + if (index < profile_manager->GetNumberOfProfiles()) { |
| + FilePath userDataFolder; |
| + PathService::Get(chrome::DIR_USER_DATA, &userDataFolder); |
| + FilePath profile_path = |
| + profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder); |
| + return browser_->GetProfile()->GetPath() == profile_path; |
| + } |
| + return delegate_->IsCommandIdChecked(command_id); |
| +} |
| + |
| +bool ProfilesSubMenuModel::IsCommandIdEnabled(int command_id) const { |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + size_t index = command_id; |
| + if (index < profile_manager->GetNumberOfProfiles()) |
| + return true; |
| + return delegate_->IsCommandIdEnabled(command_id); |
| +} |
| + |
| +bool ProfilesSubMenuModel::GetAcceleratorForCommandId( |
| + int command_id, |
| + ui::Accelerator* accelerator) { |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + size_t index = command_id; |
| + if (index < profile_manager->GetNumberOfProfiles()) |
| + return false; |
| + return delegate_->GetAcceleratorForCommandId(command_id, accelerator); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| // WrenchMenuModel |
| WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider, |
| @@ -304,7 +396,14 @@ bool WrenchMenuModel::GetIconForCommandId(int command_id, |
| } |
| void WrenchMenuModel::ExecuteCommand(int command_id) { |
| - browser_->ExecuteCommand(command_id); |
| + switch (command_id) { |
| + case IDC_CREATE_NEW_PROFILE: |
| + ProfileManager::CreateMultiProfileAsync(); |
| + break; |
| + default: |
| + browser_->ExecuteCommand(command_id); |
| + break; |
| + } |
| } |
| bool WrenchMenuModel::IsCommandIdChecked(int command_id) const { |
| @@ -318,11 +417,15 @@ bool WrenchMenuModel::IsCommandIdChecked(int command_id) const { |
| } |
| bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const { |
| - if (command_id == IDC_SHOW_BOOKMARK_BAR) { |
| - return !browser_->profile()->GetPrefs()->IsManagedPreference( |
| - prefs::kEnableBookmarkBar); |
| + switch (command_id) { |
| + case IDC_SHOW_BOOKMARK_BAR: |
| + return !browser_->profile()->GetPrefs()->IsManagedPreference( |
| + prefs::kEnableBookmarkBar); |
| + case IDC_CREATE_NEW_PROFILE: |
| + return true; |
| + default: |
| + return browser_->command_updater()->IsCommandEnabled(command_id); |
| } |
| - return browser_->command_updater()->IsCommandEnabled(command_id); |
| } |
| bool WrenchMenuModel::IsCommandIdVisible(int command_id) const { |
| @@ -458,6 +561,26 @@ void WrenchMenuModel::Build() { |
| AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); |
| AddSeparator(); |
| +#if !defined(OS_CHROMEOS) |
| + const string16 short_product_name = |
| + l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| + const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); |
| + if (browser_command_line.HasSwitch(switches::kMultiProfiles)) { |
| + if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { |
| + profiles_sub_menu_model_.reset(new ProfilesSubMenuModel(this, browser_)); |
| + AddSubMenu(IDC_PROFILE_MENU, l10n_util::GetStringFUTF16( |
| + IDS_PROFILES_MENU, short_product_name), |
| + profiles_sub_menu_model_.get()); |
| + } else { |
| + profiles_sub_menu_model_.reset(); |
| + AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( |
| + IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); |
| + } |
| + |
| + AddSeparator(); |
| + } |
| +#endif |
| + |
| #if defined(OS_CHROMEOS) |
| AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); |
| #elif defined(OS_MACOSX) |