| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ |
| 7 | 7 |
| 8 #include <vector> |
| 9 |
| 8 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 9 #include "content/browser/service_worker/service_worker_registration_status.h" | 11 #include "content/browser/service_worker/service_worker_registration_status.h" |
| 10 #include "content/browser/service_worker/service_worker_storage.h" | 12 #include "content/browser/service_worker/service_worker_storage.h" |
| 11 | 13 |
| 12 namespace content { | 14 namespace content { |
| 13 | 15 |
| 16 class ServiceWorkerJobCoordinator; |
| 17 |
| 14 // A ServiceWorkerRegisterJob lives only for the lifetime of a single | 18 // A ServiceWorkerRegisterJob lives only for the lifetime of a single |
| 15 // registration or unregistration. | 19 // registration or unregistration. |
| 16 class ServiceWorkerRegisterJob { | 20 class ServiceWorkerRegisterJob { |
| 17 public: | 21 public: |
| 22 enum RegistrationType { |
| 23 REGISTER, |
| 24 UNREGISTER, |
| 25 }; |
| 26 |
| 18 typedef base::Callback<void(ServiceWorkerRegistrationStatus status, | 27 typedef base::Callback<void(ServiceWorkerRegistrationStatus status, |
| 19 const scoped_refptr<ServiceWorkerRegistration>& | 28 const scoped_refptr<ServiceWorkerRegistration>& |
| 20 registration)> RegistrationCallback; | 29 registration)> RegistrationCallback; |
| 21 typedef base::Callback<void(ServiceWorkerRegistrationStatus status)> | 30 typedef base::Callback<void(ServiceWorkerRegistrationStatus status)> |
| 22 UnregistrationCallback; | 31 UnregistrationCallback; |
| 23 | 32 |
| 24 typedef base::Callback<void( | |
| 25 ServiceWorkerRegisterJob* job, | |
| 26 ServiceWorkerRegistrationStatus status, | |
| 27 ServiceWorkerRegistration* registration)> RegistrationCompleteCallback; | |
| 28 | |
| 29 // All type of jobs (Register and Unregister) complete through a | 33 // All type of jobs (Register and Unregister) complete through a |
| 30 // single call to this callback on the IO thread. | 34 // single call to this callback on the IO thread. |
| 31 ServiceWorkerRegisterJob(ServiceWorkerStorage* storage, | 35 ServiceWorkerRegisterJob(ServiceWorkerStorage* storage, |
| 32 const RegistrationCompleteCallback& callback); | 36 ServiceWorkerJobCoordinator* coordinator, |
| 37 const GURL& pattern, |
| 38 const GURL& script_url, |
| 39 RegistrationType type); |
| 33 ~ServiceWorkerRegisterJob(); | 40 ~ServiceWorkerRegisterJob(); |
| 34 | 41 |
| 42 void AddCallback(const RegistrationCallback& callback); |
| 43 |
| 44 void Start(); |
| 45 |
| 46 bool Equals(ServiceWorkerRegisterJob* job); |
| 47 |
| 48 private: |
| 35 // The Registration flow includes most or all of the following, | 49 // The Registration flow includes most or all of the following, |
| 36 // depending on what is already registered: | 50 // depending on what is already registered: |
| 37 // - creating a ServiceWorkerRegistration instance if there isn't | 51 // - creating a ServiceWorkerRegistration instance if there isn't |
| 38 // already something registered | 52 // already something registered |
| 39 // - creating a ServiceWorkerVersion for the new registration instance. | 53 // - creating a ServiceWorkerVersion for the new registration instance. |
| 40 // - starting a worker for the ServiceWorkerVersion | 54 // - starting a worker for the ServiceWorkerVersion |
| 41 // - telling the Version to evaluate the script | 55 // - telling the Version to evaluate the script |
| 42 // - firing the 'install' event at the ServiceWorkerVersion | 56 // - firing the 'install' event at the ServiceWorkerVersion |
| 43 // - firing the 'activate' event at the ServiceWorkerVersion | 57 // - firing the 'activate' event at the ServiceWorkerVersion |
| 44 // - Waiting for older ServiceWorkerVersions to deactivate | 58 // - Waiting for older ServiceWorkerVersions to deactivate |
| 45 // - designating the new version to be the 'active' version | 59 // - designating the new version to be the 'active' version |
| 46 // This method should be called once and only once per job. | 60 // This method should be called once and only once per job. |
| 47 void StartRegister(const GURL& pattern, const GURL& script_url); | 61 void StartRegister(); |
| 48 | 62 |
| 49 // The Unregistration process is primarily cleanup, removing | 63 // The Unregistration process is primarily cleanup, removing |
| 50 // everything that was created during the Registration process, | 64 // everything that was created during the Registration process, |
| 51 // including the ServiceWorkerRegistration itself. | 65 // including the ServiceWorkerRegistration itself. |
| 52 // This method should be called once and only once per job. | 66 // This method should be called once and only once per job. |
| 53 void StartUnregister(const GURL& pattern); | 67 void StartUnregister(); |
| 54 | 68 |
| 55 private: | |
| 56 // These are all steps in the registration and unregistration pipeline. | 69 // These are all steps in the registration and unregistration pipeline. |
| 57 void RegisterPatternAndContinue( | 70 void RegisterPatternAndContinue( |
| 58 const GURL& pattern, | |
| 59 const GURL& script_url, | |
| 60 const RegistrationCallback& callback, | 71 const RegistrationCallback& callback, |
| 61 ServiceWorkerRegistrationStatus previous_status); | 72 ServiceWorkerRegistrationStatus previous_status); |
| 62 | 73 |
| 63 void UnregisterPatternAndContinue( | 74 void UnregisterPatternAndContinue( |
| 64 const GURL& pattern, | |
| 65 const GURL& script_url, | |
| 66 const UnregistrationCallback& callback, | 75 const UnregistrationCallback& callback, |
| 67 bool found, | 76 bool found, |
| 68 ServiceWorkerRegistrationStatus previous_status, | 77 ServiceWorkerRegistrationStatus previous_status, |
| 69 const scoped_refptr<ServiceWorkerRegistration>& previous_registration); | 78 const scoped_refptr<ServiceWorkerRegistration>& previous_registration); |
| 70 | 79 |
| 71 // These methods are the last internal callback in the callback | 80 // These methods are the last internal callback in the callback |
| 72 // chain, and ultimately call callback_. | 81 // chain, and ultimately call callback_. |
| 73 void UnregisterComplete(ServiceWorkerRegistrationStatus status); | 82 void UnregisterComplete(ServiceWorkerRegistrationStatus status); |
| 74 void RegisterComplete( | 83 void RegisterComplete( |
| 75 ServiceWorkerRegistrationStatus status, | 84 ServiceWorkerRegistrationStatus status, |
| 76 const scoped_refptr<ServiceWorkerRegistration>& registration); | 85 const scoped_refptr<ServiceWorkerRegistration>& registration); |
| 77 | 86 |
| 87 void RunCallbacks( |
| 88 ServiceWorkerRegistrationStatus status, |
| 89 const scoped_refptr<ServiceWorkerRegistration>& registration); |
| 90 |
| 78 // The ServiceWorkerStorage object should always outlive | 91 // The ServiceWorkerStorage object should always outlive |
| 79 // this. | 92 // this. |
| 80 | 93 |
| 81 // TODO(alecflett) When we support job cancelling, if we are keeping | 94 // TODO(alecflett) When we support job cancelling, if we are keeping |
| 82 // this job alive for any reason, be sure to clear this variable, | 95 // this job alive for any reason, be sure to clear this variable, |
| 83 // because we may be cancelling while there are outstanding | 96 // because we may be cancelling while there are outstanding |
| 84 // callbacks that expect access to storage_. | 97 // callbacks that expect access to storage_. |
| 85 ServiceWorkerStorage* storage_; | 98 ServiceWorkerStorage* storage_; |
| 86 const RegistrationCompleteCallback callback_; | 99 ServiceWorkerJobCoordinator* coordinator_; |
| 100 const GURL pattern_; |
| 101 const GURL script_url_; |
| 102 const RegistrationType type_; |
| 103 std::vector<RegistrationCallback> callbacks_; |
| 87 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; | 104 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; |
| 88 | 105 |
| 89 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); | 106 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); |
| 90 }; | 107 }; |
| 91 } // namespace content | 108 } // namespace content |
| 92 | 109 |
| 93 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ | 110 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ |
| OLD | NEW |