1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
79ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/run_loop.h"
93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/browser/browser_thread_impl.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/test/test_browser_thread.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace content {
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TestBrowserThreadBundle::TestBrowserThreadBundle() {
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Init(DEFAULT);
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TestBrowserThreadBundle::TestBrowserThreadBundle(int options) {
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Init(options);
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TestBrowserThreadBundle::~TestBrowserThreadBundle() {
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // To avoid memory leaks, we must ensure that any tasks posted to the blocking
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // pool via PostTaskAndReply are able to reply back to the originating thread.
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Thus we must flush the blocking pool while the browser threads still exist.
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  BrowserThreadImpl::FlushThreadPoolHelperForTesting();
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // To ensure a clean teardown, each thread's message loop must be flushed
307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // just before the thread is destroyed. But destroying a fake thread does not
317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // automatically flush the message loop, so we have to do it manually.
327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // See http://crbug.com/247525 for discussion.
337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::RunLoop().RunUntilIdle();
347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  io_thread_.reset();
357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::RunLoop().RunUntilIdle();
367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  cache_thread_.reset();
377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::RunLoop().RunUntilIdle();
387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  process_launcher_thread_.reset();
397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::RunLoop().RunUntilIdle();
407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  file_user_blocking_thread_.reset();
417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::RunLoop().RunUntilIdle();
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  file_thread_.reset();
437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::RunLoop().RunUntilIdle();
447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  db_thread_.reset();
457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::RunLoop().RunUntilIdle();
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // This is the point at which we normally shut down the thread pool. So flush
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // it again in case any shutdown tasks have been posted to the pool from the
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // threads above.
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  BrowserThreadImpl::FlushThreadPoolHelperForTesting();
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ui_thread_.reset();
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void TestBrowserThreadBundle::Init(int options) {
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (options & IO_MAINLOOP) {
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    message_loop_.reset(new base::MessageLoopForIO());
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    message_loop_.reset(new base::MessageLoopForUI());
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ui_thread_.reset(new TestBrowserThread(BrowserThread::UI,
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                         message_loop_.get()));
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (options & REAL_DB_THREAD) {
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    db_thread_.reset(new TestBrowserThread(BrowserThread::DB));
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    db_thread_->Start();
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    db_thread_.reset(new TestBrowserThread(BrowserThread::DB,
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                           message_loop_.get()));
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (options & REAL_FILE_THREAD) {
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    file_thread_.reset(new TestBrowserThread(BrowserThread::FILE));
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    file_thread_->Start();
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    file_thread_.reset(new TestBrowserThread(BrowserThread::FILE,
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                             message_loop_.get()));
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (options & REAL_FILE_USER_BLOCKING_THREAD) {
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    file_user_blocking_thread_.reset(
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        new TestBrowserThread(BrowserThread::FILE_USER_BLOCKING));
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    file_user_blocking_thread_->Start();
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    file_user_blocking_thread_.reset(
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        new TestBrowserThread(BrowserThread::FILE_USER_BLOCKING,
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              message_loop_.get()));
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (options & REAL_PROCESS_LAUNCHER_THREAD) {
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    process_launcher_thread_.reset(
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        new TestBrowserThread(BrowserThread::PROCESS_LAUNCHER));
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    process_launcher_thread_->Start();
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    process_launcher_thread_.reset(
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        new TestBrowserThread(BrowserThread::PROCESS_LAUNCHER,
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              message_loop_.get()));
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (options & REAL_CACHE_THREAD) {
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    cache_thread_.reset(new TestBrowserThread(BrowserThread::CACHE));
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    cache_thread_->Start();
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    cache_thread_.reset(new TestBrowserThread(BrowserThread::CACHE,
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                              message_loop_.get()));
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (options & REAL_IO_THREAD) {
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    io_thread_.reset(new TestBrowserThread(BrowserThread::IO));
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    io_thread_->StartIOThread();
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    io_thread_.reset(
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        new TestBrowserThread(BrowserThread::IO, message_loop_.get()));
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace content
119