OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_JOB_WRAPPER_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_WRAPPER_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_WRAPPER_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_WRAPPER_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/weak_ptr.h" | |
10 #include "content/browser/loader/url_loader_request_handler.h" | 9 #include "content/browser/loader/url_loader_request_handler.h" |
11 #include "content/browser/service_worker/service_worker_fetch_dispatcher.h" | |
12 #include "content/browser/service_worker/service_worker_response_type.h" | |
13 #include "content/browser/service_worker/service_worker_url_request_job.h" | |
14 #include "storage/browser/blob/blob_reader.h" | 10 #include "storage/browser/blob/blob_reader.h" |
15 | 11 |
16 namespace content { | 12 namespace content { |
17 | 13 |
| 14 class ServiceWorkerURLRequestJob; |
| 15 class ServiceWorkerURLLoaderJob; |
| 16 |
18 // This class is a helper to support having | 17 // This class is a helper to support having |
19 // ServiceWorkerControlleeRequestHandler work with both URLRequestJobs and | 18 // ServiceWorkerControlleeRequestHandler work with both URLRequestJobs and |
20 // mojom::URLLoaders (that is, both with and without --enable-network-service). | 19 // mojom::URLLoaders (that is, both with and without --enable-network-service). |
21 // It wraps either a ServiceWorkerURLRequestJob or a callback for | 20 // It wraps either a ServiceWorkerURLRequestJob or a callback for |
22 // URLLoader and forwards to the underlying implementation. | 21 // URLLoader and forwards to the underlying implementation. |
23 class ServiceWorkerURLJobWrapper { | 22 class ServiceWorkerURLJobWrapper { |
24 public: | 23 public: |
25 class Delegate { | |
26 public: | |
27 virtual ~Delegate() {} | |
28 | |
29 virtual ServiceWorkerVersion* GetServiceWorkerVersion( | |
30 ServiceWorkerMetrics::URLRequestJobResult* result) = 0; | |
31 }; | |
32 | |
33 // Non-network service case. | 24 // Non-network service case. |
34 explicit ServiceWorkerURLJobWrapper( | 25 explicit ServiceWorkerURLJobWrapper( |
35 base::WeakPtr<ServiceWorkerURLRequestJob> url_request_job); | 26 base::WeakPtr<ServiceWorkerURLRequestJob> url_request_job); |
36 | 27 |
37 // With --enable-network-service. | 28 // With --enable-network-service. |
38 // TODO(kinuko): Implement this as a separate job class rather | 29 explicit ServiceWorkerURLJobWrapper( |
39 // than in a wrapper. | 30 std::unique_ptr<ServiceWorkerURLLoaderJob> url_loader_job); |
40 ServiceWorkerURLJobWrapper( | |
41 LoaderCallback loader_callback, | |
42 Delegate* delegate, | |
43 const ResourceRequest& resource_request, | |
44 base::WeakPtr<storage::BlobStorageContext> blob_storage_context); | |
45 | 31 |
46 ~ServiceWorkerURLJobWrapper(); | 32 ~ServiceWorkerURLJobWrapper(); |
47 | 33 |
48 // Sets the response type. | 34 // Sets the response type. |
49 void FallbackToNetwork(); | 35 void FallbackToNetwork(); |
50 void FallbackToNetworkOrRenderer(); | 36 void FallbackToNetworkOrRenderer(); |
51 void ForwardToServiceWorker(); | 37 void ForwardToServiceWorker(); |
52 | 38 |
53 // Returns true if this job should not be handled by a service worker, but | 39 // Returns true if this job should not be handled by a service worker, but |
54 // instead should fallback to the network. | 40 // instead should fallback to the network. |
(...skipping 10 matching lines...) Expand all Loading... |
65 | 51 |
66 // Determines the number of redirects used to handle the job (for metrics | 52 // Determines the number of redirects used to handle the job (for metrics |
67 // purposes). | 53 // purposes). |
68 size_t GetURLChainSize() const; | 54 size_t GetURLChainSize() const; |
69 | 55 |
70 // Returns true if the underlying job has been canceled or destroyed. | 56 // Returns true if the underlying job has been canceled or destroyed. |
71 bool WasCanceled() const; | 57 bool WasCanceled() const; |
72 | 58 |
73 private: | 59 private: |
74 enum class JobType { kURLRequest, kURLLoader }; | 60 enum class JobType { kURLRequest, kURLLoader }; |
75 | |
76 // Used only for URLLoader case. | |
77 // For FORWARD_TO_SERVICE_WORKER case. | |
78 void StartRequest(); | |
79 | |
80 void DidPrepareFetchEvent(scoped_refptr<ServiceWorkerVersion> version); | |
81 void DidDispatchFetchEvent( | |
82 ServiceWorkerStatusCode status, | |
83 ServiceWorkerFetchEventResult fetch_result, | |
84 const ServiceWorkerResponse& response, | |
85 blink::mojom::ServiceWorkerStreamHandlePtr body_as_stream, | |
86 const scoped_refptr<ServiceWorkerVersion>& version); | |
87 | |
88 std::unique_ptr<ServiceWorkerFetchRequest> CreateFetchRequest( | |
89 const ResourceRequest& request); | |
90 | |
91 void AfterRead(scoped_refptr<net::IOBuffer> buffer, int bytes); | |
92 | |
93 JobType job_type_; | |
94 | |
95 ServiceWorkerResponseType response_type_ = NOT_DETERMINED; | |
96 LoaderCallback loader_callback_; | |
97 | |
98 base::WeakPtr<ServiceWorkerURLRequestJob> url_request_job_; | 61 base::WeakPtr<ServiceWorkerURLRequestJob> url_request_job_; |
99 | 62 std::unique_ptr<ServiceWorkerURLLoaderJob> url_loader_job_; |
100 Delegate* delegate_; | |
101 ResourceRequest resource_request_; | |
102 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | |
103 std::unique_ptr<ServiceWorkerFetchDispatcher> fetch_dispatcher_; | |
104 | |
105 base::WeakPtrFactory<ServiceWorkerURLJobWrapper> weak_factory_; | |
106 | 63 |
107 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLJobWrapper); | 64 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLJobWrapper); |
108 }; | 65 }; |
109 | 66 |
110 } // namespace content | 67 } // namespace content |
111 | 68 |
112 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_WRAPPER_H_ | 69 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_WRAPPER_H_ |
OLD | NEW |