diff options
author | munjal@chromium.org <munjal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-03 23:52:03 +0000 |
---|---|---|
committer | munjal@chromium.org <munjal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-03 23:52:03 +0000 |
commit | f253006b157f9c917fba21a7312290aaa2e889e7 (patch) | |
tree | 377183f9511bed141b5fd7a706c3407a122540d9 /chrome/browser | |
parent | dadacf06556b4f63a62ec3cea91d7fc5abccd67c (diff) | |
download | chromium_src-f253006b157f9c917fba21a7312290aaa2e889e7.zip chromium_src-f253006b157f9c917fba21a7312290aaa2e889e7.tar.gz chromium_src-f253006b157f9c917fba21a7312290aaa2e889e7.tar.bz2 |
Chromium-MultiProfile-Prototype
Summary
=======
Implement a prototype of multiple profiles in Chrome by
utilizing the functionality of user-data-dir command line
flag that already exists.
A profile in this case is an umbrella for all user data
including cookies, history, bookmarks, settings, etc. Each
profile gives the user a separation of all these data
elements.
User Interface
==============
- Wrench > "New window in profile" menu item, with sub-menu items.
This new menu item has sub menu items for each existing
profile, for up to 9 profiles, and one more sub menu item
to launch a window in a new profile. The 9 sub-menu items
also have the accelerators like CTRL + SHIFT + 1, CTRL +
SHIFT + 2, etc. If there are more than 9 profiles, we
will also show an extra sub-menu item, "Other...".
- New Profile dialog box
This dialog box is shown to the use when (s)he clicks
Wrench > New window in profile > <New Profile>. It lets
the user specify a profile name, and also shows a checkbox
to create a desktop shortcut to launch Chrome in that profile.
- Choose profile dialog box
This dialog box lets the user select a profile from a drop
down to open a new window in. It also has an item <New Profile>
in the drop down, selecting which will show the new profile
dialog box mentioned above. CTRL + M shortcut also launches
this dialog box.
Code Organization
=================
chrome\browser\user_data_dir_profile_manager.h/.cc:
This class provides an abstraction of profiles on top of the user
data dir command line flag.
chrome\browser\views\user_data_dir_new_profile_dialog.h/.cc
New profile dialog box code.
chrome\browser\views\user_data_dir_profiles_dialog.h/.cc
Choose profile dialog box code.
Review URL: http://codereview.chromium.org/12895
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6333 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/browser.cc | 47 | ||||
-rw-r--r-- | chrome/browser/browser.h | 18 | ||||
-rw-r--r-- | chrome/browser/browser.vcproj | 8 | ||||
-rw-r--r-- | chrome/browser/browser_main.cc | 29 | ||||
-rw-r--r-- | chrome/browser/browser_process.h | 9 | ||||
-rw-r--r-- | chrome/browser/dom_ui/new_tab_ui.cc | 37 | ||||
-rw-r--r-- | chrome/browser/user_data_manager.cc | 307 | ||||
-rw-r--r-- | chrome/browser/user_data_manager.h | 149 | ||||
-rw-r--r-- | chrome/browser/views/browser_views.vcproj | 16 | ||||
-rw-r--r-- | chrome/browser/views/new_profile_dialog.cc | 105 | ||||
-rw-r--r-- | chrome/browser/views/new_profile_dialog.h | 59 | ||||
-rw-r--r-- | chrome/browser/views/select_profile_dialog.cc | 153 | ||||
-rw-r--r-- | chrome/browser/views/select_profile_dialog.h | 84 | ||||
-rw-r--r-- | chrome/browser/views/toolbar_view.cc | 65 | ||||
-rw-r--r-- | chrome/browser/views/toolbar_view.h | 16 |
15 files changed, 1093 insertions, 9 deletions
diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc index 10e912c..ad297be 100644 --- a/chrome/browser/browser.cc +++ b/chrome/browser/browser.cc @@ -37,11 +37,14 @@ #include "chrome/browser/site_instance.h" #include "chrome/browser/task_manager.h" #include "chrome/browser/url_fixer_upper.h" +#include "chrome/browser/user_data_manager.h" #include "chrome/browser/user_metrics.h" #include "chrome/browser/view_ids.h" #include "chrome/browser/views/download_tab_view.h" #include "chrome/browser/views/go_button.h" #include "chrome/browser/views/location_bar_view.h" +#include "chrome/browser/views/new_profile_dialog.h" +#include "chrome/browser/views/select_profile_dialog.h" #include "chrome/browser/views/status_bubble.h" #include "chrome/browser/views/toolbar_star_toggle.h" #include "chrome/browser/web_contents_view.h" @@ -654,6 +657,26 @@ void Browser::NewWindow() { Browser::OpenEmptyWindow(profile_->GetOriginalProfile()); } +void Browser::OpenSelectProfileDialog() { + UserMetrics::RecordAction(L"SelectProfile", profile_); + SelectProfileDialog::RunDialog(); +} + +void Browser::OpenNewProfileDialog() { + UserMetrics::RecordAction(L"CreateProfile", profile_); + NewProfileDialog::RunDialog(); +} + +void Browser::NewProfileWindowByName(const std::wstring& profile) { + UserMetrics::RecordAction(L"NewProfileWindowByName", profile_); + UserDataManager::Get()->LaunchChromeForProfile(profile); +} + +void Browser::NewProfileWindowByIndex(int index) { + UserMetrics::RecordAction(L"NewProfileWindowByIndex", profile_); + UserDataManager::Get()->LaunchChromeForProfile(index); +} + void Browser::NewIncognitoWindow() { UserMetrics::RecordAction(L"NewIncognitoWindow", profile_); Browser::OpenEmptyWindow(profile_->GetOffTheRecordProfile()); @@ -1182,9 +1205,21 @@ void Browser::ExecuteCommand(int id) { case IDC_SHOW_DOWNLOADS: ShowDownloadsTab(); break; case IDC_SHOW_BOOKMARK_MANAGER: OpenBookmarksManager(); break; case IDC_SHOW_BOOKMARKS_BAR: ToggleBookmarksBar(); break; + case IDC_NEWPROFILEWINDOW: + case IDC_SELECT_PROFILE: + OpenSelectProfileDialog(); + break; + case IDC_NEW_PROFILE: OpenNewProfileDialog(); break; default: - LOG(WARNING) << "Received Unimplemented Command: " << id; + // Handle the user action for creating a new window in a specific profile. + if (id >= IDC_NEWPROFILEWINDOW_MIN_ID && + id <= IDC_NEWPROFILEWINDOW_MAX_ID) { + int index = id - IDC_NEWPROFILEWINDOW_MIN_ID; + NewProfileWindowByIndex(index); + } else { + LOG(WARNING) << "Received Unimplemented Command: " << id; + } break; } } @@ -2016,6 +2051,16 @@ void Browser::UpdateNavigationCommands() { !current_tab->GetFavIcon().isNull()); controller_.UpdateCommandEnabled(IDC_DUPLICATE, CanDuplicateContentsAt(selected_index())); + + // Enable various IDC_NEWPROFILEWINDOW* commands. + controller_.UpdateCommandEnabled(IDC_NEWPROFILEWINDOW, true); + controller_.UpdateCommandEnabled(IDC_SELECT_PROFILE, true); + controller_.UpdateCommandEnabled(IDC_NEW_PROFILE, true); + for (int i = IDC_NEWPROFILEWINDOW_MIN_ID; + i <= IDC_NEWPROFILEWINDOW_MAX_ID; + ++i) { + controller_.UpdateCommandEnabled(i, true); + } } void Browser::SetStarredButtonToggled(bool starred) { diff --git a/chrome/browser/browser.h b/chrome/browser/browser.h index 6540c2e..9e8de5e 100644 --- a/chrome/browser/browser.h +++ b/chrome/browser/browser.h @@ -5,6 +5,9 @@ #ifndef CHROME_BROWSER_BROWSER_H_ #define CHROME_BROWSER_BROWSER_H_ +#include <vector> + +#include "chrome/browser/browser_process.h" #include "chrome/browser/controller.h" #include "chrome/browser/shell_dialogs.h" #include "chrome/browser/browser_window.h" @@ -76,11 +79,20 @@ class Browser : public TabStripModelDelegate, Type type() const { return type_; } Profile* profile() const { return profile_; } + const std::vector<std::wstring>& user_data_dir_profiles() const { + return g_browser_process->user_data_dir_profiles(); + } BrowserWindow* window() const { return window_; } ToolbarModel* toolbar_model() { return &toolbar_model_; } const SessionID& session_id() const { return session_id_; } CommandController* controller() { return &controller_; } + // Setters ///////////////////////////////////////////////////////////////// + + void set_user_data_dir_profiles(const std::vector<std::wstring>& profiles) { + g_browser_process->user_data_dir_profiles() = profiles; + } + // Browser Creation Helpers ///////////////////////////////////////////////// // Opens a new window with the default blank tab. @@ -217,6 +229,10 @@ class Browser : public TabStripModelDelegate, void CloseTab(); void CloseApp(); void NewWindow(); + // Commands to create a new window in a specific profile. + void NewProfileWindowByName(const std::wstring& profile); + // The index starts with 0, and specifies the index in the profiles vector. + void NewProfileWindowByIndex(int index); void NewIncognitoWindow(); void CloseWindow(); void SelectNextTab(); @@ -276,6 +292,8 @@ class Browser : public TabStripModelDelegate, void ShowDownloadsTab(); void OpenBookmarksManager(); void ToggleBookmarksBar(); + void OpenSelectProfileDialog(); + void OpenNewProfileDialog(); ///////////////////////////////////////////////////////////////////////////// diff --git a/chrome/browser/browser.vcproj b/chrome/browser/browser.vcproj index 9cb07f5..31cb529 100644 --- a/chrome/browser/browser.vcproj +++ b/chrome/browser/browser.vcproj @@ -678,6 +678,14 @@ > </File> <File + RelativePath=".\user_data_manager.cc" + > + </File> + <File + RelativePath=".\user_data_manager.h" + > + </File> + <File RelativePath=".\user_metrics.cc" > </File> diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc index ff87093..66c5f7f 100644 --- a/chrome/browser/browser_main.cc +++ b/chrome/browser/browser_main.cc @@ -39,6 +39,7 @@ #include "chrome/browser/rlz/rlz.h" #include "chrome/browser/shell_integration.h" #include "chrome/browser/url_fixer_upper.h" +#include "chrome/browser/user_data_manager.h" #include "chrome/browser/user_metrics.h" #include "chrome/browser/views/user_data_dir_dialog.h" #include "chrome/common/chrome_constants.h" @@ -323,6 +324,10 @@ int BrowserMain(CommandLine &parsed_command_line, // BrowserProcessImpl's constructor should set g_browser_process. DCHECK(g_browser_process); + std::wstring local_state_path; + PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path); + bool local_state_file_exists = file_util::PathExists(local_state_path); + // Load local state. This includes the application locale so we know which // locale dll to load. PrefService* local_state = browser_process->local_state(); @@ -359,6 +364,27 @@ int BrowserMain(CommandLine &parsed_command_line, first_run_ui_bypass = true; } + // If the local state file for the current profile doesn't exist and the + // parent profile command line flag is present, then we should inherit some + // local state from the parent profile. + // Checking that the local state file for the current profile doesn't exist + // is the most robust way to determine whether we need to inherit or not + // since the parent profile command line flag can be present even when the + // current profile is not a new one, and in that case we do not want to + // inherit and reset the user's setting. + if (!local_state_file_exists && + parsed_command_line.HasSwitch(switches::kParentProfile)) { + std::wstring parent_profile = + parsed_command_line.GetSwitchValue(switches::kParentProfile); + PrefService parent_local_state(parent_profile); + parent_local_state.RegisterStringPref(prefs::kApplicationLocale, + std::wstring()); + // Right now, we only inherit the locale setting from the parent profile. + local_state->SetString( + prefs::kApplicationLocale, + parent_local_state.GetString(prefs::kApplicationLocale)); + } + ResourceBundle::InitSharedInstance( local_state->GetString(prefs::kApplicationLocale)); // We only load the theme dll in the browser process. @@ -378,6 +404,9 @@ int BrowserMain(CommandLine &parsed_command_line, tracking_objects = tracked_objects::ThreadData::StartTracking(true); #endif + // Initialize the shared instance of user data manager. + UserDataManager::Create(); + // Try to create/load the profile. ProfileManager* profile_manager = browser_process->profile_manager(); Profile* profile = profile_manager->GetDefaultProfile(user_data_dir); diff --git a/chrome/browser/browser_process.h b/chrome/browser/browser_process.h index 75e20e4..69005ed 100644 --- a/chrome/browser/browser_process.h +++ b/chrome/browser/browser_process.h @@ -11,6 +11,7 @@ #define CHROME_BROWSER_BROWSER_PROCESS_H__ #include <string> +#include <vector> #include "base/basictypes.h" #include "base/message_loop.h" @@ -134,7 +135,15 @@ class BrowserProcess { virtual HANDLE shutdown_event() = 0; #endif + // Returns a reference to the user-data-dir based profiles vector. + std::vector<std::wstring>& user_data_dir_profiles() { + return user_data_dir_profiles_; + } + private: + // User-data-dir based profiles. + std::vector<std::wstring> user_data_dir_profiles_; + DISALLOW_EVIL_CONSTRUCTORS(BrowserProcess); }; diff --git a/chrome/browser/dom_ui/new_tab_ui.cc b/chrome/browser/dom_ui/new_tab_ui.cc index 5a87b2d..cb1fe1b 100644 --- a/chrome/browser/dom_ui/new_tab_ui.cc +++ b/chrome/browser/dom_ui/new_tab_ui.cc @@ -16,6 +16,7 @@ #include "chrome/browser/profile.h" #include "chrome/browser/render_view_host.h" #include "chrome/browser/template_url.h" +#include "chrome/browser/user_data_manager.h" #include "chrome/browser/user_metrics.h" #include "chrome/browser/views/keyword_editor_view.h" #include "chrome/common/jstemplate_builder.h" @@ -168,11 +169,27 @@ void NewTabHTMLSource::StartDataRequest(const std::string& path, NOTREACHED(); return; } + + // Show the profile name in the title and most visited labels if the current + // profile is not the default. + std::wstring title; + std::wstring most_visited; + if (UserDataManager::Get()->is_current_profile_default()) { + title = l10n_util::GetString(IDS_NEW_TAB_TITLE); + most_visited = l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED); + } else { + // Get the current profile name. + std::wstring profile_name = + UserDataManager::Get()->current_profile_name(); + title = l10n_util::GetStringF(IDS_NEW_TAB_TITLE_WITH_PROFILE_NAME, + profile_name); + most_visited = l10n_util::GetStringF( + IDS_NEW_TAB_MOST_VISITED_WITH_PROFILE_NAME, + profile_name); + } DictionaryValue localized_strings; - localized_strings.SetString(L"title", - l10n_util::GetString(IDS_NEW_TAB_TITLE)); - localized_strings.SetString(L"mostvisited", - l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED)); + localized_strings.SetString(L"title", title); + localized_strings.SetString(L"mostvisited", most_visited); localized_strings.SetString(L"searches", l10n_util::GetString(IDS_NEW_TAB_SEARCHES)); localized_strings.SetString(L"bookmarks", @@ -841,7 +858,17 @@ NewTabUIContents::NewTabUIContents(Profile* profile, incognito_(false), most_visited_handler_(NULL) { set_type(TAB_CONTENTS_NEW_TAB_UI); - set_forced_title(l10n_util::GetString(IDS_NEW_TAB_TITLE)); + + // Show profile name in the title if the current profile is not the default. + std::wstring title; + if (UserDataManager::Get()->is_current_profile_default()) { + title = l10n_util::GetString(IDS_NEW_TAB_TITLE); + } else { + title = l10n_util::GetStringF( + IDS_NEW_TAB_TITLE_WITH_PROFILE_NAME, + UserDataManager::Get()->current_profile_name()); + } + set_forced_title(title); if (profile->IsOffTheRecord()) incognito_ = true; diff --git a/chrome/browser/user_data_manager.cc b/chrome/browser/user_data_manager.cc new file mode 100644 index 0000000..33bdf00 --- /dev/null +++ b/chrome/browser/user_data_manager.cc @@ -0,0 +1,307 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/user_data_manager.h" + +#include <windows.h> +#include <string> + +#include "base/command_line.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/path_service.h" +#include "base/process_util.h" +#include "base/string_util.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/common/chrome_constants.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/l10n_util.h" +#include "chrome/installer/util/browser_distribution.h" +#include "chrome/installer/util/shell_util.h" + +#include "chromium_strings.h" + +namespace { + +// Helper to start chrome for a given profile index. The helper takes care of +// enumerating profiles on the file thread and then it launches Chrome for the +// appropriate profile on the original thread. +// An instance of this class should always be created on the heap, and it will +// delete itself when the launch is done. +class LaunchChromeForProfileIndexHelper : GetProfilesHelper::Delegate { + public: + // Creates an instance with the given data manager and to launch chrome for + // the profile with the given index. + LaunchChromeForProfileIndexHelper(const UserDataManager* manager, int index); + virtual ~LaunchChromeForProfileIndexHelper(); + + // Starts the asynchronous launch. + void StartLaunch(); + + // GetProfilesHelper::Delegate method. + void OnGetProfilesDone(const std::vector<std::wstring>& profiles); + + private: + int index_; + const UserDataManager* manager_; + scoped_refptr<GetProfilesHelper> profiles_helper_; + + DISALLOW_COPY_AND_ASSIGN(LaunchChromeForProfileIndexHelper); +}; + +} // namespace + +LaunchChromeForProfileIndexHelper::LaunchChromeForProfileIndexHelper( + const UserDataManager* manager, + int index) + : manager_(manager), + index_(index), +// Don't complain about using "this" in initializer list. +MSVC_PUSH_DISABLE_WARNING(4355) + profiles_helper_(new GetProfilesHelper(this)) { +MSVC_POP_WARNING() + DCHECK(manager); +} + +LaunchChromeForProfileIndexHelper::~LaunchChromeForProfileIndexHelper() { + profiles_helper_->OnDelegateDeleted(); +} + +void LaunchChromeForProfileIndexHelper::StartLaunch() { + profiles_helper_->GetProfiles(NULL); +} + +void LaunchChromeForProfileIndexHelper::OnGetProfilesDone( + const std::vector<std::wstring>& profiles) { + if (index_ >= 0 && index_ < static_cast<int>(profiles.size())) + manager_->LaunchChromeForProfile(profiles[index_]); + + // We are done, delete ourselves. + delete this; +} + +// Separator used in folder names between the prefix and the profile name. +// For e.g. a folder for the profile "Joe" would be named "User Data-Joe". +static const wchar_t kProfileFolderSeparator[] = L"-"; + +// static +UserDataManager* UserDataManager::instance_ = NULL; + +// static +void UserDataManager::Create() { + DCHECK(!instance_); + std::wstring user_data; + PathService::Get(chrome::DIR_USER_DATA, &user_data); + instance_ = new UserDataManager(user_data); +} + +// static +UserDataManager* UserDataManager::Get() { + DCHECK(instance_); + return instance_; +} + +UserDataManager::UserDataManager(const std::wstring& user_data_root) + : user_data_root_(user_data_root) { + // Determine current profile name and current folder name. + current_folder_name_ = file_util::GetFilenameFromPath(user_data_root); + bool success = GetProfileNameFromFolderName(current_folder_name_, + ¤t_profile_name_); + // The current profile is a default profile if the current user data folder + // name is just kUserDataDirname or when the folder name doesn't have the + // kUserDataDirname as prefix. + is_current_profile_default_ = + !success || (current_folder_name_ == chrome::kUserDataDirname); + + // (TODO:munjal) Fix issue 5070: + // http://code.google.com/p/chromium/issues/detail?id=5070 + + file_util::UpOneDirectory(&user_data_root_); +} + +UserDataManager::~UserDataManager() { +} + +// static +bool UserDataManager::GetProfileNameFromFolderName( + const std::wstring& folder_name, + std::wstring* profile_name) { + // The folder name should start with a specific prefix for it to be a valid + // profile folder. + if (folder_name.find(chrome::kUserDataDirname) != 0) + return false; + + // Seems like we cannot use arraysize macro for externally defined constants. + // Is there a way? + unsigned int prefix_length = wcslen(chrome::kUserDataDirname); + // Subtract 1 from the size of the array for trailing null character. + unsigned int separator_length = arraysize(kProfileFolderSeparator) - 1; + + // It's safe to use profile_name variable for intermediate values since we + // will always return true now. + *profile_name = folder_name.substr(prefix_length); + // Remove leading separator if present. + if (profile_name->find_first_of(kProfileFolderSeparator) == 0) + *profile_name = profile_name->substr(separator_length); + + if (profile_name->empty()) + *profile_name = chrome::kNotSignedInProfile; + + return true; +} + +// static +std::wstring UserDataManager::GetFolderNameFromProfileName( + const std::wstring& profile_name) { + std::wstring folder_name = chrome::kUserDataDirname; + if (profile_name != chrome::kNotSignedInProfile) { + folder_name += kProfileFolderSeparator; + folder_name += profile_name; + } + return folder_name; +} + +std::wstring UserDataManager::GetUserDataFolderForProfile( + const std::wstring& profile_name) const { + std::wstring folder_name = GetFolderNameFromProfileName(profile_name); + std::wstring folder_path(user_data_root_); + file_util::AppendToPath(&folder_path, folder_name); + return folder_path; +} + +std::wstring UserDataManager::GetCommandForProfile( + const std::wstring& profile_name) const { + std::wstring user_data_dir = GetUserDataFolderForProfile(profile_name); + std::wstring command; + PathService::Get(base::FILE_EXE, &command); + CommandLine::AppendSwitchWithValue(&command, + switches::kUserDataDir, + user_data_dir); + std::wstring local_state_path; + PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path); + CommandLine::AppendSwitchWithValue(&command, + switches::kParentProfile, + local_state_path); + return command; +} + +void UserDataManager::LaunchChromeForProfile( + const std::wstring& profile_name) const { + std::wstring command = GetCommandForProfile(profile_name); + base::LaunchApp(command, false, false, NULL); +} + +void UserDataManager::LaunchChromeForProfile(int index) const { + // Helper deletes itself when done. + LaunchChromeForProfileIndexHelper* helper = + new LaunchChromeForProfileIndexHelper(this, index); + helper->StartLaunch(); +} + +void UserDataManager::GetProfiles(std::vector<std::wstring>* profiles) const { + // This function should be called on the file thread. + DCHECK(MessageLoop::current() == + ChromeThread::GetMessageLoop(ChromeThread::FILE)); + file_util::FileEnumerator file_enum(user_data_root_, + false, + file_util::FileEnumerator::DIRECTORIES); + std::wstring folder_name; + while (!(folder_name = file_enum.Next()).empty()) { + folder_name = file_util::GetFilenameFromPath(folder_name); + std::wstring profile_name; + if (GetProfileNameFromFolderName(folder_name, &profile_name)) + profiles->push_back(profile_name); + } +} + +bool UserDataManager::CreateDesktopShortcutForProfile( + const std::wstring& profile_name) const { + std::wstring exe_path; + std::wstring shortcut_path; + if (!PathService::Get(base::FILE_EXE, &exe_path) || + !ShellUtil::GetDesktopPath(false, &shortcut_path)) + return false; + + // Working directory. + std::wstring exe_folder = file_util::GetDirectoryFromPath(exe_path); + + // Command and arguments. + std::wstring cmd; + cmd = StringPrintf(L"\"%ls\"", exe_path.c_str()); + std::wstring user_data_dir = GetUserDataFolderForProfile(profile_name); + std::wstring args = CommandLine::PrefixedSwitchStringWithValue( + switches::kUserDataDir, + user_data_dir); + args = StringPrintf(L"\"%ls\"", args.c_str()); + + // Shortcut path. + std::wstring shortcut_name = l10n_util::GetStringF( + IDS_START_IN_PROFILE_SHORTCUT_NAME, + profile_name); + shortcut_name.append(L".lnk"); + file_util::AppendToPath(&shortcut_path, shortcut_name); + + return file_util::CreateShortcutLink(cmd.c_str(), + shortcut_path.c_str(), + exe_folder.c_str(), + args.c_str(), + NULL, + exe_path.c_str(), + 0); +} + +GetProfilesHelper::GetProfilesHelper(Delegate* delegate) + : delegate_(delegate) { +} + +void GetProfilesHelper::GetProfiles(MessageLoop* target_loop) { + // If the target loop is not NULL then use the target loop, or if it's NULL + // then use the current message loop to post a task on it later when we are + // done building a list of profiles. + if (target_loop) { + message_loop_ = target_loop; + } else { + message_loop_ = MessageLoop::current(); + } + DCHECK(message_loop_); + MessageLoop* file_loop = ChromeThread::GetMessageLoop(ChromeThread::FILE); + file_loop->PostTask( + FROM_HERE, + NewRunnableMethod(this, &GetProfilesHelper::GetProfilesFromManager)); +} + +// Records that the delegate is closed. +void GetProfilesHelper::OnDelegateDeleted() { + delegate_ = NULL; +} + +void GetProfilesHelper::GetProfilesFromManager() { + // This function should be called on the file thread. + DCHECK(MessageLoop::current() == + ChromeThread::GetMessageLoop(ChromeThread::FILE)); + + // If the delegate is gone by now, no need to do any work. + if (!delegate_) + return; + + scoped_ptr< std::vector<std::wstring> > profiles( + new std::vector<std::wstring>); + UserDataManager::Get()->GetProfiles(profiles.get()); + + // Post a task on the original thread to call the delegate. + message_loop_->PostTask( + FROM_HERE, + NewRunnableMethod(this, + &GetProfilesHelper::InvokeDelegate, + profiles.release())); +} + +void GetProfilesHelper::InvokeDelegate(std::vector<std::wstring>* profiles) { + scoped_ptr< std::vector<std::wstring> > udd_profiles(profiles); + // If the delegate is gone by now, no need to do any work. + if (delegate_) + delegate_->OnGetProfilesDone(*udd_profiles.get()); +} diff --git a/chrome/browser/user_data_manager.h b/chrome/browser/user_data_manager.h new file mode 100644 index 0000000..f5e4522 --- /dev/null +++ b/chrome/browser/user_data_manager.h @@ -0,0 +1,149 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This class encapsulates the implementation of multiple profiles by using +// the user-data-dir functionality. + +#ifndef CHROME_BROWSER_USER_DATA_MANAGER_H_ +#define CHROME_BROWSER_USER_DATA_MANAGER_H_ + +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "base/ref_counted.h" + +class MessageLoop; + +// Provides an abstraction of profiles on top of the user data directory +// feature. Given the root of the user data directories, it provides +// functionality to enumerate the existing profiles and start Chrome in a +// given profile. +// Also holds a shared instance of its own for convenience though it's not a +// singleton class. The shared instance should be created by the main thread, +// then other threads can access and use the shared instance. +class UserDataManager { + public: + // Creates the shared instance of this class. This method is not thread-safe, + // so the shared instance should be created on the main thread. + static void Create(); + + // Returns the shared instance. CreateInstance must be called before callling + // this method. + static UserDataManager* Get(); + + // Creates a new instance with the given root folder for storing user data + // folders. + explicit UserDataManager(const std::wstring& user_data_root); + + ~UserDataManager(); + + // Returns the name of the current profile. + std::wstring current_profile_name() const { return current_profile_name_; } + + // Returns whether the current profile is the default profile or not. + bool is_current_profile_default() const { + return is_current_profile_default_; + } + + // Populates the given vector with a list of all the profiles. + // This function should be called on the file thread. + void GetProfiles(std::vector<std::wstring>* profiles) const; + + // Creates a desktop shortcut for the given profile name. + // Returns false if the shortcut creation fails; true otherwise. + bool CreateDesktopShortcutForProfile(const std::wstring& profile_name) const; + + // Starts a new Chrome instance in the given profile name. + void LaunchChromeForProfile(const std::wstring& profile_name) const; + + // Starts a new Chrome instance in the profile with the given index. The + // index is zero based, and refers to the position of the profile in the + // list of profile names in alphabetical order. + // This method launches Chrome asynchornously since it enumerates profiles + // on a separate thread. + void LaunchChromeForProfile(int index) const; + + private: + // Gets the name of the profile from the name of the folder. + // Returns false if the folder does not correspond to a profile folder, true + // otherwise. + static bool GetProfileNameFromFolderName(const std::wstring& folder_name, + std::wstring* profile_name); + + // Returns the name of the folder from the name of the profile. + static std::wstring GetFolderNameFromProfileName( + const std::wstring& profile_name); + + // Returns the path of the user data folder for the given profile. + std::wstring GetUserDataFolderForProfile( + const std::wstring& profile_name) const; + + // Returns the command to start the app in the given profile. + std::wstring GetCommandForProfile(const std::wstring& profile_name) const; + + // Shared instance. + static UserDataManager* instance_; + + // Root folder. + std::wstring user_data_root_; + + // Current user data folder. + std::wstring current_folder_name_; + + // Whether the current profile is the default profile. + bool is_current_profile_default_; + + // Current profile name. + std::wstring current_profile_name_; + + DISALLOW_COPY_AND_ASSIGN(UserDataManager); +}; + +// Helper class to enumerate the profiles asynchronously on the file thread. +// It calls the given delegate instance when the enumeration is complete. +// USAGE: Create an instance of the helper with a delegate instance, call the +// asynchronous method GetProfiles. The delegate instance will be called when +// enumerating profiles is done. +// IMPORTANT: It's the responsibility of the caller to call OnDelegateDeleted +// method when the delegate instance is deleted. Typically OnDelegateDeleted +// should be called in the destructor of the delegate. This is the way to +// tell the helper to not call the delegate when enumerating profiles is done. +class GetProfilesHelper + : public base::RefCountedThreadSafe<GetProfilesHelper> { + public: + // Interface the delegate classes should implement. + class Delegate { + public: + virtual void OnGetProfilesDone( + const std::vector<std::wstring>& profiles) = 0; + virtual ~Delegate() { } + }; + + explicit GetProfilesHelper(Delegate* delegate); + + // Asynchronous call to get the list of profiles. Calls the delegate when done + // on either the given target loop or the message loop on which this function + // is called if target loop is NULL. + void GetProfiles(MessageLoop* target_loop); + + // Records that the delegate is deleted. + void OnDelegateDeleted(); + + private: + // Helper to get the profiles from user data manager. + void GetProfilesFromManager(); + + // Helper to invoke the delegate. + void InvokeDelegate(std::vector<std::wstring>* profiles); + + // Delegate to call. + Delegate* delegate_; + // Message loop to post tasks on completion of loading profiles. + MessageLoop* message_loop_; + + DISALLOW_COPY_AND_ASSIGN(GetProfilesHelper); +}; + +#endif // CHROME_BROWSER_USER_DATA_MANAGER_H_ diff --git a/chrome/browser/views/browser_views.vcproj b/chrome/browser/views/browser_views.vcproj index 6eed9d0..b605de5 100644 --- a/chrome/browser/views/browser_views.vcproj +++ b/chrome/browser/views/browser_views.vcproj @@ -650,6 +650,14 @@ > </File> <File + RelativePath=".\new_profile_dialog.cc" + > + </File> + <File + RelativePath=".\new_profile_dialog.h" + > + </File> + <File RelativePath=".\page_info_window.cc" > </File> @@ -706,6 +714,14 @@ > </File> <File + RelativePath=".\select_profile_dialog.cc" + > + </File> + <File + RelativePath=".\select_profile_dialog.h" + > + </File> + <File RelativePath=".\shelf_item_dialog.cc" > </File> diff --git a/chrome/browser/views/new_profile_dialog.cc b/chrome/browser/views/new_profile_dialog.cc new file mode 100644 index 0000000..07c3c708 --- /dev/null +++ b/chrome/browser/views/new_profile_dialog.cc @@ -0,0 +1,105 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/views/new_profile_dialog.h" + +#include <string> + +#include "base/logging.h" +#include "base/file_util.h" +#include "chrome/app/locales/locale_settings.h" +#include "chrome/browser/user_data_manager.h" +#include "chrome/common/l10n_util.h" +#include "chrome/views/message_box_view.h" +#include "chrome/views/text_field.h" +#include "chrome/views/view.h" +#include "chrome/views/window.h" + +#include "chromium_strings.h" +#include "generated_resources.h" + +// static +void NewProfileDialog::RunDialog() { + NewProfileDialog* dlg = new NewProfileDialog(); + views::Window::CreateChromeWindow(NULL, gfx::Rect(), dlg)->Show(); +} + +NewProfileDialog::NewProfileDialog() { + std::wstring message_text = l10n_util::GetString( + IDS_NEW_PROFILE_DIALOG_LABEL_TEXT); + const int kDialogWidth = views::Window::GetLocalizedContentsWidth( + IDS_NEW_PROFILE_DIALOG_WIDTH_CHARS); + const int kMessageBoxFlags = MessageBoxView::kFlagHasOKButton | + MessageBoxView::kFlagHasCancelButton | + MessageBoxView::kFlagHasPromptField; + message_box_view_ = new MessageBoxView(kMessageBoxFlags, + message_text.c_str(), + std::wstring(), + kDialogWidth); + message_box_view_->SetCheckBoxLabel( + l10n_util::GetString(IDS_NEW_PROFILE_DIALOG_CREATE_SHORTCUT_TEXT)); + message_box_view_->SetCheckBoxSelected(true); + message_box_view_->text_box()->SetController(this); +} + +NewProfileDialog::~NewProfileDialog() { +} + +int NewProfileDialog::GetDialogButtons() const { + return DIALOGBUTTON_OK | DIALOGBUTTON_CANCEL; +} + +views::View* NewProfileDialog::GetInitiallyFocusedView() const { + views::TextField* text_box = message_box_view_->text_box(); + DCHECK(text_box); + return text_box; +} + +bool NewProfileDialog::IsDialogButtonEnabled( + DialogButton button) const { + if (button == DIALOGBUTTON_OK) { + std::wstring profile_name = message_box_view_->GetInputText(); + // TODO(munjal): Refactor the function ReplaceIllegalCharacters in + // file_util to something that just checks if there are illegal chars + // since that's what we really need. Also, replaceIllegalChars seems to + // be expensive since it builds a list of illegal characters for each call. + // So at the least fix that. + file_util::ReplaceIllegalCharacters(&profile_name, L'_'); + return !profile_name.empty() && + profile_name == message_box_view_->GetInputText(); + } + return true; +} + +std::wstring NewProfileDialog::GetWindowTitle() const { + return l10n_util::GetString(IDS_NEW_PROFILE_DIALOG_TITLE); +} + +void NewProfileDialog::WindowClosing() { + delete this; +} + +void NewProfileDialog::ContentsChanged(views::TextField* sender, + const std::wstring& new_contents) { + GetDialogClientView()->UpdateDialogButtons(); +} + +bool NewProfileDialog::Accept() { + std::wstring profile_name = message_box_view_->GetInputText(); + if (profile_name.empty()) { + NOTREACHED(); + return true; + } + // Create a desktop shortcut if the corresponding checkbox is checked. + if (message_box_view_->IsCheckBoxSelected()) + UserDataManager::Get()->CreateDesktopShortcutForProfile( + profile_name); + + UserDataManager::Get()->LaunchChromeForProfile(profile_name); + return true; +} + +views::View* NewProfileDialog::GetContentsView() { + return message_box_view_; +} diff --git a/chrome/browser/views/new_profile_dialog.h b/chrome/browser/views/new_profile_dialog.h new file mode 100644 index 0000000..970a820 --- /dev/null +++ b/chrome/browser/views/new_profile_dialog.h @@ -0,0 +1,59 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// A dialog box that prompts the user to enter a profile name, and opens a new +// window in that profile. + +#ifndef CHROME_BROWSER_VIEWS_NEW_PROFILE_DIALOG_H_ +#define CHROME_BROWSER_VIEWS_NEW_PROFILE_DIALOG_H_ + +#include "base/basictypes.h" +#include "base/message_loop.h" +#include "chrome/browser/shell_dialogs.h" +#include "chrome/views/dialog_delegate.h" +#include "chrome/views/text_field.h" + +class MessageBoxView; +namespace views { +class View; +class Window; +} + +// Dialog that prompts the user to create a new profile. +class NewProfileDialog : public views::DialogDelegate, + public views::TextField::Controller { + public: + // Creates and runs the dialog. + static void RunDialog(); + virtual ~NewProfileDialog(); + + // views::DialogDelegate methods. + virtual bool Accept(); + virtual int GetDialogButtons() const; + virtual views::View* GetInitiallyFocusedView() const; + virtual bool IsDialogButtonEnabled(DialogButton button) const; + virtual std::wstring GetWindowTitle() const; + virtual void WindowClosing(); + + // views::TextField::Controller methods. + virtual void ContentsChanged(views::TextField* sender, + const std::wstring& new_contents); + virtual void HandleKeystroke(views::TextField* sender, + UINT message, TCHAR key, UINT repeat_count, + UINT flags) {} + + // views::WindowDelegate methods. + virtual views::View* GetContentsView(); + virtual bool IsAlwaysOnTop() const { return false; } + virtual bool IsModal() const { return false; } + + private: + NewProfileDialog(); + + MessageBoxView* message_box_view_; + + DISALLOW_COPY_AND_ASSIGN(NewProfileDialog); +}; + +#endif // CHROME_BROWSER_VIEWS_NEW_PROFILE_DIALOG_H_ diff --git a/chrome/browser/views/select_profile_dialog.cc b/chrome/browser/views/select_profile_dialog.cc new file mode 100644 index 0000000..50353ba --- /dev/null +++ b/chrome/browser/views/select_profile_dialog.cc @@ -0,0 +1,153 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/views/select_profile_dialog.h" + +#include <string> + +#include "base/logging.h" +#include "base/string_util.h" +#include "chrome/app/locales/locale_settings.h" +#include "chrome/browser/user_data_manager.h" +#include "chrome/browser/views/new_profile_dialog.h" +#include "chrome/browser/views/standard_layout.h" +#include "chrome/common/l10n_util.h" +#include "chrome/views/combo_box.h" +#include "chrome/views/grid_layout.h" +#include "chrome/views/label.h" +#include "chrome/views/message_box_view.h" +#include "chrome/views/view.h" +#include "chrome/views/window.h" + +#include "chromium_strings.h" +#include "generated_resources.h" + +using views::ColumnSet; +using views::GridLayout; + +// static +void SelectProfileDialog::RunDialog() { + // When the window closes, it will delete itself. + SelectProfileDialog* dlg = new SelectProfileDialog(); + views::Window::CreateChromeWindow(NULL, gfx::Rect(), dlg)->Show(); +} + +SelectProfileDialog::SelectProfileDialog() + : helper_(new GetProfilesHelper(this)) { + // We first create an instance of the helper and then setup controls. This + // doesn't lead to race condition because once the helper is done with + // enumerating profiles by examining the file system, it posts a task on the + // thread it was called on. This is the same thread that the current code is + // running on. So that task wouldn't get executed until we are done with + // setup controls. Given that, we start the helper before setup controls so + // that file enumeration can be done as soon as possible. + helper_->GetProfiles(NULL); + SetupControls(); +} + +SelectProfileDialog::~SelectProfileDialog() { + helper_->OnDelegateDeleted(); +} + +gfx::Size SelectProfileDialog::GetPreferredSize() { + return gfx::Size(views::Window::GetLocalizedContentsSize( + IDS_SELECT_PROFILE_DIALOG_WIDTH_CHARS, + IDS_SELECT_PROFILE_DIALOG_HEIGHT_LINES)); +} + +void SelectProfileDialog::PopulateProfilesComboBox( + const std::vector<std::wstring>& profiles) { + profiles_.insert(profiles_.begin(), profiles.begin(), profiles.end()); + profile_combobox_->ModelChanged(); + GetDialogClientView()->UpdateDialogButtons(); +} + +void SelectProfileDialog::Layout() { + GetLayoutManager()->Layout(this); +} + +int SelectProfileDialog::GetDialogButtons() const { + return DIALOGBUTTON_OK | DIALOGBUTTON_CANCEL; +} + +views::View* SelectProfileDialog::GetInitiallyFocusedView() const { + return profile_combobox_; +} + +std::wstring SelectProfileDialog::GetWindowTitle() const { + return l10n_util::GetString(IDS_SELECT_PROFILE_DIALOG_TITLE); +} + +bool SelectProfileDialog::Accept() { + int index = profile_combobox_->GetSelectedItem(); + if (index < 0) { + NOTREACHED(); + return true; + } + + // If the user has selected <New Profile> from the drop down, then show the + // new profile dialog to the user. + if (index == profiles_.size()) { + NewProfileDialog::RunDialog(); + return true; + } + + std::wstring profile_name = profiles_[index]; + UserDataManager::Get()->LaunchChromeForProfile(profile_name); + return true; +} + +bool SelectProfileDialog::Cancel() { + return true; +} + +views::View* SelectProfileDialog::GetContentsView() { + return this; +} + +int SelectProfileDialog::GetItemCount(views::ComboBox* source) { + // Always show one more item in the combo box that allows the user to select + // <New Profile>. + return profiles_.size() + 1; +} + +std::wstring SelectProfileDialog::GetItemAt(views::ComboBox* source, + int index) { + DCHECK(source == profile_combobox_); + DCHECK(index >= 0 && index <= static_cast<int>(profiles_.size())); + // For the last item in the drop down, return the <New Profile> text, + // otherwise return the corresponding profile name from the vector. + return index == profiles_.size() ? + l10n_util::GetString(IDS_SELECT_PROFILE_DIALOG_NEW_PROFILE_ENTRY) : + profiles_[index]; +} + +void SelectProfileDialog::OnGetProfilesDone( + const std::vector<std::wstring>& profiles) { + PopulateProfilesComboBox(profiles); +} + +void SelectProfileDialog::SetupControls() { + // Adds all controls. + select_profile_label_ = new views::Label( + l10n_util::GetString(IDS_SELECT_PROFILE_DIALOG_LABEL_TEXT)); + profile_combobox_ = new views::ComboBox(this); + + // Arranges controls by using GridLayout. + const int column_set_id = 0; + GridLayout* layout = CreatePanelGridLayout(this); + SetLayoutManager(layout); + ColumnSet* column_set = layout->AddColumnSet(column_set_id); + column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, + GridLayout::USE_PREF, 0, 0); + column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); + column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, + GridLayout::FIXED, 200, 0); + + layout->StartRow(0, column_set_id); + layout->AddView(select_profile_label_); + layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); + layout->StartRow(0, column_set_id); + layout->AddView(profile_combobox_); +} diff --git a/chrome/browser/views/select_profile_dialog.h b/chrome/browser/views/select_profile_dialog.h new file mode 100644 index 0000000..f42df85 --- /dev/null +++ b/chrome/browser/views/select_profile_dialog.h @@ -0,0 +1,84 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// A dialog box that shows the user various profiles that currently exist +// and lets the user select one. + +#ifndef CHROME_BROWSER_VIEWS_SELECT_PROFILE_DIALOG_H_ +#define CHROME_BROWSER_VIEWS_SELECT_PROFILE_DIALOG_H_ + +#include <vector> + +#include "base/basictypes.h" +#include "base/message_loop.h" +#include "base/ref_counted.h" +#include "chrome/browser/shell_dialogs.h" +#include "chrome/browser/user_data_manager.h" +#include "chrome/views/combo_box.h" +#include "chrome/views/dialog_delegate.h" + +class SelectProfileDialogHelper; +namespace views { +class Label; +class Window; +} + +// Dialog to allow the user to select a profile to open a new window. +class SelectProfileDialog + : public views::DialogDelegate, + public views::View, + public views::ComboBox::Model, + public GetProfilesHelper::Delegate { + public: + // Creates and runs the dialog. + static void RunDialog(); + + virtual ~SelectProfileDialog(); + + // Populates the list of profiles from the given vector. + void PopulateProfilesComboBox(const std::vector<std::wstring>& profiles); + + // Returns the profile name selected by the user. + std::wstring profile_name() { return profile_name_; } + + // views::View methods. + virtual gfx::Size GetPreferredSize(); + virtual void Layout(); + + // views::DialogDelegate Methods: + virtual bool Accept(); + virtual bool Cancel(); + virtual views::View* GetContentsView(); + virtual int GetDialogButtons() const; + virtual views::View* GetInitiallyFocusedView() const; + virtual std::wstring GetWindowTitle() const; + virtual bool IsModal() const { return false; } + + // views::ComboBox::Model methods. + virtual int GetItemCount(views::ComboBox* source); + virtual std::wstring GetItemAt(views::ComboBox* source, int index); + + // GetProfilesHelper::Delegate method. + virtual void OnGetProfilesDone(const std::vector<std::wstring>& profiles); + + private: + SelectProfileDialog(); + + // Sets up all UI controls for the dialog. + void SetupControls(); + + // UI controls. + views::ComboBox* profile_combobox_; + views::Label* select_profile_label_; + + std::vector<std::wstring> profiles_; + std::wstring profile_name_; + + // Helper instance that handles all task posting activities. + scoped_refptr<GetProfilesHelper> helper_; + + DISALLOW_COPY_AND_ASSIGN(SelectProfileDialog); +}; + +#endif // CHROME_BROWSER_VIEWS_SELECT_PROFILE_DIALOG_H_ diff --git a/chrome/browser/views/toolbar_view.cc b/chrome/browser/views/toolbar_view.cc index ad3d8d0..246f1c7 100644 --- a/chrome/browser/views/toolbar_view.cc +++ b/chrome/browser/views/toolbar_view.cc @@ -19,6 +19,7 @@ #include "chrome/browser/navigation_controller.h" #include "chrome/browser/navigation_entry.h" #include "chrome/browser/profile.h" +#include "chrome/browser/user_data_manager.h" #include "chrome/browser/user_metrics.h" #include "chrome/browser/views/dom_view.h" #include "chrome/browser/views/go_button.h" @@ -74,7 +75,9 @@ BrowserToolbarView::BrowserToolbarView(CommandController* controller, profile_(NULL), acc_focused_view_(NULL), browser_(browser), - tab_(NULL) { + tab_(NULL), + profiles_helper_(new GetProfilesHelper(this)), + profiles_menu_(NULL) { back_menu_model_.reset(new BackForwardMenuModel( browser, BackForwardMenuModel::BACKWARD_MENU_DELEGATE)); forward_menu_model_.reset(new BackForwardMenuModel( @@ -87,6 +90,7 @@ BrowserToolbarView::BrowserToolbarView(CommandController* controller, } BrowserToolbarView::~BrowserToolbarView() { + profiles_helper_->OnDelegateDeleted(); } void BrowserToolbarView::Init(Profile* profile) { @@ -538,6 +542,16 @@ void BrowserToolbarView::RunAppMenu(const CPoint& pt, HWND hwnd) { l10n_util::GetString(IDS_NEWWINDOW)); menu.AppendMenuItemWithLabel(IDC_GOOFFTHERECORD, l10n_util::GetString(IDS_GOOFFTHERECORD)); + + // Enumerate profiles asynchronously and then create the parent menu item + // "Open new window in profile...". We will create the child menu items for + // this once the asynchronous call is done. See OnGetProfilesDone. + profiles_helper_->GetProfiles(NULL); + Menu* profiles_menu = menu.AppendSubMenu( + IDC_NEWPROFILEWINDOW, + l10n_util::GetString(IDS_NEWPROFILEWINDOW)); + profiles_menu_ = profiles_menu; + menu.AppendSeparator(); menu.AppendMenuItemWithLabel(IDC_SHOW_BOOKMARKS_BAR, l10n_util::GetString(IDS_SHOW_BOOKMARK_BAR)); @@ -565,6 +579,9 @@ void BrowserToolbarView::RunAppMenu(const CPoint& pt, HWND hwnd) { menu.AppendMenuItemWithLabel(IDC_EXIT, l10n_util::GetString(IDS_EXIT)); menu.RunMenuAt(pt.x, pt.y); + + // Menu is going away, so set the profiles menu pointer to NULL. + profiles_menu_ = NULL; } bool BrowserToolbarView::IsItemChecked(int id) const { @@ -590,6 +607,39 @@ void BrowserToolbarView::RunMenu(views::View* source, const CPoint& pt, } } +void BrowserToolbarView::OnGetProfilesDone( + const std::vector<std::wstring>& profiles) { + // Nothing to do if the menu has gone away. + if (!profiles_menu_) + return; + + // Store the latest list of profiles in the browser. + browser_->set_user_data_dir_profiles(profiles); + + // Number of sub menu items that we can show directly. + const int sub_items_count = IDC_NEWPROFILEWINDOW_MAX_ID - + IDC_NEWPROFILEWINDOW_MIN_ID + 1; + std::vector<std::wstring>::const_iterator iter = profiles.begin(); + // Add direct sub menu items for profiles. + for (int i = IDC_NEWPROFILEWINDOW_MIN_ID; + i <= IDC_NEWPROFILEWINDOW_MAX_ID && iter != profiles.end(); + ++i, ++iter) { + profiles_menu_->AppendMenuItemWithLabel(i, *iter); + } + // If there are more profiles then show "Other" link. + if (iter != profiles.end()) { + profiles_menu_->AppendSeparator(); + profiles_menu_->AppendMenuItemWithLabel( + IDC_SELECT_PROFILE, + l10n_util::GetString(IDS_NEWPROFILEWINDOW_OTHERPROFILE)); + } + // Always show a link to select a new profile. + profiles_menu_->AppendSeparator(); + profiles_menu_->AppendMenuItemWithLabel( + IDC_NEW_PROFILE, + l10n_util::GetString(IDS_SELECT_PROFILE_DIALOG_NEW_PROFILE_ENTRY)); +} + bool BrowserToolbarView::GetAccessibleRole(VARIANT* role) { DCHECK(role); @@ -698,6 +748,19 @@ void BrowserToolbarView::Observe(NotificationType type, } } +void BrowserToolbarView::ExecuteCommand(int id) { + // If the command id is for one of the sub-menu-items of the new profile + // window menu then we need to get the name of the profile from the menu + // item id and then pass on that to the browser to take action. + if (id >= IDC_NEWPROFILEWINDOW_MIN_ID && id <= IDC_NEWPROFILEWINDOW_MAX_ID) { + browser_->NewProfileWindowByIndex(id - IDC_NEWPROFILEWINDOW_MIN_ID); + return; + } + + // For all other menu items, use the method in the base class. + EncodingMenuControllerDelegate::ExecuteCommand(id); +} + bool BrowserToolbarView::GetAcceleratorInfo(int id, views::Accelerator* accel) { // The standard Ctrl-X, Ctrl-V and Ctrl-C are not defined as accelerators diff --git a/chrome/browser/views/toolbar_view.h b/chrome/browser/views/toolbar_view.h index 6358e7a..a8067e2 100644 --- a/chrome/browser/views/toolbar_view.h +++ b/chrome/browser/views/toolbar_view.h @@ -7,10 +7,12 @@ #include <vector> +#include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "chrome/browser/back_forward_menu_model.h" #include "chrome/browser/controller.h" #include "chrome/browser/encoding_menu_controller_delegate.h" +#include "chrome/browser/user_data_manager.h" #include "chrome/browser/views/dom_view.h" #include "chrome/browser/views/go_button.h" #include "chrome/browser/views/location_bar_view.h" @@ -37,7 +39,8 @@ class BrowserToolbarView : public views::View, public views::ViewMenuDelegate, public views::DragController, public LocationBarView::Delegate, - public NotificationObserver { + public NotificationObserver, + public GetProfilesHelper::Delegate { public: BrowserToolbarView(CommandController* controller, Browser* browser); virtual ~BrowserToolbarView(); @@ -57,11 +60,15 @@ class BrowserToolbarView : public views::View, virtual bool IsItemChecked(int id) const; // Overridden from Menu::BaseControllerDelegate: + virtual void ExecuteCommand(int id); virtual bool GetAcceleratorInfo(int id, views::Accelerator* accel); // views::MenuDelegate virtual void RunMenu(views::View* source, const CPoint& pt, HWND hwnd); + // GetProfilesHelper::Delegate method. + virtual void OnGetProfilesDone(const std::vector<std::wstring>& profiles); + // Sets the profile which is active on the currently-active tab. void SetProfile(Profile* profile); Profile* profile() { return profile_; } @@ -180,6 +187,12 @@ class BrowserToolbarView : public views::View, // Current tab we're showing state for. TabContents* tab_; + // Profiles menu to populate with profile names. + Menu* profiles_menu_; + + // Helper class to enumerate profiles information on the file thread. + scoped_refptr<GetProfilesHelper> profiles_helper_; + // Controls whether or not a home button should be shown on the toolbar. BooleanPrefMember show_home_button_; @@ -188,4 +201,3 @@ class BrowserToolbarView : public views::View, }; #endif // CHROME_BROWSER_VIEWS_TOOLBAR_VIEW_H__ - |