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/default_tick_clock.h"
10 #include "base/time/tick_clock.h"
11 #include "chrome/browser/io_thread.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/base/network_time_notifier.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::MessageLoopProxy::PostTask
),
32 base::MessageLoopProxy::current(),
34 run_loop
->QuitClosure());
42 TestingIOThreadState::TestingIOThreadState() {
43 #if defined(OS_CHROMEOS)
44 // Needed by IOThread constructor.
45 chromeos::DBusThreadManager::InitializeWithStub();
46 chromeos::NetworkHandler::Initialize();
49 io_thread_state_
.reset(
50 new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
51 TestingBrowserProcess::GetGlobal()->policy_service(),
54 // Safe because there are no virtuals.
55 base::RunLoop run_loop
;
56 CHECK(BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
57 base::Bind(&TestingIOThreadState::Initialize
,
58 base::Unretained(this),
59 ThreadSafeQuit(&run_loop
))));
62 TestingBrowserProcess::GetGlobal()->SetIOThread(io_thread_state_
.get());
65 TestingIOThreadState::~TestingIOThreadState() {
66 // Remove all the local IOThread state.
67 base::RunLoop run_loop
;
68 CHECK(BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
69 base::Bind(&TestingIOThreadState::Shutdown
,
70 base::Unretained(this),
71 ThreadSafeQuit(&run_loop
))));
73 TestingBrowserProcess::GetGlobal()->SetIOThread(NULL
);
75 io_thread_state_
.reset();
77 #if defined(OS_CHROMEOS)
78 chromeos::NetworkHandler::Shutdown();
79 chromeos::DBusThreadManager::Shutdown();
83 void TestingIOThreadState::Initialize(const base::Closure
& done
) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
86 io_thread_state_
->SetGlobalsForTesting(new IOThread::Globals());
87 io_thread_state_
->globals()->network_time_notifier
.reset(
88 new net::NetworkTimeNotifier(
89 scoped_ptr
<base::TickClock
>(new base::DefaultTickClock())));
94 void TestingIOThreadState::Shutdown(const base::Closure
& done
) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
97 delete io_thread_state_
->globals();
98 io_thread_state_
->SetGlobalsForTesting(NULL
);
102 } // namespace chrome