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_PROVIDER_HOST_H_ 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ 7 8 #include <set> 9 #include <vector> 10 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/weak_ptr.h" 13 #include "content/common/content_export.h" 14 #include "content/common/service_worker/service_worker_types.h" 15 #include "webkit/common/resource_type.h" 16 17 namespace IPC { 18 class Sender; 19 } 20 21 namespace webkit_blob { 22 class BlobStorageContext; 23 } 24 25 namespace content { 26 27 class ServiceWorkerContextCore; 28 class ServiceWorkerDispatcherHost; 29 class ServiceWorkerRequestHandler; 30 class ServiceWorkerVersion; 31 32 // This class is the browser-process representation of a service worker 33 // provider. There is a provider per document and the lifetime of this 34 // object is tied to the lifetime of its document in the renderer process. 35 // This class holds service worker state that is scoped to an individual 36 // document. 37 // 38 // Note this class can also host a running service worker, in which 39 // case it will observe resource loads made directly by the service worker. 40 class CONTENT_EXPORT ServiceWorkerProviderHost 41 : public base::SupportsWeakPtr<ServiceWorkerProviderHost> { 42 public: 43 ServiceWorkerProviderHost(int process_id, 44 int provider_id, 45 base::WeakPtr<ServiceWorkerContextCore> context, 46 ServiceWorkerDispatcherHost* dispatcher_host); 47 ~ServiceWorkerProviderHost(); 48 49 int process_id() const { return process_id_; } 50 int provider_id() const { return provider_id_; } 51 52 bool IsHostToRunningServiceWorker() { 53 return running_hosted_version_ != NULL; 54 } 55 56 // The service worker version that corresponds with 57 // navigator.serviceWorker.active for our document. 58 ServiceWorkerVersion* active_version() const { 59 return active_version_.get(); 60 } 61 62 // The service worker version that corresponds with 63 // navigate.serviceWorker.waiting for our document. 64 ServiceWorkerVersion* waiting_version() const { 65 return waiting_version_.get(); 66 } 67 68 // The running version, if any, that this provider is providing resource 69 // loads for. 70 ServiceWorkerVersion* running_hosted_version() const { 71 return running_hosted_version_.get(); 72 } 73 74 void SetDocumentUrl(const GURL& url); 75 const GURL& document_url() const { return document_url_; } 76 77 // Associates |version| to this provider as its '.active' or '.waiting' 78 // version. 79 // Giving NULL to this method will unset the corresponding field. 80 void SetActiveVersion(ServiceWorkerVersion* version); 81 void SetWaitingVersion(ServiceWorkerVersion* version); 82 83 // Returns false if the version is not in the expected STARTING in our 84 // process state. That would be indicative of a bad IPC message. 85 bool SetHostedVersionId(int64 versions_id); 86 87 // Returns a handler for a request, the handler may return NULL if 88 // the request doesn't require special handling. 89 scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler( 90 ResourceType::Type resource_type, 91 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context); 92 93 // Returns true if |version| has the same registration as active and waiting 94 // versions. 95 bool ValidateVersionForAssociation(ServiceWorkerVersion* version); 96 97 // Returns true if the context referred to by this host (i.e. |context_|) is 98 // still alive. 99 bool IsContextAlive(); 100 101 // Dispatches message event to the document. 102 void PostMessage(const base::string16& message, 103 const std::vector<int>& sent_message_port_ids); 104 105 private: 106 // Creates a ServiceWorkerHandle to retain |version| and returns a 107 // ServiceWorkerInfo with the handle ID to pass to the provider. The 108 // provider is responsible for releasing the handle. 109 ServiceWorkerObjectInfo CreateHandleAndPass(ServiceWorkerVersion* version); 110 111 const int process_id_; 112 const int provider_id_; 113 GURL document_url_; 114 scoped_refptr<ServiceWorkerVersion> active_version_; 115 scoped_refptr<ServiceWorkerVersion> waiting_version_; 116 scoped_refptr<ServiceWorkerVersion> running_hosted_version_; 117 base::WeakPtr<ServiceWorkerContextCore> context_; 118 ServiceWorkerDispatcherHost* dispatcher_host_; 119 120 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); 121 }; 122 123 } // namespace content 124 125 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ 126