service_worker_register_job.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_registration_status.h" 12#include "content/browser/service_worker/service_worker_storage.h" 13#include "content/browser/service_worker/service_worker_version.h" 14 15namespace content { 16 17class EmbeddedWorkerRegistry; 18class ServiceWorkerJobCoordinator; 19 20// A ServiceWorkerRegisterJob lives only for the lifetime of a single 21// registration or unregistration. 22class ServiceWorkerRegisterJob { 23 public: 24 enum RegistrationType { 25 REGISTER, 26 UNREGISTER, 27 }; 28 29 typedef base::Callback<void(ServiceWorkerStatusCode status, 30 const scoped_refptr<ServiceWorkerRegistration>& 31 registration)> RegistrationCallback; 32 typedef base::Callback<void(ServiceWorkerStatusCode status)> 33 UnregistrationCallback; 34 // TODO(alecflett): Unify this with RegistrationCallback 35 typedef base::Callback< 36 void(const scoped_refptr<ServiceWorkerRegistration>& registration, 37 ServiceWorkerStatusCode status)> StatusCallback; 38 39 // All type of jobs (Register and Unregister) complete through a 40 // single call to this callback on the IO thread. 41 ServiceWorkerRegisterJob(ServiceWorkerStorage* storage, 42 EmbeddedWorkerRegistry* worker_registry, 43 ServiceWorkerJobCoordinator* coordinator, 44 const GURL& pattern, 45 const GURL& script_url, 46 RegistrationType type); 47 ~ServiceWorkerRegisterJob(); 48 49 void AddCallback(const RegistrationCallback& callback, int process_id); 50 51 void Start(); 52 53 bool Equals(ServiceWorkerRegisterJob* job); 54 55 private: 56 // The Registration flow includes most or all of the following, 57 // depending on what is already registered: 58 // - creating a ServiceWorkerRegistration instance if there isn't 59 // already something registered 60 // - creating a ServiceWorkerVersion for the new registration instance. 61 // - starting a worker for the ServiceWorkerVersion 62 // - telling the Version to evaluate the script 63 // - firing the 'install' event at the ServiceWorkerVersion 64 // - firing the 'activate' event at the ServiceWorkerVersion 65 // - Waiting for older ServiceWorkerVersions to deactivate 66 // - designating the new version to be the 'active' version 67 // This method should be called once and only once per job. 68 void StartRegister(); 69 70 // The Unregistration process is primarily cleanup, removing 71 // everything that was created during the Registration process, 72 // including the ServiceWorkerRegistration itself. 73 // This method should be called once and only once per job. 74 void StartUnregister(); 75 76 // These are all steps in the registration and unregistration pipeline. 77 void RegisterPatternAndContinue( 78 const RegistrationCallback& callback, 79 ServiceWorkerStatusCode previous_status); 80 81 void UnregisterPatternAndContinue( 82 const UnregistrationCallback& callback, 83 bool found, 84 ServiceWorkerStatusCode previous_status, 85 const scoped_refptr<ServiceWorkerRegistration>& previous_registration); 86 87 void StartWorkerAndContinue( 88 const StatusCallback& callback, 89 ServiceWorkerStatusCode status, 90 const scoped_refptr<ServiceWorkerRegistration>& registration); 91 92 // These methods are the last internal callback in the callback 93 // chain, and ultimately call callback_. 94 void UnregisterComplete(ServiceWorkerStatusCode status); 95 void RegisterComplete( 96 const scoped_refptr<ServiceWorkerRegistration>& registration, 97 ServiceWorkerStatusCode start_status); 98 99 void RunCallbacks( 100 ServiceWorkerStatusCode status, 101 const scoped_refptr<ServiceWorkerRegistration>& registration); 102 103 // The ServiceWorkerStorage object should always outlive 104 // this. 105 106 // TODO(alecflett) When we support job cancelling, if we are keeping 107 // this job alive for any reason, be sure to clear this variable, 108 // because we may be cancelling while there are outstanding 109 // callbacks that expect access to storage_. 110 ServiceWorkerStorage* storage_; 111 EmbeddedWorkerRegistry* worker_registry_; 112 ServiceWorkerJobCoordinator* coordinator_; 113 scoped_refptr<ServiceWorkerVersion> pending_version_; 114 const GURL pattern_; 115 const GURL script_url_; 116 const RegistrationType type_; 117 std::vector<RegistrationCallback> callbacks_; 118 std::vector<int> pending_process_ids_; 119 base::WeakPtrFactory<ServiceWorkerRegisterJob> weak_factory_; 120 121 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRegisterJob); 122}; 123} // namespace content 124 125#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REGISTER_JOB_H_ 126