diff options
author | rlp@chromium.org <rlp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-12 23:38:03 +0000 |
---|---|---|
committer | rlp@chromium.org <rlp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-12 23:38:03 +0000 |
commit | 27917083687c4398f21234ed51ee69e6863ed82a (patch) | |
tree | 507384f8eeb0ef50ff73faeb4e8af84069f5c72d /chrome | |
parent | bdc1b48740f319838da527e4a57e4ba454bfdfc4 (diff) | |
download | chromium_src-27917083687c4398f21234ed51ee69e6863ed82a.zip chromium_src-27917083687c4398f21234ed51ee69e6863ed82a.tar.gz chromium_src-27917083687c4398f21234ed51ee69e6863ed82a.tar.bz2 |
Autoload any profiles with background applications running.
BUG=91560
TEST=added ProfileManagerTest.AutoloadProfilesWithBackgroundApps
Review URL: http://codereview.chromium.org/8080011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105191 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/background/background_mode_manager.cc | 24 | ||||
-rw-r--r-- | chrome/browser/background/background_mode_manager.h | 23 | ||||
-rw-r--r-- | chrome/browser/background/background_mode_manager_unittest.cc | 121 | ||||
-rw-r--r-- | chrome/browser/browser_process_impl.cc | 3 | ||||
-rw-r--r-- | chrome/browser/chrome_browser_main.cc | 7 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_info_cache.cc | 22 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_info_cache.h | 4 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_info_cache_unittest.cc | 27 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_info_interface.h | 5 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_manager.cc | 13 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_manager.h | 3 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_manager_unittest.cc | 26 |
12 files changed, 238 insertions, 40 deletions
diff --git a/chrome/browser/background/background_mode_manager.cc b/chrome/browser/background/background_mode_manager.cc index 0f67dbf..a106a27 100644 --- a/chrome/browser/background/background_mode_manager.cc +++ b/chrome/browser/background/background_mode_manager.cc @@ -15,6 +15,7 @@ #include "chrome/browser/browser_shutdown.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_info_cache.h" #include "chrome/browser/status_icons/status_icon.h" #include "chrome/browser/status_icons/status_tray.h" #include "chrome/browser/ui/browser_list.h" @@ -129,8 +130,11 @@ bool BackgroundModeManager::BackgroundModeData::BackgroundModeDataCompare( /////////////////////////////////////////////////////////////////////////////// // BackgroundModeManager, public -BackgroundModeManager::BackgroundModeManager(CommandLine* command_line) - : status_tray_(NULL), +BackgroundModeManager::BackgroundModeManager( + CommandLine* command_line, + ProfileInfoCache* profile_cache) + : profile_cache_(profile_cache), + status_tray_(NULL), status_icon_(NULL), context_menu_(NULL), in_background_mode_(false), @@ -320,6 +324,12 @@ void BackgroundModeManager::OnApplicationListChanged(Profile* profile) { // Figure out what just happened based on the count of background apps. int count = GetBackgroundAppCount(); + // Update the profile cache with the fact whether background apps are running + // for this profile. + profile_cache_->SetBackgroundStatusOfProfileAtIndex( + profile_cache_->GetIndexOfProfileWithPath(profile->GetPath()), + GetBackgroundAppCountForProfile(profile) != 0); + if (count == 0) { // We've uninstalled our last background app, make sure we exit background // mode and no longer launch on startup. @@ -484,6 +494,12 @@ int BackgroundModeManager::GetBackgroundAppCount() const { return count; } +int BackgroundModeManager::GetBackgroundAppCountForProfile( + Profile* const profile) const { + BackgroundModeData* bmd = GetBackgroundModeData(profile); + return bmd->GetBackgroundAppCount(); +} + void BackgroundModeManager::OnBackgroundAppInstalled( const Extension* extension) { // Background mode is disabled - don't do anything. @@ -606,9 +622,9 @@ void BackgroundModeManager::RemoveStatusTrayIcon() { } BackgroundModeManager::BackgroundModeData* -BackgroundModeManager::GetBackgroundModeData(Profile* profile) { +BackgroundModeManager::GetBackgroundModeData(Profile* const profile) const { DCHECK(background_mode_data_.find(profile) != background_mode_data_.end()); - return background_mode_data_[profile].get(); + return background_mode_data_.find(profile)->second.get(); } // static diff --git a/chrome/browser/background/background_mode_manager.h b/chrome/browser/background/background_mode_manager.h index 832c5ad..fba7d25 100644 --- a/chrome/browser/background/background_mode_manager.h +++ b/chrome/browser/background/background_mode_manager.h @@ -22,6 +22,7 @@ class CommandLine; class Extension; class PrefService; class Profile; +class ProfileInfoCache; class StatusIcon; class StatusTray; @@ -45,13 +46,19 @@ class BackgroundModeManager public ProfileKeyedService, public ui::SimpleMenuModel::Delegate { public: - explicit BackgroundModeManager(CommandLine* command_line); + BackgroundModeManager(CommandLine* command_line, + ProfileInfoCache* profile_cache); virtual ~BackgroundModeManager(); static void RegisterPrefs(PrefService* prefs); virtual void RegisterProfile(Profile* profile); + // Returns true if background mode is permanently disabled for this Chrome + // session. + static bool IsBackgroundModePermanentlyDisabled( + const CommandLine* command_line); + static void LaunchBackgroundApplication(Profile* profile, const Extension* extension); @@ -68,6 +75,8 @@ class BackgroundModeManager EnableAfterBackgroundAppInstall); FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, MultiProfile); + FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, + ProfileInfoCacheStorage); class BackgroundModeData : public ui::SimpleMenuModel::Delegate { public: @@ -200,7 +209,7 @@ class BackgroundModeManager // Returns the BackgroundModeData associated with this profile. If it does // not exist, returns NULL. BackgroundModeManager::BackgroundModeData* GetBackgroundModeData( - Profile* profile); + Profile* const profile) const; // Returns true if the "Let chrome run in the background" pref is checked. // (virtual to allow overriding in tests). @@ -216,10 +225,12 @@ class BackgroundModeManager // overriding in unit tests). virtual int GetBackgroundAppCount() const; - // Returns true if background mode is permanently disabled for this chrome - // session. - static bool IsBackgroundModePermanentlyDisabled( - const CommandLine* command_line); + // Returns the number of background apps for a profile. + virtual int GetBackgroundAppCountForProfile(Profile* const profile) const; + + // Reference to the profile info cache. It is used to update the background + // app status of profiles when they open/close background apps. + ProfileInfoCache* profile_cache_; // Registrars for managing our change observers. NotificationRegistrar registrar_; diff --git a/chrome/browser/background/background_mode_manager_unittest.cc b/chrome/browser/background/background_mode_manager_unittest.cc index 41a0192..b33f32e 100644 --- a/chrome/browser/background/background_mode_manager_unittest.cc +++ b/chrome/browser/background/background_mode_manager_unittest.cc @@ -5,28 +5,36 @@ #include "base/command_line.h" #include "base/memory/scoped_ptr.h" #include "chrome/browser/background/background_mode_manager.h" +#include "chrome/browser/profiles/profile_info_cache.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/common/chrome_switches.h" #include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_profile.h" +#include "chrome/test/base/testing_profile_manager.h" #include "testing/gtest/include/gtest/gtest.h" class BackgroundModeManagerTest : public testing::Test { public: - BackgroundModeManagerTest() {} + BackgroundModeManagerTest() + : profile_manager_( + static_cast<TestingBrowserProcess*>(g_browser_process)) {} ~BackgroundModeManagerTest() {} void SetUp() { command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); + ASSERT_TRUE(profile_manager_.SetUp()); } scoped_ptr<CommandLine> command_line_; + TestingProfileManager profile_manager_; }; class TestBackgroundModeManager : public BackgroundModeManager { public: - explicit TestBackgroundModeManager(CommandLine* command_line) - : BackgroundModeManager(command_line), + explicit TestBackgroundModeManager( + CommandLine* command_line, ProfileInfoCache* cache) + : BackgroundModeManager(command_line, cache), enabled_(true), app_count_(0), + profile_app_count_(0), have_status_tray_(false), launch_on_startup_(false) {} virtual void EnableLaunchOnStartup(bool launch) OVERRIDE { @@ -35,14 +43,22 @@ class TestBackgroundModeManager : public BackgroundModeManager { virtual void CreateStatusTrayIcon() OVERRIDE { have_status_tray_ = true; } virtual void RemoveStatusTrayIcon() OVERRIDE { have_status_tray_ = false; } virtual int GetBackgroundAppCount() const OVERRIDE { return app_count_; } + virtual int GetBackgroundAppCountForProfile( + Profile* const profile) const OVERRIDE { + return profile_app_count_; + } virtual bool IsBackgroundModePrefEnabled() const OVERRIDE { return enabled_; } void SetBackgroundAppCount(int count) { app_count_ = count; } + void SetBackgroundAppCountForProfile(int count) { + profile_app_count_ = count; + } void SetEnabled(bool enabled) { enabled_ = enabled; } bool HaveStatusTray() const { return have_status_tray_; } bool IsLaunchOnStartup() const { return launch_on_startup_; } private: bool enabled_; int app_count_; + int profile_app_count_; // Flags to track whether we are launching on startup/have a status tray. bool have_status_tray_; @@ -64,28 +80,30 @@ static void AssertBackgroundModeInactive( } TEST_F(BackgroundModeManagerTest, BackgroundAppLoadUnload) { - TestingProfile profile; - TestBackgroundModeManager manager(command_line_.get()); - manager.RegisterProfile(&profile); + TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); + TestBackgroundModeManager manager( + command_line_.get(), profile_manager_.profile_info_cache()); + manager.RegisterProfile(profile); EXPECT_FALSE(BrowserList::WillKeepAlive()); // Mimic app load. manager.OnBackgroundAppInstalled(NULL); manager.SetBackgroundAppCount(1); - manager.OnApplicationListChanged(&profile); + manager.OnApplicationListChanged(profile); AssertBackgroundModeActive(manager); // Mimic app unload. manager.SetBackgroundAppCount(0); - manager.OnApplicationListChanged(&profile); + manager.OnApplicationListChanged(profile); AssertBackgroundModeInactive(manager); } // App installs while background mode is disabled should do nothing. TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) { - TestingProfile profile; - TestBackgroundModeManager manager(command_line_.get()); - manager.RegisterProfile(&profile); + TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); + TestBackgroundModeManager manager( + command_line_.get(), profile_manager_.profile_info_cache()); + manager.RegisterProfile(profile); // Turn off background mode. manager.SetEnabled(false); manager.DisableBackgroundMode(); @@ -95,11 +113,11 @@ TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) { // be modified. manager.OnBackgroundAppInstalled(NULL); manager.SetBackgroundAppCount(1); - manager.OnApplicationListChanged(&profile); + manager.OnApplicationListChanged(profile); AssertBackgroundModeInactive(manager); manager.SetBackgroundAppCount(0); - manager.OnApplicationListChanged(&profile); + manager.OnApplicationListChanged(profile); AssertBackgroundModeInactive(manager); // Re-enable background mode. @@ -112,9 +130,10 @@ TEST_F(BackgroundModeManagerTest, BackgroundAppInstallUninstallWhileDisabled) { // App installs while disabled should do nothing until background mode is // enabled.. TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) { - TestingProfile profile; - TestBackgroundModeManager manager(command_line_.get()); - manager.RegisterProfile(&profile); + TestingProfile* profile = profile_manager_.CreateTestingProfile("p1"); + TestBackgroundModeManager manager( + command_line_.get(), profile_manager_.profile_info_cache()); + manager.RegisterProfile(profile); // Install app, should show status tray icon. manager.OnBackgroundAppInstalled(NULL); @@ -122,7 +141,7 @@ TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) { // BackgroundApplicationListModel which would result in another // call to CreateStatusTray. manager.SetBackgroundAppCount(1); - manager.OnApplicationListChanged(&profile); + manager.OnApplicationListChanged(profile); AssertBackgroundModeActive(manager); // Turn off background mode - should hide status tray icon. @@ -138,28 +157,29 @@ TEST_F(BackgroundModeManagerTest, EnableAfterBackgroundAppInstall) { // Uninstall app, should hide status tray icon again. manager.SetBackgroundAppCount(0); - manager.OnApplicationListChanged(&profile); + manager.OnApplicationListChanged(profile); AssertBackgroundModeInactive(manager); } TEST_F(BackgroundModeManagerTest, MultiProfile) { - TestingProfile profile1; - TestingProfile profile2; - TestBackgroundModeManager manager(command_line_.get()); - manager.RegisterProfile(&profile1); - manager.RegisterProfile(&profile2); + TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); + TestingProfile* profile2 = profile_manager_.CreateTestingProfile("p2"); + TestBackgroundModeManager manager( + command_line_.get(), profile_manager_.profile_info_cache()); + manager.RegisterProfile(profile1); + manager.RegisterProfile(profile2); EXPECT_FALSE(BrowserList::WillKeepAlive()); // Install app, should show status tray icon. manager.OnBackgroundAppInstalled(NULL); manager.SetBackgroundAppCount(1); - manager.OnApplicationListChanged(&profile1); + manager.OnApplicationListChanged(profile1); AssertBackgroundModeActive(manager); // Install app for other profile, hsould show other status tray icon. manager.OnBackgroundAppInstalled(NULL); manager.SetBackgroundAppCount(2); - manager.OnApplicationListChanged(&profile2); + manager.OnApplicationListChanged(profile2); AssertBackgroundModeActive(manager); // Should hide both status tray icons. @@ -173,11 +193,58 @@ TEST_F(BackgroundModeManagerTest, MultiProfile) { AssertBackgroundModeActive(manager); manager.SetBackgroundAppCount(1); - manager.OnApplicationListChanged(&profile2); + manager.OnApplicationListChanged(profile2); // There is still one background app alive AssertBackgroundModeActive(manager); manager.SetBackgroundAppCount(0); - manager.OnApplicationListChanged(&profile1); + manager.OnApplicationListChanged(profile1); AssertBackgroundModeInactive(manager); } + +TEST_F(BackgroundModeManagerTest, ProfileInfoCacheStorage) { + TestingProfile* profile1 = profile_manager_.CreateTestingProfile("p1"); + TestingProfile* profile2 = profile_manager_.CreateTestingProfile("p2"); + TestBackgroundModeManager manager( + command_line_.get(), profile_manager_.profile_info_cache()); + manager.RegisterProfile(profile1); + manager.RegisterProfile(profile2); + EXPECT_FALSE(BrowserList::WillKeepAlive()); + + ProfileInfoCache* cache = profile_manager_.profile_info_cache(); + EXPECT_EQ(2u, cache->GetNumberOfProfiles()); + + EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(0)); + EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(1)); + + // Install app, should show status tray icon. + manager.OnBackgroundAppInstalled(NULL); + manager.SetBackgroundAppCount(1); + manager.SetBackgroundAppCountForProfile(1); + manager.OnApplicationListChanged(profile1); + + // Install app for other profile. + manager.OnBackgroundAppInstalled(NULL); + manager.SetBackgroundAppCount(1); + manager.SetBackgroundAppCountForProfile(1); + manager.OnApplicationListChanged(profile2); + + EXPECT_TRUE(cache->GetBackgroundStatusOfProfileAtIndex(0)); + EXPECT_TRUE(cache->GetBackgroundStatusOfProfileAtIndex(1)); + + manager.SetBackgroundAppCountForProfile(0); + manager.OnApplicationListChanged(profile1); + + size_t p1_index = cache->GetIndexOfProfileWithPath(profile1->GetPath()); + EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(p1_index)); + + manager.SetBackgroundAppCountForProfile(0); + manager.OnApplicationListChanged(profile2); + + size_t p2_index = cache->GetIndexOfProfileWithPath(profile1->GetPath()); + EXPECT_FALSE(cache->GetBackgroundStatusOfProfileAtIndex(p2_index)); + + // Even though neither has background status on, there should still be two + // profiles in the cache. + EXPECT_EQ(2u, cache->GetNumberOfProfiles()); +} diff --git a/chrome/browser/browser_process_impl.cc b/chrome/browser/browser_process_impl.cc index 24df1a4..6c932f2 100644 --- a/chrome/browser/browser_process_impl.cc +++ b/chrome/browser/browser_process_impl.cc @@ -1020,7 +1020,8 @@ void BrowserProcessImpl::CreateTabCloseableStateWatcher() { void BrowserProcessImpl::CreateBackgroundModeManager() { DCHECK(background_mode_manager_.get() == NULL); background_mode_manager_.reset( - new BackgroundModeManager(CommandLine::ForCurrentProcess())); + new BackgroundModeManager(CommandLine::ForCurrentProcess(), + &profile_manager()->GetProfileInfoCache())); } void BrowserProcessImpl::CreateStatusTray() { diff --git a/chrome/browser/chrome_browser_main.cc b/chrome/browser/chrome_browser_main.cc index 5f9ddf9..411e096 100644 --- a/chrome/browser/chrome_browser_main.cc +++ b/chrome/browser/chrome_browser_main.cc @@ -29,6 +29,7 @@ #include "base/values.h" #include "build/build_config.h" #include "chrome/browser/about_flags.h" +#include "chrome/browser/background/background_mode_manager.h" #include "chrome/browser/browser_process_impl.h" #include "chrome/browser/browser_shutdown.h" #include "chrome/browser/chrome_browser_main_gtk.h" @@ -1579,6 +1580,12 @@ int ChromeBrowserMainParts::PreMainMessageLoopRunInternal() { if (!profile_) return content::RESULT_CODE_NORMAL_EXIT; + // Autoload any profiles which are running background apps. + // TODO(rlp): Do this on a separate thread. See http://crbug.com/99075. + if (!BackgroundModeManager::IsBackgroundModePermanentlyDisabled( + &parsed_command_line())) { + g_browser_process->profile_manager()->AutoloadProfiles(); + } // Post-profile init --------------------------------------------------------- #if defined(OS_CHROMEOS) diff --git a/chrome/browser/profiles/profile_info_cache.cc b/chrome/browser/profiles/profile_info_cache.cc index fcf9f00..96a17e1 100644 --- a/chrome/browser/profiles/profile_info_cache.cc +++ b/chrome/browser/profiles/profile_info_cache.cc @@ -27,6 +27,7 @@ namespace { const char kNameKey[] = "name"; const char kUserNameKey[] = "user_name"; const char kAvatarIconKey[] = "avatar_icon"; +const char kBackgroundAppsKey[] = "background_apps"; const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; const int kDefaultAvatarIconResources[] = { @@ -98,6 +99,8 @@ void ProfileInfoCache::AddProfileToCache(const FilePath& profile_path, info->SetString(kNameKey, name); info->SetString(kUserNameKey, username); info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); + // Default value for whether background apps are running is false. + info->SetBoolean(kBackgroundAppsKey, false); cache->Set(key, info.release()); sorted_keys_.insert(FindPositionForProfile(key, name), key); @@ -167,6 +170,14 @@ const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex( return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id); } +bool ProfileInfoCache::GetBackgroundStatusOfProfileAtIndex( + size_t index) const { + bool background_app_status; + GetInfoForProfileAtIndex(index)->GetBoolean(kBackgroundAppsKey, + &background_app_status); + return background_app_status; +} + size_t ProfileInfoCache::GetAvatarIconIndexOfProfileAtIndex(size_t index) const { std::string icon_url; @@ -203,6 +214,17 @@ void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index, SetInfoForProfileAtIndex(index, info.release()); } +void ProfileInfoCache::SetBackgroundStatusOfProfileAtIndex( + size_t index, + bool running_background_apps) { + if (GetBackgroundStatusOfProfileAtIndex(index) == running_background_apps) + return; + scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); + info->SetBoolean(kBackgroundAppsKey, running_background_apps); + // This takes ownership of |info|. + SetInfoForProfileAtIndex(index, info.release()); +} + string16 ProfileInfoCache::ChooseNameForNewProfile() { for (int name_index = 1; ; ++name_index) { string16 name = l10n_util::GetStringFUTF16Int( diff --git a/chrome/browser/profiles/profile_info_cache.h b/chrome/browser/profiles/profile_info_cache.h index b4fe501..f369ef0 100644 --- a/chrome/browser/profiles/profile_info_cache.h +++ b/chrome/browser/profiles/profile_info_cache.h @@ -49,12 +49,16 @@ class ProfileInfoCache : public ProfileInfoInterface { virtual string16 GetUserNameOfProfileAtIndex(size_t index) const OVERRIDE; virtual const gfx::Image& GetAvatarIconOfProfileAtIndex( size_t index) const OVERRIDE; + virtual bool GetBackgroundStatusOfProfileAtIndex( + size_t index) const OVERRIDE; size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const; void SetNameOfProfileAtIndex(size_t index, const string16& name); void SetUserNameOfProfileAtIndex(size_t index, const string16& user_name); void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index); + void SetBackgroundStatusOfProfileAtIndex(size_t index, + bool running_background_apps); // Returns unique name that can be assigned to a newly created profile. string16 ChooseNameForNewProfile(); diff --git a/chrome/browser/profiles/profile_info_cache_unittest.cc b/chrome/browser/profiles/profile_info_cache_unittest.cc index 5fea20f..be03de0 100644 --- a/chrome/browser/profiles/profile_info_cache_unittest.cc +++ b/chrome/browser/profiles/profile_info_cache_unittest.cc @@ -114,4 +114,31 @@ TEST_F(ProfileInfoCacheUnittests, MutateProfile) { GetCache()->GetAvatarIconOfProfileAtIndex(1); } +TEST_F(ProfileInfoCacheUnittests, BackgroundModeStatus) { + GetCache()->AddProfileToCache( + GetUserDataDir().Append(StringToFilePath("path_1")), + ASCIIToUTF16("name_1"), string16(), 0); + GetCache()->AddProfileToCache( + GetUserDataDir().Append(StringToFilePath("path_2")), + ASCIIToUTF16("name_2"), string16(), 0); + + EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); + EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); + + GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true); + + EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); + EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); + + GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true); + + EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); + EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); + + GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false); + + EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0)); + EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); +} + } // namespace diff --git a/chrome/browser/profiles/profile_info_interface.h b/chrome/browser/profiles/profile_info_interface.h index 9677be9..6a08f9a 100644 --- a/chrome/browser/profiles/profile_info_interface.h +++ b/chrome/browser/profiles/profile_info_interface.h @@ -31,6 +31,11 @@ class ProfileInfoInterface { virtual const gfx::Image& GetAvatarIconOfProfileAtIndex( size_t index) const = 0; + // Returns true if the profile at the given index is currently running any + // background apps. + virtual bool GetBackgroundStatusOfProfileAtIndex( + size_t index) const = 0; + protected: virtual ~ProfileInfoInterface() {} }; diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc index a05ab60..bf88997c 100644 --- a/chrome/browser/profiles/profile_manager.cc +++ b/chrome/browser/profiles/profile_manager.cc @@ -545,6 +545,19 @@ bool ProfileManager::IsMultipleProfilesEnabled() { #endif } +void ProfileManager::AutoloadProfiles() { + ProfileInfoCache& cache = GetProfileInfoCache(); + size_t number_of_profiles = cache.GetNumberOfProfiles(); + for (size_t p = 0; p < number_of_profiles; ++p) { + if (cache.GetBackgroundStatusOfProfileAtIndex(p)) { + // If status is true, that profile is running background apps. By calling + // GetProfile, we automatically cause the profile to be loaded which will + // register it with the BackgroundModeManager. + GetProfile(cache.GetPathOfProfileAtIndex(p)); + } + } +} + ProfileManagerWithoutInit::ProfileManagerWithoutInit( const FilePath& user_data_dir) : ProfileManager(user_data_dir) { } diff --git a/chrome/browser/profiles/profile_manager.h b/chrome/browser/profiles/profile_manager.h index f894555..27347ab 100644 --- a/chrome/browser/profiles/profile_manager.h +++ b/chrome/browser/profiles/profile_manager.h @@ -180,6 +180,9 @@ class ProfileManager : public base::NonThreadSafe, // Checks if multiple profiles is enabled. static bool IsMultipleProfilesEnabled(); + // Autoloads profiles if they are running background apps. + void AutoloadProfiles(); + // Register and add testing profile to the ProfileManager. Use ONLY in tests. // This allows the creation of Profiles outside of the standard creation path // for testing. If |addToCache|, add to ProfileInfoCache as well. diff --git a/chrome/browser/profiles/profile_manager_unittest.cc b/chrome/browser/profiles/profile_manager_unittest.cc index 49a5a72..4e23a73 100644 --- a/chrome/browser/profiles/profile_manager_unittest.cc +++ b/chrome/browser/profiles/profile_manager_unittest.cc @@ -9,6 +9,7 @@ #include "base/path_service.h" #include "base/scoped_temp_dir.h" #include "base/system_monitor/system_monitor.h" +#include "base/utf_string_conversions.h" #include "base/values.h" #include "build/build_config.h" #include "chrome/browser/browser_process.h" @@ -16,6 +17,7 @@ #include "chrome/browser/io_thread.h" #include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_info_cache.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_notification_types.h" @@ -48,8 +50,8 @@ class ProfileManagerTest : public testing::Test { ui_thread_(BrowserThread::UI, &message_loop_), db_thread_(BrowserThread::DB, &message_loop_), file_thread_(BrowserThread::FILE, &message_loop_), - io_thread_(local_state_.Get(), NULL, extension_event_router_forwarder_), - profile_manager_(new ProfileManagerWithoutInit(temp_dir_.path())) { + io_thread_(local_state_.Get(), NULL, + extension_event_router_forwarder_) { #if defined(OS_MACOSX) base::SystemMonitor::AllocateSystemIOPorts(); #endif @@ -61,6 +63,7 @@ class ProfileManagerTest : public testing::Test { virtual void SetUp() { // Create a new temporary directory, and store the path ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + profile_manager_.reset(new ProfileManagerWithoutInit(temp_dir_.path())); } virtual void TearDown() { @@ -242,3 +245,22 @@ TEST_F(ProfileManagerTest, CreateProfilesAsync) { message_loop_.RunAllPending(); } + +TEST_F(ProfileManagerTest, AutoloadProfilesWithBackgroundApps) { + ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); + + EXPECT_EQ(0u, cache.GetNumberOfProfiles()); + cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_1"), + ASCIIToUTF16("name_1"), string16(), 0); + cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_2"), + ASCIIToUTF16("name_2"), string16(), 0); + cache.AddProfileToCache(cache.GetUserDataDir().AppendASCII("path_3"), + ASCIIToUTF16("name_3"), string16(), 0); + cache.SetBackgroundStatusOfProfileAtIndex(0, true); + cache.SetBackgroundStatusOfProfileAtIndex(2, true); + EXPECT_EQ(3u, cache.GetNumberOfProfiles()); + + profile_manager_->AutoloadProfiles(); + + EXPECT_EQ(2u, profile_manager_->GetLoadedProfiles().size()); +} |