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 3// found in the LICENSE file. 4 5#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ 6#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ 7 8#include <vector> 9 10#include "base/memory/weak_ptr.h" 11#include "content/browser/service_worker/embedded_worker_instance.h" 12#include "content/browser/service_worker/service_worker_register_job_base.h" 13#include "content/browser/service_worker/service_worker_registration.h" 14#include "content/common/service_worker/service_worker_status_code.h" 15#include "url/gurl.h" 16 17namespace content { 18 19class ServiceWorkerJobCoordinator; 20class ServiceWorkerStorage; 21 22// Handles the initial registration of a Service Worker and the 23// subsequent update of existing registrations. 24// 25// The control flow includes most or all of the following, 26// depending on what is already registered: 27// - creating a ServiceWorkerRegistration instance if there isn't 28// already something registered 29// - creating a ServiceWorkerVersion for the new version. 30// - starting a worker for the ServiceWorkerVersion 31// - firing the 'install' event at the ServiceWorkerVersion 32// - firing the 'activate' event at the ServiceWorkerVersion 33// - waiting for older ServiceWorkerVersions to deactivate 34// - designating the new version to be the 'active' version 35// - updating storage 36class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase, 37 public EmbeddedWorkerInstance::Listener, 38 public ServiceWorkerRegistration::Listener { 39 public: 40 typedef base::Callback<void(ServiceWorkerStatusCode status, 41 ServiceWorkerRegistration* registration, 42 ServiceWorkerVersion* version)> 43 RegistrationCallback; 44 45 // For registration jobs. 46 CONTENT_EXPORT ServiceWorkerRegisterJob( 47 base::WeakPtr<ServiceWorkerContextCore> context, 48 const GURL& pattern, 49 const GURL& script_url); 50 51 // For update jobs. 52 CONTENT_EXPORT ServiceWorkerRegisterJob( 53 base::WeakPtr<ServiceWorkerContextCore> context, 54 ServiceWorkerRegistration* registration); 55 virtual ~ServiceWorkerRegisterJob(); 56 57 // Registers a callback to be called when the promise would resolve (whether 58 // successfully or not). Multiple callbacks may be registered. 59 // If |provider_host| is not NULL, its process will be regarded as a candidate 60 // process to run the worker. 61 void AddCallback(const RegistrationCallback& callback, 62 ServiceWorkerProviderHost* provider_host); 63 64 // ServiceWorkerRegisterJobBase implementation: 65 virtual void Start() OVERRIDE; 66 virtual void Abort() OVERRIDE; 67 virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE; 68 virtual RegistrationJobType GetType() OVERRIDE; 69 70 private: 71 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, 72 AssociateInstallingVersionToDocuments); 73 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, 74 DisassociateVersionFromDocuments); 75 76 enum Phase { 77 INITIAL, 78 START, 79 WAIT_FOR_UNINSTALL, 80 REGISTER, 81 UPDATE, 82 INSTALL, 83 STORE, 84 COMPLETE, 85 ABORT, 86 }; 87 88 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the 89 // getter/setter functions. 90 struct Internal { 91 Internal(); 92 ~Internal(); 93 scoped_refptr<ServiceWorkerRegistration> registration; 94 95 // Holds the version created by this job. It can be the 'installing', 96 // 'waiting', or 'active' version depending on the phase. 97 scoped_refptr<ServiceWorkerVersion> new_version; 98 99 scoped_refptr<ServiceWorkerRegistration> uninstalling_registration; 100 }; 101 102 void set_registration( 103 const scoped_refptr<ServiceWorkerRegistration>& registration); 104 ServiceWorkerRegistration* registration(); 105 void set_new_version(ServiceWorkerVersion* version); 106 ServiceWorkerVersion* new_version(); 107 void set_uninstalling_registration( 108 const scoped_refptr<ServiceWorkerRegistration>& registration); 109 ServiceWorkerRegistration* uninstalling_registration(); 110 111 void SetPhase(Phase phase); 112 113 void ContinueWithRegistration( 114 ServiceWorkerStatusCode status, 115 const scoped_refptr<ServiceWorkerRegistration>& registration); 116 void ContinueWithUpdate( 117 ServiceWorkerStatusCode status, 118 const scoped_refptr<ServiceWorkerRegistration>& registration); 119 void RegisterAndContinue(ServiceWorkerStatusCode status); 120 void WaitForUninstall( 121 const scoped_refptr<ServiceWorkerRegistration>& registration); 122 void ContinueWithRegistrationForSameScriptUrl( 123 const scoped_refptr<ServiceWorkerRegistration>& existing_registration, 124 ServiceWorkerStatusCode status); 125 void UpdateAndContinue(); 126 void OnStartWorkerFinished(ServiceWorkerStatusCode status); 127 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status); 128 void InstallAndContinue(); 129 void OnInstallFinished(ServiceWorkerStatusCode status); 130 void ActivateAndContinue(); 131 void OnActivateFinished(ServiceWorkerStatusCode status); 132 void Complete(ServiceWorkerStatusCode status); 133 void CompleteInternal(ServiceWorkerStatusCode status); 134 void ResolvePromise(ServiceWorkerStatusCode status, 135 ServiceWorkerRegistration* registration, 136 ServiceWorkerVersion* version); 137 138 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload. 139 virtual void OnPausedAfterDownload() OVERRIDE; 140 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 141 142 // ServiceWorkerRegistration::Listener overrides 143 virtual void OnRegistrationFinishedUninstalling( 144 ServiceWorkerRegistration* registration) OVERRIDE; 145 146 void OnCompareScriptResourcesComplete( 147 ServiceWorkerVersion* most_recent_version, 148 ServiceWorkerStatusCode status, 149 bool are_equal); 150 151 void AssociateProviderHostsToRegistration( 152 ServiceWorkerRegistration* registration); 153 154 // The ServiceWorkerContextCore object should always outlive this. 155 base::WeakPtr<ServiceWorkerContextCore> context_; 156 157 RegistrationJobType job_type_; 158 const GURL pattern_; 159 const GURL script_url_; 160 std::vector<RegistrationCallback> callbacks_; 161 Phase phase_; 162 Internal internal_; 163 bool is_promise_resolved_; 164 ServiceWorkerStatusCode promise_resolved_status_; 165 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_; 166 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_; 167 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; 168 169 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); 170}; 171 172} // namespace content 173 174#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ 175