summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormarja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-19 11:34:59 +0000
committermarja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-19 11:34:59 +0000
commit72e60aa2742b986c47cbec86150356f21ac7e6e9 (patch)
tree0469bf46c1f2abbc4ac565094d35e0dbb6c9eeb6
parent8d8b239819e5ec32212ed53de34b528492a0c281 (diff)
downloadchromium_src-72e60aa2742b986c47cbec86150356f21ac7e6e9.zip
chromium_src-72e60aa2742b986c47cbec86150356f21ac7e6e9.tar.gz
chromium_src-72e60aa2742b986c47cbec86150356f21ac7e6e9.tar.bz2
Fix: after updating, restore tabs from all profiles.
Also, launch the profiles in the order they became active. (Windows are still not launched in the original order, since that information is lost.) BUG=109816 TEST=See bug & BrowserInitTest.UpdateWithTwoProfiles Review URL: http://codereview.chromium.org/9232007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118286 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/sessions/session_service.cc6
-rw-r--r--chrome/browser/ui/browser_init.cc95
-rw-r--r--chrome/browser/ui/browser_init.h14
-rw-r--r--chrome/browser/ui/browser_init_browsertest.cc84
4 files changed, 151 insertions, 48 deletions
diff --git a/chrome/browser/sessions/session_service.cc b/chrome/browser/sessions/session_service.cc
index 7ec80a4..68c7d67 100644
--- a/chrome/browser/sessions/session_service.cc
+++ b/chrome/browser/sessions/session_service.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 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.
@@ -11,6 +11,7 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
+#include "base/command_line.h"
#include "base/file_util.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop.h"
@@ -485,7 +486,8 @@ bool SessionService::RestoreIfNecessary(const std::vector<GURL>& urls_to_open,
MoveCurrentSessionToLastSession();
move_on_new_browser_ = false;
}
- SessionStartupPref pref = SessionStartupPref::GetStartupPref(profile());
+ SessionStartupPref pref = BrowserInit::GetSessionStartupPref(
+ *CommandLine::ForCurrentProcess(), profile());
if (pref.type == SessionStartupPref::LAST) {
SessionRestore::RestoreSession(
profile(), browser,
diff --git a/chrome/browser/ui/browser_init.cc b/chrome/browser/ui/browser_init.cc
index 8338322..fef3efe 100644
--- a/chrome/browser/ui/browser_init.cc
+++ b/chrome/browser/ui/browser_init.cc
@@ -465,24 +465,6 @@ bool SessionCrashedInfoBarDelegate::Accept() {
// Utility functions ----------------------------------------------------------
-SessionStartupPref GetSessionStartupPref(const CommandLine& command_line,
- Profile* profile) {
- SessionStartupPref pref = SessionStartupPref::GetStartupPref(profile);
- if (command_line.HasSwitch(switches::kRestoreLastSession) ||
- BrowserInit::WasRestarted()) {
- pref.type = SessionStartupPref::LAST;
- }
- if (pref.type == SessionStartupPref::LAST &&
- IncognitoModePrefs::ShouldLaunchIncognito(command_line,
- profile->GetPrefs())) {
- // We don't store session information when incognito. If the user has
- // chosen to restore last session and launched incognito, fallback to
- // default launch behavior.
- pref.type = SessionStartupPref::DEFAULT;
- }
- return pref;
-}
-
enum LaunchMode {
LM_TO_BE_DECIDED = 0, // Possibly direct launch or via a shortcut.
LM_AS_WEBAPP, // Launched as a installed web application.
@@ -641,6 +623,9 @@ BrowserInit::BrowserInit() {}
BrowserInit::~BrowserInit() {}
+// static
+bool BrowserInit::was_restarted_read_ = false;
+
void BrowserInit::AddFirstRunTab(const GURL& url) {
first_run_tabs_.push_back(url);
}
@@ -735,18 +720,36 @@ bool BrowserInit::WasRestarted() {
// Stores the value of the preference kWasRestarted had when it was read.
static bool was_restarted = false;
- // True if we have already read and reset the preference kWasRestarted.
- static bool was_restarted_read = false;
-
- if (!was_restarted_read) {
+ if (!was_restarted_read_) {
PrefService* pref_service = g_browser_process->local_state();
was_restarted = pref_service->GetBoolean(prefs::kWasRestarted);
pref_service->SetBoolean(prefs::kWasRestarted, false);
- was_restarted_read = true;
+ was_restarted_read_ = true;
}
return was_restarted;
}
+// static
+SessionStartupPref BrowserInit::GetSessionStartupPref(
+ const CommandLine& command_line,
+ Profile* profile) {
+ SessionStartupPref pref = SessionStartupPref::GetStartupPref(profile);
+ if (command_line.HasSwitch(switches::kRestoreLastSession) ||
+ BrowserInit::WasRestarted()) {
+ pref.type = SessionStartupPref::LAST;
+ }
+ if (pref.type == SessionStartupPref::LAST &&
+ IncognitoModePrefs::ShouldLaunchIncognito(command_line,
+ profile->GetPrefs())) {
+ // We don't store session information when incognito. If the user has
+ // chosen to restore last session and launched incognito, fallback to
+ // default launch behavior.
+ pref.type = SessionStartupPref::DEFAULT;
+ }
+ return pref;
+}
+
+
// BrowserInit::LaunchWithProfile::Tab ----------------------------------------
BrowserInit::LaunchWithProfile::Tab::Tab() : is_app(false), is_pinned(true) {}
@@ -1677,26 +1680,34 @@ bool BrowserInit::ProcessCmdLineImpl(
IS_PROCESS_STARTUP : IS_NOT_PROCESS_STARTUP;
IsFirstRun is_first_run = first_run::IsChromeFirstRun() ?
IS_FIRST_RUN : IS_NOT_FIRST_RUN;
- // Launch the last used profile with the full command line, and the other
- // opened profiles without the URLs to launch.
- CommandLine command_line_without_urls(command_line.GetProgram());
- const CommandLine::SwitchMap& switches = command_line.GetSwitches();
- for (CommandLine::SwitchMap::const_iterator switch_it = switches.begin();
- switch_it != switches.end(); ++switch_it) {
- command_line_without_urls.AppendSwitchNative(switch_it->first,
- switch_it->second);
- }
- if (!browser_init->LaunchBrowser(command_line, last_used_profile, cur_dir,
- is_process_startup, is_first_run, return_code))
- return false;
- is_process_startup = BrowserInit::IS_NOT_PROCESS_STARTUP;
-
- for (Profiles::const_iterator it = last_opened_profiles.begin();
- it != last_opened_profiles.end(); ++it) {
- if (*it != last_used_profile &&
- !browser_init->LaunchBrowser(command_line_without_urls, *it,
- cur_dir, is_process_startup, is_first_run, return_code))
+ // If this is the first launch, or the user has exited the browser by
+ // closing all windows for all profiles, there are no last used profiles. In
+ // that case, launch the |last_used_profile|. It will be the initial
+ // profile, or the profile which owned the last window, respectively.
+ if (last_opened_profiles.empty()) {
+ if (!browser_init->LaunchBrowser(command_line, last_used_profile, cur_dir,
+ is_process_startup, is_first_run, return_code))
return false;
+ } else {
+ // Launch the last used profile with the full command line, and the other
+ // opened profiles without the URLs to launch.
+ CommandLine command_line_without_urls(command_line.GetProgram());
+ const CommandLine::SwitchMap& switches = command_line.GetSwitches();
+ for (CommandLine::SwitchMap::const_iterator switch_it = switches.begin();
+ switch_it != switches.end(); ++switch_it) {
+ command_line_without_urls.AppendSwitchNative(switch_it->first,
+ switch_it->second);
+ }
+ // Launch the profiles in the order they became active.
+ for (Profiles::const_iterator it = last_opened_profiles.begin();
+ it != last_opened_profiles.end(); ++it) {
+ if (!browser_init->LaunchBrowser((*it == last_used_profile) ?
+ command_line : command_line_without_urls, *it, cur_dir,
+ is_process_startup, is_first_run, return_code))
+ return false;
+ // We've launched at least one browser.
+ is_process_startup = BrowserInit::IS_NOT_PROCESS_STARTUP;
+ }
}
}
return true;
diff --git a/chrome/browser/ui/browser_init.h b/chrome/browser/ui/browser_init.h
index 8ccec34..6675ec1 100644
--- a/chrome/browser/ui/browser_init.h
+++ b/chrome/browser/ui/browser_init.h
@@ -12,6 +12,7 @@
#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/gtest_prod_util.h"
+#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "googleurl/src/gurl.h"
@@ -87,6 +88,10 @@ class BrowserInit {
// the first time.
static bool WasRestarted();
+ static SessionStartupPref GetSessionStartupPref(
+ const CommandLine& command_line,
+ Profile* profile);
+
// LaunchWithProfile ---------------------------------------------------------
//
// Assists launching the application and appending the initial tabs for a
@@ -247,6 +252,10 @@ class BrowserInit {
private:
friend class CloudPrintProxyPolicyTest;
friend class CloudPrintProxyPolicyStartupTest;
+ FRIEND_TEST_ALL_PREFIXES(BrowserInitTest,
+ ReadingWasRestartedAfterNormalStart);
+ FRIEND_TEST_ALL_PREFIXES(BrowserInitTest, ReadingWasRestartedAfterRestart);
+ FRIEND_TEST_ALL_PREFIXES(BrowserInitTest, UpdateWithTwoProfiles);
// Returns the list of URLs to open from the command line. The returned
// vector is empty if the user didn't specify any URLs on the command line.
@@ -273,6 +282,11 @@ class BrowserInit {
// Additional tabs to open during first run.
std::vector<GURL> first_run_tabs_;
+ // True if we have already read and reset the preference kWasRestarted. (A
+ // member variable instead of a static variable inside WasRestarted because
+ // of testing.)
+ static bool was_restarted_read_;
+
DISALLOW_COPY_AND_ASSIGN(BrowserInit);
};
diff --git a/chrome/browser/ui/browser_init_browsertest.cc b/chrome/browser/ui/browser_init_browsertest.cc
index 748392d..1ab3823 100644
--- a/chrome/browser/ui/browser_init_browsertest.cc
+++ b/chrome/browser/ui/browser_init_browsertest.cc
@@ -13,12 +13,14 @@
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_init.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
+#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
@@ -321,6 +323,7 @@ IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenAppShortcutPanel) {
IN_PROC_BROWSER_TEST_F(BrowserInitTest, ReadingWasRestartedAfterRestart) {
// Tests that BrowserInit::WasRestarted reads and resets the preference
// kWasRestarted correctly.
+ BrowserInit::was_restarted_read_ = false;
PrefService* pref_service = g_browser_process->local_state();
pref_service->SetBoolean(prefs::kWasRestarted, true);
EXPECT_TRUE(BrowserInit::WasRestarted());
@@ -331,6 +334,7 @@ IN_PROC_BROWSER_TEST_F(BrowserInitTest, ReadingWasRestartedAfterRestart) {
IN_PROC_BROWSER_TEST_F(BrowserInitTest, ReadingWasRestartedAfterNormalStart) {
// Tests that BrowserInit::WasRestarted reads and resets the preference
// kWasRestarted correctly.
+ BrowserInit::was_restarted_read_ = false;
PrefService* pref_service = g_browser_process->local_state();
pref_service->SetBoolean(prefs::kWasRestarted, false);
EXPECT_FALSE(BrowserInit::WasRestarted());
@@ -375,9 +379,11 @@ IN_PROC_BROWSER_TEST_F(BrowserInitTest, StartupURLsForTwoProfiles) {
int return_code;
BrowserInit browser_init;
- std::vector<Profile*> other_profiles(1, other_profile);
+ std::vector<Profile*> last_opened_profiles;
+ last_opened_profiles.push_back(default_profile);
+ last_opened_profiles.push_back(other_profile);
browser_init.Start(dummy, profile_manager->user_data_dir(), default_profile,
- other_profiles, &return_code);
+ last_opened_profiles, &return_code);
// urls1 were opened in a browser for default_profile, and urls2 were opened
// in a browser for other_profile.
@@ -385,8 +391,7 @@ IN_PROC_BROWSER_TEST_F(BrowserInitTest, StartupURLsForTwoProfiles) {
// |browser()| is still around at this point, even though we've closed it's
// window. Thus the browser count for default_profile is 2.
ASSERT_EQ(2u, BrowserList::GetBrowserCount(default_profile));
- new_browser = FindOneOtherBrowserForProfile(default_profile,
- browser());
+ new_browser = FindOneOtherBrowserForProfile(default_profile, browser());
ASSERT_TRUE(new_browser);
ASSERT_EQ(1, new_browser->tab_count());
EXPECT_EQ(urls1[0], new_browser->GetWebContentsAt(0)->GetURL());
@@ -397,3 +402,74 @@ IN_PROC_BROWSER_TEST_F(BrowserInitTest, StartupURLsForTwoProfiles) {
ASSERT_EQ(1, new_browser->tab_count());
EXPECT_EQ(urls2[0], new_browser->GetWebContentsAt(0)->GetURL());
}
+
+IN_PROC_BROWSER_TEST_F(BrowserInitTest, UpdateWithTwoProfiles) {
+ // Make BrowserInit::WasRestarted() return true.
+ BrowserInit::was_restarted_read_ = false;
+ PrefService* pref_service = g_browser_process->local_state();
+ pref_service->SetBoolean(prefs::kWasRestarted, true);
+
+ ProfileManager* profile_manager = g_browser_process->profile_manager();
+
+ // Create two profiles.
+ FilePath dest_path = profile_manager->user_data_dir();
+
+ Profile* profile1 = profile_manager->GetProfile(
+ dest_path.Append(FILE_PATH_LITERAL("New Profile 1")));
+ ASSERT_TRUE(profile1);
+
+ Profile* profile2 = profile_manager->GetProfile(
+ dest_path.Append(FILE_PATH_LITERAL("New Profile 2")));
+ ASSERT_TRUE(profile2);
+
+ // Use a couple arbitrary URLs.
+ std::vector<GURL> urls1;
+ urls1.push_back(ui_test_utils::GetTestUrl(
+ FilePath(FilePath::kCurrentDirectory),
+ FilePath(FILE_PATH_LITERAL("title1.html"))));
+ std::vector<GURL> urls2;
+ urls2.push_back(ui_test_utils::GetTestUrl(
+ FilePath(FilePath::kCurrentDirectory),
+ FilePath(FILE_PATH_LITERAL("title2.html"))));
+
+ // Set different startup preferences for the 2 profiles.
+ SessionStartupPref pref1(SessionStartupPref::URLS);
+ pref1.urls = urls1;
+ SessionStartupPref::SetStartupPref(profile1, pref1);
+ SessionStartupPref pref2(SessionStartupPref::URLS);
+ pref2.urls = urls2;
+ SessionStartupPref::SetStartupPref(profile2, pref2);
+
+ // Simulate a launch after a browser update.
+ CommandLine dummy(CommandLine::NO_PROGRAM);
+ int return_code;
+ BrowserInit browser_init;
+ std::vector<Profile*> last_opened_profiles;
+ last_opened_profiles.push_back(profile1);
+ last_opened_profiles.push_back(profile2);
+ browser_init.Start(dummy, profile_manager->user_data_dir(), profile1,
+ last_opened_profiles, &return_code);
+
+ while (SessionRestore::IsRestoring())
+ MessageLoop::current()->RunAllPending();
+
+ // The startup URLs are ignored, and instead the last open sessions are
+ // restored.
+ EXPECT_TRUE(profile1->restored_last_session());
+ EXPECT_TRUE(profile2->restored_last_session());
+
+ Browser* new_browser = NULL;
+ ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile1));
+ new_browser = FindOneOtherBrowserForProfile(profile1, NULL);
+ ASSERT_TRUE(new_browser);
+ ASSERT_EQ(1, new_browser->tab_count());
+ EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
+ new_browser->GetWebContentsAt(0)->GetURL());
+
+ ASSERT_EQ(1u, BrowserList::GetBrowserCount(profile2));
+ new_browser = FindOneOtherBrowserForProfile(profile2, NULL);
+ ASSERT_TRUE(new_browser);
+ ASSERT_EQ(1, new_browser->tab_count());
+ EXPECT_EQ(GURL(chrome::kChromeUINewTabURL),
+ new_browser->GetWebContentsAt(0)->GetURL());
+}