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.
4
5#include "chrome/test/base/testing_io_thread_state.h"
6
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"
13
14#if defined(OS_CHROMEOS)
15#include "chromeos/dbus/dbus_thread_manager.h"
16#include "chromeos/network/network_handler.h"
17#endif
18
19using content::BrowserThread;
20
21namespace {
22
23base::Closure ThreadSafeQuit(base::RunLoop* run_loop) {
24  if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
25    return run_loop->QuitClosure();
26  } else {
27    using base::Bind;
28    using base::IgnoreResult;
29    return Bind(IgnoreResult(&base::MessageLoopProxy::PostTask),
30                base::MessageLoopProxy::current(),
31                FROM_HERE,
32                run_loop->QuitClosure());
33  }
34}
35
36}  // namespace
37
38namespace chrome {
39
40TestingIOThreadState::TestingIOThreadState() {
41#if defined(OS_CHROMEOS)
42  // Needed by IOThread constructor.
43  chromeos::DBusThreadManager::Initialize();
44  chromeos::NetworkHandler::Initialize();
45#endif
46
47  io_thread_state_.reset(
48      new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
49                   TestingBrowserProcess::GetGlobal()->policy_service(),
50                   NULL, NULL));
51
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))));
58  run_loop.Run();
59
60  TestingBrowserProcess::GetGlobal()->SetIOThread(io_thread_state_.get());
61}
62
63TestingIOThreadState::~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))));
70  run_loop.Run();
71  TestingBrowserProcess::GetGlobal()->SetIOThread(NULL);
72
73  io_thread_state_.reset();
74
75#if defined(OS_CHROMEOS)
76  chromeos::NetworkHandler::Shutdown();
77  chromeos::DBusThreadManager::Shutdown();
78#endif
79}
80
81void TestingIOThreadState::Initialize(const base::Closure& done) {
82  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
83
84  io_thread_state_->SetGlobalsForTesting(new IOThread::Globals());
85
86  done.Run();
87}
88
89void TestingIOThreadState::Shutdown(const base::Closure& done) {
90  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
91
92  delete io_thread_state_->globals();
93  io_thread_state_->SetGlobalsForTesting(NULL);
94  done.Run();
95}
96
97}  // namespace chrome
98