harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 1 | // 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 | |
peter | 8ea74ef3 | 2017-03-27 22:20:56 | [diff] [blame] | 8 | #include <map> |
Dan Elphick | 7c245b2 | 2017-07-27 16:35:52 | [diff] [blame] | 9 | #include <memory> |
peter | 16b9360 | 2017-03-28 15:14:29 | [diff] [blame] | 10 | #include <string> |
peter | 8ea74ef3 | 2017-03-27 22:20:56 | [diff] [blame] | 11 | #include <vector> |
harkness | 35b3ce0d | 2017-03-09 21:36:53 | [diff] [blame] | 12 | |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 13 | #include "base/macros.h" |
Matt Falkenhagen | 41ccef45 | 2019-08-28 14:19:02 | [diff] [blame] | 14 | #include "base/memory/ref_counted_delete_on_sequence.h" |
johnme | 57400c1 | 2017-07-11 15:56:35 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Rayan Kanso | 4b7e257 | 2018-07-25 18:35:05 | [diff] [blame] | 16 | #include "base/optional.h" |
Dan Elphick | 97521318 | 2017-09-22 10:37:47 | [diff] [blame] | 17 | #include "content/browser/background_fetch/background_fetch_delegate_proxy.h" |
John Mellor | 4119fed5 | 2017-09-18 20:20:53 | [diff] [blame] | 18 | #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" |
Rayan Kanso | b5ce058 | 2018-07-06 16:03:19 | [diff] [blame] | 19 | #include "content/browser/background_fetch/storage/get_initialization_data_task.h" |
Rayan Kanso | 8cfef33 | 2019-05-08 16:25:48 | [diff] [blame] | 20 | #include "content/browser/devtools/devtools_background_services_context_impl.h" |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 21 | #include "content/common/content_export.h" |
harkness | 35b3ce0d | 2017-03-09 21:36:53 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
Richard Li | 912523f | 2018-12-13 08:10:40 | [diff] [blame] | 23 | #include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h" |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 24 | |
Peter Beverloo | 3f5b892 | 2017-11-01 13:22:15 | [diff] [blame] | 25 | namespace storage { |
Rayan Kanso | 77bf192 | 2018-08-17 16:44:23 | [diff] [blame] | 26 | class QuotaManagerProxy; |
Rayan Kanso | d274cb9 | 2019-03-11 15:59:41 | [diff] [blame] | 27 | } // namespace storage |
Peter Beverloo | 3f5b892 | 2017-11-01 13:22:15 | [diff] [blame] | 28 | |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 29 | namespace content { |
| 30 | |
Rayan Kanso | 4d15c063 | 2018-06-04 19:43:00 | [diff] [blame] | 31 | class BackgroundFetchDataManager; |
peter | 8ea74ef3 | 2017-03-27 22:20:56 | [diff] [blame] | 32 | class BackgroundFetchRegistrationId; |
Peter Beverloo | 83ee664 | 2017-10-11 20:23:17 | [diff] [blame] | 33 | class BackgroundFetchRegistrationNotifier; |
Mugdha Lakhani | 90d1f5a | 2018-08-21 18:13:12 | [diff] [blame] | 34 | class BackgroundFetchRequestMatchParams; |
Dan Elphick | 1465585 | 2018-01-03 16:13:29 | [diff] [blame] | 35 | class BackgroundFetchScheduler; |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 36 | class BrowserContext; |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 37 | class ServiceWorkerContextWrapper; |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 38 | |
| 39 | // The BackgroundFetchContext is the central moderator of ongoing background |
| 40 | // fetch requests from the Mojo service and from other callers. |
johnme | 57400c1 | 2017-07-11 15:56:35 | [diff] [blame] | 41 | // Background Fetch requests function similarly to normal fetches except that |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 42 | // they are persistent across Chromium or service worker shutdown. |
Matt Falkenhagen | 41ccef45 | 2019-08-28 14:19:02 | [diff] [blame] | 43 | // |
| 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. |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 47 | class CONTENT_EXPORT BackgroundFetchContext |
Matt Falkenhagen | 41ccef45 | 2019-08-28 14:19:02 | [diff] [blame] | 48 | : public base::RefCountedDeleteOnSequence<BackgroundFetchContext> { |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 49 | public: |
| 50 | // The BackgroundFetchContext will watch the ServiceWorkerContextWrapper so |
| 51 | // that it can respond to service worker events such as unregister. |
John Mellor | 4018b9b | 2017-08-25 16:36:27 | [diff] [blame] | 52 | BackgroundFetchContext( |
| 53 | BrowserContext* browser_context, |
Adrienne Walker | 99a61f3 | 2021-01-22 12:31:56 | [diff] [blame] | 54 | StoragePartition* storage_partition, |
Rayan Kanso | ecd2331 | 2018-05-29 15:36:41 | [diff] [blame] | 55 | const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context, |
Rayan Kanso | d274cb9 | 2019-03-11 15:59:41 | [diff] [blame] | 56 | scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
Rayan Kanso | 8cfef33 | 2019-05-08 16:25:48 | [diff] [blame] | 57 | scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context); |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 58 | |
Matt Falkenhagen | 41ccef45 | 2019-08-28 14:19:02 | [diff] [blame] | 59 | void InitializeOnCoreThread(); |
Mugdha Lakhani | 9de4429 | 2018-06-07 16:14:53 | [diff] [blame] | 60 | |
Rayan Kanso | 5442277 | 2018-06-22 19:07:05 | [diff] [blame] | 61 | // Called by the StoragePartitionImpl destructor. |
| 62 | void Shutdown(); |
| 63 | |
Dan Elphick | 746a67f | 2017-11-06 14:57:07 | [diff] [blame] | 64 | // 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 Elphick | d28d18a | 2017-11-07 14:26:37 | [diff] [blame] | 73 | // 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 | |
peter | 8ea74ef3 | 2017-03-27 22:20:56 | [diff] [blame] | 80 | // 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 Elphick | bfd47f0 | 2017-11-03 15:43:34 | [diff] [blame] | 82 | // been registered, or an error occurred that prevents it from doing so. |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 83 | void StartFetch(const BackgroundFetchRegistrationId& registration_id, |
Richard Li | fad6942 | 2018-11-09 02:26:41 | [diff] [blame] | 84 | std::vector<blink::mojom::FetchAPIRequestPtr> requests, |
Richard Li | f74adb2 | 2018-12-05 06:58:19 | [diff] [blame] | 85 | blink::mojom::BackgroundFetchOptionsPtr options, |
Mugdha Lakhani | 4de0e19e | 2018-03-15 13:07:32 | [diff] [blame] | 86 | const SkBitmap& icon, |
Mugdha Lakhani | 7431ee0 | 2018-09-07 12:15:26 | [diff] [blame] | 87 | blink::mojom::BackgroundFetchUkmDataPtr ukm_data, |
Rayan Kanso | 997f496c | 2018-12-18 16:28:29 | [diff] [blame] | 88 | int render_frame_tree_node_id, |
John Abd-El-Malek | 92bf360 | 2019-07-31 02:25:48 | [diff] [blame] | 89 | const WebContents::Getter& wc_getter, |
tzik | cf7bcd65 | 2017-06-15 04:19:30 | [diff] [blame] | 90 | blink::mojom::BackgroundFetchService::FetchCallback callback); |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 91 | |
Mugdha Lakhani | 31a74e6 | 2018-03-27 16:03:58 | [diff] [blame] | 92 | // Gets display size for the icon for Background Fetch UI. |
| 93 | void GetIconDisplaySize( |
| 94 | blink::mojom::BackgroundFetchService::GetIconDisplaySizeCallback |
| 95 | callback); |
| 96 | |
Mugdha Lakhani | 90d1f5a | 2018-08-21 18:13:12 | [diff] [blame] | 97 | // 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 Kanso | f41e81f9 | 2019-04-04 12:08:37 | [diff] [blame] | 101 | blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback |
| 102 | callback); |
Mugdha Lakhani | 90d1f5a | 2018-08-21 18:13:12 | [diff] [blame] | 103 | |
Dan Elphick | 553789d | 2017-10-26 22:36:38 | [diff] [blame] | 104 | // 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 Kanso | f41e81f9 | 2019-04-04 12:08:37 | [diff] [blame] | 107 | void Abort( |
| 108 | const BackgroundFetchRegistrationId& registration_id, |
| 109 | blink::mojom::BackgroundFetchRegistrationService::AbortCallback callback); |
peter | 16b9360 | 2017-03-28 15:14:29 | [diff] [blame] | 110 | |
Peter Beverloo | 83ee664 | 2017-10-11 20:23:17 | [diff] [blame] | 111 | // 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 Kim | f01877d | 2019-08-08 00:43:02 | [diff] [blame] | 116 | mojo::PendingRemote<blink::mojom::BackgroundFetchRegistrationObserver> |
| 117 | observer); |
Peter Beverloo | 83ee664 | 2017-10-11 20:23:17 | [diff] [blame] | 118 | |
Rayan Kanso | 4b7e257 | 2018-07-25 18:35:05 | [diff] [blame] | 119 | // 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 Elphick | e11715a | 2017-11-06 15:59:49 | [diff] [blame] | 124 | void UpdateUI( |
Rayan Kanso | 080a36d | 2018-04-10 09:53:23 | [diff] [blame] | 125 | const BackgroundFetchRegistrationId& registration_id, |
Rayan Kanso | 4b7e257 | 2018-07-25 18:35:05 | [diff] [blame] | 126 | const base::Optional<std::string>& title, |
| 127 | const base::Optional<SkBitmap>& icon, |
Rayan Kanso | f41e81f9 | 2019-04-04 12:08:37 | [diff] [blame] | 128 | blink::mojom::BackgroundFetchRegistrationService::UpdateUICallback |
| 129 | callback); |
Dan Elphick | bfd47f0 | 2017-11-03 15:43:34 | [diff] [blame] | 130 | |
Mugdha Lakhani | dbd756f | 2018-12-20 12:02:39 | [diff] [blame] | 131 | BackgroundFetchRegistrationNotifier* registration_notifier() const { |
| 132 | return registration_notifier_.get(); |
| 133 | } |
| 134 | |
Hiroki Nakagawa | b8fe7ff | 2019-12-11 04:21:28 | [diff] [blame] | 135 | base::WeakPtr<BackgroundFetchContext> GetWeakPtr(); |
| 136 | |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 137 | private: |
Mugdha Lakhani | 3bcbe72 | 2018-09-26 18:35:40 | [diff] [blame] | 138 | using GetPermissionCallback = |
| 139 | base::OnceCallback<void(BackgroundFetchPermission)>; |
Rayan Kanso | 697f60ca | 2018-08-23 08:10:00 | [diff] [blame] | 140 | |
Peter Beverloo | 8788693 | 2018-07-25 15:34:17 | [diff] [blame] | 141 | FRIEND_TEST_ALL_PREFIXES(BackgroundFetchServiceTest, |
| 142 | JobsInitializedOnBrowserRestart); |
Rayan Kanso | 4d15c063 | 2018-06-04 19:43:00 | [diff] [blame] | 143 | friend class BackgroundFetchServiceTest; |
Mugdha Lakhani | 9de4429 | 2018-06-07 16:14:53 | [diff] [blame] | 144 | friend class BackgroundFetchJobControllerTest; |
harkness | 35b3ce0d | 2017-03-09 21:36:53 | [diff] [blame] | 145 | friend class base::DeleteHelper<BackgroundFetchContext>; |
Matt Falkenhagen | 41ccef45 | 2019-08-28 14:19:02 | [diff] [blame] | 146 | friend class base::RefCountedDeleteOnSequence<BackgroundFetchContext>; |
harkness | 35b3ce0d | 2017-03-09 21:36:53 | [diff] [blame] | 147 | |
Rayan Kanso | 1bfe9be | 2018-11-05 21:42:46 | [diff] [blame] | 148 | ~BackgroundFetchContext(); |
harkness | 35b3ce0d | 2017-03-09 21:36:53 | [diff] [blame] | 149 | |
Matt Falkenhagen | 41ccef45 | 2019-08-28 14:19:02 | [diff] [blame] | 150 | void ShutdownOnCoreThread(); |
Rayan Kanso | 5442277 | 2018-06-22 19:07:05 | [diff] [blame] | 151 | |
Dan Elphick | 746a67f | 2017-11-06 14:57:07 | [diff] [blame] | 152 | // 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 Kanso | f41e81f9 | 2019-04-04 12:08:37 | [diff] [blame] | 157 | BackgroundFetchRegistrationId registration_id, |
| 158 | blink::mojom::BackgroundFetchRegistrationDataPtr registration_data); |
Dan Elphick | 746a67f | 2017-11-06 14:57:07 | [diff] [blame] | 159 | |
peter | 8ea74ef3 | 2017-03-27 22:20:56 | [diff] [blame] | 160 | // Called when a new registration has been created by the data manager. |
| 161 | void DidCreateRegistration( |
| 162 | const BackgroundFetchRegistrationId& registration_id, |
John Mellor | 5438b0c | 2017-10-12 20:03:00 | [diff] [blame] | 163 | blink::mojom::BackgroundFetchError error, |
Rayan Kanso | f41e81f9 | 2019-04-04 12:08:37 | [diff] [blame] | 164 | blink::mojom::BackgroundFetchRegistrationDataPtr registration_data); |
peter | 8ea74ef3 | 2017-03-27 22:20:56 | [diff] [blame] | 165 | |
Mugdha Lakhani | 90d1f5a | 2018-08-21 18:13:12 | [diff] [blame] | 166 | // 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 Lakhani | dbd756f | 2018-12-20 12:02:39 | [diff] [blame] | 170 | const std::string& unique_id, |
Rayan Kanso | f41e81f9 | 2019-04-04 12:08:37 | [diff] [blame] | 171 | blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback |
| 172 | callback, |
Mugdha Lakhani | 90d1f5a | 2018-08-21 18:13:12 | [diff] [blame] | 173 | blink::mojom::BackgroundFetchError error, |
Richard Li | c6731e8 | 2018-12-13 05:24:05 | [diff] [blame] | 174 | std::vector<blink::mojom::BackgroundFetchSettledFetchPtr> |
| 175 | settled_fetches); |
Mugdha Lakhani | 90d1f5a | 2018-08-21 18:13:12 | [diff] [blame] | 176 | |
Rayan Kanso | b5ce058 | 2018-07-06 16:03:19 | [diff] [blame] | 177 | // 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 Kanso | 4d15c063 | 2018-06-04 19:43:00 | [diff] [blame] | 183 | // 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 Kanso | 697f60ca | 2018-08-23 08:10:00 | [diff] [blame] | 188 | // Callback for GetPermissionForOrigin. |
Mugdha Lakhani | 361cca9 | 2018-09-05 09:45:37 | [diff] [blame] | 189 | void DidGetPermission(const BackgroundFetchRegistrationId& registration_id, |
Richard Li | fad6942 | 2018-11-09 02:26:41 | [diff] [blame] | 190 | std::vector<blink::mojom::FetchAPIRequestPtr> requests, |
Richard Li | f74adb2 | 2018-12-05 06:58:19 | [diff] [blame] | 191 | blink::mojom::BackgroundFetchOptionsPtr options, |
Mugdha Lakhani | 361cca9 | 2018-09-05 09:45:37 | [diff] [blame] | 192 | const SkBitmap& icon, |
Mugdha Lakhani | 7431ee0 | 2018-09-07 12:15:26 | [diff] [blame] | 193 | blink::mojom::BackgroundFetchUkmDataPtr ukm_data, |
Mugdha Lakhani | 361cca9 | 2018-09-05 09:45:37 | [diff] [blame] | 194 | int frame_tree_node_id, |
Mugdha Lakhani | 3bcbe72 | 2018-09-26 18:35:40 | [diff] [blame] | 195 | BackgroundFetchPermission permission); |
Rayan Kanso | 697f60ca | 2018-08-23 08:10:00 | [diff] [blame] | 196 | |
peter | 765cc1c8 | 2017-03-30 12:03:27 | [diff] [blame] | 197 | // |this| is owned, indirectly, by the BrowserContext. |
harkness | 35b3ce0d | 2017-03-09 21:36:53 | [diff] [blame] | 198 | BrowserContext* browser_context_; |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 199 | |
Rayan Kanso | 4d15c063 | 2018-06-04 19:43:00 | [diff] [blame] | 200 | std::unique_ptr<BackgroundFetchDataManager> data_manager_; |
Mugdha Lakhani | 9de4429 | 2018-06-07 16:14:53 | [diff] [blame] | 201 | scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
Rayan Kanso | 8cfef33 | 2019-05-08 16:25:48 | [diff] [blame] | 202 | scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context_; |
John Mellor | 5438b0c | 2017-10-12 20:03:00 | [diff] [blame] | 203 | std::unique_ptr<BackgroundFetchRegistrationNotifier> registration_notifier_; |
Dan Elphick | 97521318 | 2017-09-22 10:37:47 | [diff] [blame] | 204 | BackgroundFetchDelegateProxy delegate_proxy_; |
Dan Elphick | 1465585 | 2018-01-03 16:13:29 | [diff] [blame] | 205 | std::unique_ptr<BackgroundFetchScheduler> scheduler_; |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 206 | |
Mugdha Lakhani | 9de4429 | 2018-06-07 16:14:53 | [diff] [blame] | 207 | // 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 Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 215 | base::WeakPtrFactory<BackgroundFetchContext> weak_factory_{ |
| 216 | this}; // Must be last. |
johnme | 57400c1 | 2017-07-11 15:56:35 | [diff] [blame] | 217 | |
harkness | 7b36c43 | 2017-02-15 20:05:36 | [diff] [blame] | 218 | DISALLOW_COPY_AND_ASSIGN(BackgroundFetchContext); |
| 219 | }; |
| 220 | |
| 221 | } // namespace content |
| 222 | |
| 223 | #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_ |