blob: bccaaa8a84eae26cb07dccb77867fa07aff9deff [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]57624ab2013-08-01 16:01:512// 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 Drissmanadac21992023-01-11 23:46:3910#include "base/functional/callback.h"
Patrick Monette643cdf62021-10-15 19:13:4211#include "base/task/single_thread_task_runner.h"
Chidera Olibie76bdf2a2025-07-04 19:10:3212#include "base/time/time.h"
[email protected]57624ab2013-08-01 16:01:5113#include "build/build_config.h"
Chidera Olibie76bdf2a2025-07-04 19:10:3214#include "content/common/content_export.h"
[email protected]57624ab2013-08-01 16:01:5115#include "content/public/browser/browser_main_runner.h"
16
17namespace 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.
danakjf416ce9d2019-12-11 20:45:4522using StartupTask = base::OnceCallback<int(void)>;
[email protected]57624ab2013-08-01 16:01:5123
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]232e09d2013-08-27 15:29:5634class CONTENT_EXPORT StartupTaskRunner {
[email protected]57624ab2013-08-01 16:01:5135
36 public:
37 // Constructor: Note that |startup_complete_callback| is optional. If it is
Chidera Olibie76bdf2a2025-07-04 19:10:3238 // 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(
Chidera Olibie568c9e7a2025-09-11 17:55:1741 base::OnceCallback<void(int, base::TimeDelta, base::TimeDelta)>
42 startup_complete_callback,
Chidera Olibie76bdf2a2025-07-04 19:10:3243 scoped_refptr<base::SingleThreadTaskRunner> proxy);
[email protected]57624ab2013-08-01 16:01:5144
Peter Boström828b9022021-09-21 02:28:4345 StartupTaskRunner(const StartupTaskRunner&) = delete;
46 StartupTaskRunner& operator=(const StartupTaskRunner&) = delete;
47
[email protected]232e09d2013-08-27 15:29:5648 ~StartupTaskRunner();
[email protected]57624ab2013-08-01 16:01:5149
[email protected]232e09d2013-08-27 15:29:5650 // Add a task to the queue of startup tasks to be run.
Tommy Nyquist4b749d02018-03-20 21:46:2951 void AddTask(StartupTask callback);
[email protected]232e09d2013-08-27 15:29:5652
53 // Start running the tasks asynchronously.
54 void StartRunningTasksAsync();
55
56 // Run all tasks, or all remaining tasks, synchronously
Chidera Olibie568c9e7a2025-09-11 17:55:1757 // `was_posted` means the caller had posted this task to the task_runner
58 // instead of executing it inline.
59 void RunAllTasksNow(bool was_posted);
[email protected]57624ab2013-08-01 16:01:5160
61 private:
62 friend class base::RefCounted<StartupTaskRunner>;
[email protected]57624ab2013-08-01 16:01:5163
64 std::list<StartupTask> task_list_;
65 void WrappedTask();
66
Chidera Olibie568c9e7a2025-09-11 17:55:1767 base::OnceCallback<void(int, base::TimeDelta, base::TimeDelta)>
68 startup_complete_callback_;
[email protected]57624ab2013-08-01 16:01:5169 scoped_refptr<base::SingleThreadTaskRunner> proxy_;
Chidera Olibie568c9e7a2025-09-11 17:55:1770 // The longest of the wall-clock durations of each individual task that was
71 // posted to the task_runner.
72 base::TimeDelta longest_duration_of_posted_startup_tasks_;
73 // The total wall-clock duration of all the tasks that were run as posted
74 // tasks.
75 base::TimeDelta total_duration_of_posted_startup_tasks_;
[email protected]57624ab2013-08-01 16:01:5176};
77
78} // namespace content
79#endif // CONTENT_BROWSER_STARTUP_TASK_RUNNER_H_