service_worker_register_job.h revision 116680a4aac90f2aa7413d9095a592090648e557
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 registration of a Service Worker. 23// 24// The registration flow includes most or all of the following, 25// depending on what is already registered: 26// - creating a ServiceWorkerRegistration instance if there isn't 27// already something registered 28// - creating a ServiceWorkerVersion for the new registration instance. 29// - starting a worker for the ServiceWorkerVersion 30// - telling the Version to evaluate the script 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 35class ServiceWorkerRegisterJob 36 : public ServiceWorkerRegisterJobBase, 37 public EmbeddedWorkerInstance::Listener { 38 public: 39 typedef base::Callback<void(ServiceWorkerStatusCode status, 40 ServiceWorkerRegistration* registration, 41 ServiceWorkerVersion* version)> 42 RegistrationCallback; 43 44 CONTENT_EXPORT ServiceWorkerRegisterJob( 45 base::WeakPtr<ServiceWorkerContextCore> context, 46 const GURL& pattern, 47 const GURL& script_url); 48 virtual ~ServiceWorkerRegisterJob(); 49 50 // Registers a callback to be called when the promise would resolve (whether 51 // successfully or not). Multiple callbacks may be registered. If |process_id| 52 // is not -1, it's added to the existing clients when deciding in which 53 // process to create the Service Worker instance. If there are no existing 54 // clients, a new RenderProcessHost will be created. 55 void AddCallback(const RegistrationCallback& callback, int process_id); 56 57 // ServiceWorkerRegisterJobBase implementation: 58 virtual void Start() OVERRIDE; 59 virtual void Abort() OVERRIDE; 60 virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE; 61 virtual RegistrationJobType GetType() OVERRIDE; 62 63 private: 64 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, 65 AssociateInstallingVersionToDocuments); 66 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProviderHostWaitingVersionTest, 67 DisassociateVersionFromDocuments); 68 69 enum Phase { 70 INITIAL, 71 START, 72 REGISTER, 73 UPDATE, 74 INSTALL, 75 STORE, 76 ACTIVATE, 77 COMPLETE, 78 ABORT, 79 }; 80 81 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the 82 // getter/setter functions. 83 struct Internal { 84 Internal(); 85 ~Internal(); 86 scoped_refptr<ServiceWorkerRegistration> registration; 87 88 // Holds the version created by this job. It can be the 'installing', 89 // 'waiting', or 'active' version depending on the phase. 90 scoped_refptr<ServiceWorkerVersion> new_version; 91 }; 92 93 void set_registration(ServiceWorkerRegistration* registration); 94 ServiceWorkerRegistration* registration(); 95 void set_new_version(ServiceWorkerVersion* version); 96 ServiceWorkerVersion* new_version(); 97 98 void SetPhase(Phase phase); 99 100 void HandleExistingRegistrationAndContinue( 101 ServiceWorkerStatusCode status, 102 const scoped_refptr<ServiceWorkerRegistration>& registration); 103 void RegisterAndContinue(ServiceWorkerStatusCode status); 104 void UpdateAndContinue(ServiceWorkerStatusCode status); 105 void OnStartWorkerFinished(ServiceWorkerStatusCode status); 106 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status); 107 void InstallAndContinue(); 108 void OnInstallFinished(ServiceWorkerStatusCode status); 109 void ActivateAndContinue(); 110 void OnActivateFinished(ServiceWorkerStatusCode status); 111 void Complete(ServiceWorkerStatusCode status); 112 void CompleteInternal(ServiceWorkerStatusCode status); 113 114 void ResolvePromise(ServiceWorkerStatusCode status, 115 ServiceWorkerRegistration* registration, 116 ServiceWorkerVersion* version); 117 118 // EmbeddedWorkerInstance::Listener override of OnPausedAfterDownload. 119 virtual void OnPausedAfterDownload() OVERRIDE; 120 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 121 122 // Associates an installing version to documents matched with a scope of the 123 // version. 124 CONTENT_EXPORT static void AssociateInstallingVersionToDocuments( 125 base::WeakPtr<ServiceWorkerContextCore> context, 126 ServiceWorkerVersion* version); 127 128 // Associates a waiting version to documents matched with a scope of the 129 // version. 130 static void AssociateWaitingVersionToDocuments( 131 base::WeakPtr<ServiceWorkerContextCore> context, 132 ServiceWorkerVersion* version); 133 134 // Associates an active version to documents matched with a scope of the 135 // version. 136 static void AssociateActiveVersionToDocuments( 137 base::WeakPtr<ServiceWorkerContextCore> context, 138 ServiceWorkerVersion* version); 139 140 // Disassociates a version specified by |version_id| from documents. 141 CONTENT_EXPORT static void DisassociateVersionFromDocuments( 142 base::WeakPtr<ServiceWorkerContextCore> context, 143 ServiceWorkerVersion* version); 144 145 // The ServiceWorkerContextCore object should always outlive this. 146 base::WeakPtr<ServiceWorkerContextCore> context_; 147 148 const GURL pattern_; 149 const GURL script_url_; 150 std::vector<RegistrationCallback> callbacks_; 151 std::vector<int> pending_process_ids_; 152 Phase phase_; 153 Internal internal_; 154 bool is_promise_resolved_; 155 ServiceWorkerStatusCode promise_resolved_status_; 156 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_; 157 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_; 158 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; 159 160 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); 161}; 162 163} // namespace content 164 165#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ 166