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
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_reader.h"
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/sequenced_task_runner.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/values.h"
19 #include "base/version.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/net/url_request_mock_util.h"
23 #include "chrome/browser/profiles/chrome_version_service.h"
24 #include "chrome/browser/profiles/profile_impl.h"
25 #include "chrome/browser/profiles/profile_manager.h"
26 #include "chrome/browser/profiles/startup_task_runner_service.h"
27 #include "chrome/browser/profiles/startup_task_runner_service_factory.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
30 #include "chrome/common/chrome_constants.h"
31 #include "chrome/common/chrome_version_info.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/test/base/in_process_browser_test.h"
34 #include "chrome/test/base/ui_test_utils.h"
35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/test/test_utils.h"
37 #include "net/base/net_errors.h"
38 #include "net/test/url_request/url_request_failed_job.h"
39 #include "net/url_request/url_fetcher.h"
40 #include "net/url_request/url_fetcher_delegate.h"
41 #include "net/url_request/url_request_context_getter.h"
42 #include "net/url_request/url_request_status.h"
43 #include "testing/gmock/include/gmock/gmock.h"
44 #include "testing/gtest/include/gtest/gtest.h"
47 #if defined(OS_CHROMEOS)
48 #include "chrome/browser/chromeos/profiles/profile_helper.h"
49 #include "chromeos/chromeos_switches.h"
54 // Simple URLFetcherDelegate with an expected final status and the ability to
55 // wait until a request completes. It's not considered a failure for the request
57 class TestURLFetcherDelegate
: public net::URLFetcherDelegate
{
59 // Creating the TestURLFetcherDelegate automatically creates and starts a
61 TestURLFetcherDelegate(
62 scoped_refptr
<net::URLRequestContextGetter
> context_getter
,
64 net::URLRequestStatus expected_request_status
)
65 : expected_request_status_(expected_request_status
),
67 fetcher_(net::URLFetcher::Create(url
, net::URLFetcher::GET
, this)) {
68 fetcher_
->SetRequestContext(context_getter
.get());
72 ~TestURLFetcherDelegate() override
{}
74 void OnURLFetchComplete(const net::URLFetcher
* source
) override
{
75 EXPECT_EQ(expected_request_status_
.status(), source
->GetStatus().status());
76 EXPECT_EQ(expected_request_status_
.error(), source
->GetStatus().error());
81 void WaitForCompletion() {
85 bool is_complete() const { return is_complete_
; }
88 const net::URLRequestStatus expected_request_status_
;
89 base::RunLoop run_loop_
;
92 scoped_ptr
<net::URLFetcher
> fetcher_
;
94 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherDelegate
);
97 class MockProfileDelegate
: public Profile::Delegate
{
99 MOCK_METHOD1(OnPrefsLoaded
, void(Profile
*));
100 MOCK_METHOD3(OnProfileCreated
, void(Profile
*, bool, bool));
103 // Creates a prefs file in the given directory.
104 void CreatePrefsFileInDirectory(const base::FilePath
& directory_path
) {
105 base::FilePath
pref_path(directory_path
.Append(chrome::kPreferencesFilename
));
106 std::string
data("{}");
107 ASSERT_TRUE(base::WriteFile(pref_path
, data
.c_str(), data
.size()));
110 void CheckChromeVersion(Profile
*profile
, bool is_new
) {
111 std::string created_by_version
;
113 chrome::VersionInfo version_info
;
114 created_by_version
= version_info
.Version();
116 created_by_version
= "1.0.0.0";
118 std::string pref_version
=
119 ChromeVersionService::GetVersion(profile
->GetPrefs());
120 // Assert that created_by_version pref gets set to current version.
121 EXPECT_EQ(created_by_version
, pref_version
);
125 base::WaitableEvent
* is_blocked
,
126 base::WaitableEvent
* unblock
) {
127 is_blocked
->Signal();
131 void FlushTaskRunner(base::SequencedTaskRunner
* runner
) {
133 base::WaitableEvent
unblock(false, false);
135 runner
->PostTask(FROM_HERE
,
136 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&unblock
)));
142 // Give threads a chance to do their stuff before shutting down (i.e.
143 // deleting scoped temp dir etc).
144 // Should not be necessary anymore once Profile deletion is fixed
145 // (see crbug.com/88586).
146 content::RunAllPendingInMessageLoop();
147 content::RunAllPendingInMessageLoop(content::BrowserThread::DB
);
148 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
153 class ProfileBrowserTest
: public InProcessBrowserTest
{
155 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
156 #if defined(OS_CHROMEOS)
157 command_line
->AppendSwitch(
158 chromeos::switches::kIgnoreUserProfileMappingForTests
);
162 // content::BrowserTestBase implementation:
164 void SetUpOnMainThread() override
{
165 content::BrowserThread::PostTask(
166 content::BrowserThread::IO
, FROM_HERE
,
167 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled
, true));
170 void TearDownOnMainThread() override
{
171 content::BrowserThread::PostTask(
172 content::BrowserThread::IO
, FROM_HERE
,
173 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled
, false));
176 scoped_ptr
<Profile
> CreateProfile(
177 const base::FilePath
& path
,
178 Profile::Delegate
* delegate
,
179 Profile::CreateMode create_mode
) {
180 scoped_ptr
<Profile
> profile(Profile::CreateProfile(
181 path
, delegate
, create_mode
));
182 EXPECT_TRUE(profile
.get());
184 // Store the Profile's IO task runner so we can wind it down.
185 profile_io_task_runner_
= profile
->GetIOTaskRunner();
187 return profile
.Pass();
190 void FlushIoTaskRunnerAndSpinThreads() {
191 FlushTaskRunner(profile_io_task_runner_
.get());
195 // Starts a test where a URLFetcher is active during profile shutdown. The
196 // test completes during teardown of the test fixture. The request should be
197 // canceled by |context_getter| during profile shutdown, before the
198 // URLRequestContext is destroyed. If that doesn't happen, the Context's
199 // will still have oustanding requests during its destruction, and will
200 // trigger a CHECK failure.
201 void StartActiveFetcherDuringProfileShutdownTest(
202 scoped_refptr
<net::URLRequestContextGetter
> context_getter
) {
203 // This method should only be called once per test.
204 DCHECK(!url_fetcher_delegate_
);
206 // Start a hanging request. This request may or may not completed before
207 // the end of the request.
208 url_fetcher_delegate_
.reset(new TestURLFetcherDelegate(
209 context_getter
.get(),
210 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING
),
211 net::URLRequestStatus(net::URLRequestStatus::CANCELED
,
212 net::ERR_CONTEXT_SHUT_DOWN
)));
214 // Start a second mock request that just fails, and wait for it to complete.
215 // This ensures the first request has reached the network stack.
216 TestURLFetcherDelegate
url_fetcher_delegate2(
217 context_getter
.get(),
218 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_FAILED
),
219 net::URLRequestStatus(net::URLRequestStatus::FAILED
,
221 url_fetcher_delegate2
.WaitForCompletion();
223 // The first request should still be hung.
224 EXPECT_FALSE(url_fetcher_delegate_
->is_complete());
227 // Runs a test where an incognito profile's URLFetcher is active during
228 // teardown of the profile, and makes sure the request fails as expected.
229 // Also tries issuing a request after the incognito profile has been
231 static void RunURLFetcherActiveDuringIncognitoTeardownTest(
232 Browser
* incognito_browser
,
233 scoped_refptr
<net::URLRequestContextGetter
> context_getter
) {
234 // Start a hanging request.
235 TestURLFetcherDelegate
url_fetcher_delegate1(
236 context_getter
.get(),
237 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING
),
238 net::URLRequestStatus(net::URLRequestStatus::CANCELED
,
239 net::ERR_CONTEXT_SHUT_DOWN
));
241 // Start a second mock request that just fails, and wait for it to complete.
242 // This ensures the first request has reached the network stack.
243 TestURLFetcherDelegate
url_fetcher_delegate2(
244 context_getter
.get(),
245 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_FAILED
),
246 net::URLRequestStatus(net::URLRequestStatus::FAILED
,
248 url_fetcher_delegate2
.WaitForCompletion();
250 // The first request should still be hung.
251 EXPECT_FALSE(url_fetcher_delegate1
.is_complete());
253 // Close all incognito tabs, starting profile shutdown.
254 incognito_browser
->tab_strip_model()->CloseAllTabs();
256 // The request should have been canceled when the Profile shut down.
257 url_fetcher_delegate1
.WaitForCompletion();
259 // Requests issued after Profile shutdown should fail in a similar manner.
260 TestURLFetcherDelegate
url_fetcher_delegate3(
261 context_getter
.get(),
262 net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING
),
263 net::URLRequestStatus(net::URLRequestStatus::CANCELED
,
264 net::ERR_CONTEXT_SHUT_DOWN
));
265 url_fetcher_delegate3
.WaitForCompletion();
268 scoped_refptr
<base::SequencedTaskRunner
> profile_io_task_runner_
;
270 // URLFetcherDelegate that outlives the Profile, to test shutdown.
271 scoped_ptr
<TestURLFetcherDelegate
> url_fetcher_delegate_
;
274 // Test OnProfileCreate is called with is_new_profile set to true when
275 // creating a new profile synchronously.
276 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, CreateNewProfileSynchronous
) {
277 base::ScopedTempDir temp_dir
;
278 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
280 MockProfileDelegate delegate
;
281 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
284 scoped_ptr
<Profile
> profile(CreateProfile(
285 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
286 CheckChromeVersion(profile
.get(), true);
289 FlushIoTaskRunnerAndSpinThreads();
292 // Test OnProfileCreate is called with is_new_profile set to false when
293 // creating a profile synchronously with an existing prefs file.
294 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, CreateOldProfileSynchronous
) {
295 base::ScopedTempDir temp_dir
;
296 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
297 CreatePrefsFileInDirectory(temp_dir
.path());
299 MockProfileDelegate delegate
;
300 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, false));
303 scoped_ptr
<Profile
> profile(CreateProfile(
304 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
305 CheckChromeVersion(profile
.get(), false);
308 FlushIoTaskRunnerAndSpinThreads();
311 // Flaky: http://crbug.com/393177
312 // Test OnProfileCreate is called with is_new_profile set to true when
313 // creating a new profile asynchronously.
314 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
315 DISABLED_CreateNewProfileAsynchronous
) {
316 base::ScopedTempDir temp_dir
;
317 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
319 MockProfileDelegate delegate
;
320 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
323 content::WindowedNotificationObserver
observer(
324 chrome::NOTIFICATION_PROFILE_CREATED
,
325 content::NotificationService::AllSources());
327 scoped_ptr
<Profile
> profile(CreateProfile(
328 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_ASYNCHRONOUS
));
330 // Wait for the profile to be created.
332 CheckChromeVersion(profile
.get(), true);
335 FlushIoTaskRunnerAndSpinThreads();
339 // Flaky: http://crbug.com/393177
340 // Test OnProfileCreate is called with is_new_profile set to false when
341 // creating a profile asynchronously with an existing prefs file.
342 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
343 DISABLED_CreateOldProfileAsynchronous
) {
344 base::ScopedTempDir temp_dir
;
345 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
346 CreatePrefsFileInDirectory(temp_dir
.path());
348 MockProfileDelegate delegate
;
349 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, false));
352 content::WindowedNotificationObserver
observer(
353 chrome::NOTIFICATION_PROFILE_CREATED
,
354 content::NotificationService::AllSources());
356 scoped_ptr
<Profile
> profile(CreateProfile(
357 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_ASYNCHRONOUS
));
359 // Wait for the profile to be created.
361 CheckChromeVersion(profile
.get(), false);
364 FlushIoTaskRunnerAndSpinThreads();
367 // Flaky: http://crbug.com/393177
368 // Test that a README file is created for profiles that didn't have it.
369 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, DISABLED_ProfileReadmeCreated
) {
370 base::ScopedTempDir temp_dir
;
371 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
373 MockProfileDelegate delegate
;
374 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
376 // No delay before README creation.
377 ProfileImpl::create_readme_delay_ms
= 0;
380 content::WindowedNotificationObserver
observer(
381 chrome::NOTIFICATION_PROFILE_CREATED
,
382 content::NotificationService::AllSources());
384 scoped_ptr
<Profile
> profile(CreateProfile(
385 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_ASYNCHRONOUS
));
387 // Wait for the profile to be created.
390 // Wait for file thread to create the README.
391 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
393 // Verify that README exists.
394 EXPECT_TRUE(base::PathExists(
395 temp_dir
.path().Append(chrome::kReadmeFilename
)));
398 FlushIoTaskRunnerAndSpinThreads();
401 // Test that Profile can be deleted before README file is created.
402 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, ProfileDeletedBeforeReadmeCreated
) {
403 base::ScopedTempDir temp_dir
;
404 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
406 MockProfileDelegate delegate
;
407 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
409 // No delay before README creation.
410 ProfileImpl::create_readme_delay_ms
= 0;
412 base::WaitableEvent
is_blocked(false, false);
413 base::WaitableEvent
* unblock
= new base::WaitableEvent(false, false);
415 // Block file thread.
416 content::BrowserThread::PostTask(
417 content::BrowserThread::FILE, FROM_HERE
,
418 base::Bind(&BlockThread
, &is_blocked
, base::Owned(unblock
)));
419 // Wait for file thread to actually be blocked.
422 scoped_ptr
<Profile
> profile(CreateProfile(
423 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
425 // Delete the Profile instance before we give the file thread a chance to
426 // create the README.
429 // Now unblock the file thread again and run pending tasks (this includes the
430 // task for README creation).
433 FlushIoTaskRunnerAndSpinThreads();
436 // Test that repeated setting of exit type is handled correctly.
437 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, ExitType
) {
438 base::ScopedTempDir temp_dir
;
439 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
441 MockProfileDelegate delegate
;
442 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
444 scoped_ptr
<Profile
> profile(CreateProfile(
445 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
447 PrefService
* prefs
= profile
->GetPrefs();
448 // The initial state is crashed; store for later reference.
449 std::string
crash_value(prefs
->GetString(prefs::kSessionExitType
));
451 // The first call to a type other than crashed should change the value.
452 profile
->SetExitType(Profile::EXIT_SESSION_ENDED
);
453 std::string
first_call_value(prefs
->GetString(prefs::kSessionExitType
));
454 EXPECT_NE(crash_value
, first_call_value
);
456 // Subsequent calls to a non-crash value should be ignored.
457 profile
->SetExitType(Profile::EXIT_NORMAL
);
458 std::string
second_call_value(prefs
->GetString(prefs::kSessionExitType
));
459 EXPECT_EQ(first_call_value
, second_call_value
);
461 // Setting back to a crashed value should work.
462 profile
->SetExitType(Profile::EXIT_CRASHED
);
463 std::string
final_value(prefs
->GetString(prefs::kSessionExitType
));
464 EXPECT_EQ(crash_value
, final_value
);
467 FlushIoTaskRunnerAndSpinThreads();
470 // The EndSession IO synchronization is only critical on Windows, but also
471 // happens under the USE_X11 define. See BrowserProcessImpl::EndSession.
472 #if defined(USE_X11) || defined(OS_WIN) || defined(USE_OZONE)
476 std::string
GetExitTypePreferenceFromDisk(Profile
* profile
) {
477 base::FilePath prefs_path
=
478 profile
->GetPath().Append(chrome::kPreferencesFilename
);
480 if (!base::ReadFileToString(prefs_path
, &prefs
))
481 return std::string();
483 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(prefs
);
485 return std::string();
487 base::DictionaryValue
* dict
= NULL
;
488 if (!value
->GetAsDictionary(&dict
) || !dict
)
489 return std::string();
491 std::string exit_type
;
492 if (!dict
->GetString("profile.exit_type", &exit_type
))
493 return std::string();
500 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
501 WritesProfilesSynchronouslyOnEndSession
) {
502 base::ScopedTempDir temp_dir
;
503 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
505 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
506 ASSERT_TRUE(profile_manager
);
507 std::vector
<Profile
*> loaded_profiles
= profile_manager
->GetLoadedProfiles();
509 ASSERT_NE(loaded_profiles
.size(), 0UL);
510 Profile
* profile
= loaded_profiles
[0];
512 #if defined(OS_CHROMEOS)
513 for (const auto& loaded_profile
: loaded_profiles
) {
514 if (!chromeos::ProfileHelper::IsSigninProfile(loaded_profile
)) {
515 profile
= loaded_profile
;
521 // This retry loop reduces flakiness due to the fact that this ultimately
522 // tests whether or not a code path hits a timed wait.
523 bool succeeded
= false;
524 for (size_t retries
= 0; !succeeded
&& retries
< 3; ++retries
) {
525 // Flush the profile data to disk for all loaded profiles.
526 profile
->SetExitType(Profile::EXIT_CRASHED
);
527 profile
->GetPrefs()->CommitPendingWrite();
528 FlushTaskRunner(profile
->GetIOTaskRunner().get());
530 // Make sure that the prefs file was written with the expected key/value.
531 ASSERT_EQ(GetExitTypePreferenceFromDisk(profile
), "Crashed");
533 // The blocking wait in EndSession has a timeout.
534 base::Time start
= base::Time::Now();
536 // This must not return until the profile data has been written to disk.
537 // If this test flakes, then logoff on Windows has broken again.
538 g_browser_process
->EndSession();
540 base::Time end
= base::Time::Now();
542 // The EndSession timeout is 10 seconds. If we take more than half that,
543 // go around again, as we may have timed out on the wait.
544 // This helps against flakes, and also ensures that if the IO thread starts
545 // blocking systemically for that length of time (e.g. deadlocking or such),
546 // we'll get a consistent test failure.
547 if (end
- start
> base::TimeDelta::FromSeconds(5))
550 // Make sure that the prefs file was written with the expected key/value.
551 ASSERT_EQ(GetExitTypePreferenceFromDisk(profile
), "SessionEnded");
557 ASSERT_TRUE(succeeded
) << "profile->EndSession() timed out too often.";
560 #endif // defined(USE_X11) || defined(OS_WIN) || defined(USE_OZONE)
562 // The following tests make sure that it's safe to shut down while one of the
563 // Profile's URLRequestContextGetters is in use by a URLFetcher.
565 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
566 URLFetcherUsingMainContextDuringShutdown
) {
567 StartActiveFetcherDuringProfileShutdownTest(
568 browser()->profile()->GetRequestContext());
571 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
572 URLFetcherUsingMediaContextDuringShutdown
) {
573 StartActiveFetcherDuringProfileShutdownTest(
574 browser()->profile()->GetMediaRequestContext());
577 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
578 URLFetcherUsingExtensionContextDuringShutdown
) {
579 StartActiveFetcherDuringProfileShutdownTest(
580 browser()->profile()->GetRequestContextForExtensions());
583 // The following tests make sure that it's safe to destroy an incognito profile
584 // while one of the its URLRequestContextGetters is in use by a URLFetcher.
586 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
587 URLFetcherUsingMainContextDuringIncognitoTeardown
) {
588 Browser
* incognito_browser
=
589 ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
590 GURL("about:blank"));
591 RunURLFetcherActiveDuringIncognitoTeardownTest(
592 incognito_browser
, incognito_browser
->profile()->GetRequestContext());
595 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
596 URLFetcherUsingExtensionContextDuringIncognitoTeardown
) {
597 Browser
* incognito_browser
=
598 ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
599 GURL("about:blank"));
600 RunURLFetcherActiveDuringIncognitoTeardownTest(
602 incognito_browser
->profile()->GetRequestContextForExtensions());