Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: chrome/browser/sessions/better_session_restore_browsertest.cc

Issue 25414005: Clear session-only data when all browser windows are closed. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/content_settings/cookie_settings.h" 14 #include "chrome/browser/content_settings/cookie_settings.h"
14 #include "chrome/browser/infobars/confirm_infobar_delegate.h" 15 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
15 #include "chrome/browser/infobars/infobar_service.h" 16 #include "chrome/browser/infobars/infobar_service.h"
17 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/prefs/session_startup_pref.h" 18 #include "chrome/browser/prefs/session_startup_pref.h"
17 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_impl.h" 20 #include "chrome/browser/profiles/profile_impl.h"
19 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/sessions/session_backend.h" 22 #include "chrome/browser/sessions/session_backend.h"
21 #include "chrome/browser/sessions/session_service_factory.h" 23 #include "chrome/browser/sessions/session_service_factory.h"
22 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_commands.h" 25 #include "chrome/browser/ui/browser_commands.h"
24 #include "chrome/browser/ui/browser_iterator.h" 26 #include "chrome/browser/ui/browser_iterator.h"
25 #include "chrome/browser/ui/browser_window.h" 27 #include "chrome/browser/ui/browser_window.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 std::string(bytes_reader->bytes(), bytes_reader->length()); 92 std::string(bytes_reader->bytes(), bytes_reader->length());
91 } 93 }
92 } 94 }
93 } 95 }
94 return new net::URLRequestTestJob( 96 return new net::URLRequestTestJob(
95 request, network_delegate, net::URLRequestTestJob::test_headers(), 97 request, network_delegate, net::URLRequestTestJob::test_headers(),
96 "<html><head><title>PASS</title></head><body>Data posted</body></html>", 98 "<html><head><title>PASS</title></head><body>Data posted</body></html>",
97 true); 99 true);
98 } 100 }
99 101
102 class WebContentsCloseObserver : public content::WebContentsObserver {
103 public:
104 explicit WebContentsCloseObserver(content::WebContents* web_contents)
105 : content::WebContentsObserver(web_contents) {}
106
107 virtual void WebContentsDestroyed(
108 content::WebContents* web_contents) OVERRIDE {
109 content::BrowserThread::PostTask(
110 content::BrowserThread::UI, FROM_HERE, run_loop_.QuitClosure());
111 }
112
113 void Wait() {
114 run_loop_.Run();
115 }
116
117 private:
118 base::RunLoop run_loop_;
119
120 DISALLOW_COPY_AND_ASSIGN(WebContentsCloseObserver);
121 };
122
100 } // namespace 123 } // namespace
101 124
102 class BetterSessionRestoreTest : public InProcessBrowserTest { 125 class BetterSessionRestoreTest : public InProcessBrowserTest {
103 public: 126 public:
104 BetterSessionRestoreTest() 127 BetterSessionRestoreTest()
105 : fake_server_address_("http://www.test.com/"), 128 : fake_server_address_("http://www.test.com/"),
106 test_path_("session_restore/"), 129 test_path_("session_restore/"),
107 title_pass_(ASCIIToUTF16("PASS")), 130 title_pass_(ASCIIToUTF16("PASS")),
108 title_storing_(ASCIIToUTF16("STORING")), 131 title_storing_(ASCIIToUTF16("STORING")),
109 title_error_write_failed_(ASCIIToUTF16("ERROR_WRITE_FAILED")), 132 title_error_write_failed_(ASCIIToUTF16("ERROR_WRITE_FAILED")),
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 200
178 void CheckReloadedPageRestored() { 201 void CheckReloadedPageRestored() {
179 CheckTitle(browser(), title_pass_); 202 CheckTitle(browser(), title_pass_);
180 } 203 }
181 204
182 void CheckReloadedPageRestored(Browser* browser) { 205 void CheckReloadedPageRestored(Browser* browser) {
183 CheckTitle(browser, title_pass_); 206 CheckTitle(browser, title_pass_);
184 } 207 }
185 208
186 void CheckReloadedPageNotRestored() { 209 void CheckReloadedPageNotRestored() {
187 CheckTitle(browser(), title_storing_); 210 CheckReloadedPageNotRestored(browser());
211 }
212
213 void CheckReloadedPageNotRestored(Browser* browser) {
214 CheckTitle(browser, title_storing_);
188 } 215 }
189 216
190 void CheckTitle(Browser* browser, const string16& expected_title) { 217 void CheckTitle(Browser* browser, const string16& expected_title) {
191 content::WebContents* web_contents = 218 content::WebContents* web_contents =
192 browser->tab_strip_model()->GetWebContentsAt(0); 219 browser->tab_strip_model()->GetWebContentsAt(0);
193 content::TitleWatcher title_watcher(web_contents, expected_title); 220 content::TitleWatcher title_watcher(web_contents, expected_title);
194 title_watcher.AlsoWaitForTitle(title_pass_); 221 title_watcher.AlsoWaitForTitle(title_pass_);
195 title_watcher.AlsoWaitForTitle(title_storing_); 222 title_watcher.AlsoWaitForTitle(title_storing_);
196 title_watcher.AlsoWaitForTitle(title_error_write_failed_); 223 title_watcher.AlsoWaitForTitle(title_error_write_failed_);
197 title_watcher.AlsoWaitForTitle(title_error_empty_); 224 title_watcher.AlsoWaitForTitle(title_error_empty_);
(...skipping 25 matching lines...) Expand all
223 std::string::npos); 250 std::string::npos);
224 if (password_present) { 251 if (password_present) {
225 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") != 252 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") !=
226 std::string::npos); 253 std::string::npos);
227 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") != 254 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") !=
228 std::string::npos); 255 std::string::npos);
229 } 256 }
230 } 257 }
231 258
232 void CheckFormRestored(bool text_present, bool password_present) { 259 void CheckFormRestored(bool text_present, bool password_present) {
233 CheckReloadedPageRestored(); 260 CheckFormRestored(browser(), text_present, password_present);
261 }
262
263 void CheckFormRestored(
264 Browser* browser, bool text_present, bool password_present) {
265 CheckReloadedPageRestored(browser);
234 if (text_present) { 266 if (text_present) {
235 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-text") != 267 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-text") !=
236 std::string::npos); 268 std::string::npos);
237 EXPECT_TRUE(g_last_upload_bytes.Get().find("text-entered") != 269 EXPECT_TRUE(g_last_upload_bytes.Get().find("text-entered") !=
238 std::string::npos); 270 std::string::npos);
239 } else { 271 } else {
240 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-text") == 272 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-text") ==
241 std::string::npos); 273 std::string::npos);
242 EXPECT_TRUE(g_last_upload_bytes.Get().find("text-entered") == 274 EXPECT_TRUE(g_last_upload_bytes.Get().find("text-entered") ==
243 std::string::npos); 275 std::string::npos);
244 } 276 }
245 if (password_present) { 277 if (password_present) {
246 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") != 278 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") !=
247 std::string::npos); 279 std::string::npos);
248 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") != 280 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") !=
249 std::string::npos); 281 std::string::npos);
250 } else { 282 } else {
251 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") == 283 EXPECT_TRUE(g_last_upload_bytes.Get().find("posted-password") ==
252 std::string::npos); 284 std::string::npos);
253 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") == 285 EXPECT_TRUE(g_last_upload_bytes.Get().find("password-entered") ==
254 std::string::npos); 286 std::string::npos);
255 } 287 }
256 } 288 }
257 289
290 virtual Browser* RelaunchBrowser(Browser* browser) {
291 Profile* profile = browser->profile();
292 SessionServiceFactory::GetForProfile(profile)->
293 force_browser_not_alive_with_no_windows_ = true;
294 content::WindowedNotificationObserver browser_close_observer(
295 chrome::NOTIFICATION_BROWSER_CLOSED,
296 content::NotificationService::AllSources());
297 chrome::StartKeepAlive();
298 browser->window()->Close();
299 browser_close_observer.Wait();
300
301 Browser* new_browser = CreateBrowser(profile);
302 chrome::EndKeepAlive();
303 content::WebContents* web_contents =
304 new_browser->tab_strip_model()->GetActiveWebContents();
305 EXPECT_EQ(std::string(content::kAboutBlankURL),
306 web_contents->GetURL().spec());
307 return new_browser;
308 }
309
258 std::string fake_server_address() { 310 std::string fake_server_address() {
259 return fake_server_address_; 311 return fake_server_address_;
260 } 312 }
261 313
262 std::string test_path() { 314 std::string test_path() {
263 return test_path_; 315 return test_path_;
264 } 316 }
265 317
266 private: 318 private:
267 const std::string fake_server_address_; 319 const std::string fake_server_address_;
268 const std::string test_path_; 320 const std::string test_path_;
269 const string16 title_pass_; 321 const string16 title_pass_;
270 const string16 title_storing_; 322 const string16 title_storing_;
271 const string16 title_error_write_failed_; 323 const string16 title_error_write_failed_;
272 const string16 title_error_empty_; 324 const string16 title_error_empty_;
273 325
274 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest); 326 DISALLOW_COPY_AND_ASSIGN(BetterSessionRestoreTest);
275 }; 327 };
276 328
277 class ContinueWhereILeftOffTest : public BetterSessionRestoreTest { 329 class ContinueWhereILeftOffTest : public BetterSessionRestoreTest {
278 public: 330 public:
279 ContinueWhereILeftOffTest() { } 331 ContinueWhereILeftOffTest() { }
280 332
281 virtual void SetUpOnMainThread() OVERRIDE { 333 virtual void SetUpOnMainThread() OVERRIDE {
282 BetterSessionRestoreTest::SetUpOnMainThread(); 334 BetterSessionRestoreTest::SetUpOnMainThread();
283 SessionStartupPref::SetStartupPref( 335 SessionStartupPref::SetStartupPref(
284 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); 336 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST));
285 } 337 }
286 338
339 protected:
340 virtual Browser* RelaunchBrowser(Browser* browser) OVERRIDE {
341 const GURL url =
342 browser->tab_strip_model()->GetActiveWebContents()->GetURL();
343 content::WindowedNotificationObserver session_restore_observer(
344 chrome::NOTIFICATION_SESSION_RESTORE_DONE,
345 content::NotificationService::AllSources());
346 Browser* new_browser = BetterSessionRestoreTest::RelaunchBrowser(browser);
347 session_restore_observer.Wait();
348 WebContentsCloseObserver tab_close_observer(
349 new_browser->tab_strip_model()->GetActiveWebContents());
350 new_browser->tab_strip_model()->GetActiveWebContents()->Close();
351 tab_close_observer.Wait();
352 return new_browser;
353 }
354
287 DISALLOW_COPY_AND_ASSIGN(ContinueWhereILeftOffTest); 355 DISALLOW_COPY_AND_ASSIGN(ContinueWhereILeftOffTest);
288 }; 356 };
289 357
290 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_SessionCookies) { 358 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_SessionCookies) {
291 // Set the startup preference to "continue where I left off" and visit a page 359 // Set the startup preference to "continue where I left off" and visit a page
292 // which stores a session cookie. 360 // which stores a session cookie.
293 StoreDataWithPage("session_cookies.html"); 361 StoreDataWithPage("session_cookies.html");
294 } 362 }
295 363
296 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionCookies) { 364 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionCookies) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_PostWithPassword) { 421 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PRE_PostWithPassword) {
354 PostFormWithPage("post_with_password.html", true); 422 PostFormWithPage("post_with_password.html", true);
355 } 423 }
356 424
357 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostWithPassword) { 425 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostWithPassword) {
358 CheckReloadedPageRestored(); 426 CheckReloadedPageRestored();
359 // The form data contained passwords, so it's removed completely. 427 // The form data contained passwords, so it's removed completely.
360 CheckFormRestored(false, false); 428 CheckFormRestored(false, false);
361 } 429 }
362 430
431 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, SessionCookiesBrowserClose) {
432 // Set the startup preference to "continue where I left off" and visit a page
433 // which stores a session cookie.
434 StoreDataWithPage("session_cookies.html");
435 Browser* new_browser = RelaunchBrowser(browser());
436 // The browsing session will be continued; just wait for the page to reload
437 // and check the stored data.
438 CheckReloadedPageRestored(new_browser);
439 }
440
441 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
442 CookiesClearedOnBrowserClose) {
443 StoreDataWithPage("cookies.html");
444 // Normally cookies are restored.
445 Browser* new_browser = RelaunchBrowser(browser());
446 CheckReloadedPageRestored(new_browser);
447 // ... but not if the content setting is set to clear on exit.
448 CookieSettings::Factory::GetForProfile(new_browser->profile())->
449 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
450 new_browser = RelaunchBrowser(new_browser);
451 CheckReloadedPageNotRestored(new_browser);
452 }
453
454 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest, PostBrowserClose) {
455 PostFormWithPage("post.html", false);
456 Browser* new_browser = RelaunchBrowser(browser());
457 CheckFormRestored(new_browser, true, false);
458 }
459
460 IN_PROC_BROWSER_TEST_F(ContinueWhereILeftOffTest,
461 PostWithPasswordBrowserClose) {
462 PostFormWithPage("post_with_password.html", true);
463 Browser* new_browser = RelaunchBrowser(browser());
464 CheckReloadedPageRestored(new_browser);
465 // The form data contained passwords, so it's removed completely.
466 CheckFormRestored(new_browser, false, false);
467 }
468
363 class RestartTest : public BetterSessionRestoreTest { 469 class RestartTest : public BetterSessionRestoreTest {
364 public: 470 public:
365 RestartTest() { } 471 RestartTest() { }
366 virtual ~RestartTest() { } 472 virtual ~RestartTest() { }
367 protected: 473 protected:
368 void Restart() { 474 void Restart() {
369 // Simluate restarting the browser, but let the test exit peacefully. 475 // Simluate restarting the browser, but let the test exit peacefully.
370 for (chrome::BrowserIterator it; !it.done(); it.Next()) 476 for (chrome::BrowserIterator it; !it.done(); it.Next())
371 content::BrowserContext::SaveSessionState(it->profile()); 477 content::BrowserContext::SaveSessionState(it->profile());
372 PrefService* pref_service = g_browser_process->local_state(); 478 PrefService* pref_service = g_browser_process->local_state();
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); 629 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
524 } 630 }
525 631
526 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) { 632 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnExit) {
527 content::WebContents* web_contents = 633 content::WebContents* web_contents =
528 browser()->tab_strip_model()->GetActiveWebContents(); 634 browser()->tab_strip_model()->GetActiveWebContents();
529 EXPECT_EQ(std::string(content::kAboutBlankURL), 635 EXPECT_EQ(std::string(content::kAboutBlankURL),
530 web_contents->GetURL().spec()); 636 web_contents->GetURL().spec());
531 StoreDataWithPage("local_storage.html"); 637 StoreDataWithPage("local_storage.html");
532 } 638 }
639
640 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, SessionCookiesBrowserClose) {
641 StoreDataWithPage("session_cookies.html");
642 // When we navigate to the page again, it doens't see the data previously
643 // stored.
644 Browser* new_browser = RelaunchBrowser(browser());
645 StoreDataWithPage(new_browser, "session_cookies.html");
646 }
647
648 IN_PROC_BROWSER_TEST_F(NoSessionRestoreTest, CookiesClearedOnBrowserClose) {
649 StoreDataWithPage("cookies.html");
650
651 // Normally cookies are restored.
652 Browser* new_browser = RelaunchBrowser(browser());
653 NavigateAndCheckStoredData(new_browser, "cookies.html");
654
655 // ... but not if the content setting is set to clear on exit.
656 CookieSettings::Factory::GetForProfile(new_browser->profile())->
657 SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY);
658 new_browser = RelaunchBrowser(new_browser);
659 StoreDataWithPage(new_browser, "local_storage.html");
660 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/sessions/session_data_deleter.h » ('j') | chrome/browser/sessions/session_service.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698