OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
13 #include "webkit/common/resource_type.h" | 13 #include "webkit/common/resource_type.h" |
14 | 14 |
15 namespace ipc { | 15 namespace ipc { |
16 class Sender; | 16 class Sender; |
17 } | 17 } |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 class ServiceWorkerContextCore; | 21 class ServiceWorkerContextCore; |
22 class ServiceWorkerDispatcherHost; | 22 class ServiceWorkerDispatcherHost; |
| 23 class ServiceWorkerRequestHandler; |
23 class ServiceWorkerVersion; | 24 class ServiceWorkerVersion; |
24 | 25 |
25 // This class is the browser-process representation of a serice worker | 26 // This class is the browser-process representation of a serice worker |
26 // provider. There is a provider per document and the lifetime of this | 27 // provider. There is a provider per document and the lifetime of this |
27 // object is tied to the lifetime of its document in the renderer process. | 28 // object is tied to the lifetime of its document in the renderer process. |
28 // This class holds service worker state this is scoped to an individual | 29 // This class holds service worker state that is scoped to an individual |
29 // document. | 30 // document. |
| 31 // |
| 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. |
30 class CONTENT_EXPORT ServiceWorkerProviderHost | 34 class CONTENT_EXPORT ServiceWorkerProviderHost |
31 : public base::SupportsWeakPtr<ServiceWorkerProviderHost> { | 35 : public base::SupportsWeakPtr<ServiceWorkerProviderHost> { |
32 public: | 36 public: |
33 ServiceWorkerProviderHost(int process_id, | 37 ServiceWorkerProviderHost(int process_id, |
34 int provider_id, | 38 int provider_id, |
35 base::WeakPtr<ServiceWorkerContextCore> context, | 39 base::WeakPtr<ServiceWorkerContextCore> context, |
36 ServiceWorkerDispatcherHost* dispatcher_host); | 40 ServiceWorkerDispatcherHost* dispatcher_host); |
37 ~ServiceWorkerProviderHost(); | 41 ~ServiceWorkerProviderHost(); |
38 | 42 |
39 int process_id() const { return process_id_; } | 43 int process_id() const { return process_id_; } |
40 int provider_id() const { return provider_id_; } | 44 int provider_id() const { return provider_id_; } |
41 const std::set<int>& script_client_thread_ids() const { | 45 const std::set<int>& script_client_thread_ids() const { |
42 return script_client_thread_ids_; | 46 return script_client_thread_ids_; |
43 } | 47 } |
44 | 48 |
| 49 bool IsHostToRunningServiceWorker() { |
| 50 return running_hosted_version_ != NULL; |
| 51 } |
| 52 |
45 // The service worker version that corresponds with | 53 // The service worker version that corresponds with |
46 // navigator.serviceWorker.active for our document. | 54 // navigator.serviceWorker.active for our document. |
47 ServiceWorkerVersion* active_version() const { | 55 ServiceWorkerVersion* active_version() const { |
48 return active_version_.get(); | 56 return active_version_.get(); |
49 } | 57 } |
50 | 58 |
51 // The service worker version that corresponds with | 59 // The service worker version that corresponds with |
52 // navigate.serviceWorker.pending for our document. | 60 // navigate.serviceWorker.pending for our document. |
53 ServiceWorkerVersion* pending_version() const { | 61 ServiceWorkerVersion* pending_version() const { |
54 return pending_version_.get(); | 62 return pending_version_.get(); |
55 } | 63 } |
56 | 64 |
57 // The version, if any, that this provider is providing resource loads for. | 65 // The running version, if any, that this provider is providing resource |
58 // This host observes resource loads made by the serviceworker itself. | 66 // loads for. |
59 ServiceWorkerVersion* hosted_version() const { | 67 ServiceWorkerVersion* running_hosted_version() const { |
60 return hosted_version_.get(); | 68 return running_hosted_version_.get(); |
61 } | 69 } |
62 | 70 |
63 void set_document_url(const GURL& url) { document_url_ = url; } | 71 void set_document_url(const GURL& url) { document_url_ = url; } |
64 const GURL& document_url() const { return document_url_; } | 72 const GURL& document_url() const { return document_url_; } |
65 | 73 |
66 // Adds and removes script client thread ID, who is listening events | 74 // Adds and removes script client thread ID, who is listening events |
67 // dispatched from ServiceWorker to the document (and any of its dedicated | 75 // dispatched from ServiceWorker to the document (and any of its dedicated |
68 // workers) corresponding to this provider. | 76 // workers) corresponding to this provider. |
69 void AddScriptClient(int thread_id); | 77 void AddScriptClient(int thread_id); |
70 void RemoveScriptClient(int thread_id); | 78 void RemoveScriptClient(int thread_id); |
71 | 79 |
72 // Associate |version| to this provider as its '.active' or '.pending' | 80 // Associate |version| to this provider as its '.active' or '.pending' |
73 // version. | 81 // version. |
74 // Giving NULL to this method will unset the corresponding field. | 82 // Giving NULL to this method will unset the corresponding field. |
75 void SetActiveVersion(ServiceWorkerVersion* version); | 83 void SetActiveVersion(ServiceWorkerVersion* version); |
76 void SetPendingVersion(ServiceWorkerVersion* version); | 84 void SetPendingVersion(ServiceWorkerVersion* version); |
77 | 85 |
78 // Returns false if the version is not in the expected STARTING in our | 86 // Returns false if the version is not in the expected STARTING in our |
79 // our process state. That would be indicative of a bad IPC message. | 87 // our process state. That would be indicative of a bad IPC message. |
80 bool SetHostedVersionId(int64 versions_id); | 88 bool SetHostedVersionId(int64 versions_id); |
81 | 89 |
82 // Returns true if this provider host should handle requests for | 90 // Returns a handler for a request, the handler may return NULL if |
83 // |resource_type|. | 91 // the request doesn't require special handling. |
84 bool ShouldHandleRequest(ResourceType::Type resource_type) const; | 92 scoped_ptr<ServiceWorkerRequestHandler> CreateRequestHandler( |
| 93 ResourceType::Type resource_type); |
85 | 94 |
86 private: | 95 private: |
87 const int process_id_; | 96 const int process_id_; |
88 const int provider_id_; | 97 const int provider_id_; |
89 GURL document_url_; | 98 GURL document_url_; |
90 std::set<int> script_client_thread_ids_; | 99 std::set<int> script_client_thread_ids_; |
91 scoped_refptr<ServiceWorkerVersion> active_version_; | 100 scoped_refptr<ServiceWorkerVersion> active_version_; |
92 scoped_refptr<ServiceWorkerVersion> pending_version_; | 101 scoped_refptr<ServiceWorkerVersion> pending_version_; |
93 scoped_refptr<ServiceWorkerVersion> hosted_version_; | 102 scoped_refptr<ServiceWorkerVersion> running_hosted_version_; |
94 base::WeakPtr<ServiceWorkerContextCore> context_; | 103 base::WeakPtr<ServiceWorkerContextCore> context_; |
95 ServiceWorkerDispatcherHost* dispatcher_host_; | 104 ServiceWorkerDispatcherHost* dispatcher_host_; |
96 | 105 |
97 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); | 106 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHost); |
98 }; | 107 }; |
99 | 108 |
100 } // namespace content | 109 } // namespace content |
101 | 110 |
102 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ | 111 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_HOST_H_ |
OLD | NEW |