[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_BROWSER_PROCESS_SUB_THREAD_H_ |
6 | #define CONTENT_BROWSER_BROWSER_PROCESS_SUB_THREAD_H_ | ||||
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 7 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 8 | #include <memory> |
9 | |||||
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 10 | #include "base/macros.h" |
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 11 | #include "base/threading/thread.h" |
12 | #include "base/threading/thread_checker.h" | ||||
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | 84c13c03 | 2011-09-23 00:12:22 | [diff] [blame] | 14 | #include "content/common/content_export.h" |
Gabriel Charette | 790754c | 2018-03-16 21:32:59 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 16 | |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 17 | #if defined(OS_WIN) |
18 | namespace base { | ||||
19 | namespace win { | ||||
20 | class ScopedCOMInitializer; | ||||
21 | } | ||||
22 | } | ||||
23 | #endif | ||||
24 | |||||
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 25 | namespace content { |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 26 | class NotificationService; |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 27 | } |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 28 | |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 29 | namespace content { |
30 | |||||
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 31 | // ---------------------------------------------------------------------------- |
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 32 | // A BrowserProcessSubThread is a physical thread backing a BrowserThread. |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 33 | // |
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 Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 37 | class CONTENT_EXPORT BrowserProcessSubThread : public base::Thread { |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 38 | public: |
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 39 | // Constructs a BrowserProcessSubThread for |identifier|. |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 40 | explicit BrowserProcessSubThread(BrowserThread::ID identifier); |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 41 | ~BrowserProcessSubThread() override; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 42 | |
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 43 | // 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 Han | 6740d62 | 2018-05-22 19:21:07 | [diff] [blame] | 56 | // 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] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 60 | protected: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 61 | void Init() override; |
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 62 | void Run(base::RunLoop* run_loop) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 63 | void CleanUp() override; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 64 | |
65 | private: | ||||
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 66 | // 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] | af66993 | 2012-01-17 19:26:58 | [diff] [blame] | 87 | |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 88 | #if defined (OS_WIN) |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 89 | std::unique_ptr<base::win::ScopedCOMInitializer> com_initializer_; |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 90 | #endif |
91 | |||||
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 92 | // Each specialized thread has its own notification service. |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 93 | std::unique_ptr<NotificationService> notification_service_; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 94 | |
Gabriel Charette | 8eb4dff | 2018-03-27 14:22:54 | [diff] [blame] | 95 | THREAD_CHECKER(browser_thread_checker_); |
96 | |||||
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(BrowserProcessSubThread); |
98 | }; | ||||
99 | |||||
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 100 | } // namespace content |
101 | |||||
[email protected] | b0f146f | 2011-09-15 22:14:25 | [diff] [blame] | 102 | #endif // CONTENT_BROWSER_BROWSER_PROCESS_SUB_THREAD_H_ |