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/message_loop/message_loop_proxy.h"
8 #include "base/run_loop.h"
9 #include "base/time/tick_clock.h"
10 #include "chrome/browser/io_thread.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "content/public/browser/browser_thread.h"
14 #if defined(OS_CHROMEOS)
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/network/network_handler.h"
19 using content::BrowserThread
;
23 base::Closure
ThreadSafeQuit(base::RunLoop
* run_loop
) {
24 if (BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
25 return run_loop
->QuitClosure();
28 using base::IgnoreResult
;
29 return Bind(IgnoreResult(&base::MessageLoopProxy::PostTask
),
30 base::MessageLoopProxy::current(),
32 run_loop
->QuitClosure());
40 TestingIOThreadState::TestingIOThreadState() {
41 #if defined(OS_CHROMEOS)
42 // Needed by IOThread constructor.
43 chromeos::DBusThreadManager::Initialize();
44 chromeos::NetworkHandler::Initialize();
47 io_thread_state_
.reset(
48 new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
49 TestingBrowserProcess::GetGlobal()->policy_service(),
52 // Safe because there are no virtuals.
53 base::RunLoop run_loop
;
54 CHECK(BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
55 base::Bind(&TestingIOThreadState::Initialize
,
56 base::Unretained(this),
57 ThreadSafeQuit(&run_loop
))));
60 TestingBrowserProcess::GetGlobal()->SetIOThread(io_thread_state_
.get());
63 TestingIOThreadState::~TestingIOThreadState() {
64 // Remove all the local IOThread state.
65 base::RunLoop run_loop
;
66 CHECK(BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
67 base::Bind(&TestingIOThreadState::Shutdown
,
68 base::Unretained(this),
69 ThreadSafeQuit(&run_loop
))));
71 TestingBrowserProcess::GetGlobal()->SetIOThread(NULL
);
73 io_thread_state_
.reset();
75 #if defined(OS_CHROMEOS)
76 chromeos::NetworkHandler::Shutdown();
77 chromeos::DBusThreadManager::Shutdown();
81 void TestingIOThreadState::Initialize(const base::Closure
& done
) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
84 io_thread_state_
->SetGlobalsForTesting(new IOThread::Globals());
89 void TestingIOThreadState::Shutdown(const base::Closure
& done
) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
92 delete io_thread_state_
->globals();
93 io_thread_state_
->SetGlobalsForTesting(NULL
);