Index: content/browser/background_sync/background_sync_manager.h |
diff --git a/content/browser/background_sync/background_sync_manager.h b/content/browser/background_sync/background_sync_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a13c7e489854070c61287c0845fe8a3408eb631e |
--- /dev/null |
+++ b/content/browser/background_sync/background_sync_manager.h |
@@ -0,0 +1,171 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
+#define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |
+ |
+#include <list> |
+#include <map> |
+ |
+#include "base/callback.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/strings/string16.h" |
+#include "content/browser/service_worker/service_worker_storage.h" |
+#include "content/common/content_export.h" |
+#include "content/common/service_worker/service_worker_status_code.h" |
+#include "url/gurl.h" |
+ |
+namespace content { |
+ |
+class ServiceWorkerCacheScheduler; |
+class ServiceWorkerContextWrapper; |
+ |
+// BackgroundSyncManager manages and stores the set of background sync |
+// registrations across all registered service workers for a profile. |
+// Registrations are stored along with their associated Service Worker |
+// registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, |
+// the sync registrations are removed. This class expects to be run on the IO |
+// thread. The asynchronous methods are executed sequentially. |
+ |
+// TODO(jkarlin): Check permissions when registering, scheduling, and firing |
+// 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.
|
+// fire a sync event. |
+// TODO(jkarlin): Unregister syncs when permission is revoked. |
+// TODO(jkarlin): Create a background sync scheduler to actually run the |
+// registered events. |
+// TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the |
+// Background" is true and a sync is registered. |
+// TODO(jkarlin): Unregister syncs when storage for an origin is cleared. |
+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.
|
+ public: |
+ enum ErrorType { |
+ ErrorTypeOK = 0, |
+ ErrorTypeExists, |
+ ErrorTypeStorage, |
+ ErrorTypeNotFound |
+ }; |
+ |
+ // TODO(jkarlin): Remove this and use the struct from IPC messages once it |
+ // lands. |
+ struct BackgroundSyncRegistration { |
+ BackgroundSyncRegistration() {} |
+ explicit BackgroundSyncRegistration(const base::string16& id) : id(id) {} |
+ 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
|
+ }; |
+ |
+ using StatusCallback = base::Callback<void(ErrorType)>; |
+ using StatusAndRegistrationCallback = |
+ base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; |
+ |
+ static scoped_ptr<BackgroundSyncManager> Create( |
+ const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
+ virtual ~BackgroundSyncManager(); |
+ |
+ // Stores the given background sync registration and adds it to the scheduling |
+ // 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
|
+ // ErrorTypeExists if the registration is already registered. Returns |
+ // ErrorTypeOK and the registration on success. |
+ void Register(const GURL& origin, |
+ int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registratin, |
+ const StatusAndRegistrationCallback& callback); |
+ |
+ // Removes the given background sync registration from storage and the |
+ // scheduling queue. Returns ErrorTypeNotFound if not registered. Returns |
+ // ErrorTypeOK on success. |
+ void Unregister(const GURL& origin, |
+ int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration, |
+ const StatusCallback& callback); |
+ |
+ // Finds the background sync registration associated with |
+ // |sw_registration_id|. Returns ErrorTypeNotFound if it doesn't exist. |
+ // Returns ErrorTypeOK on success. |
+ void GetRegistration(const GURL& origin, |
+ int64 sw_registration_id, |
+ const base::string16 sync_registration_id, |
+ const StatusAndRegistrationCallback& callback); |
+ |
+ private: |
+ using PermissionStatusCallback = base::Callback<void(bool)>; |
+ using IdToRegistrationMap = |
+ std::map<int64, std::list<BackgroundSyncRegistration>>; |
+ |
+ explicit BackgroundSyncManager( |
+ const scoped_refptr<ServiceWorkerContextWrapper>& context); |
+ |
+ bool HasRegistration(int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration); |
+ |
+ // Store all registrations for a given |sw_registration_id|. |
+ void StoreRegistrations(const GURL& origin, |
+ int64 sw_registration_id, |
+ const ServiceWorkerStorage::StatusCallback& callback); |
+ |
+ // If the registration is in the map, removes it. Otherwise returns false. |
+ bool RemoveRegistrationFromMap( |
+ int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration); |
+ |
+ // If the registration is not in the map, adds it. Otherwise returns false. |
+ bool AddRegistrationToMap( |
+ int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration); |
+ |
+ // Init must be called before any public member function. |
+ void Init(); |
+ void InitImpl(); |
+ void InitDidGetUserData( |
+ const std::vector<std::pair<int64, std::string>>& user_data, |
+ ServiceWorkerStatusCode status); |
+ |
+ // Register callbacks |
+ void RegisterImpl(const GURL& origin, |
+ int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration, |
+ const StatusAndRegistrationCallback& callback); |
+ void RegisterDidStore(int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration, |
+ const StatusAndRegistrationCallback& callback, |
+ ServiceWorkerStatusCode status); |
+ |
+ // Unregister callbacks |
+ void UnregisterImpl(const GURL& origin, |
+ int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration, |
+ const StatusCallback& callback); |
+ void UnregisterDidStore(int64 sw_registration_id, |
+ const BackgroundSyncRegistration& sync_registration, |
+ const StatusCallback& callback, |
+ ServiceWorkerStatusCode status); |
+ |
+ // GetRegistration callbacks |
+ void GetRegistrationImpl(const GURL& origin, |
+ int64 sw_registration_id, |
+ const base::string16 sync_registration_id, |
+ const StatusAndRegistrationCallback& callback); |
+ |
+ // Operation Scheduling callbacks |
+ void PendingStatusAndRegistrationCallback( |
+ const StatusAndRegistrationCallback& callback, |
+ ErrorType error, |
+ const BackgroundSyncRegistration& sync_registration); |
+ void PendingStatusCallback(const StatusCallback& callback, ErrorType error); |
+ |
+ bool initialized_; |
+ bool initializing_; |
+ |
+ scoped_ptr<IdToRegistrationMap> registration_map_; |
+ scoped_ptr<ServiceWorkerCacheScheduler> op_scheduler_; |
+ scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
+ |
+ base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ |