Chromium Code Reviews| 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.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/strings/string16.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 ServiceWorkerCacheScheduler; | |
| 23 class ServiceWorkerContextWrapper; | |
| 24 | |
| 25 // BackgroundSyncManager manages and stores the set of background sync | |
| 26 // registrations across all registered service workers for a profile. | |
| 27 // Registrations are stored along with their associated Service Worker | |
| 28 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, | |
| 29 // the sync registrations are removed. This class expects to be run on the IO | |
| 30 // thread. The asynchronous methods are executed sequentially. | |
| 31 | |
| 32 // TODO(jkarlin): Check permissions when registering, scheduling, and firing | |
| 33 // background sync. In the meantime, --enable-service-worker-sync is required to | |
|
michaeln
2015/02/27 02:56:30
Any reason to not use switches::kEnableExperimenta
jkarlin
2015/02/27 16:30:38
We have that as well. Is kEnableExperimentalWebPla
michaeln
2015/02/28 00:34:23
I think it's ok to put this behind the big flag. I
jkarlin
2015/03/02 14:41:03
Acknowledged.
| |
| 34 // fire a sync event. | |
| 35 // TODO(jkarlin): Unregister syncs when permission is revoked. | |
| 36 // TODO(jkarlin): Create a background sync scheduler to actually run the | |
| 37 // registered events. | |
| 38 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the | |
| 39 // Background" is true and a sync is registered. | |
| 40 // TODO(jkarlin): Unregister syncs when storage for an origin is cleared. | |
| 41 class CONTENT_EXPORT BackgroundSyncManager { | |
|
michaeln
2015/02/27 02:56:30
I wonder if this is a good candidate for using moj
jkarlin
2015/02/27 16:30:38
I think Ian (iclelland@) has already written a fai
michaeln
2015/02/28 00:34:23
that makes sense
jkarlin
2015/03/02 14:41:03
Acknowledged.
| |
| 42 public: | |
| 43 enum ErrorType { | |
| 44 ErrorTypeOK = 0, | |
| 45 ErrorTypeExists, | |
| 46 ErrorTypeStorage, | |
| 47 ErrorTypeNotFound | |
| 48 }; | |
| 49 | |
| 50 // TODO(jkarlin): Remove this and use the struct from IPC messages once it | |
| 51 // lands. | |
| 52 struct BackgroundSyncRegistration { | |
| 53 BackgroundSyncRegistration() {} | |
| 54 explicit BackgroundSyncRegistration(const base::string16& id) : id(id) {} | |
| 55 base::string16 id; | |
|
michaeln
2015/02/27 02:56:30
Maybe use a utf8 std::string here, do the utf8/16
jkarlin
2015/02/27 16:30:38
If the plan is to ultimately support UTF16 without
jkarlin
2015/03/02 14:41:03
I'm going with your suggested plan of lenient conv
| |
| 56 }; | |
| 57 | |
| 58 using StatusCallback = base::Callback<void(ErrorType)>; | |
| 59 using StatusAndRegistrationCallback = | |
| 60 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; | |
| 61 | |
| 62 static scoped_ptr<BackgroundSyncManager> Create( | |
| 63 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | |
| 64 virtual ~BackgroundSyncManager(); | |
| 65 | |
| 66 // Stores the given background sync registration and adds it to the scheduling | |
| 67 // queue. Will request permission from the user if necessary. Returns | |
|
michaeln
2015/02/27 02:56:30
Having this method be responsible for making the u
jkarlin
2015/02/27 16:30:38
Registration is the event at which we prompt. Same
michaeln
2015/02/28 00:34:23
when you get to that part in the impl, you'll see
jkarlin
2015/03/02 14:41:03
True. Some of this code may need to be broken out
| |
| 68 // ErrorTypeExists if the registration is already registered. Returns | |
| 69 // ErrorTypeOK and the registration on success. | |
| 70 void Register(const GURL& origin, | |
| 71 int64 sw_registration_id, | |
| 72 const BackgroundSyncRegistration& sync_registratin, | |
| 73 const StatusAndRegistrationCallback& callback); | |
| 74 | |
| 75 // Removes the given background sync registration from storage and the | |
| 76 // scheduling queue. Returns ErrorTypeNotFound if not registered. Returns | |
| 77 // ErrorTypeOK on success. | |
| 78 void Unregister(const GURL& origin, | |
| 79 int64 sw_registration_id, | |
| 80 const BackgroundSyncRegistration& sync_registration, | |
| 81 const StatusCallback& callback); | |
| 82 | |
| 83 // Finds the background sync registration associated with | |
| 84 // |sw_registration_id|. Returns ErrorTypeNotFound if it doesn't exist. | |
| 85 // Returns ErrorTypeOK on success. | |
| 86 void GetRegistration(const GURL& origin, | |
| 87 int64 sw_registration_id, | |
| 88 const base::string16 sync_registration_id, | |
| 89 const StatusAndRegistrationCallback& callback); | |
| 90 | |
| 91 private: | |
| 92 using PermissionStatusCallback = base::Callback<void(bool)>; | |
| 93 using IdToRegistrationMap = | |
| 94 std::map<int64, std::list<BackgroundSyncRegistration>>; | |
| 95 | |
| 96 explicit BackgroundSyncManager( | |
| 97 const scoped_refptr<ServiceWorkerContextWrapper>& context); | |
| 98 | |
| 99 bool HasRegistration(int64 sw_registration_id, | |
| 100 const BackgroundSyncRegistration& sync_registration); | |
| 101 | |
| 102 // Store all registrations for a given |sw_registration_id|. | |
| 103 void StoreRegistrations(const GURL& origin, | |
| 104 int64 sw_registration_id, | |
| 105 const ServiceWorkerStorage::StatusCallback& callback); | |
| 106 | |
| 107 // If the registration is in the map, removes it. Otherwise returns false. | |
| 108 bool RemoveRegistrationFromMap( | |
| 109 int64 sw_registration_id, | |
| 110 const BackgroundSyncRegistration& sync_registration); | |
| 111 | |
| 112 // If the registration is not in the map, adds it. Otherwise returns false. | |
| 113 bool AddRegistrationToMap( | |
| 114 int64 sw_registration_id, | |
| 115 const BackgroundSyncRegistration& sync_registration); | |
| 116 | |
| 117 // Init must be called before any public member function. | |
| 118 void Init(); | |
| 119 void InitImpl(); | |
| 120 void InitDidGetUserData( | |
| 121 const std::vector<std::pair<int64, std::string>>& user_data, | |
| 122 ServiceWorkerStatusCode status); | |
| 123 | |
| 124 // Register callbacks | |
| 125 void RegisterImpl(const GURL& origin, | |
| 126 int64 sw_registration_id, | |
| 127 const BackgroundSyncRegistration& sync_registration, | |
| 128 const StatusAndRegistrationCallback& callback); | |
| 129 void RegisterDidStore(int64 sw_registration_id, | |
| 130 const BackgroundSyncRegistration& sync_registration, | |
| 131 const StatusAndRegistrationCallback& callback, | |
| 132 ServiceWorkerStatusCode status); | |
| 133 | |
| 134 // Unregister callbacks | |
| 135 void UnregisterImpl(const GURL& origin, | |
| 136 int64 sw_registration_id, | |
| 137 const BackgroundSyncRegistration& sync_registration, | |
| 138 const StatusCallback& callback); | |
| 139 void UnregisterDidStore(int64 sw_registration_id, | |
| 140 const BackgroundSyncRegistration& sync_registration, | |
| 141 const StatusCallback& callback, | |
| 142 ServiceWorkerStatusCode status); | |
| 143 | |
| 144 // GetRegistration callbacks | |
| 145 void GetRegistrationImpl(const GURL& origin, | |
| 146 int64 sw_registration_id, | |
| 147 const base::string16 sync_registration_id, | |
| 148 const StatusAndRegistrationCallback& callback); | |
| 149 | |
| 150 // Operation Scheduling callbacks | |
| 151 void PendingStatusAndRegistrationCallback( | |
| 152 const StatusAndRegistrationCallback& callback, | |
| 153 ErrorType error, | |
| 154 const BackgroundSyncRegistration& sync_registration); | |
| 155 void PendingStatusCallback(const StatusCallback& callback, ErrorType error); | |
| 156 | |
| 157 bool initialized_; | |
| 158 bool initializing_; | |
| 159 | |
| 160 scoped_ptr<IdToRegistrationMap> registration_map_; | |
| 161 scoped_ptr<ServiceWorkerCacheScheduler> op_scheduler_; | |
| 162 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
| 163 | |
| 164 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | |
| 167 }; | |
| 168 | |
| 169 } // namespace content | |
| 170 | |
| 171 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | |
| OLD | NEW |