Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1116)

Side by Side Diff: net/url_request/url_request_job.h

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 NET_URL_REQUEST_URL_REQUEST_JOB_H_ 5 #ifndef NET_URL_REQUEST_URL_REQUEST_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_JOB_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_JOB_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/power_monitor/power_observer.h" 17 #include "base/power_monitor/power_observer.h"
18 #include "net/base/host_port_pair.h" 18 #include "net/base/host_port_pair.h"
19 #include "net/base/load_states.h" 19 #include "net/base/load_states.h"
20 #include "net/base/net_error_details.h" 20 #include "net/base/net_error_details.h"
21 #include "net/base/net_export.h" 21 #include "net/base/net_export.h"
22 #include "net/base/request_priority.h" 22 #include "net/base/request_priority.h"
23 #include "net/base/upload_progress.h" 23 #include "net/base/upload_progress.h"
24 #include "net/cookies/canonical_cookie.h" 24 #include "net/cookies/canonical_cookie.h"
25 #include "net/filter/stream_source.h"
25 #include "net/socket/connection_attempts.h" 26 #include "net/socket/connection_attempts.h"
26 #include "net/url_request/redirect_info.h" 27 #include "net/url_request/redirect_info.h"
27 #include "net/url_request/url_request.h" 28 #include "net/url_request/url_request.h"
28 #include "url/gurl.h" 29 #include "url/gurl.h"
29 30
30 namespace net { 31 namespace net {
31 32
32 class AuthChallengeInfo; 33 class AuthChallengeInfo;
33 class AuthCredentials; 34 class AuthCredentials;
34 class CookieOptions; 35 class CookieOptions;
35 class Filter; 36 class Filter;
36 class HttpRequestHeaders; 37 class HttpRequestHeaders;
37 class HttpResponseInfo; 38 class HttpResponseInfo;
38 class IOBuffer; 39 class IOBuffer;
39 struct LoadTimingInfo; 40 struct LoadTimingInfo;
40 class NetworkDelegate; 41 class NetworkDelegate;
41 class SSLCertRequestInfo; 42 class SSLCertRequestInfo;
42 class SSLInfo; 43 class SSLInfo;
43 class SSLPrivateKey; 44 class SSLPrivateKey;
44 class UploadDataStream; 45 class UploadDataStream;
45 class URLRequestStatus; 46 class URLRequestStatus;
46 class X509Certificate; 47 class X509Certificate;
47 48
48 class NET_EXPORT URLRequestJob : public base::PowerObserver { 49 class NET_EXPORT URLRequestJob : public base::PowerObserver {
49 public: 50 public:
51 typedef base::Callback<void(net::Error, size_t)> ReadRawCompleteCallback;
50 explicit URLRequestJob(URLRequest* request, 52 explicit URLRequestJob(URLRequest* request,
51 NetworkDelegate* network_delegate); 53 NetworkDelegate* network_delegate);
52 ~URLRequestJob() override; 54 ~URLRequestJob() override;
53 55
54 // Returns the request that owns this job. 56 // Returns the request that owns this job.
55 URLRequest* request() const { 57 URLRequest* request() const {
56 return request_; 58 return request_;
57 } 59 }
58 60
59 // Sets the upload data, most requests have no upload data, so this is a NOP. 61 // Sets the upload data, most requests have no upload data, so this is a NOP.
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 // Returns the cookie values included in the response, if applicable. 144 // Returns the cookie values included in the response, if applicable.
143 // Returns true if applicable. 145 // Returns true if applicable.
144 // NOTE: This removes the cookies from the job, so it will only return 146 // NOTE: This removes the cookies from the job, so it will only return
145 // useful results once per job. 147 // useful results once per job.
146 virtual bool GetResponseCookies(std::vector<std::string>* cookies); 148 virtual bool GetResponseCookies(std::vector<std::string>* cookies);
147 149
148 // Populates the network error details of the most recent origin that the 150 // Populates the network error details of the most recent origin that the
149 // network stack makes the request to. 151 // network stack makes the request to.
150 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const; 152 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const;
151 153
152 // Called to setup a stream filter for this request. An example of filter is 154 // Called to set up a StreamSource chain for this request.
153 // content encoding/decoding. 155 // Subclasses should return the appropriate first StreamSource of the chain,
154 // Subclasses should return the appropriate Filter, or NULL for no Filter. 156 // or nullptr if no StreamSource should be used. This class takes ownership of
155 // This class takes ownership of the returned Filter. 157 // the returned StreamSource.
156 // 158 virtual std::unique_ptr<StreamSource> SetupSource();
157 // The default implementation returns NULL.
158 virtual Filter* SetupFilter() const;
159 159
160 // Called to determine if this response is a redirect. Only makes sense 160 // Called to determine if this response is a redirect. Only makes sense
161 // for some types of requests. This method returns true if the response 161 // for some types of requests. This method returns true if the response
162 // is a redirect, and fills in the location param with the URL of the 162 // is a redirect, and fills in the location param with the URL of the
163 // redirect. The HTTP status code (e.g., 302) is filled into 163 // redirect. The HTTP status code (e.g., 302) is filled into
164 // |*http_status_code| to signify the type of redirect. 164 // |*http_status_code| to signify the type of redirect.
165 // 165 //
166 // The caller is responsible for following the redirect by setting up an 166 // The caller is responsible for following the redirect by setting up an
167 // appropriate replacement Job. Note that the redirected location may be 167 // appropriate replacement Job. Note that the redirected location may be
168 // invalid, the caller should be sure it can handle this. 168 // invalid, the caller should be sure it can handle this.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 // has failed or the response headers have been received. 245 // has failed or the response headers have been received.
246 virtual void GetConnectionAttempts(ConnectionAttempts* out) const; 246 virtual void GetConnectionAttempts(ConnectionAttempts* out) const;
247 247
248 // Given |policy|, |referrer|, and |redirect_destination|, returns the 248 // Given |policy|, |referrer|, and |redirect_destination|, returns the
249 // referrer URL mandated by |request|'s referrer policy. 249 // referrer URL mandated by |request|'s referrer policy.
250 static GURL ComputeReferrerForRedirect(URLRequest::ReferrerPolicy policy, 250 static GURL ComputeReferrerForRedirect(URLRequest::ReferrerPolicy policy,
251 const std::string& referrer, 251 const std::string& referrer,
252 const GURL& redirect_destination); 252 const GURL& redirect_destination);
253 253
254 protected: 254 protected:
255 // Callback for asynchronous reads from |source_|. See the documentation for
256 // |Read| above for the contract of this method.
257 void SourceReadComplete(Error error, size_t bytes_read);
258
255 // Notifies the job that a certificate is requested. 259 // Notifies the job that a certificate is requested.
256 void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info); 260 void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info);
257 261
258 // Notifies the job about an SSL certificate error. 262 // Notifies the job about an SSL certificate error.
259 void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal); 263 void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal);
260 264
261 // Delegates to URLRequest::Delegate. 265 // Delegates to URLRequest::Delegate.
262 bool CanGetCookies(const CookieList& cookie_list) const; 266 bool CanGetCookies(const CookieList& cookie_list) const;
263 267
264 // Delegates to URLRequest::Delegate. 268 // Delegates to URLRequest::Delegate.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 309
306 // Called to tell the job that a filter has successfully reached the end of 310 // Called to tell the job that a filter has successfully reached the end of
307 // the stream. 311 // the stream.
308 virtual void DoneReading(); 312 virtual void DoneReading();
309 313
310 // Called to tell the job that the body won't be read because it's a redirect. 314 // Called to tell the job that the body won't be read because it's a redirect.
311 // This is needed so that redirect headers can be cached even though their 315 // This is needed so that redirect headers can be cached even though their
312 // bodies are never read. 316 // bodies are never read.
313 virtual void DoneReadingRedirectResponse(); 317 virtual void DoneReadingRedirectResponse();
314 318
315 // Reads filtered data from the request. Returns OK if immediately successful,
316 // ERR_IO_PENDING if the request couldn't complete synchronously, and some
317 // other error code if the request failed synchronously. Note that this
318 // function can issue new asynchronous requests if needed, in which case it
319 // returns ERR_IO_PENDING. If this method completes synchronously,
320 // |*bytes_read| is the number of bytes output by the filter chain if this
321 // method returns OK, or zero if this method returns an error.
322 Error ReadFilteredData(int* bytes_read);
323
324 // Whether the response is being filtered in this job.
325 // Only valid after NotifyHeadersComplete() has been called.
326 bool HasFilter() { return filter_ != NULL; }
327
328 // At or near destruction time, a derived class may request that the filters
329 // be destroyed so that statistics can be gathered while the derived class is
330 // still present to assist in calculations. This is used by URLRequestHttpJob
331 // to get SDCH to emit stats.
332 void DestroyFilters();
333
334 // Provides derived classes with access to the request's network delegate. 319 // Provides derived classes with access to the request's network delegate.
335 NetworkDelegate* network_delegate() { return network_delegate_; } 320 NetworkDelegate* network_delegate() { return network_delegate_; }
336 321
337 // The status of the job. 322 // The status of the job.
338 const URLRequestStatus GetStatus(); 323 const URLRequestStatus GetStatus();
339 324
340 // Set the proxy server that was used, if any. 325 // Set the proxy server that was used, if any.
341 void SetProxyServer(const HostPortPair& proxy_server); 326 void SetProxyServer(const HostPortPair& proxy_server);
342 327
343 // The number of bytes read before passing to the filter. This value reflects 328 // The number of bytes read before passing to the filter. This value reflects
344 // bytes read even when there is no filter. 329 // bytes read even when there is no filter.
345 int64_t prefilter_bytes_read() const { return prefilter_bytes_read_; } 330 int64_t prefilter_bytes_read() const;
346 331
347 // The number of bytes read after passing through the filter. This value 332 // The number of bytes read after passing through the filter. This value
348 // reflects bytes read even when there is no filter. 333 // reflects bytes read even when there is no filter.
349 int64_t postfilter_bytes_read() const { return postfilter_bytes_read_; } 334 int64_t postfilter_bytes_read() const;
350 335
351 // Turns an integer result code into an Error and a count of bytes read. 336 // Turns an integer result code into an Error and a count of bytes read.
352 // The semantics are: 337 // The semantics are:
353 // |result| >= 0: |*error| == OK, |*count| == |result| 338 // |result| >= 0: |*error| == OK, |*count| == |result|
354 // |result| < 0: |*error| = |result|, |*count| == 0 339 // |result| < 0: |*error| = |result|, |*count| == 0
355 static void ConvertResultToError(int result, Error* error, int* count); 340 static void ConvertResultToError(int result, Error* error, int* count);
356 341
357 // Completion callback for raw reads. See |ReadRawData| for details. 342 // Completion callback for raw reads. See |ReadRawData| for details.
358 // |bytes_read| is either >= 0 to indicate a successful read and count of 343 // |bytes_read| is either >= 0 to indicate a successful read and count of
359 // bytes read, or < 0 to indicate an error. 344 // bytes read, or < 0 to indicate an error.
360 void ReadRawDataComplete(int bytes_read); 345 void ReadRawDataComplete(int bytes_read);
361 346
362 // The request that initiated this job. This value will never be nullptr. 347 // The request that initiated this job. This value will never be nullptr.
363 URLRequest* request_; 348 URLRequest* request_;
364 349
365 private: 350 private:
351 class URLRequestJobStreamSource;
Randy Smith (Not in Mondays) 2016/04/26 21:54:02 I don't see this class referred to in the header f
xunjieli 2016/07/20 21:00:48 The declaration here is needed because this class
366 // Set the status of the associated URLRequest. 352 // Set the status of the associated URLRequest.
367 // TODO(mmenke): Make the URLRequest manage its own status. 353 // TODO(mmenke): Make the URLRequest manage its own status.
368 void SetStatus(const URLRequestStatus& status); 354 void SetStatus(const URLRequestStatus& status);
369 355
370 // When data filtering is enabled, this function is used to read data
371 // for the filter. Returns a net error code to indicate if raw data was
372 // successfully read, an error happened, or the IO is pending.
373 Error ReadRawDataForFilter(int* bytes_read);
374
375 // Informs the filter chain that data has been read into its buffer.
376 void PushInputToFilter(int bytes_read);
377
378 // Invokes ReadRawData and records bytes read if the read completes 356 // Invokes ReadRawData and records bytes read if the read completes
379 // synchronously. 357 // synchronously.
380 Error ReadRawDataHelper(IOBuffer* buf, int buf_size, int* bytes_read); 358 Error ReadRawDataHelper(IOBuffer* buf,
359 int buf_size,
360 int* bytes_read,
361 const StreamSource::OnReadCompleteCallback& callback);
381 362
382 // Called in response to a redirect that was not canceled to follow the 363 // Called in response to a redirect that was not canceled to follow the
383 // redirect. The current job will be replaced with a new job loading the 364 // redirect. The current job will be replaced with a new job loading the
384 // given redirect destination. 365 // given redirect destination.
385 void FollowRedirect(const RedirectInfo& redirect_info); 366 void FollowRedirect(const RedirectInfo& redirect_info);
386 367
387 // Called after every raw read. If |bytes_read| is > 0, this indicates 368 // Called after every raw read. If |bytes_read| is > 0, this indicates
388 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| 369 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read|
389 // is 0, this indicates that there is no additional data to read. |error| 370 // is 0, this indicates that there is no additional data to read. |error|
390 // specifies whether an error occurred and no bytes were read. 371 // specifies whether an error occurred and no bytes were read.
391 void GatherRawReadStats(Error error, int bytes_read); 372 void GatherRawReadStats(Error error, int bytes_read);
392 373
393 // Updates the profiling info and notifies observers that an additional 374 // Updates the profiling info and notifies observers that an additional
394 // |bytes_read| unfiltered bytes have been read for this job. 375 // |bytes_read| unfiltered bytes have been read for this job.
395 void RecordBytesRead(int bytes_read); 376 void RecordBytesRead(int bytes_read);
396 377
397 // Called to query whether there is data available in the filter to be read
398 // out.
399 bool FilterHasData();
400
401 // NotifyDone marks that request is done. It is really a glorified 378 // NotifyDone marks that request is done. It is really a glorified
402 // set_status, but also does internal state checking and job tracking. It 379 // set_status, but also does internal state checking and job tracking. It
403 // should be called once per request, when the job is finished doing all IO. 380 // should be called once per request, when the job is finished doing all IO.
404 void NotifyDone(const URLRequestStatus& status); 381 void NotifyDone(const URLRequestStatus& status);
405 382
406 // Some work performed by NotifyDone must be completed asynchronously so 383 // Some work performed by NotifyDone must be completed asynchronously so
407 // as to avoid re-entering URLRequest::Delegate. This method performs that 384 // as to avoid re-entering URLRequest::Delegate. This method performs that
408 // work. 385 // work.
409 void CompleteNotifyDone(); 386 void CompleteNotifyDone();
410 387
411 // Subclasses may implement this method to record packet arrival times. 388 // Subclasses may implement this method to record packet arrival times.
412 // The default implementation does nothing. Only invoked when bytes have been 389 // The default implementation does nothing. Only invoked when bytes have been
413 // read since the last invocation. 390 // read since the last invocation.
414 virtual void UpdatePacketReadTimes(); 391 virtual void UpdatePacketReadTimes();
415 392
416 // Computes a new RedirectInfo based on receiving a redirect response of 393 // Computes a new RedirectInfo based on receiving a redirect response of
417 // |location| and |http_status_code|. 394 // |location| and |http_status_code|.
418 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code); 395 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code);
419 396
420 // Notify the network delegate that more bytes have been received or sent over 397 // Notify the network delegate that more bytes have been received or sent over
421 // the network, if bytes have been received or sent since the previous 398 // the network, if bytes have been received or sent since the previous
422 // notification. 399 // notification.
423 void MaybeNotifyNetworkBytes(); 400 void MaybeNotifyNetworkBytes();
424 401
425 // Indicates that the job is done producing data, either it has completed 402 // Indicates that the job is done producing data, either it has completed
426 // all the data or an error has been encountered. Set exclusively by 403 // all the data or an error has been encountered. Set exclusively by
427 // NotifyDone so that it is kept in sync with the request. 404 // NotifyDone so that it is kept in sync with the request.
428 bool done_; 405 bool done_;
429 406
430 int64_t prefilter_bytes_read_; 407 // The first StreamSource of the StreamSource chain used.
431 int64_t postfilter_bytes_read_; 408 std::unique_ptr<StreamSource> source_;
432
433 // The data stream filter which is enabled on demand.
434 std::unique_ptr<Filter> filter_;
435 409
436 // If the filter filled its output buffer, then there is a change that it 410 // If the filter filled its output buffer, then there is a change that it
437 // still has internal data to emit, and this flag is set. 411 // still has internal data to emit, and this flag is set.
438 bool filter_needs_more_output_space_; 412 bool filter_needs_more_output_space_;
439 413
440 // When we filter data, we receive data into the filter buffers. After
441 // processing the filtered data, we return the data in the caller's buffer.
442 // While the async IO is in progress, we save the user buffer here, and
443 // when the IO completes, we fill this in.
444 scoped_refptr<IOBuffer> filtered_read_buffer_;
445 int filtered_read_buffer_len_;
446
447 // We keep a pointer to the read buffer while asynchronous reads are 414 // We keep a pointer to the read buffer while asynchronous reads are
448 // in progress, so we are able to pass those bytes to job observers. 415 // in progress, so we are able to pass those bytes to job observers.
449 scoped_refptr<IOBuffer> raw_read_buffer_; 416 scoped_refptr<IOBuffer> raw_read_buffer_;
450 417
451 // Used by HandleResponseIfNecessary to track whether we've sent the 418 // Used by HandleResponseIfNecessary to track whether we've sent the
452 // OnResponseStarted callback and potentially redirect callbacks as well. 419 // OnResponseStarted callback and potentially redirect callbacks as well.
453 bool has_handled_response_; 420 bool has_handled_response_;
454 421
455 // Expected content size 422 // Expected content size
456 int64_t expected_content_size_; 423 int64_t expected_content_size_;
457 424
458 // Set when a redirect is deferred. 425 // Set when a redirect is deferred.
459 RedirectInfo deferred_redirect_info_; 426 RedirectInfo deferred_redirect_info_;
460 427
461 // The network delegate to use with this request, if any. 428 // The network delegate to use with this request, if any.
462 NetworkDelegate* network_delegate_; 429 NetworkDelegate* network_delegate_;
463 430
464 // The value from GetTotalReceivedBytes() the last time 431 // The value from GetTotalReceivedBytes() the last time
465 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been 432 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been
466 // newly received since the last notification. 433 // newly received since the last notification.
467 int64_t last_notified_total_received_bytes_; 434 int64_t last_notified_total_received_bytes_;
468 435
469 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes() 436 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes()
470 // was called. Used to calculate how bytes have been newly sent since the last 437 // was called. Used to calculate how bytes have been newly sent since the last
471 // notification. 438 // notification.
472 int64_t last_notified_total_sent_bytes_; 439 int64_t last_notified_total_sent_bytes_;
473 440
441 ReadRawCompleteCallback read_raw_callback_;
442
443 // Raw network bytes read from job subclass.
444 size_t raw_bytes_read_;
445
446 size_t postfilter_bytes_read_;
447
474 base::WeakPtrFactory<URLRequestJob> weak_factory_; 448 base::WeakPtrFactory<URLRequestJob> weak_factory_;
475 449
476 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); 450 DISALLOW_COPY_AND_ASSIGN(URLRequestJob);
477 }; 451 };
478 452
479 } // namespace net 453 } // namespace net
480 454
481 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ 455 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698