Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / base / testing_io_thread_state.cc
blobe79423fb67954f5baa05b86f75190734b01330f9
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/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"
14 #if defined(OS_CHROMEOS)
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/network/network_handler.h"
17 #endif
19 using content::BrowserThread;
21 namespace {
23 base::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());
36 } // namespace
38 namespace chrome {
40 TestingIOThreadState::TestingIOThreadState() {
41 #if defined(OS_CHROMEOS)
42 // Needed by IOThread constructor.
43 chromeos::DBusThreadManager::Initialize();
44 chromeos::NetworkHandler::Initialize();
45 #endif
47 io_thread_state_.reset(
48 new IOThread(TestingBrowserProcess::GetGlobal()->local_state(),
49 TestingBrowserProcess::GetGlobal()->policy_service(),
50 NULL, NULL));
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();
60 TestingBrowserProcess::GetGlobal()->SetIOThread(io_thread_state_.get());
63 TestingIOThreadState::~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);
73 io_thread_state_.reset();
75 #if defined(OS_CHROMEOS)
76 chromeos::NetworkHandler::Shutdown();
77 chromeos::DBusThreadManager::Shutdown();
78 #endif
81 void TestingIOThreadState::Initialize(const base::Closure& done) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
84 io_thread_state_->SetGlobalsForTesting(new IOThread::Globals());
86 done.Run();
89 void TestingIOThreadState::Shutdown(const base::Closure& done) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
92 delete io_thread_state_->globals();
93 io_thread_state_->SetGlobalsForTesting(NULL);
94 done.Run();
97 } // namespace chrome