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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "content/common/content_export.h"
13 #include "webkit/common/resource_type.h"
21 class ServiceWorkerContextCore
;
22 class ServiceWorkerDispatcherHost
;
23 class ServiceWorkerRequestHandler
;
24 class ServiceWorkerVersion
;
26 // This class is the browser-process representation of a serice worker
27 // provider. There is a provider per document and the lifetime of this
28 // object is tied to the lifetime of its document in the renderer process.
29 // This class holds service worker state that is scoped to an individual
32 // Note this class can also host a running service worker, in which
33 // case it will observe resource loads made directly by the service worker.
34 class CONTENT_EXPORT ServiceWorkerProviderHost
35 : public base::SupportsWeakPtr
<ServiceWorkerProviderHost
> {
37 ServiceWorkerProviderHost(int process_id
,
39 base::WeakPtr
<ServiceWorkerContextCore
> context
,
40 ServiceWorkerDispatcherHost
* dispatcher_host
);
41 ~ServiceWorkerProviderHost();
43 int process_id() const { return process_id_
; }
44 int provider_id() const { return provider_id_
; }
45 const std::set
<int>& script_client_thread_ids() const {
46 return script_client_thread_ids_
;
49 bool IsHostToRunningServiceWorker() {
50 return running_hosted_version_
!= NULL
;
53 // The service worker version that corresponds with
54 // navigator.serviceWorker.active for our document.
55 ServiceWorkerVersion
* active_version() const {
56 return active_version_
.get();
59 // The service worker version that corresponds with
60 // navigate.serviceWorker.pending for our document.
61 ServiceWorkerVersion
* pending_version() const {
62 return pending_version_
.get();
65 // The running version, if any, that this provider is providing resource
67 ServiceWorkerVersion
* running_hosted_version() const {
68 return running_hosted_version_
.get();
71 void set_document_url(const GURL
& url
) { document_url_
= url
; }
72 const GURL
& document_url() const { return document_url_
; }
74 // Adds and removes script client thread ID, who is listening events
75 // dispatched from ServiceWorker to the document (and any of its dedicated
76 // workers) corresponding to this provider.
77 void AddScriptClient(int thread_id
);
78 void RemoveScriptClient(int thread_id
);
80 // Associate |version| to this provider as its '.active' or '.pending'
82 // Giving NULL to this method will unset the corresponding field.
83 void SetActiveVersion(ServiceWorkerVersion
* version
);
84 void SetPendingVersion(ServiceWorkerVersion
* version
);
86 // Returns false if the version is not in the expected STARTING in our
87 // our process state. That would be indicative of a bad IPC message.
88 bool SetHostedVersionId(int64 versions_id
);
90 // Returns a handler for a request, the handler may return NULL if
91 // the request doesn't require special handling.
92 scoped_ptr
<ServiceWorkerRequestHandler
> CreateRequestHandler(
93 ResourceType::Type resource_type
);
96 const int process_id_
;
97 const int provider_id_
;
99 std::set
<int> script_client_thread_ids_
;
100 scoped_refptr
<ServiceWorkerVersion
> active_version_
;
101 scoped_refptr
<ServiceWorkerVersion
> pending_version_
;
102 scoped_refptr
<ServiceWorkerVersion
> running_hosted_version_
;
103 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
104 ServiceWorkerDispatcherHost
* dispatcher_host_
;
106 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost
);
109 } // namespace content
111 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_