blob: 2ab5895c63037dc329c96865e8829ba4b82bdd94 [file] [log] [blame]
harkness7b36c432017-02-15 20:05:361// Copyright 2017 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 CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_
6#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_
7
peter8ea74ef32017-03-27 22:20:568#include <map>
Dan Elphick7c245b22017-07-27 16:35:529#include <memory>
peter16b93602017-03-28 15:14:2910#include <string>
peter8ea74ef32017-03-27 22:20:5611#include <vector>
harkness35b3ce0d2017-03-09 21:36:5312
harkness7b36c432017-02-15 20:05:3613#include "base/macros.h"
Matt Falkenhagen41ccef452019-08-28 14:19:0214#include "base/memory/ref_counted_delete_on_sequence.h"
johnme57400c12017-07-11 15:56:3515#include "base/memory/weak_ptr.h"
Rayan Kanso4b7e2572018-07-25 18:35:0516#include "base/optional.h"
Dan Elphick975213182017-09-22 10:37:4717#include "content/browser/background_fetch/background_fetch_delegate_proxy.h"
John Mellor4119fed52017-09-18 20:20:5318#include "content/browser/background_fetch/background_fetch_event_dispatcher.h"
Rayan Kansob5ce0582018-07-06 16:03:1919#include "content/browser/background_fetch/storage/get_initialization_data_task.h"
Rayan Kanso8cfef332019-05-08 16:25:4820#include "content/browser/devtools/devtools_background_services_context_impl.h"
harkness7b36c432017-02-15 20:05:3621#include "content/common/content_export.h"
harkness35b3ce0d2017-03-09 21:36:5322#include "content/public/browser/browser_thread.h"
Richard Li912523f2018-12-13 08:10:4023#include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
harkness7b36c432017-02-15 20:05:3624
Peter Beverloo3f5b8922017-11-01 13:22:1525namespace storage {
Rayan Kanso77bf1922018-08-17 16:44:2326class QuotaManagerProxy;
Rayan Kansod274cb92019-03-11 15:59:4127} // namespace storage
Peter Beverloo3f5b8922017-11-01 13:22:1528
harkness7b36c432017-02-15 20:05:3629namespace content {
30
Rayan Kanso4d15c0632018-06-04 19:43:0031class BackgroundFetchDataManager;
peter8ea74ef32017-03-27 22:20:5632class BackgroundFetchRegistrationId;
Peter Beverloo83ee6642017-10-11 20:23:1733class BackgroundFetchRegistrationNotifier;
Mugdha Lakhani90d1f5a2018-08-21 18:13:1234class BackgroundFetchRequestMatchParams;
Dan Elphick14655852018-01-03 16:13:2935class BackgroundFetchScheduler;
harkness7b36c432017-02-15 20:05:3636class BrowserContext;
harkness7b36c432017-02-15 20:05:3637class ServiceWorkerContextWrapper;
harkness7b36c432017-02-15 20:05:3638
39// The BackgroundFetchContext is the central moderator of ongoing background
40// fetch requests from the Mojo service and from other callers.
johnme57400c12017-07-11 15:56:3541// Background Fetch requests function similarly to normal fetches except that
harkness7b36c432017-02-15 20:05:3642// they are persistent across Chromium or service worker shutdown.
Matt Falkenhagen41ccef452019-08-28 14:19:0243//
44// Deleted on the service worker core thread.
45// TODO(crbug.com/824858): Make this single-threaded after the service worker
46// core thread moves to the UI thread.
harkness7b36c432017-02-15 20:05:3647class CONTENT_EXPORT BackgroundFetchContext
Matt Falkenhagen41ccef452019-08-28 14:19:0248 : public base::RefCountedDeleteOnSequence<BackgroundFetchContext> {
harkness7b36c432017-02-15 20:05:3649 public:
50 // The BackgroundFetchContext will watch the ServiceWorkerContextWrapper so
51 // that it can respond to service worker events such as unregister.
John Mellor4018b9b2017-08-25 16:36:2752 BackgroundFetchContext(
53 BrowserContext* browser_context,
Adrienne Walker99a61f32021-01-22 12:31:5654 StoragePartition* storage_partition,
Rayan Kansoecd23312018-05-29 15:36:4155 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context,
Rayan Kansod274cb92019-03-11 15:59:4156 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
Rayan Kanso8cfef332019-05-08 16:25:4857 scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context);
harkness7b36c432017-02-15 20:05:3658
Matt Falkenhagen41ccef452019-08-28 14:19:0259 void InitializeOnCoreThread();
Mugdha Lakhani9de44292018-06-07 16:14:5360
Rayan Kanso54422772018-06-22 19:07:0561 // Called by the StoragePartitionImpl destructor.
62 void Shutdown();
63
Dan Elphick746a67f2017-11-06 14:57:0764 // Gets the active Background Fetch registration identified by |developer_id|
65 // for the given |service_worker_id| and |origin|. The |callback| will be
66 // invoked with the registration when it has been retrieved.
67 void GetRegistration(
68 int64_t service_worker_registration_id,
69 const url::Origin& origin,
70 const std::string& developer_id,
71 blink::mojom::BackgroundFetchService::GetRegistrationCallback callback);
72
Dan Elphickd28d18a2017-11-07 14:26:3773 // Gets all the Background Fetch registration |developer_id|s for a Service
74 // Worker and invokes |callback| with that list.
75 void GetDeveloperIdsForServiceWorker(
76 int64_t service_worker_registration_id,
77 const url::Origin& origin,
78 blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback);
79
peter8ea74ef32017-03-27 22:20:5680 // Starts a Background Fetch for the |registration_id|. The |requests| will be
81 // asynchronously fetched. The |callback| will be invoked when the fetch has
Dan Elphickbfd47f02017-11-03 15:43:3482 // been registered, or an error occurred that prevents it from doing so.
tzikcf7bcd652017-06-15 04:19:3083 void StartFetch(const BackgroundFetchRegistrationId& registration_id,
Richard Lifad69422018-11-09 02:26:4184 std::vector<blink::mojom::FetchAPIRequestPtr> requests,
Richard Lif74adb22018-12-05 06:58:1985 blink::mojom::BackgroundFetchOptionsPtr options,
Mugdha Lakhani4de0e19e2018-03-15 13:07:3286 const SkBitmap& icon,
Mugdha Lakhani7431ee02018-09-07 12:15:2687 blink::mojom::BackgroundFetchUkmDataPtr ukm_data,
Rayan Kanso997f496c2018-12-18 16:28:2988 int render_frame_tree_node_id,
John Abd-El-Malek92bf3602019-07-31 02:25:4889 const WebContents::Getter& wc_getter,
tzikcf7bcd652017-06-15 04:19:3090 blink::mojom::BackgroundFetchService::FetchCallback callback);
harkness7b36c432017-02-15 20:05:3691
Mugdha Lakhani31a74e62018-03-27 16:03:5892 // Gets display size for the icon for Background Fetch UI.
93 void GetIconDisplaySize(
94 blink::mojom::BackgroundFetchService::GetIconDisplaySizeCallback
95 callback);
96
Mugdha Lakhani90d1f5a2018-08-21 18:13:1297 // Matches Background Fetch requests from the cache and returns responses.
98 void MatchRequests(
99 const BackgroundFetchRegistrationId& registration_id,
100 std::unique_ptr<BackgroundFetchRequestMatchParams> match_params,
Rayan Kansof41e81f92019-04-04 12:08:37101 blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback
102 callback);
Mugdha Lakhani90d1f5a2018-08-21 18:13:12103
Dan Elphick553789d2017-10-26 22:36:38104 // Aborts the Background Fetch for the |registration_id|. The callback will be
105 // invoked with INVALID_ID if the registration has already completed or
106 // aborted, STORAGE_ERROR if an I/O error occurs, or NONE for success.
Rayan Kansof41e81f92019-04-04 12:08:37107 void Abort(
108 const BackgroundFetchRegistrationId& registration_id,
109 blink::mojom::BackgroundFetchRegistrationService::AbortCallback callback);
peter16b93602017-03-28 15:14:29110
Peter Beverloo83ee6642017-10-11 20:23:17111 // Registers the |observer| to be notified of progress events for the
112 // registration identified by |unique_id| whenever they happen. The observer
113 // will unregister itself when the Mojo endpoint goes away.
114 void AddRegistrationObserver(
115 const std::string& unique_id,
Julie Jeongeun Kimf01877d2019-08-08 00:43:02116 mojo::PendingRemote<blink::mojom::BackgroundFetchRegistrationObserver>
117 observer);
Peter Beverloo83ee6642017-10-11 20:23:17118
Rayan Kanso4b7e2572018-07-25 18:35:05119 // Updates the title or icon of the Background Fetch identified by
120 // |registration_id|. The |callback| will be invoked when the title has been
121 // updated, or an error occurred that prevents it from doing so.
122 // The icon is wrapped in an optional. If the optional has a value then the
123 // internal |icon| is guarnteed to be not null.
Dan Elphicke11715a2017-11-06 15:59:49124 void UpdateUI(
Rayan Kanso080a36d2018-04-10 09:53:23125 const BackgroundFetchRegistrationId& registration_id,
Rayan Kanso4b7e2572018-07-25 18:35:05126 const base::Optional<std::string>& title,
127 const base::Optional<SkBitmap>& icon,
Rayan Kansof41e81f92019-04-04 12:08:37128 blink::mojom::BackgroundFetchRegistrationService::UpdateUICallback
129 callback);
Dan Elphickbfd47f02017-11-03 15:43:34130
Mugdha Lakhanidbd756f2018-12-20 12:02:39131 BackgroundFetchRegistrationNotifier* registration_notifier() const {
132 return registration_notifier_.get();
133 }
134
Hiroki Nakagawab8fe7ff2019-12-11 04:21:28135 base::WeakPtr<BackgroundFetchContext> GetWeakPtr();
136
harkness7b36c432017-02-15 20:05:36137 private:
Mugdha Lakhani3bcbe722018-09-26 18:35:40138 using GetPermissionCallback =
139 base::OnceCallback<void(BackgroundFetchPermission)>;
Rayan Kanso697f60ca2018-08-23 08:10:00140
Peter Beverloo87886932018-07-25 15:34:17141 FRIEND_TEST_ALL_PREFIXES(BackgroundFetchServiceTest,
142 JobsInitializedOnBrowserRestart);
Rayan Kanso4d15c0632018-06-04 19:43:00143 friend class BackgroundFetchServiceTest;
Mugdha Lakhani9de44292018-06-07 16:14:53144 friend class BackgroundFetchJobControllerTest;
harkness35b3ce0d2017-03-09 21:36:53145 friend class base::DeleteHelper<BackgroundFetchContext>;
Matt Falkenhagen41ccef452019-08-28 14:19:02146 friend class base::RefCountedDeleteOnSequence<BackgroundFetchContext>;
harkness35b3ce0d2017-03-09 21:36:53147
Rayan Kanso1bfe9be2018-11-05 21:42:46148 ~BackgroundFetchContext();
harkness35b3ce0d2017-03-09 21:36:53149
Matt Falkenhagen41ccef452019-08-28 14:19:02150 void ShutdownOnCoreThread();
Rayan Kanso54422772018-06-22 19:07:05151
Dan Elphick746a67f2017-11-06 14:57:07152 // Called when an existing registration has been retrieved from the data
153 // manager. If the registration does not exist then |registration| is nullptr.
154 void DidGetRegistration(
155 blink::mojom::BackgroundFetchService::GetRegistrationCallback callback,
156 blink::mojom::BackgroundFetchError error,
Rayan Kansof41e81f92019-04-04 12:08:37157 BackgroundFetchRegistrationId registration_id,
158 blink::mojom::BackgroundFetchRegistrationDataPtr registration_data);
Dan Elphick746a67f2017-11-06 14:57:07159
peter8ea74ef32017-03-27 22:20:56160 // Called when a new registration has been created by the data manager.
161 void DidCreateRegistration(
162 const BackgroundFetchRegistrationId& registration_id,
John Mellor5438b0c2017-10-12 20:03:00163 blink::mojom::BackgroundFetchError error,
Rayan Kansof41e81f92019-04-04 12:08:37164 blink::mojom::BackgroundFetchRegistrationDataPtr registration_data);
peter8ea74ef32017-03-27 22:20:56165
Mugdha Lakhani90d1f5a2018-08-21 18:13:12166 // Called when the sequence of matching settled fetches have been received
167 // from storage, and |callback| can be invoked to pass these on to the
168 // renderer.
169 void DidGetMatchingRequests(
Mugdha Lakhanidbd756f2018-12-20 12:02:39170 const std::string& unique_id,
Rayan Kansof41e81f92019-04-04 12:08:37171 blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback
172 callback,
Mugdha Lakhani90d1f5a2018-08-21 18:13:12173 blink::mojom::BackgroundFetchError error,
Richard Lic6731e82018-12-13 05:24:05174 std::vector<blink::mojom::BackgroundFetchSettledFetchPtr>
175 settled_fetches);
Mugdha Lakhani90d1f5a2018-08-21 18:13:12176
Rayan Kansob5ce0582018-07-06 16:03:19177 // Called when the data manager finishes getting the initialization data.
178 void DidGetInitializationData(
179 blink::mojom::BackgroundFetchError error,
180 std::vector<background_fetch::BackgroundFetchInitializationData>
181 initialization_data);
182
Rayan Kanso4d15c0632018-06-04 19:43:00183 // Switches out |data_manager_| with a DataManager configured for testing
184 // environments. Must be called directly after the constructor.
185 void SetDataManagerForTesting(
186 std::unique_ptr<BackgroundFetchDataManager> data_manager);
187
Rayan Kanso697f60ca2018-08-23 08:10:00188 // Callback for GetPermissionForOrigin.
Mugdha Lakhani361cca92018-09-05 09:45:37189 void DidGetPermission(const BackgroundFetchRegistrationId& registration_id,
Richard Lifad69422018-11-09 02:26:41190 std::vector<blink::mojom::FetchAPIRequestPtr> requests,
Richard Lif74adb22018-12-05 06:58:19191 blink::mojom::BackgroundFetchOptionsPtr options,
Mugdha Lakhani361cca92018-09-05 09:45:37192 const SkBitmap& icon,
Mugdha Lakhani7431ee02018-09-07 12:15:26193 blink::mojom::BackgroundFetchUkmDataPtr ukm_data,
Mugdha Lakhani361cca92018-09-05 09:45:37194 int frame_tree_node_id,
Mugdha Lakhani3bcbe722018-09-26 18:35:40195 BackgroundFetchPermission permission);
Rayan Kanso697f60ca2018-08-23 08:10:00196
peter765cc1c82017-03-30 12:03:27197 // |this| is owned, indirectly, by the BrowserContext.
harkness35b3ce0d2017-03-09 21:36:53198 BrowserContext* browser_context_;
harkness7b36c432017-02-15 20:05:36199
Rayan Kanso4d15c0632018-06-04 19:43:00200 std::unique_ptr<BackgroundFetchDataManager> data_manager_;
Mugdha Lakhani9de44292018-06-07 16:14:53201 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
Rayan Kanso8cfef332019-05-08 16:25:48202 scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context_;
John Mellor5438b0c2017-10-12 20:03:00203 std::unique_ptr<BackgroundFetchRegistrationNotifier> registration_notifier_;
Dan Elphick975213182017-09-22 10:37:47204 BackgroundFetchDelegateProxy delegate_proxy_;
Dan Elphick14655852018-01-03 16:13:29205 std::unique_ptr<BackgroundFetchScheduler> scheduler_;
harkness7b36c432017-02-15 20:05:36206
Mugdha Lakhani9de44292018-06-07 16:14:53207 // Map from BackgroundFetchRegistrationIds to FetchCallbacks for active
208 // fetches. Must be destroyed before |data_manager_| and
209 // |registration_notifier_|. Since FetchCallback is a OnceCallback, please
210 // erase the map entry once the calback has been invoked.
211 std::map<BackgroundFetchRegistrationId,
212 blink::mojom::BackgroundFetchService::FetchCallback>
213 fetch_callbacks_;
214
Jeremy Roman3bca4bf2019-07-11 03:41:25215 base::WeakPtrFactory<BackgroundFetchContext> weak_factory_{
216 this}; // Must be last.
johnme57400c12017-07-11 15:56:35217
harkness7b36c432017-02-15 20:05:36218 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchContext);
219};
220
221} // namespace content
222
223#endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_