OLD | NEW |
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_APPCACHE_APPCACHE_URL_LOADER_JOB_H_ | 5 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_URL_LOADER_JOB_H_ |
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_URL_LOADER_JOB_H_ | 6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_URL_LOADER_JOB_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "content/browser/appcache/appcache_entry.h" | 13 #include "content/browser/appcache/appcache_entry.h" |
14 #include "content/browser/appcache/appcache_job.h" | 14 #include "content/browser/appcache/appcache_job.h" |
15 #include "content/browser/appcache/appcache_request_handler.h" | 15 #include "content/browser/appcache/appcache_request_handler.h" |
16 #include "content/browser/appcache/appcache_response.h" | 16 #include "content/browser/appcache/appcache_response.h" |
17 #include "content/browser/appcache/appcache_storage.h" | 17 #include "content/browser/appcache/appcache_storage.h" |
18 #include "content/browser/loader/url_loader_request_handler.h" | 18 #include "content/browser/loader/url_loader_request_handler.h" |
19 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
20 #include "content/public/common/resource_request.h" | 20 #include "content/public/common/resource_request.h" |
21 #include "content/public/common/url_loader.mojom.h" | 21 #include "content/public/common/url_loader.mojom.h" |
22 #include "mojo/public/cpp/bindings/associated_binding.h" | 22 #include "mojo/public/cpp/bindings/associated_binding.h" |
23 #include "mojo/public/cpp/bindings/binding.h" | 23 #include "mojo/public/cpp/bindings/binding.h" |
24 #include "mojo/public/cpp/system/data_pipe.h" | 24 #include "mojo/public/cpp/system/data_pipe.h" |
25 | 25 |
26 namespace content { | 26 namespace content { |
27 | 27 |
28 class AppCacheRequest; | 28 class AppCacheRequest; |
| 29 class AppCacheURLLoaderRequest; |
29 class NetToMojoPendingBuffer; | 30 class NetToMojoPendingBuffer; |
30 class URLLoaderFactoryGetter; | 31 class URLLoaderFactoryGetter; |
31 struct SubresourceLoadInfo; | |
32 | 32 |
33 // Holds information about the subresource load request like the routing id, | 33 // Holds information about the subresource load request like the routing id, |
34 // request id, the client pointer, etc. | 34 // request id, the client pointer, etc. |
35 struct SubresourceLoadInfo { | 35 struct SubresourceLoadInfo { |
36 SubresourceLoadInfo(); | 36 SubresourceLoadInfo(); |
37 ~SubresourceLoadInfo(); | 37 ~SubresourceLoadInfo(); |
38 | 38 |
39 mojom::URLLoaderAssociatedRequest url_loader_request; | 39 mojom::URLLoaderAssociatedRequest url_loader_request; |
40 int32_t routing_id; | 40 int32_t routing_id; |
41 int32_t request_id; | 41 int32_t request_id; |
42 uint32_t options; | 42 uint32_t options; |
43 ResourceRequest request; | 43 ResourceRequest request; |
44 mojom::URLLoaderClientPtr client; | 44 mojom::URLLoaderClientPtr client; |
45 net::MutableNetworkTrafficAnnotationTag traffic_annotation; | 45 net::MutableNetworkTrafficAnnotationTag traffic_annotation; |
46 }; | 46 }; |
47 | 47 |
48 // AppCacheJob wrapper for a mojom::URLLoader implementation which returns | 48 // AppCacheJob wrapper for a mojom::URLLoader implementation which returns |
49 // responses stored in the AppCache. | 49 // responses stored in the AppCache. |
50 class CONTENT_EXPORT AppCacheURLLoaderJob : public AppCacheJob, | 50 class CONTENT_EXPORT AppCacheURLLoaderJob : public AppCacheJob, |
51 public AppCacheStorage::Delegate, | 51 public AppCacheStorage::Delegate, |
52 public mojom::URLLoader { | 52 public mojom::URLLoader, |
| 53 public mojom::URLLoaderClient { |
53 public: | 54 public: |
54 ~AppCacheURLLoaderJob() override; | 55 ~AppCacheURLLoaderJob() override; |
55 | 56 |
56 // Sets up the bindings. | 57 // Sets up the bindings. |
57 void Start(mojom::URLLoaderRequest request, mojom::URLLoaderClientPtr client); | 58 void Start(mojom::URLLoaderRequest request, mojom::URLLoaderClientPtr client); |
58 | 59 |
59 // AppCacheJob overrides. | 60 // AppCacheJob overrides. |
60 void Kill() override; | 61 void Kill() override; |
61 bool IsStarted() const override; | 62 bool IsStarted() const override; |
62 void DeliverAppCachedResponse(const GURL& manifest_url, | 63 void DeliverAppCachedResponse(const GURL& manifest_url, |
63 int64_t cache_id, | 64 int64_t cache_id, |
64 const AppCacheEntry& entry, | 65 const AppCacheEntry& entry, |
65 bool is_fallback) override; | 66 bool is_fallback) override; |
66 void DeliverNetworkResponse() override; | 67 void DeliverNetworkResponse() override; |
67 void DeliverErrorResponse() override; | 68 void DeliverErrorResponse() override; |
68 const GURL& GetURL() const override; | 69 const GURL& GetURL() const override; |
69 AppCacheURLLoaderJob* AsURLLoaderJob() override; | 70 AppCacheURLLoaderJob* AsURLLoaderJob() override; |
70 | 71 |
71 // mojom::URLLoader implementation: | 72 // mojom::URLLoader implementation: |
72 void FollowRedirect() override; | 73 void FollowRedirect() override; |
73 void SetPriority(net::RequestPriority priority, | 74 void SetPriority(net::RequestPriority priority, |
74 int32_t intra_priority_value) override; | 75 int32_t intra_priority_value) override; |
75 | 76 |
| 77 // mojom::URLLoaderClient implementation. |
| 78 // These methods are called by the network loader for subresource requests |
| 79 // which go to the network. We serve fallback content in these methods |
| 80 // if applicable. |
| 81 void OnReceiveResponse(const ResourceResponseHead& response_head, |
| 82 const base::Optional<net::SSLInfo>& ssl_info, |
| 83 mojom::DownloadedTempFilePtr downloaded_file) override; |
| 84 void OnReceiveRedirect(const net::RedirectInfo& redirect_info, |
| 85 const ResourceResponseHead& response_head) override; |
| 86 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; |
| 87 void OnUploadProgress(int64_t current_position, |
| 88 int64_t total_size, |
| 89 OnUploadProgressCallback ack_callback) override; |
| 90 void OnReceiveCachedMetadata(const std::vector<uint8_t>& data) override; |
| 91 void OnTransferSizeUpdated(int32_t transfer_size_diff) override; |
| 92 void OnStartLoadingResponseBody( |
| 93 mojo::ScopedDataPipeConsumerHandle body) override; |
| 94 void OnComplete(const ResourceRequestCompletionStatus& status) override; |
| 95 |
76 void set_main_resource_loader_callback(LoaderCallback callback) { | 96 void set_main_resource_loader_callback(LoaderCallback callback) { |
77 main_resource_loader_callback_ = std::move(callback); | 97 main_resource_loader_callback_ = std::move(callback); |
78 } | 98 } |
79 | 99 |
80 // Subresource request load information is passed in the | 100 // Subresource request load information is passed in the |
81 // |subresource_load_info| parameter. This includes the request id, the | 101 // |subresource_load_info| parameter. This includes the request id, the |
82 // client pointer, etc. | 102 // client pointer, etc. |
83 // |default_url_loader| is used to retrieve the network loader for requests | 103 // |default_url_loader| is used to retrieve the network loader for requests |
84 // intended to be sent to the network. | 104 // intended to be sent to the network. |
85 void SetSubresourceLoadInfo( | 105 void SetSubresourceLoadInfo( |
86 std::unique_ptr<SubresourceLoadInfo> subresource_load_info, | 106 std::unique_ptr<SubresourceLoadInfo> subresource_load_info, |
87 URLLoaderFactoryGetter* default_url_loader); | 107 URLLoaderFactoryGetter* default_url_loader); |
88 | 108 |
89 // Ownership of the |handler| is transferred to us via this call. This is | 109 // Ownership of the |handler| is transferred to us via this call. This is |
90 // only for subresource requests. | 110 // only for subresource requests. |
91 void set_request_handler(std::unique_ptr<AppCacheRequestHandler> handler) { | 111 void set_request_handler(std::unique_ptr<AppCacheRequestHandler> handler) { |
92 sub_resource_handler_ = std::move(handler); | 112 sub_resource_handler_ = std::move(handler); |
93 } | 113 } |
94 | 114 |
95 protected: | 115 protected: |
96 // AppCacheJob::Create() creates this instance. | 116 // AppCacheJob::Create() creates this instance. |
97 friend class AppCacheJob; | 117 friend class AppCacheJob; |
98 | 118 |
99 AppCacheURLLoaderJob(const ResourceRequest& request, | 119 AppCacheURLLoaderJob(const ResourceRequest& request, |
| 120 AppCacheURLLoaderRequest* appcache_request, |
100 AppCacheStorage* storage); | 121 AppCacheStorage* storage); |
101 | 122 |
102 // AppCacheStorage::Delegate methods | 123 // AppCacheStorage::Delegate methods |
103 void OnResponseInfoLoaded(AppCacheResponseInfo* response_info, | 124 void OnResponseInfoLoaded(AppCacheResponseInfo* response_info, |
104 int64_t response_id) override; | 125 int64_t response_id) override; |
105 | 126 |
106 // AppCacheResponseReader completion callback | 127 // AppCacheResponseReader completion callback |
107 void OnReadComplete(int result); | 128 void OnReadComplete(int result); |
108 | 129 |
109 void OnConnectionError(); | 130 void OnConnectionError(); |
110 | 131 |
111 // Helper to send the AppCacheResponseInfo to the URLLoaderClient. | 132 // Helper to send the AppCacheResponseInfo to the URLLoaderClient. |
112 void SendResponseInfo(); | 133 void SendResponseInfo(); |
113 | 134 |
114 // Helper function to read the data from the AppCache. | 135 // Helper function to read the data from the AppCache. |
115 void ReadMore(); | 136 void ReadMore(); |
116 | 137 |
117 // Callback invoked when the data pipe can be written to. | 138 // Callback invoked when the data pipe can be written to. |
118 void OnResponseBodyStreamReady(MojoResult result); | 139 void OnResponseBodyStreamReady(MojoResult result); |
119 | 140 |
120 // Notifies the client about request completion. | 141 // Notifies the client about request completion. |
121 void NotifyCompleted(int error_code); | 142 void NotifyCompleted(int error_code); |
122 | 143 |
| 144 // Disconnects the mojo pipe to the network loader and releases related |
| 145 // resources. |
| 146 void DisconnectFromNetworkLoader(); |
| 147 |
123 // The current request. | 148 // The current request. |
124 ResourceRequest request_; | 149 ResourceRequest request_; |
125 | 150 |
126 base::WeakPtr<AppCacheStorage> storage_; | 151 base::WeakPtr<AppCacheStorage> storage_; |
127 | 152 |
128 // Time when the request started. | 153 // Time when the request started. |
129 base::TimeTicks start_time_tick_; | 154 base::TimeTicks start_time_tick_; |
130 | 155 |
131 // The AppCache manifest URL. | 156 // The AppCache manifest URL. |
132 GURL manifest_url_; | 157 GURL manifest_url_; |
133 | 158 |
134 // The AppCache id. | 159 // The AppCache id. |
135 int64_t cache_id_; | 160 int64_t cache_id_; |
136 | 161 |
137 AppCacheEntry entry_; | 162 AppCacheEntry entry_; |
138 | 163 |
139 // Set to true if we are loading fallback content. | 164 // Set to true if we are loading fallback content. |
140 bool is_fallback_; | 165 bool is_fallback_; |
141 | 166 |
142 // The data pipe used to transfer AppCache data to the client. | 167 // The data pipe used to transfer AppCache data to the client. |
143 mojo::DataPipe data_pipe_; | 168 mojo::DataPipe data_pipe_; |
144 | 169 |
145 // Binds the URLLoaderClient with us. | 170 // Binds the URLLoaderClient with us. |
146 mojo::Binding<mojom::URLLoader> binding_; | 171 mojo::Binding<mojom::URLLoader> binding_; |
147 | 172 |
148 // The URLLoaderClient pointer. We call this interface with notifications | 173 // The URLLoaderClient pointer. We call this interface with notifications |
149 // about the URL load | 174 // about the URL load |
150 mojom::URLLoaderClientPtr client_info_; | 175 mojom::URLLoaderClientPtr client_; |
151 | 176 |
152 // mojo data pipe entities. | 177 // mojo data pipe entities. |
153 mojo::ScopedDataPipeProducerHandle response_body_stream_; | 178 mojo::ScopedDataPipeProducerHandle response_body_stream_; |
154 | 179 |
155 scoped_refptr<NetToMojoPendingBuffer> pending_write_; | 180 scoped_refptr<NetToMojoPendingBuffer> pending_write_; |
156 | 181 |
157 mojo::SimpleWatcher writable_handle_watcher_; | 182 mojo::SimpleWatcher writable_handle_watcher_; |
158 | 183 |
159 // The Callback to be invoked in the network service land to indicate if | 184 // The Callback to be invoked in the network service land to indicate if |
160 // the main resource request can be serviced via the AppCache. | 185 // the main resource request can be serviced via the AppCache. |
161 LoaderCallback main_resource_loader_callback_; | 186 LoaderCallback main_resource_loader_callback_; |
162 | 187 |
163 // We own the AppCacheRequestHandler instance for subresource requests. | 188 // We own the AppCacheRequestHandler instance for subresource requests. |
164 std::unique_ptr<AppCacheRequestHandler> sub_resource_handler_; | 189 std::unique_ptr<AppCacheRequestHandler> sub_resource_handler_; |
165 | 190 |
166 scoped_refptr<URLLoaderFactoryGetter> default_url_loader_factory_getter_; | 191 scoped_refptr<URLLoaderFactoryGetter> default_url_loader_factory_getter_; |
167 | 192 |
168 // Holds subresource url loader information. | 193 // Holds subresource url loader information. |
169 std::unique_ptr<SubresourceLoadInfo> subresource_load_info_; | 194 std::unique_ptr<SubresourceLoadInfo> subresource_load_info_; |
170 | 195 |
171 // Timing information for the most recent request. Its start times are | 196 // Timing information for the most recent request. Its start times are |
172 // populated in DeliverAppCachedResponse(). | 197 // populated in DeliverAppCachedResponse(). |
173 net::LoadTimingInfo load_timing_info_; | 198 net::LoadTimingInfo load_timing_info_; |
174 | 199 |
175 // Used for subresource requests which go to the network. | 200 // Used for subresource requests which go to the network. |
176 mojom::URLLoaderAssociatedPtr network_loader_request_; | 201 mojom::URLLoaderAssociatedPtr network_loader_; |
177 | 202 |
178 // Binds the subresource URLLoaderClient with us. We can use the regular | 203 // Binds the subresource URLLoaderClient with us. We can use the regular |
179 // binding_ member above when we remove the need for the associated requests | 204 // binding_ member above when we remove the need for the associated requests |
180 // issue with URLLoaderFactory. | 205 // issue with URLLoaderFactory. |
181 std::unique_ptr<mojo::AssociatedBinding<mojom::URLLoader>> | 206 std::unique_ptr<mojo::AssociatedBinding<mojom::URLLoader>> |
182 associated_binding_; | 207 associated_binding_; |
183 | 208 |
| 209 // Network URLLoaderClient binding for subresource requests. |
| 210 mojo::Binding<mojom::URLLoaderClient> network_loader_client_binding_; |
| 211 |
| 212 // The AppCacheURLLoaderRequest instance. We use this to set the response |
| 213 // info when we receive it. |
| 214 AppCacheURLLoaderRequest* appcache_request_; |
| 215 |
184 DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoaderJob); | 216 DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoaderJob); |
185 }; | 217 }; |
186 | 218 |
187 } // namespace content | 219 } // namespace content |
188 | 220 |
189 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_URL_LOADER_JOB_H_ | 221 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_URL_LOADER_JOB_H_ |
OLD | NEW |