OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "content/browser/service_worker/service_worker_request_handler.h" | 5 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" |
6 | 6 |
7 #include "content/browser/service_worker/service_worker_context_core.h" | 7 #include "content/browser/service_worker/service_worker_context_core.h" |
8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
9 #include "content/browser/service_worker/service_worker_provider_host.h" | 8 #include "content/browser/service_worker/service_worker_provider_host.h" |
10 #include "content/browser/service_worker/service_worker_registration.h" | 9 #include "content/browser/service_worker/service_worker_registration.h" |
11 #include "content/browser/service_worker/service_worker_url_request_job.h" | 10 #include "content/browser/service_worker/service_worker_url_request_job.h" |
12 #include "content/browser/service_worker/service_worker_utils.h" | 11 #include "content/browser/service_worker/service_worker_utils.h" |
13 #include "content/common/service_worker/service_worker_types.h" | 12 #include "content/common/service_worker/service_worker_types.h" |
14 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
15 | 14 |
16 namespace content { | 15 namespace content { |
17 | 16 |
18 namespace { | 17 ServiceWorkerControlleeRequestHandler::ServiceWorkerControlleeRequestHandler( |
19 | 18 base::WeakPtr<ServiceWorkerContextCore> context, |
20 int kUserDataKey; // Key value is not important. | 19 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
21 | 20 ResourceType::Type resource_type) |
22 class ServiceWorkerRequestInterceptor | 21 : ServiceWorkerRequestHandler(context, provider_host, resource_type), |
23 : public net::URLRequestJobFactory::ProtocolHandler { | 22 weak_factory_(this) { |
24 public: | |
25 ServiceWorkerRequestInterceptor() {} | |
26 virtual ~ServiceWorkerRequestInterceptor() {} | |
27 virtual net::URLRequestJob* MaybeCreateJob( | |
28 net::URLRequest* request, | |
29 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
30 ServiceWorkerRequestHandler* handler = | |
31 ServiceWorkerRequestHandler::GetHandler(request); | |
32 if (!handler) | |
33 return NULL; | |
34 return handler->MaybeCreateJob(request, network_delegate); | |
35 } | |
36 | |
37 private: | |
38 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor); | |
39 }; | |
40 | |
41 } // namespace | |
42 | |
43 void ServiceWorkerRequestHandler::InitializeHandler( | |
44 net::URLRequest* request, | |
45 ServiceWorkerContextWrapper* context_wrapper, | |
46 int process_id, | |
47 int provider_id, | |
48 ResourceType::Type resource_type) { | |
49 if (!ServiceWorkerUtils::IsFeatureEnabled()) | |
50 return; | |
51 | |
52 if (!context_wrapper || !context_wrapper->context() || | |
53 provider_id == kInvalidServiceWorkerProviderId) { | |
54 return; | |
55 } | |
56 | |
57 ServiceWorkerProviderHost* provider_host = | |
58 context_wrapper->context()->GetProviderHost(process_id, provider_id); | |
59 if (!provider_host) | |
60 return; | |
61 | |
62 if (!provider_host->ShouldHandleRequest(resource_type)) | |
63 return; | |
64 | |
65 scoped_ptr<ServiceWorkerRequestHandler> handler( | |
66 new ServiceWorkerRequestHandler(context_wrapper->context()->AsWeakPtr(), | |
67 provider_host->AsWeakPtr(), | |
68 resource_type)); | |
69 request->SetUserData(&kUserDataKey, handler.release()); | |
70 } | 23 } |
71 | 24 |
72 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler( | 25 ServiceWorkerControlleeRequestHandler:: |
73 net::URLRequest* request) { | 26 ~ServiceWorkerControlleeRequestHandler() { |
74 return reinterpret_cast<ServiceWorkerRequestHandler*>( | |
75 request->GetUserData(&kUserDataKey)); | |
76 } | 27 } |
77 | 28 |
78 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> | 29 net::URLRequestJob* ServiceWorkerControlleeRequestHandler::MaybeCreateJob( |
79 ServiceWorkerRequestHandler::CreateInterceptor() { | |
80 return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( | |
81 new ServiceWorkerRequestInterceptor); | |
82 } | |
83 | |
84 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() { | |
85 } | |
86 | |
87 net::URLRequestJob* ServiceWorkerRequestHandler::MaybeCreateJob( | |
88 net::URLRequest* request, | 30 net::URLRequest* request, |
89 net::NetworkDelegate* network_delegate) { | 31 net::NetworkDelegate* network_delegate) { |
90 if (!context_ || !provider_host_) { | 32 if (!context_ || !provider_host_) { |
91 // We can't do anything other than to fall back to network. | 33 // We can't do anything other than to fall back to network. |
92 job_ = NULL; | 34 job_ = NULL; |
93 return NULL; | 35 return NULL; |
94 } | 36 } |
95 | 37 |
96 // This may get called multiple times for original and redirect requests: | 38 // This may get called multiple times for original and redirect requests: |
97 // A. original request case: job_ is null, no previous location info. | 39 // A. original request case: job_ is null, no previous location info. |
(...skipping 23 matching lines...) Expand all Loading... |
121 // If we know we can fallback to network at this point (in case | 63 // If we know we can fallback to network at this point (in case |
122 // the storage lookup returned immediately), just return NULL here to | 64 // the storage lookup returned immediately), just return NULL here to |
123 // fallback to network. | 65 // fallback to network. |
124 job_ = NULL; | 66 job_ = NULL; |
125 return NULL; | 67 return NULL; |
126 } | 68 } |
127 | 69 |
128 return job_.get(); | 70 return job_.get(); |
129 } | 71 } |
130 | 72 |
131 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler( | 73 void ServiceWorkerControlleeRequestHandler::PrepareForMainResource( |
132 base::WeakPtr<ServiceWorkerContextCore> context, | 74 const GURL& url) { |
133 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | |
134 ResourceType::Type resource_type) | |
135 : context_(context), | |
136 provider_host_(provider_host), | |
137 resource_type_(resource_type), | |
138 weak_factory_(this) { | |
139 } | |
140 | |
141 void ServiceWorkerRequestHandler::PrepareForMainResource(const GURL& url) { | |
142 DCHECK(job_.get()); | 75 DCHECK(job_.get()); |
143 DCHECK(context_); | 76 DCHECK(context_); |
144 // The corresponding provider_host may already have associate version in | 77 // The corresponding provider_host may already have associate version in |
145 // redirect case, unassociate it now. | 78 // redirect case, unassociate it now. |
146 provider_host_->SetActiveVersion(NULL); | 79 provider_host_->SetActiveVersion(NULL); |
147 provider_host_->SetPendingVersion(NULL); | 80 provider_host_->SetPendingVersion(NULL); |
148 provider_host_->set_document_url(url); | 81 provider_host_->set_document_url(url); |
149 context_->storage()->FindRegistrationForDocument( | 82 context_->storage()->FindRegistrationForDocument( |
150 url, | 83 url, |
151 base::Bind(&self::DidLookupRegistrationForMainResource, | 84 base::Bind(&self::DidLookupRegistrationForMainResource, |
152 weak_factory_.GetWeakPtr())); | 85 weak_factory_.GetWeakPtr())); |
153 } | 86 } |
154 | 87 |
155 void ServiceWorkerRequestHandler::DidLookupRegistrationForMainResource( | 88 void |
| 89 ServiceWorkerControlleeRequestHandler::DidLookupRegistrationForMainResource( |
156 ServiceWorkerStatusCode status, | 90 ServiceWorkerStatusCode status, |
157 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 91 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
158 DCHECK(job_.get()); | 92 DCHECK(job_.get()); |
159 if (status != SERVICE_WORKER_OK || !registration->active_version()) { | 93 if (status != SERVICE_WORKER_OK || !registration->active_version()) { |
160 // No registration, or no active version for the registration is available. | 94 // No registration, or no active version for the registration is available. |
161 job_->FallbackToNetwork(); | 95 job_->FallbackToNetwork(); |
162 return; | 96 return; |
163 } | 97 } |
| 98 // TODO(michaeln): should SetPendingVersion() even if no active version so |
| 99 // so the versions in the pipeline (.installing, .waiting) show up in the |
| 100 // attribute values. |
164 DCHECK(registration); | 101 DCHECK(registration); |
165 provider_host_->SetActiveVersion(registration->active_version()); | 102 provider_host_->SetActiveVersion(registration->active_version()); |
166 provider_host_->SetPendingVersion(registration->pending_version()); | 103 provider_host_->SetPendingVersion(registration->pending_version()); |
167 job_->ForwardToServiceWorker(); | 104 job_->ForwardToServiceWorker(); |
168 } | 105 } |
169 | 106 |
170 void ServiceWorkerRequestHandler::PrepareForSubResource() { | 107 void ServiceWorkerControlleeRequestHandler::PrepareForSubResource() { |
171 DCHECK(job_.get()); | 108 DCHECK(job_.get()); |
172 DCHECK(context_); | 109 DCHECK(context_); |
173 DCHECK(provider_host_->active_version()); | 110 DCHECK(provider_host_->active_version()); |
174 job_->ForwardToServiceWorker(); | 111 job_->ForwardToServiceWorker(); |
175 } | 112 } |
176 | 113 |
177 } // namespace content | 114 } // namespace content |
OLD | NEW |