Implement nacl_irt_memory for non-sfi mode.
[chromium-blink-merge.git] / chrome / test / base / testing_io_thread_state.cc
blob46f396afd38de14ca370dad5f5b7fe025790b46a
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"
19 #endif
21 using content::BrowserThread;
23 namespace {
25 base::Closure ThreadSafeQuit(base::RunLoop* run_loop) {
26 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
27 return run_loop->QuitClosure();
28 } else {
29 using base::Bind;
30 using base::IgnoreResult;
31 return Bind(IgnoreResult(&base::MessageLoopProxy::PostTask),
32 base::MessageLoopProxy::current(),
33 FROM_HERE,
34 run_loop->QuitClosure());
38 } // namespace
40 namespace chrome {
42 TestingIOThreadState::TestingIOThreadState() {
43 #if defined(OS_CHROMEOS)
44 // Needed by IOThread constructor.
45 chromeos::DBusThreadManager::InitializeWithStub();
46 chromeos::NetworkHandler::Initialize();
47 #endif
49 io_thread_state_.reset(
50 new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
51 TestingBrowserProcess::GetGlobal()->policy_service(),
52 NULL, NULL));
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))));
60 run_loop.Run();
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))));
72 run_loop.Run();
73 TestingBrowserProcess::GetGlobal()->SetIOThread(NULL);
75 io_thread_state_.reset();
77 #if defined(OS_CHROMEOS)
78 chromeos::NetworkHandler::Shutdown();
79 chromeos::DBusThreadManager::Shutdown();
80 #endif
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())));
91 done.Run();
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);
99 done.Run();
102 } // namespace chrome