michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 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" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 10 | #include "base/macros.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 12 | #include "base/task_runner.h" |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 13 | |
michaeln | 68bf4a8e | 2015-08-11 01:37:31 | [diff] [blame] | 14 | namespace android { |
| 15 | class AfterStartupTaskUtilsJNI; |
| 16 | } |
| 17 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 18 | class AfterStartupTaskUtils { |
| 19 | public: |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 20 | // A helper TaskRunner which merely forwards to |
| 21 | // AfterStartupTaskUtils::PostTask(). Doesn't support tasks with a non-zero |
| 22 | // delay. |
| 23 | class Runner : public base::TaskRunner { |
| 24 | public: |
| 25 | explicit Runner(scoped_refptr<base::TaskRunner> destination_runner); |
| 26 | |
| 27 | // Overrides from base::TaskRunner: |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 28 | bool PostDelayedTask(const base::Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 29 | base::OnceClosure task, |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 30 | base::TimeDelta delay) override; |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 31 | bool RunsTasksInCurrentSequence() const override; |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 32 | |
| 33 | private: |
| 34 | ~Runner() override; |
| 35 | |
| 36 | const scoped_refptr<base::TaskRunner> destination_runner_; |
| 37 | |
| 38 | DISALLOW_COPY_AND_ASSIGN(Runner); |
| 39 | }; |
| 40 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 41 | // Observes startup and when complete runs tasks that have accrued. |
| 42 | static void StartMonitoringStartup(); |
| 43 | |
| 44 | // Used to augment the behavior of BrowserThread::PostAfterStartupTask |
| 45 | // for chrome. Tasks are queued until startup is complete. |
| 46 | // Note: see browser_thread.h |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 47 | static void PostTask( |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 48 | const base::Location& from_here, |
gab | 27e6d33f | 2016-08-11 13:15:33 | [diff] [blame] | 49 | const scoped_refptr<base::TaskRunner>& destination_runner, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 50 | base::OnceClosure task); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 51 | |
fserb | 3f669c5 | 2015-06-26 16:45:09 | [diff] [blame] | 52 | // Returns true if browser startup is complete. Only use this on a one-off |
| 53 | // basis; If you need to poll this function constantly, use the above |
| 54 | // PostTask() API instead. |
| 55 | static bool IsBrowserStartupComplete(); |
| 56 | |
wkorman | 8a21c4f | 2015-11-18 19:06:11 | [diff] [blame] | 57 | // For use by unit tests where we don't have normal content loading |
| 58 | // infrastructure and thus StartMonitoringStartup() is unsuitable. |
| 59 | static void SetBrowserStartupIsCompleteForTesting(); |
| 60 | |
| 61 | static void UnsafeResetForTesting(); |
| 62 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 63 | private: |
wkorman | 8a21c4f | 2015-11-18 19:06:11 | [diff] [blame] | 64 | // TODO(wkorman): Look into why Android calls |
| 65 | // SetBrowserStartupIsComplete() directly. Ideally it would use |
| 66 | // StartMonitoringStartup() as the normal approach. |
michaeln | 68bf4a8e | 2015-08-11 01:37:31 | [diff] [blame] | 67 | friend class android::AfterStartupTaskUtilsJNI; |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 68 | |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 69 | static void SetBrowserStartupIsComplete(); |
michaeln | 96f887e2 | 2015-04-13 23:58:31 | [diff] [blame] | 70 | |
| 71 | DISALLOW_IMPLICIT_CONSTRUCTORS(AfterStartupTaskUtils); |
| 72 | }; |
| 73 | |
| 74 | #endif // CHROME_BROWSER_AFTER_STARTUP_TASK_UTILS_H_ |