blob: 105aad10d8ec7d07832ded86269846a8be346d63 [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(
41 base::OnceCallback<void(int, base::TimeDelta)> startup_complete_callback,
42 scoped_refptr<base::SingleThreadTaskRunner> proxy);
[email protected]57624ab2013-08-01 16:01:5143
Peter Boström828b9022021-09-21 02:28:4344 StartupTaskRunner(const StartupTaskRunner&) = delete;
45 StartupTaskRunner& operator=(const StartupTaskRunner&) = delete;
46
[email protected]232e09d2013-08-27 15:29:5647 ~StartupTaskRunner();
[email protected]57624ab2013-08-01 16:01:5148
[email protected]232e09d2013-08-27 15:29:5649 // Add a task to the queue of startup tasks to be run.
Tommy Nyquist4b749d02018-03-20 21:46:2950 void AddTask(StartupTask callback);
[email protected]232e09d2013-08-27 15:29:5651
52 // Start running the tasks asynchronously.
53 void StartRunningTasksAsync();
54
55 // Run all tasks, or all remaining tasks, synchronously
56 void RunAllTasksNow();
[email protected]57624ab2013-08-01 16:01:5157
58 private:
59 friend class base::RefCounted<StartupTaskRunner>;
[email protected]57624ab2013-08-01 16:01:5160
61 std::list<StartupTask> task_list_;
62 void WrappedTask();
63
Chidera Olibie76bdf2a2025-07-04 19:10:3264 base::OnceCallback<void(int, base::TimeDelta)> startup_complete_callback_;
[email protected]57624ab2013-08-01 16:01:5165 scoped_refptr<base::SingleThreadTaskRunner> proxy_;
Chidera Olibie76bdf2a2025-07-04 19:10:3266 // 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]57624ab2013-08-01 16:01:5170};
71
72} // namespace content
73#endif // CONTENT_BROWSER_STARTUP_TASK_RUNNER_H_