Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
michaeln | 96f887e2 | 2015-04-13 23:58: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 CHROME_BROWSER_AFTER_STARTUP_TASK_UTILS_H_ |
| 6 | #define CHROME_BROWSER_AFTER_STARTUP_TASK_UTILS_H_ |
| 7 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 8 | #include "base/bind.h" |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 9 | #include "base/callback.h" |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 10 | #include "base/location.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
| 12 | |
michaeln | 68bf4a8e | 2015-08-11 01:37:31 | [diff] [blame] | 13 | namespace android { |
| 14 | class AfterStartupTaskUtilsJNI; |
| 15 | } |
| 16 | |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 17 | namespace base { |
| 18 | class SequencedTaskRunner; |
| 19 | } |
| 20 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 21 | class AfterStartupTaskUtils { |
| 22 | public: |
Peter Boström | fadb175 | 2021-09-30 19:17:01 | [diff] [blame] | 23 | AfterStartupTaskUtils() = delete; |
| 24 | AfterStartupTaskUtils(const AfterStartupTaskUtils&) = delete; |
| 25 | AfterStartupTaskUtils& operator=(const AfterStartupTaskUtils&) = delete; |
| 26 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 27 | // Observes startup and when complete runs tasks that have accrued. |
| 28 | static void StartMonitoringStartup(); |
| 29 | |
Joe Mason | caa69054 | 2022-07-12 11:02:16 | [diff] [blame] | 30 | // Queues `task` to run on `destination_runner` after startup is complete. |
| 31 | // Note: prefer to simply post a task with BEST_EFFORT priority. This will |
| 32 | // delay the task until higher priority tasks are finished, which includes |
| 33 | // critical startup tasks. The BrowserThread::PostBestEffortTask() helper can |
| 34 | // post a BEST_EFFORT task to an arbitrary task runner. |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 35 | static void PostTask( |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 36 | const base::Location& from_here, |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 37 | const scoped_refptr<base::SequencedTaskRunner>& destination_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 38 | base::OnceClosure task); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 39 | |
fserb | 3f669c5 | 2015-06-26 16:45:09 | [diff] [blame] | 40 | // Returns true if browser startup is complete. Only use this on a one-off |
| 41 | // basis; If you need to poll this function constantly, use the above |
| 42 | // PostTask() API instead. |
| 43 | static bool IsBrowserStartupComplete(); |
| 44 | |
wkorman | 8a21c4f | 2015-11-18 19:06:11 | [diff] [blame] | 45 | // For use by unit tests where we don't have normal content loading |
| 46 | // infrastructure and thus StartMonitoringStartup() is unsuitable. |
| 47 | static void SetBrowserStartupIsCompleteForTesting(); |
| 48 | |
| 49 | static void UnsafeResetForTesting(); |
| 50 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 51 | private: |
wkorman | 8a21c4f | 2015-11-18 19:06:11 | [diff] [blame] | 52 | // TODO(wkorman): Look into why Android calls |
| 53 | // SetBrowserStartupIsComplete() directly. Ideally it would use |
| 54 | // StartMonitoringStartup() as the normal approach. |
michaeln | 68bf4a8e | 2015-08-11 01:37:31 | [diff] [blame] | 55 | friend class android::AfterStartupTaskUtilsJNI; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 56 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 57 | static void SetBrowserStartupIsComplete(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #endif // CHROME_BROWSER_AFTER_STARTUP_TASK_UTILS_H_ |