• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
15 
16 #if defined(OS_CHROMEOS)
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/network/network_handler.h"
19 #endif
20 
21 using content::BrowserThread;
22 
23 namespace {
24 
ThreadSafeQuit(base::RunLoop * run_loop)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());
35   }
36 }
37 
38 }  // namespace
39 
40 namespace chrome {
41 
TestingIOThreadState()42 TestingIOThreadState::TestingIOThreadState() {
43 #if defined(OS_CHROMEOS)
44   // Needed by IOThread constructor.
45   chromeos::DBusThreadManager::InitializeWithStub();
46   chromeos::NetworkHandler::Initialize();
47 #endif
48 
49   io_thread_state_.reset(
50       new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
51                    TestingBrowserProcess::GetGlobal()->policy_service(),
52                    NULL, NULL));
53 
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();
61 
62   TestingBrowserProcess::GetGlobal()->SetIOThread(io_thread_state_.get());
63 }
64 
~TestingIOThreadState()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);
74 
75   io_thread_state_.reset();
76 
77 #if defined(OS_CHROMEOS)
78   chromeos::NetworkHandler::Shutdown();
79   chromeos::DBusThreadManager::Shutdown();
80 #endif
81 }
82 
Initialize(const base::Closure & done)83 void TestingIOThreadState::Initialize(const base::Closure& done) {
84   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
85 
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())));
90 
91   done.Run();
92 }
93 
Shutdown(const base::Closure & done)94 void TestingIOThreadState::Shutdown(const base::Closure& done) {
95   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
96 
97   delete io_thread_state_->globals();
98   io_thread_state_->SetGlobalsForTesting(NULL);
99   done.Run();
100 }
101 
102 }  // namespace chrome
103