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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 | |
10 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
11 #include "base/supports_user_data.h" | 10 #include "base/supports_user_data.h" |
12 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
13 #include "content/common/service_worker/service_worker_status_code.h" | 12 #include "content/common/service_worker/service_worker_status_code.h" |
14 #include "net/url_request/url_request_job_factory.h" | 13 #include "net/url_request/url_request_job_factory.h" |
15 #include "webkit/common/resource_type.h" | 14 #include "webkit/common/resource_type.h" |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 class NetworkDelegate; | 17 class NetworkDelegate; |
19 class URLRequest; | 18 class URLRequest; |
20 } | 19 } |
21 | 20 |
22 namespace content { | 21 namespace content { |
23 | 22 |
24 class ServiceWorkerContextCore; | 23 class ServiceWorkerContextCore; |
25 class ServiceWorkerContextWrapper; | 24 class ServiceWorkerContextWrapper; |
26 class ServiceWorkerProviderHost; | 25 class ServiceWorkerProviderHost; |
27 class ServiceWorkerRegistration; | |
28 class ServiceWorkerURLRequestJob; | |
29 | 26 |
| 27 // Abstract base class for routing network requests to ServiceWorkers. |
30 // Created one per URLRequest and attached to each request. | 28 // Created one per URLRequest and attached to each request. |
31 class CONTENT_EXPORT ServiceWorkerRequestHandler | 29 class CONTENT_EXPORT ServiceWorkerRequestHandler |
32 : public base::SupportsUserData::Data { | 30 : public base::SupportsUserData::Data { |
33 public: | 31 public: |
34 // Attaches a newly created handler if the given |request| needs to | 32 // Attaches a newly created handler if the given |request| needs to |
35 // be handled by ServiceWorker. | 33 // be handled by ServiceWorker. |
36 // TODO(kinuko): While utilizing UserData to attach data to URLRequest | 34 // TODO(kinuko): While utilizing UserData to attach data to URLRequest |
37 // has some precedence, it might be better to attach this handler in a more | 35 // has some precedence, it might be better to attach this handler in a more |
38 // explicit way within content layer, e.g. have ResourceRequestInfoImpl | 36 // explicit way within content layer, e.g. have ResourceRequestInfoImpl |
39 // own it. | 37 // own it. |
40 static void InitializeHandler( | 38 static void InitializeHandler( |
41 net::URLRequest* request, | 39 net::URLRequest* request, |
42 ServiceWorkerContextWrapper* context_wrapper, | 40 ServiceWorkerContextWrapper* context_wrapper, |
43 int process_id, | 41 int process_id, |
44 int provider_id, | 42 int provider_id, |
45 ResourceType::Type resource_type); | 43 ResourceType::Type resource_type); |
46 | 44 |
47 // Returns the handler attached to |request|. This may return NULL | 45 // Returns the handler attached to |request|. This may return NULL |
48 // if no handler is attached. | 46 // if no handler is attached. |
49 static ServiceWorkerRequestHandler* GetHandler( | 47 static ServiceWorkerRequestHandler* GetHandler( |
50 net::URLRequest* request); | 48 net::URLRequest* request); |
51 | 49 |
52 // Creates a protocol interceptor for ServiceWorker. | 50 // Creates a protocol interceptor for ServiceWorker. |
53 static scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> | 51 static scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
54 CreateInterceptor(); | 52 CreateInterceptor(); |
55 | 53 |
56 virtual ~ServiceWorkerRequestHandler(); | 54 virtual ~ServiceWorkerRequestHandler(); |
57 | 55 |
58 // Called via custom URLRequestJobFactory. | 56 // Called via custom URLRequestJobFactory. |
59 net::URLRequestJob* MaybeCreateJob( | 57 virtual net::URLRequestJob* MaybeCreateJob( |
60 net::URLRequest* request, | 58 net::URLRequest* request, |
61 net::NetworkDelegate* network_delegate); | 59 net::NetworkDelegate* network_delegate) = 0; |
62 | 60 |
63 private: | 61 protected: |
64 typedef ServiceWorkerRequestHandler self; | |
65 | |
66 ServiceWorkerRequestHandler( | 62 ServiceWorkerRequestHandler( |
67 base::WeakPtr<ServiceWorkerContextCore> context, | 63 base::WeakPtr<ServiceWorkerContextCore> context, |
68 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 64 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
69 ResourceType::Type resource_type); | 65 ResourceType::Type resource_type); |
70 | 66 |
71 // For main resource case. | |
72 void PrepareForMainResource(const GURL& url); | |
73 void DidLookupRegistrationForMainResource( | |
74 ServiceWorkerStatusCode status, | |
75 const scoped_refptr<ServiceWorkerRegistration>& registration); | |
76 | |
77 // For sub resource case. | |
78 void PrepareForSubResource(); | |
79 | |
80 base::WeakPtr<ServiceWorkerContextCore> context_; | 67 base::WeakPtr<ServiceWorkerContextCore> context_; |
81 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; | 68 base::WeakPtr<ServiceWorkerProviderHost> provider_host_; |
82 ResourceType::Type resource_type_; | 69 ResourceType::Type resource_type_; |
83 scoped_refptr<ServiceWorkerURLRequestJob> job_; | |
84 | |
85 base::WeakPtrFactory<ServiceWorkerRequestHandler> weak_factory_; | |
86 | 70 |
87 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestHandler); | 71 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestHandler); |
88 }; | 72 }; |
89 | 73 |
90 } // namespace content | 74 } // namespace content |
91 | 75 |
92 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_ | 76 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_ |
OLD | NEW |