Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [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_STARTUP_TASK_RUNNER_H_ |
| 6 | #define CONTENT_BROWSER_STARTUP_TASK_RUNNER_H_ |
| 7 | |
| 8 | #include <list> |
| 9 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 10 | #include "base/functional/callback.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 11 | #include "base/task/single_thread_task_runner.h" |
Chidera Olibie | 76bdf2a | 2025-07-04 19:10:32 | [diff] [blame] | 12 | #include "base/time/time.h" |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 13 | #include "build/build_config.h" |
Chidera Olibie | 76bdf2a | 2025-07-04 19:10:32 | [diff] [blame] | 14 | #include "content/common/content_export.h" |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 15 | #include "content/public/browser/browser_main_runner.h" |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | // A startup task is a void function returning the status on completion. |
| 20 | // a status of > 0 indicates a failure, and that no further startup tasks should |
| 21 | // be run. |
danakj | f416ce9d | 2019-12-11 20:45:45 | [diff] [blame] | 22 | using StartupTask = base::OnceCallback<int(void)>; |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 23 | |
| 24 | // This class runs startup tasks. The tasks are either run immediately inline, |
| 25 | // or are queued one at a time on the UI thread's message loop. If the events |
| 26 | // are queued, UI events that are received during startup will be acted upon |
| 27 | // between startup tasks. The motivation for this is that, on targets where the |
| 28 | // UI is already started, it allows us to keep the UI responsive during startup. |
| 29 | // |
| 30 | // Note that this differs from a SingleThreadedTaskRunner in that there may be |
| 31 | // no opportunity to handle UI events between the tasks of a |
| 32 | // SingleThreadedTaskRunner. |
| 33 | |
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 34 | class CONTENT_EXPORT StartupTaskRunner { |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 35 | |
| 36 | public: |
| 37 | // Constructor: Note that |startup_complete_callback| is optional. If it is |
Chidera Olibie | 76bdf2a | 2025-07-04 19:10:32 | [diff] [blame] | 38 | // not null it will be called, once all the startup tasks have run, with the |
| 39 | // result of running tasks and the duration spent blocking the UI thread. |
| 40 | StartupTaskRunner( |
| 41 | base::OnceCallback<void(int, base::TimeDelta)> startup_complete_callback, |
| 42 | scoped_refptr<base::SingleThreadTaskRunner> proxy); |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 43 | |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 44 | StartupTaskRunner(const StartupTaskRunner&) = delete; |
| 45 | StartupTaskRunner& operator=(const StartupTaskRunner&) = delete; |
| 46 | |
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 47 | ~StartupTaskRunner(); |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 48 | |
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 49 | // Add a task to the queue of startup tasks to be run. |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 50 | void AddTask(StartupTask callback); |
[email protected] | 232e09d | 2013-08-27 15:29:56 | [diff] [blame] | 51 | |
| 52 | // Start running the tasks asynchronously. |
| 53 | void StartRunningTasksAsync(); |
| 54 | |
| 55 | // Run all tasks, or all remaining tasks, synchronously |
| 56 | void RunAllTasksNow(); |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | friend class base::RefCounted<StartupTaskRunner>; |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 60 | |
| 61 | std::list<StartupTask> task_list_; |
| 62 | void WrappedTask(); |
| 63 | |
Chidera Olibie | 76bdf2a | 2025-07-04 19:10:32 | [diff] [blame] | 64 | base::OnceCallback<void(int, base::TimeDelta)> startup_complete_callback_; |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 65 | scoped_refptr<base::SingleThreadTaskRunner> proxy_; |
Chidera Olibie | 76bdf2a | 2025-07-04 19:10:32 | [diff] [blame] | 66 | // Longest time spent blocking the thread when running the tasks. This is |
| 67 | // either the max duration of each individual task when running async or the |
| 68 | // time it took to run all tasks synchronously. |
| 69 | base::TimeDelta longest_blocking_duration_; |
[email protected] | 57624ab | 2013-08-01 16:01:51 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | } // namespace content |
| 73 | #endif // CONTENT_BROWSER_STARTUP_TASK_RUNNER_H_ |