summaryrefslogtreecommitdiffstats
path: root/chrome/test/base/testing_io_thread_state.cc
blob: 09b07f20db2c831bb27aee8a68dbb323cae96129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/base/testing_io_thread_state.h"

#include "base/location.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/tick_clock.h"
#include "chrome/browser/io_thread.h"
#include "chrome/test/base/testing_browser_process.h"
#include "content/public/browser/browser_thread.h"

#if defined(OS_CHROMEOS)
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/network/network_handler.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"
#endif

using content::BrowserThread;

namespace {

base::Closure ThreadSafeQuit(base::RunLoop* run_loop) {
  if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
    return run_loop->QuitClosure();
  } else {
    using base::Bind;
    using base::IgnoreResult;
    return Bind(IgnoreResult(&base::SingleThreadTaskRunner::PostTask),
                base::ThreadTaskRunnerHandle::Get(), FROM_HERE,
                run_loop->QuitClosure());
  }
}

}  // namespace

namespace chrome {

TestingIOThreadState::TestingIOThreadState() {
#if defined(OS_CHROMEOS)
  // Needed by IOThread constructor.
  chromeos::DBusThreadManager::Initialize();
  bluez::BluezDBusManager::Initialize(
      chromeos::DBusThreadManager::Get()->GetSystemBus(),
      chromeos::DBusThreadManager::Get()->IsUsingStub(
          chromeos::DBusClientBundle::BLUETOOTH));
  chromeos::NetworkHandler::Initialize();
#endif

  io_thread_state_.reset(
      new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
                   TestingBrowserProcess::GetGlobal()->policy_service(),
                   NULL, NULL));

  // Safe because there are no virtuals.
  base::RunLoop run_loop;
  CHECK(BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
                                base::Bind(&TestingIOThreadState::Initialize,
                                           base::Unretained(this),
                                           ThreadSafeQuit(&run_loop))));
  run_loop.Run();

  TestingBrowserProcess::GetGlobal()->SetIOThread(io_thread_state_.get());
}

TestingIOThreadState::~TestingIOThreadState() {
  // Remove all the local IOThread state.
  base::RunLoop run_loop;
  CHECK(BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
                                base::Bind(&TestingIOThreadState::Shutdown,
                                           base::Unretained(this),
                                           ThreadSafeQuit(&run_loop))));
  run_loop.Run();
  TestingBrowserProcess::GetGlobal()->SetIOThread(NULL);

  io_thread_state_.reset();

#if defined(OS_CHROMEOS)
  chromeos::NetworkHandler::Shutdown();
  bluez::BluezDBusManager::Shutdown();
  chromeos::DBusThreadManager::Shutdown();
#endif
}

void TestingIOThreadState::Initialize(const base::Closure& done) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  io_thread_state_->SetGlobalsForTesting(new IOThread::Globals());

  done.Run();
}

void TestingIOThreadState::Shutdown(const base::Closure& done) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  delete io_thread_state_->globals();
  io_thread_state_->SetGlobalsForTesting(NULL);
  done.Run();
}

}  // namespace chrome