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