Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [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 | |
| 5 | #ifndef CONTENT_BROWSER_SCHEDULER_BROWSER_TASK_EXECUTOR_H_ |
| 6 | #define CONTENT_BROWSER_SCHEDULER_BROWSER_TASK_EXECUTOR_H_ |
| 7 | |
Alex Clarke | 4779e4bd | 2019-02-15 22:32:03 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Eric Seckler | 0618f40 | 2018-10-29 12:08:52 | [diff] [blame] | 10 | #include "base/memory/scoped_refptr.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 11 | #include "base/task/sequenced_task_runner.h" |
| 12 | #include "base/task/single_thread_task_runner.h" |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [diff] [blame] | 13 | #include "build/build_config.h" |
Gabriel Charette | 748577aa | 2019-08-12 12:53:55 | [diff] [blame] | 14 | #include "content/browser/scheduler/browser_io_thread_delegate.h" |
Carlos Caballero | 5f6212b | 2019-05-13 13:45:16 | [diff] [blame] | 15 | #include "content/browser/scheduler/browser_ui_thread_scheduler.h" |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [diff] [blame] | 16 | #include "content/common/content_export.h" |
Eric Seckler | 501f9a1 | 2018-09-14 18:09:44 | [diff] [blame] | 17 | #include "content/public/browser/browser_task_traits.h" |
Carlos Caballero | ff8c503 | 2019-04-01 11:15:08 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [diff] [blame] | 19 | |
| 20 | namespace content { |
| 21 | |
John Abd-El-Malek | f3243dc | 2021-05-07 16:20:01 | [diff] [blame] | 22 | class BrowserProcessIOThread; |
Alex Clarke | 831ed1e6 | 2019-02-18 21:10:08 | [diff] [blame] | 23 | |
Chidera Olibie | 0edf0e4 | 2025-01-21 12:18:16 | [diff] [blame] | 24 | // The BrowserTaskExecutor's job is to map base::TaskTraits to actual task |
| 25 | // queues for the browser process. |
| 26 | class CONTENT_EXPORT BrowserTaskExecutor { |
Alex Clarke | bbf891dc | 2019-10-09 14:18:02 | [diff] [blame] | 27 | public: |
Chidera Olibie | 0edf0e4 | 2025-01-21 12:18:16 | [diff] [blame] | 28 | // Creates and registers a BrowserTaskExecutor on the current thread which |
| 29 | // owns a BrowserUIThreadScheduler. This facilitates posting tasks to a |
| 30 | // BrowserThread via //base/task/post_task.h. |
| 31 | // TODO(crbug.com/40108370): Clean this up now that post_task.h is deprecated. |
Chidera Olibie | debdc54 | 2025-03-25 23:14:08 | [diff] [blame] | 32 | // By default, BrowserThread::UI task queues except best effort ones are also |
| 33 | // enabled. The ContentBrowserClient may decide to defer enabling the task |
| 34 | // queues. |
Chidera Olibie | 0edf0e4 | 2025-01-21 12:18:16 | [diff] [blame] | 35 | // TODO(carlscab): These queues should be enabled in |
| 36 | // BrowserMainLoop::InitializeMainThread() but some Android tests fail if we |
| 37 | // do so. |
| 38 | static void Create(); |
| 39 | |
| 40 | BrowserTaskExecutor(const BrowserTaskExecutor&) = delete; |
| 41 | BrowserTaskExecutor& operator=(const BrowserTaskExecutor&) = delete; |
Alex Clarke | bbf891dc | 2019-10-09 14:18:02 | [diff] [blame] | 42 | |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 43 | // Returns the task runner for |traits| under |identifier|. Note: during the |
| 44 | // migration away from task traits extension, |traits| may also contain a |
| 45 | // browser thread id, if so, it should match |identifier| (|identifier| has to |
| 46 | // be provided explicitly because in the new source of tasks it's not part of |
| 47 | // |traits|) -- ref. crbug.com/1026641. |
| 48 | scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner( |
| 49 | BrowserThread::ID identifier, |
Michael Thiessen | 44c306f8 | 2023-04-06 12:42:17 | [diff] [blame] | 50 | const BrowserTaskTraits& traits) const; |
Alex Clarke | bbf891dc | 2019-10-09 14:18:02 | [diff] [blame] | 51 | |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 52 | // Helper to match a QueueType from TaskTraits. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 53 | // TODO(crbug.com/40108370): Take BrowserTaskTraits as a parameter when |
| 54 | // getting off the need to support base::TaskTraits currently passed to this |
| 55 | // class in its role as a base::TaskExecutor. |
Michael Thiessen | 44c306f8 | 2023-04-06 12:42:17 | [diff] [blame] | 56 | static BrowserTaskQueues::QueueType GetQueueType( |
| 57 | const BrowserTaskTraits& traits); |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 58 | |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 59 | // Creates the IO thread using the scheduling infrastructure set up in the |
| 60 | // Create() method. That means that clients have access to TaskRunners |
| 61 | // associated with the IO thread before that thread is even created. In order |
Gabriel Charette | 1811051 | 2019-08-15 15:27:05 | [diff] [blame] | 62 | // to do so this class will own the Thread::Delegate for the IO thread |
| 63 | // (BrowserIOThreadDelegate) until the thread is created, at which point |
| 64 | // ownership will be transferred and the |BrowserTaskExecutor| will only |
| 65 | // communicate with it via TaskRunner instances. |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 66 | // |
| 67 | // Browser task queues will initially be disabled, that is tasks posted to |
| 68 | // them will not run. But the default task runner of the thread (the one you |
Sean Maher | 70f294293 | 2023-01-04 22:15:06 | [diff] [blame] | 69 | // get via SingleThreadTaskRunner::GetCurrentDefault()) will be active. This |
| 70 | // is the same task runner you get by calling |
| 71 | // BrowserProcessIOThread::task_runner(). The queues can be initialized by |
| 72 | // calling InitializeIOThread which is done during Chromium startup in |
| 73 | // BrowserMainLoop::CreateThreads. |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 74 | // |
| 75 | // Early on during Chromium startup we initialize the ServiceManager and it |
| 76 | // needs to run tasks immediately. The ServiceManager itself does not know |
| 77 | // about the IO thread (it does not use the browser task traits), it only uses |
| 78 | // the task runner provided to it during initialization and possibly |
Sean Maher | 70f294293 | 2023-01-04 22:15:06 | [diff] [blame] | 79 | // SingleThreadTaskRunner::GetCurrentDefault() from tasks it posts. But we |
| 80 | // currently run it on the IO thread so we need the default task runner to be |
| 81 | // active for its tasks to run. Note that since tasks posted via the browser |
| 82 | // task traits will not run they won't be able to access the default task |
| 83 | // runner either, so for those tasks the default task queue is also |
| 84 | // "disabled". |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 85 | // |
| 86 | // Attention: This method can only be called once (as there must be only one |
| 87 | // IO thread). |
Carlos Caballero | bdb3070 | 2019-07-31 08:08:28 | [diff] [blame] | 88 | // Attention: Must be called after Create() |
| 89 | // Attention: Can not be called after Shutdown() or ResetForTesting() |
John Abd-El-Malek | f3243dc | 2021-05-07 16:20:01 | [diff] [blame] | 90 | static std::unique_ptr<BrowserProcessIOThread> CreateIOThread(); |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 91 | |
| 92 | // Enables non best effort queues on the IO thread. Usually called from |
| 93 | // BrowserMainLoop::CreateThreads. |
| 94 | static void InitializeIOThread(); |
| 95 | |
Minoru Chikamune | a031756 | 2022-03-22 15:30:21 | [diff] [blame] | 96 | // Informs BrowserTaskExecutor that startup is complete. |
| 97 | // It will communicate that to UI and IO thread BrowserTaskQueues. |
Carlos Caballero | 72e8a20 | 2019-05-21 16:51:17 | [diff] [blame] | 98 | // Can be called multiple times. |
Minoru Chikamune | a031756 | 2022-03-22 15:30:21 | [diff] [blame] | 99 | static void OnStartupComplete(); |
Carlos Caballero | 72e8a20 | 2019-05-21 16:51:17 | [diff] [blame] | 100 | |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 101 | // Helpers to statically call into BaseBrowserTaskExecutor::GetTaskRunner() |
| 102 | // from browser_thread_impl.cc. Callers should use browser_thread.h's |
| 103 | // GetUIThreadTaskRunner over this. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 104 | // TODO(crbug.com/40108370): Clean up this indirection after the migration |
| 105 | // (once registering a base::BrowserTaskExecutor is no longer necessary). |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 106 | static scoped_refptr<base::SingleThreadTaskRunner> GetUIThreadTaskRunner( |
| 107 | const BrowserTaskTraits& traits); |
| 108 | static scoped_refptr<base::SingleThreadTaskRunner> GetIOThreadTaskRunner( |
| 109 | const BrowserTaskTraits& traits); |
| 110 | |
Michael Thiessen | 7f5dfb9 | 2023-03-30 16:50:21 | [diff] [blame] | 111 | // As Create but with the user provided objects. |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 112 | static void CreateForTesting( |
| 113 | std::unique_ptr<BrowserUIThreadScheduler> browser_ui_thread_scheduler, |
Gabriel Charette | 748577aa | 2019-08-12 12:53:55 | [diff] [blame] | 114 | std::unique_ptr<BrowserIOThreadDelegate> browser_io_thread_delegate); |
Alex Clarke | 4779e4bd | 2019-02-15 22:32:03 | [diff] [blame] | 115 | |
mikt | 541ac71 | 2025-08-04 03:16:22 | [diff] [blame] | 116 | // This must be called after the FeatureList has been initialized in order |
| 117 | // for scheduling experiments to function. |
| 118 | static void InstallPartitionAllocSchedulerLoopQuarantineTaskObserver(); |
| 119 | |
Alex Clarke | 4779e4bd | 2019-02-15 22:32:03 | [diff] [blame] | 120 | // Winds down the BrowserTaskExecutor, after this no tasks can be executed |
| 121 | // and the base::TaskExecutor APIs are non-functional but won't crash if |
Carlos Caballero | bdb3070 | 2019-07-31 08:08:28 | [diff] [blame] | 122 | // called. In unittests however we need to clean up, so |
| 123 | // BrowserTaskExecutor::ResetForTesting should be |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 124 | // called (~BrowserTaskEnvironment() takes care of this). |
Alex Clarke | 4779e4bd | 2019-02-15 22:32:03 | [diff] [blame] | 125 | static void Shutdown(); |
| 126 | |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [diff] [blame] | 127 | // Unregister and delete the TaskExecutor after a test. |
| 128 | static void ResetForTesting(); |
| 129 | |
Carlos Caballero | ff8c503 | 2019-04-01 11:15:08 | [diff] [blame] | 130 | // Runs all pending tasks for the given thread. Tasks posted after this method |
| 131 | // is called (in particular any task posted from within any of the pending |
| 132 | // tasks) will be queued but not run. Conceptually this call will disable all |
| 133 | // queues, run any pending tasks, and re-enable all the queues. |
| 134 | // |
| 135 | // If any of the pending tasks posted a task, these could be run by calling |
| 136 | // this method again or running a regular RunLoop. But if that were the case |
| 137 | // you should probably rewrite you tests to wait for a specific event instead. |
Carlos Caballero | ff8c503 | 2019-04-01 11:15:08 | [diff] [blame] | 138 | static void RunAllPendingTasksOnThreadForTesting( |
| 139 | BrowserThread::ID identifier); |
| 140 | |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [diff] [blame] | 141 | private: |
Chidera Olibie | debdc54 | 2025-03-25 23:14:08 | [diff] [blame] | 142 | FRIEND_TEST_ALL_PREFIXES(BrowserTaskExecutorWithCustomSchedulerTest, |
| 143 | OnlyDefaultTaskRunnerActiveAfterCreationForIO); |
Karolina Soltys | b083f93 | 2019-09-25 16:18:06 | [diff] [blame] | 144 | friend class BrowserIOThreadDelegate; |
Alex Clarke | bbf891dc | 2019-10-09 14:18:02 | [diff] [blame] | 145 | |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 146 | static void CreateInternal( |
| 147 | std::unique_ptr<BrowserUIThreadScheduler> browser_ui_thread_scheduler, |
Gabriel Charette | 748577aa | 2019-08-12 12:53:55 | [diff] [blame] | 148 | std::unique_ptr<BrowserIOThreadDelegate> browser_io_thread_delegate); |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 149 | |
Alex Clarke | 831ed1e6 | 2019-02-18 21:10:08 | [diff] [blame] | 150 | explicit BrowserTaskExecutor( |
Carlos Caballero | e840fc3 | 2019-05-27 14:16:37 | [diff] [blame] | 151 | std::unique_ptr<BrowserUIThreadScheduler> browser_ui_thread_scheduler, |
Gabriel Charette | 748577aa | 2019-08-12 12:53:55 | [diff] [blame] | 152 | std::unique_ptr<BrowserIOThreadDelegate> browser_io_thread_delegate); |
Chidera Olibie | 0edf0e4 | 2025-01-21 12:18:16 | [diff] [blame] | 153 | ~BrowserTaskExecutor(); |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [diff] [blame] | 154 | |
Karolina Soltys | b083f93 | 2019-09-25 16:18:06 | [diff] [blame] | 155 | static BrowserTaskExecutor* Get(); |
| 156 | |
Chidera Olibie | 0edf0e4 | 2025-01-21 12:18:16 | [diff] [blame] | 157 | std::unique_ptr<BrowserUIThreadScheduler> browser_ui_thread_scheduler_; |
| 158 | scoped_refptr<BrowserUIThreadScheduler::Handle> browser_ui_thread_handle_; |
| 159 | |
| 160 | std::unique_ptr<BrowserIOThreadDelegate> browser_io_thread_delegate_; |
| 161 | scoped_refptr<BrowserIOThreadDelegate::Handle> browser_io_thread_handle_; |
Alex Clarke | 7dc412d | 2018-09-14 10:02:31 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | } // namespace content |
| 165 | |
| 166 | #endif // CONTENT_BROWSER_SCHEDULER_BROWSER_TASK_EXECUTOR_H_ |