OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_BROWSER_THREAD_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_BROWSER_THREAD_IMPL_H_ |
6 #define CONTENT_BROWSER_BROWSER_THREAD_IMPL_H_ | 6 #define CONTENT_BROWSER_BROWSER_THREAD_IMPL_H_ |
7 | 7 |
8 #include "base/threading/platform_thread.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/single_thread_task_runner.h" |
9 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
10 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
12 | 13 |
13 namespace content { | 14 namespace content { |
14 | 15 |
15 // Very few users should use this directly. To mock BrowserThreads, tests should | 16 // Very few users should use this directly. To mock BrowserThreads, tests should |
16 // use TestBrowserThreadBundle instead. | 17 // use TestBrowserThreadBundle instead. |
17 class CONTENT_EXPORT BrowserThreadImpl : public BrowserThread, | 18 class CONTENT_EXPORT BrowserThreadImpl : public BrowserThread, |
18 public base::Thread { | 19 public base::Thread { |
19 public: | 20 public: |
20 // Construct a BrowserThreadImpl with the supplied identifier. It is an error | 21 // Construct a BrowserThreadImpl with the supplied identifier. It is an error |
21 // to construct a BrowserThreadImpl that already exists. | 22 // to construct a BrowserThreadImpl that already exists. |
22 explicit BrowserThreadImpl(BrowserThread::ID identifier); | 23 explicit BrowserThreadImpl(BrowserThread::ID identifier); |
23 | 24 |
24 // Special constructor for the main (UI) thread and unittests. If a | 25 // Special constructor for the main (UI) thread and unittests. If a |
25 // |message_loop| is provied, we use a dummy thread here since the main | 26 // |message_loop| is provied, we use a dummy thread here since the main |
26 // thread already exists. | 27 // thread already exists. |
27 BrowserThreadImpl(BrowserThread::ID identifier, | 28 BrowserThreadImpl(BrowserThread::ID identifier, |
28 base::MessageLoop* message_loop); | 29 base::MessageLoop* message_loop); |
29 ~BrowserThreadImpl() override; | 30 ~BrowserThreadImpl() override; |
30 | 31 |
31 bool Start(); | 32 bool Start(); |
32 bool StartWithOptions(const Options& options); | 33 bool StartWithOptions(const Options& options); |
33 bool StartAndWaitForTesting(); | 34 bool StartAndWaitForTesting(); |
34 | 35 |
| 36 // Redirects tasks posted to |identifier| to |task_runner|. |
| 37 static void RedirectThreadIDToTaskRunner( |
| 38 BrowserThread::ID identifier, |
| 39 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 40 |
| 41 // Makes this |identifier| no longer accept tasks and synchronously flushes |
| 42 // any tasks previously posted to it. |
| 43 // Can only be called after a matching RedirectThreadIDToTaskRunner call. |
| 44 static void StopRedirectionOfThreadID(BrowserThread::ID identifier); |
| 45 |
35 static void ShutdownThreadPool(); | 46 static void ShutdownThreadPool(); |
36 | 47 |
| 48 // Resets globals for |identifier|. Used in tests to clear global state that |
| 49 // would otherwise leak to the next test. Globals are not otherwise fully |
| 50 // cleaned up in ~BrowserThreadImpl() as there are subtle differences between |
| 51 // UNINITIALIZED and SHUTDOWN state (e.g. globals.task_runners are kept around |
| 52 // on shutdown). Must be called after ~BrowserThreadImpl() for the given |
| 53 // |identifier|. |
| 54 static void ResetGlobalsForTesting(BrowserThread::ID identifier); |
| 55 |
37 protected: | 56 protected: |
38 void Init() override; | 57 void Init() override; |
39 void Run(base::RunLoop* run_loop) override; | 58 void Run(base::RunLoop* run_loop) override; |
40 void CleanUp() override; | 59 void CleanUp() override; |
41 | 60 |
42 private: | 61 private: |
43 // We implement all the functionality of the public BrowserThread | 62 // We implement all the functionality of the public BrowserThread |
44 // functions, but state is stored in the BrowserThreadImpl to keep | 63 // functions, but state is stored in the BrowserThreadImpl to keep |
45 // the API cleaner. Therefore make BrowserThread a friend class. | 64 // the API cleaner. Therefore make BrowserThread a friend class. |
46 friend class BrowserThread; | 65 friend class BrowserThread; |
(...skipping 24 matching lines...) Expand all Loading... |
71 static void FlushThreadPoolHelperForTesting(); | 90 static void FlushThreadPoolHelperForTesting(); |
72 | 91 |
73 // The identifier of this thread. Only one thread can exist with a given | 92 // The identifier of this thread. Only one thread can exist with a given |
74 // identifier at a given time. | 93 // identifier at a given time. |
75 ID identifier_; | 94 ID identifier_; |
76 }; | 95 }; |
77 | 96 |
78 } // namespace content | 97 } // namespace content |
79 | 98 |
80 #endif // CONTENT_BROWSER_BROWSER_THREAD_IMPL_H_ | 99 #endif // CONTENT_BROWSER_BROWSER_THREAD_IMPL_H_ |
OLD | NEW |