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