OLD | NEW |
(Empty) | |
| 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 CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
| 6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <map> |
| 10 |
| 11 #include "base/callback_forward.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "content/browser/service_worker/service_worker_cache_scheduler.h" |
| 15 #include "content/browser/service_worker/service_worker_storage.h" |
| 16 #include "content/common/content_export.h" |
| 17 #include "content/common/service_worker/service_worker_status_code.h" |
| 18 #include "url/gurl.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 class ServiceWorkerContextWrapper; |
| 23 |
| 24 // BackgroundSyncManager manages and stores the set of background sync |
| 25 // registrations across all registered service workers for a profile. |
| 26 // Registrations are stored along with their associated Service Worker |
| 27 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, |
| 28 // the sync registrations are removed. This class expects to be run on the IO |
| 29 // thread. The asynchronous methods are executed sequentially. |
| 30 |
| 31 // TODO(jkarlin): Check permissions when registering, scheduling, and firing |
| 32 // background sync. In the meantime, --enable-service-worker-sync is required to |
| 33 // fire a sync event. |
| 34 // TODO(jkarlin): Unregister syncs when permission is revoked. |
| 35 // TODO(jkarlin): Create a background sync scheduler to actually run the |
| 36 // registered events. |
| 37 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the |
| 38 // Background" is true and a sync is registered. |
| 39 // TODO(jkarlin): Unregister syncs when storage for an origin is cleared. |
| 40 // TODO(jkarlin): Detect and handle a corrupt or broken backend. |
| 41 class CONTENT_EXPORT BackgroundSyncManager { |
| 42 public: |
| 43 enum ErrorType { |
| 44 ERROR_TYPE_OK = 0, |
| 45 ERROR_TYPE_STORAGE, |
| 46 ERROR_TYPE_NOT_FOUND |
| 47 }; |
| 48 |
| 49 // TODO(jkarlin): Remove this and use the struct from IPC messages once it |
| 50 // lands. |
| 51 struct CONTENT_EXPORT BackgroundSyncRegistration { |
| 52 using RegistrationId = int64; |
| 53 static const RegistrationId kInvalidRegistrationId; |
| 54 |
| 55 BackgroundSyncRegistration() |
| 56 : BackgroundSyncRegistration(kInvalidRegistrationId, "") {} |
| 57 explicit BackgroundSyncRegistration(const std::string& name) |
| 58 : BackgroundSyncRegistration(kInvalidRegistrationId, name) {} |
| 59 BackgroundSyncRegistration(int64 id, const std::string& name) |
| 60 : id(id), min_period(0), name(name) {} |
| 61 |
| 62 bool Equals(const BackgroundSyncRegistration& other) { |
| 63 return this->name == other.name && this->min_period == other.min_period; |
| 64 } |
| 65 |
| 66 RegistrationId id; |
| 67 int64 min_period; |
| 68 std::string name; |
| 69 }; |
| 70 |
| 71 struct CONTENT_EXPORT BackgroundSyncRegistrations { |
| 72 using NameToRegistrationMap = |
| 73 std::map<std::string, BackgroundSyncRegistration>; |
| 74 static const BackgroundSyncRegistration::RegistrationId kInitialId; |
| 75 |
| 76 BackgroundSyncRegistrations(); |
| 77 explicit BackgroundSyncRegistrations( |
| 78 BackgroundSyncRegistration::RegistrationId next_id); |
| 79 ~BackgroundSyncRegistrations(); |
| 80 |
| 81 NameToRegistrationMap name_to_registration_map; |
| 82 BackgroundSyncRegistration::RegistrationId next_id; |
| 83 }; |
| 84 |
| 85 using StatusCallback = base::Callback<void(ErrorType)>; |
| 86 using StatusAndRegistrationCallback = |
| 87 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; |
| 88 |
| 89 static scoped_ptr<BackgroundSyncManager> Create( |
| 90 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| 91 virtual ~BackgroundSyncManager(); |
| 92 |
| 93 // Stores the given background sync registration and adds it to the scheduling |
| 94 // queue. Overwrites any existing registration with the same name but |
| 95 // different parameters (other than the id). Calls |callback| with ErrorTypeOK |
| 96 // and the accepted registration on success. The accepted registration will |
| 97 // have a unique id. It may also have altered parameters if the user or UA |
| 98 // chose different parameters than those supplied. |
| 99 void Register(const GURL& origin, |
| 100 int64 sw_registration_id, |
| 101 const BackgroundSyncRegistration& sync_registration, |
| 102 const StatusAndRegistrationCallback& callback); |
| 103 |
| 104 // Removes the background sync registration with |sync_registration_name| if |
| 105 // the |sync_registration_id| matches. |sync_registration_id| will not match |
| 106 // if, for instance, a new registration with the same name has replaced it. |
| 107 // Calls |callback| with ErrorTypeNotFound if no match is found. Calls |
| 108 // |callback| with ErrorTypeOK on success. |
| 109 void Unregister( |
| 110 const GURL& origin, |
| 111 int64 sw_registration_id, |
| 112 const std::string& sync_registration_name, |
| 113 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 114 const StatusCallback& callback); |
| 115 |
| 116 // Finds the background sync registration associated with |
| 117 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't |
| 118 // exist. Calls |callback| with ErrorTypeOK on success. |
| 119 void GetRegistration(const GURL& origin, |
| 120 int64 sw_registration_id, |
| 121 const std::string sync_registration_name, |
| 122 const StatusAndRegistrationCallback& callback); |
| 123 |
| 124 protected: |
| 125 explicit BackgroundSyncManager( |
| 126 const scoped_refptr<ServiceWorkerContextWrapper>& context); |
| 127 |
| 128 // Init must be called before any public member function. Only call it once. |
| 129 void Init(); |
| 130 |
| 131 // The following methods are virtual for testing. |
| 132 virtual void StoreDataInBackend( |
| 133 int64 sw_registration_id, |
| 134 const GURL& origin, |
| 135 const std::string& key, |
| 136 const std::string& data, |
| 137 const ServiceWorkerStorage::StatusCallback& callback); |
| 138 virtual void GetDataFromBackend( |
| 139 const std::string& key, |
| 140 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 141 callback); |
| 142 |
| 143 private: |
| 144 using PermissionStatusCallback = base::Callback<void(bool)>; |
| 145 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>; |
| 146 |
| 147 // Returns the existing registration in |existing_registration| if it is not |
| 148 // null. |
| 149 bool LookupRegistration(int64 sw_registration_id, |
| 150 const std::string& sync_registration_name, |
| 151 BackgroundSyncRegistration* existing_registration); |
| 152 |
| 153 // Store all registrations for a given |sw_registration_id|. |
| 154 void StoreRegistrations(const GURL& origin, |
| 155 int64 sw_registration_id, |
| 156 const ServiceWorkerStorage::StatusCallback& callback); |
| 157 |
| 158 // If the registration is in the map, removes it and returns the removed |
| 159 // registration in |old_registration|. |old_registration| may be null. |
| 160 void RemoveRegistrationFromMap(int64 sw_registration_id, |
| 161 const std::string& sync_registration_name, |
| 162 BackgroundSyncRegistration* old_registration); |
| 163 |
| 164 void AddRegistrationToMap( |
| 165 int64 sw_registration_id, |
| 166 const BackgroundSyncRegistration& sync_registration); |
| 167 |
| 168 void InitImpl(); |
| 169 void InitDidGetDataFromBackend( |
| 170 const std::vector<std::pair<int64, std::string>>& user_data, |
| 171 ServiceWorkerStatusCode status); |
| 172 |
| 173 // Register callbacks |
| 174 void RegisterImpl(const GURL& origin, |
| 175 int64 sw_registration_id, |
| 176 const BackgroundSyncRegistration& sync_registration, |
| 177 const StatusAndRegistrationCallback& callback); |
| 178 void RegisterDidStore(int64 sw_registration_id, |
| 179 const BackgroundSyncRegistration& sync_registration, |
| 180 const BackgroundSyncRegistration& previous_registration, |
| 181 const StatusAndRegistrationCallback& callback, |
| 182 ServiceWorkerStatusCode status); |
| 183 |
| 184 // Unregister callbacks |
| 185 void UnregisterImpl( |
| 186 const GURL& origin, |
| 187 int64 sw_registration_id, |
| 188 const std::string& sync_registration_name, |
| 189 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 190 const StatusCallback& callback); |
| 191 void UnregisterDidStore( |
| 192 int64 sw_registration_id, |
| 193 const BackgroundSyncRegistration& old_sync_registration, |
| 194 const StatusCallback& callback, |
| 195 ServiceWorkerStatusCode status); |
| 196 |
| 197 // GetRegistration callbacks |
| 198 void GetRegistrationImpl(const GURL& origin, |
| 199 int64 sw_registration_id, |
| 200 const std::string sync_registration_name, |
| 201 const StatusAndRegistrationCallback& callback); |
| 202 |
| 203 // Operation Scheduling callbacks |
| 204 void PendingStatusAndRegistrationCallback( |
| 205 const StatusAndRegistrationCallback& callback, |
| 206 ErrorType error, |
| 207 const BackgroundSyncRegistration& sync_registration); |
| 208 void PendingStatusCallback(const StatusCallback& callback, ErrorType error); |
| 209 |
| 210 SWIdToRegistrationsMap sw_to_registrations_map_; |
| 211 ServiceWorkerCacheScheduler op_scheduler_; |
| 212 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 213 |
| 214 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
| 215 |
| 216 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
| 217 }; |
| 218 |
| 219 } // namespace content |
| 220 |
| 221 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
OLD | NEW |