1 // Copyright 2013 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/test/base/testing_io_thread_state.h"
7 #include "base/location.h"
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/time/tick_clock.h"
12 #include "chrome/browser/io_thread.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "content/public/browser/browser_thread.h"
16 #if defined(OS_CHROMEOS)
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/network/network_handler.h"
21 using content::BrowserThread
;
25 base::Closure
ThreadSafeQuit(base::RunLoop
* run_loop
) {
26 if (BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
27 return run_loop
->QuitClosure();
30 using base::IgnoreResult
;
31 return Bind(IgnoreResult(&base::SingleThreadTaskRunner::PostTask
),
32 base::ThreadTaskRunnerHandle::Get(), FROM_HERE
,
33 run_loop
->QuitClosure());
41 TestingIOThreadState::TestingIOThreadState() {
42 #if defined(OS_CHROMEOS)
43 // Needed by IOThread constructor.
44 chromeos::DBusThreadManager::Initialize();
45 chromeos::NetworkHandler::Initialize();
48 io_thread_state_
.reset(
49 new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
50 TestingBrowserProcess::GetGlobal()->policy_service(),
53 // Safe because there are no virtuals.
54 base::RunLoop run_loop
;
55 CHECK(BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
56 base::Bind(&TestingIOThreadState::Initialize
,
57 base::Unretained(this),
58 ThreadSafeQuit(&run_loop
))));
61 TestingBrowserProcess::GetGlobal()->SetIOThread(io_thread_state_
.get());
64 TestingIOThreadState::~TestingIOThreadState() {
65 // Remove all the local IOThread state.
66 base::RunLoop run_loop
;
67 CHECK(BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
68 base::Bind(&TestingIOThreadState::Shutdown
,
69 base::Unretained(this),
70 ThreadSafeQuit(&run_loop
))));
72 TestingBrowserProcess::GetGlobal()->SetIOThread(NULL
);
74 io_thread_state_
.reset();
76 #if defined(OS_CHROMEOS)
77 chromeos::NetworkHandler::Shutdown();
78 chromeos::DBusThreadManager::Shutdown();
82 void TestingIOThreadState::Initialize(const base::Closure
& done
) {
83 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
85 io_thread_state_
->SetGlobalsForTesting(new IOThread::Globals());
90 void TestingIOThreadState::Shutdown(const base::Closure
& done
) {
91 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
93 delete io_thread_state_
->globals();
94 io_thread_state_
->SetGlobalsForTesting(NULL
);