blob: edc223865bada7e557da945fecc7a635c2ab1eff [file] [log] [blame]
[email protected]ef2bf422012-05-11 03:27:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0ac83682010-01-22 17:46:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b0f146f2011-09-15 22:14:255#ifndef CONTENT_BROWSER_BROWSER_PROCESS_SUB_THREAD_H_
6#define CONTENT_BROWSER_BROWSER_PROCESS_SUB_THREAD_H_
[email protected]0ac83682010-01-22 17:46:277
dcheng59716272016-04-09 05:19:088#include <memory>
9
avib7348942015-12-25 20:57:1010#include "base/macros.h"
Gabriel Charette8eb4dff2018-03-27 14:22:5411#include "base/threading/thread.h"
12#include "base/threading/thread_checker.h"
avib7348942015-12-25 20:57:1013#include "build/build_config.h"
[email protected]84c13c032011-09-23 00:12:2214#include "content/common/content_export.h"
Gabriel Charette790754c2018-03-16 21:32:5915#include "content/public/browser/browser_thread.h"
[email protected]0ac83682010-01-22 17:46:2716
[email protected]451fd902012-10-03 17:14:4817#if defined(OS_WIN)
18namespace base {
19namespace win {
20class ScopedCOMInitializer;
21}
22}
23#endif
24
[email protected]ad50def52011-10-19 23:17:0725namespace content {
[email protected]0ac83682010-01-22 17:46:2726class NotificationService;
[email protected]ad50def52011-10-19 23:17:0727}
[email protected]0ac83682010-01-22 17:46:2728
[email protected]c38831a12011-10-28 12:44:4929namespace content {
30
[email protected]0ac83682010-01-22 17:46:2731// ----------------------------------------------------------------------------
Gabriel Charette8eb4dff2018-03-27 14:22:5432// A BrowserProcessSubThread is a physical thread backing a BrowserThread.
[email protected]0ac83682010-01-22 17:46:2733//
34// Applications must initialize the COM library before they can call
35// COM library functions other than CoGetMalloc and memory allocation
36// functions, so this class initializes COM for those users.
Gabriel Charette8eb4dff2018-03-27 14:22:5437class CONTENT_EXPORT BrowserProcessSubThread : public base::Thread {
[email protected]0ac83682010-01-22 17:46:2738 public:
Gabriel Charette8eb4dff2018-03-27 14:22:5439 // Constructs a BrowserProcessSubThread for |identifier|.
[email protected]d04e7662010-10-10 22:24:4840 explicit BrowserProcessSubThread(BrowserThread::ID identifier);
dchengc2282aa2014-10-21 12:07:5841 ~BrowserProcessSubThread() override;
[email protected]0ac83682010-01-22 17:46:2742
Gabriel Charette8eb4dff2018-03-27 14:22:5443 // Registers this thread to represent |identifier_| in the browser_thread.h
44 // API. This thread must already be running when this is called. This can only
45 // be called once per BrowserProcessSubThread instance.
46 void RegisterAsBrowserThread();
47
48 // Ideally there wouldn't be a special blanket allowance to block the
49 // BrowserThreads in tests but TestBrowserThreadImpl previously bypassed
50 // BrowserProcessSubThread and hence wasn't subject to ThreadRestrictions...
51 // Flipping that around in favor of explicit scoped allowances would be
52 // preferable but a non-trivial amount of work. Can only be called before
53 // starting this BrowserProcessSubThread.
54 void AllowBlockingForTesting();
55
Xi Han6740d622018-05-22 19:21:0756 // Creates and starts the IO thread. It should not be promoted to
57 // BrowserThread::IO until BrowserMainLoop::CreateThreads().
58 static std::unique_ptr<BrowserProcessSubThread> CreateIOThread();
59
[email protected]0ac83682010-01-22 17:46:2760 protected:
dchengc2282aa2014-10-21 12:07:5861 void Init() override;
Gabriel Charette8eb4dff2018-03-27 14:22:5462 void Run(base::RunLoop* run_loop) override;
dchengc2282aa2014-10-21 12:07:5863 void CleanUp() override;
[email protected]0ac83682010-01-22 17:46:2764
65 private:
Gabriel Charette8eb4dff2018-03-27 14:22:5466 // Second Init() phase that must happen on this thread but can only happen
67 // after it's promoted to a BrowserThread in |RegisterAsBrowserThread()|.
68 void CompleteInitializationOnBrowserThread();
69
70 // These methods merely forwards to Thread::Run() but are useful to identify
71 // which BrowserThread this represents in stack traces.
72 void UIThreadRun(base::RunLoop* run_loop);
73 void IOThreadRun(base::RunLoop* run_loop);
74
75 // This method encapsulates cleanup that needs to happen on the IO thread.
76 void IOThreadCleanUp();
77
78 const BrowserThread::ID identifier_;
79
80 // BrowserThreads are not allowed to do file I/O nor wait on synchronization
81 // primivives except when explicitly allowed in tests.
82 bool is_blocking_allowed_for_testing_ = false;
83
84 // The BrowserThread registration for this |identifier_|, initialized in
85 // RegisterAsBrowserThread().
86 std::unique_ptr<BrowserThreadImpl> browser_thread_;
[email protected]af669932012-01-17 19:26:5887
[email protected]451fd902012-10-03 17:14:4888#if defined (OS_WIN)
dcheng59716272016-04-09 05:19:0889 std::unique_ptr<base::win::ScopedCOMInitializer> com_initializer_;
[email protected]451fd902012-10-03 17:14:4890#endif
91
[email protected]0ac83682010-01-22 17:46:2792 // Each specialized thread has its own notification service.
dcheng59716272016-04-09 05:19:0893 std::unique_ptr<NotificationService> notification_service_;
[email protected]0ac83682010-01-22 17:46:2794
Gabriel Charette8eb4dff2018-03-27 14:22:5495 THREAD_CHECKER(browser_thread_checker_);
96
[email protected]0ac83682010-01-22 17:46:2797 DISALLOW_COPY_AND_ASSIGN(BrowserProcessSubThread);
98};
99
[email protected]c38831a12011-10-28 12:44:49100} // namespace content
101
[email protected]b0f146f2011-09-15 22:14:25102#endif // CONTENT_BROWSER_BROWSER_PROCESS_SUB_THREAD_H_