service_worker_register_job.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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/service_worker_register_job_base.h" 12#include "content/browser/service_worker/service_worker_registration.h" 13#include "content/common/service_worker/service_worker_status_code.h" 14#include "url/gurl.h" 15 16namespace content { 17 18class ServiceWorkerJobCoordinator; 19class ServiceWorkerStorage; 20 21// Handles the registration of a Service Worker. 22// 23// The registration flow includes most or all of the following, 24// depending on what is already registered: 25// - creating a ServiceWorkerRegistration instance if there isn't 26// already something registered 27// - creating a ServiceWorkerVersion for the new registration instance. 28// - starting a worker for the ServiceWorkerVersion 29// - telling the Version to evaluate the script 30// - firing the 'install' event at the ServiceWorkerVersion 31// - firing the 'activate' event at the ServiceWorkerVersion 32// - waiting for older ServiceWorkerVersions to deactivate 33// - designating the new version to be the 'active' version 34class ServiceWorkerRegisterJob : public ServiceWorkerRegisterJobBase { 35 public: 36 typedef base::Callback<void(ServiceWorkerStatusCode status, 37 ServiceWorkerRegistration* registration, 38 ServiceWorkerVersion* version)> 39 RegistrationCallback; 40 41 CONTENT_EXPORT ServiceWorkerRegisterJob( 42 base::WeakPtr<ServiceWorkerContextCore> context, 43 const GURL& pattern, 44 const GURL& script_url); 45 virtual ~ServiceWorkerRegisterJob(); 46 47 // Registers a callback to be called when the promise would resolve (whether 48 // successfully or not). Multiple callbacks may be registered. If |process_id| 49 // is not -1, it's added to the existing clients when deciding in which 50 // process to create the Service Worker instance. If there are no existing 51 // clients, a new RenderProcessHost will be created. 52 void AddCallback(const RegistrationCallback& callback, int process_id); 53 54 // ServiceWorkerRegisterJobBase implementation: 55 virtual void Start() OVERRIDE; 56 virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE; 57 virtual RegistrationJobType GetType() OVERRIDE; 58 59 private: 60 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerRegisterJobAndProviderHostTest, 61 AssociatePendingVersionToDocuments); 62 63 enum Phase { 64 INITIAL, 65 START, 66 REGISTER, 67 UPDATE, 68 INSTALL, 69 STORE, 70 ACTIVATE, 71 COMPLETE 72 }; 73 74 // Holds internal state of ServiceWorkerRegistrationJob, to compel use of the 75 // getter/setter functions. 76 struct Internal { 77 Internal(); 78 ~Internal(); 79 scoped_refptr<ServiceWorkerRegistration> registration; 80 scoped_refptr<ServiceWorkerVersion> pending_version; 81 }; 82 83 void set_registration(ServiceWorkerRegistration* registration); 84 ServiceWorkerRegistration* registration(); 85 void set_pending_version(ServiceWorkerVersion* version); 86 ServiceWorkerVersion* pending_version(); 87 88 void SetPhase(Phase phase); 89 90 void HandleExistingRegistrationAndContinue( 91 ServiceWorkerStatusCode status, 92 const scoped_refptr<ServiceWorkerRegistration>& registration); 93 void RegisterAndContinue(ServiceWorkerStatusCode status); 94 void UpdateAndContinue(ServiceWorkerStatusCode status); 95 void OnStartWorkerFinished(ServiceWorkerStatusCode status); 96 void OnStoreRegistrationComplete(ServiceWorkerStatusCode status); 97 void InstallAndContinue(); 98 void OnInstallFinished(ServiceWorkerStatusCode status); 99 void ActivateAndContinue(); 100 void OnActivateFinished(ServiceWorkerStatusCode status); 101 void Complete(ServiceWorkerStatusCode status); 102 103 void ResolvePromise(ServiceWorkerStatusCode status, 104 ServiceWorkerRegistration* registration, 105 ServiceWorkerVersion* version); 106 107 CONTENT_EXPORT void AssociatePendingVersionToDocuments( 108 ServiceWorkerVersion* version); 109 110 // The ServiceWorkerContextCore object should always outlive this. 111 base::WeakPtr<ServiceWorkerContextCore> context_; 112 113 const GURL pattern_; 114 const GURL script_url_; 115 std::vector<RegistrationCallback> callbacks_; 116 std::vector<int> pending_process_ids_; 117 Phase phase_; 118 Internal internal_; 119 bool is_promise_resolved_; 120 ServiceWorkerStatusCode promise_resolved_status_; 121 scoped_refptr<ServiceWorkerRegistration> promise_resolved_registration_; 122 scoped_refptr<ServiceWorkerVersion> promise_resolved_version_; 123 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; 124 125 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); 126}; 127 128} // namespace content 129 130#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ 131