1 // Copyright 2020 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 COMPONENTS_BACKGROUND_TASK_SCHEDULER_BACKGROUND_TASK_H_ 6 #define COMPONENTS_BACKGROUND_TASK_SCHEDULER_BACKGROUND_TASK_H_ 7 8 #include "base/callback.h" 9 #include "base/macros.h" 10 #include "components/background_task_scheduler/task_parameters.h" 11 #include "components/keyed_service/core/simple_factory_key.h" 12 #include "content/public/browser/browser_context.h" 13 14 namespace background_task { 15 16 using TaskFinishedCallback = base::OnceCallback<void(bool)>; 17 18 // Entry point for callbacks from BackgroundTaskScheduler. Any classes 19 // implementing this interface must have a public constructor which takes no 20 // arguments. The callback will be executed on the main thread. 21 class BackgroundTask { 22 public: 23 // The following two methods represent the callback from 24 // BackgroundTaskScheduler when your task should start processing. It is 25 // invoked on the main thread, and if your task finishes quickly, you should 26 // return false from this method when you are done processing. If this is a 27 // long-running task, you should return true from this method, and instead 28 // invoke the |callback| when the processing is finished on some other thread. 29 // While this method is running the system holds a wakelock. If false is 30 // returned from this method, the wakelock is immediately released, but if 31 // this method returns true, the wakelock is not released until either the 32 // |callback| is invoked, or the system calls onStopTask. Depending on whether 33 // Chrome is running in service manager only mode or full browser mode, one or 34 // both of the following two methods are invoked. 35 36 // Callback invoked when chrome is running in service manager only mode. User 37 // can start executing the task here or save the params and wait till full 38 // browser is started and OnFullBrowserLoaded is invoked. OnStartTaskInReducedMode(const TaskParameters & task_params,TaskFinishedCallback callback,SimpleFactoryKey * key)39 virtual void OnStartTaskInReducedMode(const TaskParameters& task_params, 40 TaskFinishedCallback callback, 41 SimpleFactoryKey* key) {} 42 43 // Callback invoked when Chrome is running in full browser mode. This is 44 // invoked only if the chrome was started in reduced mode. OnStartTaskWithFullBrowser(const TaskParameters & task_params,TaskFinishedCallback callback,content::BrowserContext * browser_context)45 virtual void OnStartTaskWithFullBrowser( 46 const TaskParameters& task_params, 47 TaskFinishedCallback callback, 48 content::BrowserContext* browser_context) {} 49 50 // Callback invoked whenever the full browser starts after starting first in 51 // service manager only mode. OnFullBrowserLoaded(content::BrowserContext * browser_context)52 virtual void OnFullBrowserLoaded(content::BrowserContext* browser_context) {} 53 54 // Callback from BackgroundTaskScheduler when the system has determined that 55 // the execution of the task must stop immediately, even before the 56 // TaskFinishedCallback has been invoked. This will typically happen whenever 57 // the required conditions for the task are no longer met. See TaskInfo for 58 // more details. A wakelock is held by the system while this callback is 59 // invoked, and immediately released after this method returns. 60 virtual bool OnStopTask(const TaskParameters& task_params) = 0; 61 62 // Destructor. ~BackgroundTask()63 virtual ~BackgroundTask() {} 64 65 protected: 66 BackgroundTask() = default; 67 68 private: 69 DISALLOW_COPY_AND_ASSIGN(BackgroundTask); 70 }; 71 72 } // namespace background_task 73 74 #endif // COMPONENTS_BACKGROUND_TASK_SCHEDULER_BACKGROUND_TASK_H_ 75