service_worker_register_job.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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 37 : public ServiceWorkerRegisterJobBase, 38 public EmbeddedWorkerInstance::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. If |process_id| 59 // is not -1, it's added to the existing clients when deciding in which 60 // process to create the Service Worker instance. If there are no existing 61 // clients, a new RenderProcessHost will be created. 62 void AddCallback(const RegistrationCallback& callback, int process_id); 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 REGISTER, 80 UPDATE, 81 INSTALL, 82 STORE, 83 COMPLETE, 84 ABORT, 85 }; 86 87 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the 88 // getter/setter functions. 89 struct Internal { 90 Internal(); 91 ~Internal(); 92 scoped_refptr<ServiceWorkerRegistration> registration; 93 94 // Holds the version created by this job. It can be the 'installing', 95 // 'waiting', or 'active' version depending on the phase. 96 scoped_refptr<ServiceWorkerVersion> new_version; 97 }; 98 99 void set_registration(ServiceWorkerRegistration* registration); 100 ServiceWorkerRegistration* registration(); 101 void set_new_version(ServiceWorkerVersion* version); 102 ServiceWorkerVersion* new_version(); 103 104 void SetPhase(Phase phase); 105 106 void ContinueWithRegistration( 107 ServiceWorkerStatusCode status, 108 const scoped_refptr<ServiceWorkerRegistration>& registration); 109 void ContinueWithUpdate( 110 ServiceWorkerStatusCode status, 111 const scoped_refptr<ServiceWorkerRegistration>& registration); 112 void RegisterAndContinue(ServiceWorkerStatusCode status); 113 void UpdateAndContinue(); 114 void OnStartWorkerFinished(ServiceWorkerStatusCode status); 115 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status); 116 void InstallAndContinue(); 117 void OnInstallFinished(ServiceWorkerStatusCode status); 118 void ActivateAndContinue(); 119 void OnActivateFinished(ServiceWorkerStatusCode status); 120 void Complete(ServiceWorkerStatusCode status); 121 void CompleteInternal(ServiceWorkerStatusCode status); 122 void ResolvePromise(ServiceWorkerStatusCode status, 123 ServiceWorkerRegistration* registration, 124 ServiceWorkerVersion* version); 125 126 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload. 127 virtual void OnPausedAfterDownload() OVERRIDE; 128 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 129 130 void OnCompareScriptResourcesComplete( 131 ServiceWorkerVersion* most_recent_version, 132 ServiceWorkerStatusCode status, 133 bool are_equal); 134 135 void AssociateProviderHostsToRegistration( 136 ServiceWorkerRegistration* registration); 137 138 // The ServiceWorkerContextCore object should always outlive this. 139 base::WeakPtr<ServiceWorkerContextCore> context_; 140 141 RegistrationJobType job_type_; 142 const GURL pattern_; 143 const GURL script_url_; 144 std::vector<RegistrationCallback> callbacks_; 145 std::vector<int> pending_process_ids_; 146 Phase phase_; 147 Internal internal_; 148 bool is_promise_resolved_; 149 ServiceWorkerStatusCode promise_resolved_status_; 150 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_; 151 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_; 152 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; 153 154 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); 155}; 156 157} // namespace content 158 159#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ 160