Index: chrome/browser/ui/toolbar/wrench_menu_model.cc |
=================================================================== |
--- chrome/browser/ui/toolbar/wrench_menu_model.cc (revision 88807) |
+++ chrome/browser/ui/toolbar/wrench_menu_model.cc (working copy) |
@@ -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,13 +18,16 @@ |
#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" |
#include "chrome/browser/task_manager/task_manager.h" |
#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_window.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 +214,95 @@ |
//////////////////////////////////////////////////////////////////////////////// |
+// 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 { |
+ 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 +397,14 @@ |
} |
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 +418,17 @@ |
} |
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; |
+ case IDC_PROFILE_MENU: |
+ 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 +564,26 @@ |
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) |