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 class CONTENT_EXPORT BackgroundSyncManager { |
| 41 public: |
| 42 enum ErrorType { |
| 43 ERROR_TYPE_OK = 0, |
| 44 ERROR_TYPE_STORAGE, |
| 45 ERROR_TYPE_NOT_FOUND |
| 46 }; |
| 47 |
| 48 // TODO(jkarlin): Remove this and use the struct from IPC messages once it |
| 49 // lands. |
| 50 struct BackgroundSyncRegistration { |
| 51 using RegistrationId = int64; |
| 52 static const RegistrationId kInvalidRegistrationId; |
| 53 |
| 54 BackgroundSyncRegistration() |
| 55 : BackgroundSyncRegistration(kInvalidRegistrationId, "") {} |
| 56 explicit BackgroundSyncRegistration(const std::string& name) |
| 57 : BackgroundSyncRegistration(kInvalidRegistrationId, name) {} |
| 58 BackgroundSyncRegistration(int64 id, const std::string& name) |
| 59 : id(id), min_period(0), name(name) {} |
| 60 |
| 61 bool Equals(const BackgroundSyncRegistration& other) { |
| 62 return this->name == other.name && this->min_period == other.min_period; |
| 63 } |
| 64 |
| 65 RegistrationId id; |
| 66 int64 min_period; |
| 67 std::string name; |
| 68 }; |
| 69 |
| 70 struct BackgroundSyncRegistrations { |
| 71 using NameToRegistrationMap = |
| 72 std::map<std::string, BackgroundSyncRegistration>; |
| 73 static const BackgroundSyncRegistration::RegistrationId kInitialId; |
| 74 |
| 75 BackgroundSyncRegistrations(); |
| 76 explicit BackgroundSyncRegistrations( |
| 77 BackgroundSyncRegistration::RegistrationId next_id); |
| 78 ~BackgroundSyncRegistrations(); |
| 79 |
| 80 NameToRegistrationMap name_to_registration_map; |
| 81 BackgroundSyncRegistration::RegistrationId next_id; |
| 82 }; |
| 83 |
| 84 using StatusCallback = base::Callback<void(ErrorType)>; |
| 85 using StatusAndRegistrationCallback = |
| 86 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; |
| 87 |
| 88 static scoped_ptr<BackgroundSyncManager> Create( |
| 89 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| 90 virtual ~BackgroundSyncManager(); |
| 91 |
| 92 // Stores the given background sync registration and adds it to the scheduling |
| 93 // queue. Overwrites any existing registration with the same name but |
| 94 // different parameters (other than the id). Calls |callback| with ErrorTypeOK |
| 95 // and the accepted registration on success. The accepted registration will |
| 96 // have a unique id. It may also have altered parameters if the user or UA |
| 97 // chose different parameters than those supplied. |
| 98 void Register(const GURL& origin, |
| 99 int64 sw_registration_id, |
| 100 const BackgroundSyncRegistration& sync_registration, |
| 101 const StatusAndRegistrationCallback& callback); |
| 102 |
| 103 // Removes the background sync registration with |sync_registration_name| if |
| 104 // the |sync_registration_id| matches. |sync_registration_id| will not match |
| 105 // if, for instance, a new registration with the same name has replaced it. |
| 106 // Calls |callback| with ErrorTypeNotFound if no match is found. Calls |
| 107 // |callback| with ErrorTypeOK on success. |
| 108 void Unregister( |
| 109 const GURL& origin, |
| 110 int64 sw_registration_id, |
| 111 const std::string& sync_registration_name, |
| 112 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 113 const StatusCallback& callback); |
| 114 |
| 115 // Finds the background sync registration associated with |
| 116 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't |
| 117 // exist. Calls |callback| with ErrorTypeOK on success. |
| 118 void GetRegistration(const GURL& origin, |
| 119 int64 sw_registration_id, |
| 120 const std::string sync_registration_name, |
| 121 const StatusAndRegistrationCallback& callback); |
| 122 |
| 123 protected: |
| 124 explicit BackgroundSyncManager( |
| 125 const scoped_refptr<ServiceWorkerContextWrapper>& context); |
| 126 |
| 127 // Init must be called before any public member function. Only call it once. |
| 128 void Init(); |
| 129 |
| 130 // The following methods are virtual for testing. |
| 131 virtual void StoreDataInBackend( |
| 132 int64 sw_registration_id, |
| 133 const GURL& origin, |
| 134 const std::string& key, |
| 135 const std::string& data, |
| 136 const ServiceWorkerStorage::StatusCallback& callback); |
| 137 virtual void GetDataFromBackend( |
| 138 const std::string& key, |
| 139 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 140 callback); |
| 141 |
| 142 private: |
| 143 using PermissionStatusCallback = base::Callback<void(bool)>; |
| 144 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>; |
| 145 |
| 146 // Returns the existing registration in |existing_registration| if it is not |
| 147 // null. |
| 148 bool LookupRegistration(int64 sw_registration_id, |
| 149 const std::string& sync_registration_name, |
| 150 BackgroundSyncRegistration* existing_registration); |
| 151 |
| 152 // Store all registrations for a given |sw_registration_id|. |
| 153 void StoreRegistrations(const GURL& origin, |
| 154 int64 sw_registration_id, |
| 155 const ServiceWorkerStorage::StatusCallback& callback); |
| 156 |
| 157 // If the registration is in the map, removes it and returns the removed |
| 158 // registration in |old_registration|. |old_registration| may be null. |
| 159 void RemoveRegistrationFromMap(int64 sw_registration_id, |
| 160 const std::string& sync_registration_name, |
| 161 BackgroundSyncRegistration* old_registration); |
| 162 |
| 163 void AddRegistrationToMap( |
| 164 int64 sw_registration_id, |
| 165 const BackgroundSyncRegistration& sync_registration); |
| 166 |
| 167 void InitImpl(); |
| 168 void InitDidGetDataFromBackend( |
| 169 const std::vector<std::pair<int64, std::string>>& user_data, |
| 170 ServiceWorkerStatusCode status); |
| 171 |
| 172 // Register callbacks |
| 173 void RegisterImpl(const GURL& origin, |
| 174 int64 sw_registration_id, |
| 175 const BackgroundSyncRegistration& sync_registration, |
| 176 const StatusAndRegistrationCallback& callback); |
| 177 void RegisterDidStore(int64 sw_registration_id, |
| 178 const BackgroundSyncRegistration& sync_registration, |
| 179 const BackgroundSyncRegistration& previous_registration, |
| 180 const StatusAndRegistrationCallback& callback, |
| 181 ServiceWorkerStatusCode status); |
| 182 |
| 183 // Unregister callbacks |
| 184 void UnregisterImpl( |
| 185 const GURL& origin, |
| 186 int64 sw_registration_id, |
| 187 const std::string& sync_registration_name, |
| 188 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
| 189 const StatusCallback& callback); |
| 190 void UnregisterDidStore( |
| 191 int64 sw_registration_id, |
| 192 const BackgroundSyncRegistration& old_sync_registration, |
| 193 const StatusCallback& callback, |
| 194 ServiceWorkerStatusCode status); |
| 195 |
| 196 // GetRegistration callbacks |
| 197 void GetRegistrationImpl(const GURL& origin, |
| 198 int64 sw_registration_id, |
| 199 const std::string sync_registration_name, |
| 200 const StatusAndRegistrationCallback& callback); |
| 201 |
| 202 // Operation Scheduling callbacks |
| 203 void PendingStatusAndRegistrationCallback( |
| 204 const StatusAndRegistrationCallback& callback, |
| 205 ErrorType error, |
| 206 const BackgroundSyncRegistration& sync_registration); |
| 207 void PendingStatusCallback(const StatusCallback& callback, ErrorType error); |
| 208 |
| 209 SWIdToRegistrationsMap sw_to_registrations_map_; |
| 210 ServiceWorkerCacheScheduler op_scheduler_; |
| 211 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 212 |
| 213 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
| 214 |
| 215 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
| 216 }; |
| 217 |
| 218 } // namespace content |
| 219 |
| 220 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
OLD | NEW |