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

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: rebased Created 4 years, 4 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
« no previous file with comments | « net/url_request/url_request_http_job_unittest.cc ('k') | net/url_request/url_request_job.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/source_stream.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;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 137
137 // Gets the remote endpoint that the network stack is currently fetching the 138 // Gets the remote endpoint that the network stack is currently fetching the
138 // URL from. Returns true and fills in |endpoint| if it is available; returns 139 // URL from. Returns true and fills in |endpoint| if it is available; returns
139 // false and leaves |endpoint| unchanged if it is unavailable. 140 // false and leaves |endpoint| unchanged if it is unavailable.
140 virtual bool GetRemoteEndpoint(IPEndPoint* endpoint) const; 141 virtual bool GetRemoteEndpoint(IPEndPoint* endpoint) const;
141 142
142 // Populates the network error details of the most recent origin that the 143 // Populates the network error details of the most recent origin that the
143 // network stack makes the request to. 144 // network stack makes the request to.
144 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const; 145 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const;
145 146
146 // Called to setup a stream filter for this request. An example of filter is
147 // content encoding/decoding.
148 // Subclasses should return the appropriate Filter, or NULL for no Filter.
149 // This class takes ownership of the returned Filter.
150 //
151 // The default implementation returns NULL.
152 virtual std::unique_ptr<Filter> SetupFilter() const;
153
154 // Called to determine if this response is a redirect. Only makes sense 147 // Called to determine if this response is a redirect. Only makes sense
155 // for some types of requests. This method returns true if the response 148 // for some types of requests. This method returns true if the response
156 // is a redirect, and fills in the location param with the URL of the 149 // is a redirect, and fills in the location param with the URL of the
157 // redirect. The HTTP status code (e.g., 302) is filled into 150 // redirect. The HTTP status code (e.g., 302) is filled into
158 // |*http_status_code| to signify the type of redirect. 151 // |*http_status_code| to signify the type of redirect.
159 // 152 //
160 // The caller is responsible for following the redirect by setting up an 153 // The caller is responsible for following the redirect by setting up an
161 // appropriate replacement Job. Note that the redirected location may be 154 // appropriate replacement Job. Note that the redirected location may be
162 // invalid, the caller should be sure it can handle this. 155 // invalid, the caller should be sure it can handle this.
163 // 156 //
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 293
301 // Called to tell the job that a filter has successfully reached the end of 294 // Called to tell the job that a filter has successfully reached the end of
302 // the stream. 295 // the stream.
303 virtual void DoneReading(); 296 virtual void DoneReading();
304 297
305 // Called to tell the job that the body won't be read because it's a redirect. 298 // Called to tell the job that the body won't be read because it's a redirect.
306 // This is needed so that redirect headers can be cached even though their 299 // This is needed so that redirect headers can be cached even though their
307 // bodies are never read. 300 // bodies are never read.
308 virtual void DoneReadingRedirectResponse(); 301 virtual void DoneReadingRedirectResponse();
309 302
310 // Reads filtered data from the request. Returns OK if immediately successful, 303 // Called to set up a SourceStream chain for this request.
311 // ERR_IO_PENDING if the request couldn't complete synchronously, and some 304 // Subclasses should return the appropriate last SourceStream of the chain,
312 // other error code if the request failed synchronously. Note that this 305 // or nullptr on error.
313 // function can issue new asynchronous requests if needed, in which case it 306 virtual std::unique_ptr<SourceStream> SetUpSourceStream();
314 // returns ERR_IO_PENDING. If this method completes synchronously,
315 // |*bytes_read| is the number of bytes output by the filter chain if this
316 // method returns OK, or zero if this method returns an error.
317 Error ReadFilteredData(int* bytes_read);
318
319 // Whether the response is being filtered in this job.
320 // Only valid after NotifyHeadersComplete() has been called.
321 bool HasFilter() { return filter_ != NULL; }
322
323 // At or near destruction time, a derived class may request that the filters
324 // be destroyed so that statistics can be gathered while the derived class is
325 // still present to assist in calculations. This is used by URLRequestHttpJob
326 // to get SDCH to emit stats.
327 void DestroyFilters();
328 307
329 // Provides derived classes with access to the request's network delegate. 308 // Provides derived classes with access to the request's network delegate.
330 NetworkDelegate* network_delegate() { return network_delegate_; } 309 NetworkDelegate* network_delegate() { return network_delegate_; }
331 310
332 // The status of the job. 311 // The status of the job.
333 const URLRequestStatus GetStatus(); 312 const URLRequestStatus GetStatus();
334 313
335 // Set the proxy server that was used, if any. 314 // Set the proxy server that was used, if any.
336 void SetProxyServer(const HostPortPair& proxy_server); 315 void SetProxyServer(const HostPortPair& proxy_server);
337 316
338 // The number of bytes read after passing through the filter. This value 317 // The number of bytes read after passing through the filter. This value
339 // reflects bytes read even when there is no filter. 318 // reflects bytes read even when there is no filter.
340 int64_t postfilter_bytes_read() const { return postfilter_bytes_read_; } 319 int64_t postfilter_bytes_read() const { return postfilter_bytes_read_; }
341 320
342 // Turns an integer result code into an Error and a count of bytes read. 321 // Turns an integer result code into an Error and a count of bytes read.
343 // The semantics are: 322 // The semantics are:
344 // |result| >= 0: |*error| == OK, |*count| == |result| 323 // |result| >= 0: |*error| == OK, |*count| == |result|
345 // |result| < 0: |*error| = |result|, |*count| == 0 324 // |result| < 0: |*error| = |result|, |*count| == 0
346 static void ConvertResultToError(int result, Error* error, int* count); 325 static void ConvertResultToError(int result, Error* error, int* count);
347 326
348 // Completion callback for raw reads. See |ReadRawData| for details. 327 // Completion callback for raw reads. See |ReadRawData| for details.
349 // |bytes_read| is either >= 0 to indicate a successful read and count of 328 // |bytes_read| is either >= 0 to indicate a successful read and count of
350 // bytes read, or < 0 to indicate an error. 329 // bytes read, or < 0 to indicate an error.
351 void ReadRawDataComplete(int bytes_read); 330 void ReadRawDataComplete(int bytes_read);
352 331
353 // The request that initiated this job. This value will never be nullptr. 332 // The request that initiated this job. This value will never be nullptr.
354 URLRequest* request_; 333 URLRequest* request_;
355 334
356 private: 335 private:
336 class URLRequestJobSourceStream;
337
357 // Set the status of the associated URLRequest. 338 // Set the status of the associated URLRequest.
358 // TODO(mmenke): Make the URLRequest manage its own status. 339 // TODO(mmenke): Make the URLRequest manage its own status.
359 void SetStatus(const URLRequestStatus& status); 340 void SetStatus(const URLRequestStatus& status);
360 341
361 // When data filtering is enabled, this function is used to read data 342 // Helper method used to perform tasks after reading from |source_stream_| is
362 // for the filter. Returns a net error code to indicate if raw data was 343 // completed. |synchronous| true if the read completed synchronously.
363 // successfully read, an error happened, or the IO is pending. 344 // See the documentation for |Read| above for the contract of this method.
364 Error ReadRawDataForFilter(int* bytes_read); 345 void SourceStreamReadComplete(bool synchronous, int result);
365
366 // Informs the filter chain that data has been read into its buffer.
367 void PushInputToFilter(int bytes_read);
368 346
369 // Invokes ReadRawData and records bytes read if the read completes 347 // Invokes ReadRawData and records bytes read if the read completes
370 // synchronously. 348 // synchronously.
371 Error ReadRawDataHelper(IOBuffer* buf, int buf_size, int* bytes_read); 349 int ReadRawDataHelper(IOBuffer* buf,
350 int buf_size,
351 const CompletionCallback& callback);
372 352
373 // Called in response to a redirect that was not canceled to follow the 353 // Called in response to a redirect that was not canceled to follow the
374 // redirect. The current job will be replaced with a new job loading the 354 // redirect. The current job will be replaced with a new job loading the
375 // given redirect destination. 355 // given redirect destination.
376 void FollowRedirect(const RedirectInfo& redirect_info); 356 void FollowRedirect(const RedirectInfo& redirect_info);
377 357
378 // Called after every raw read. If |bytes_read| is > 0, this indicates 358 // Called after every raw read. If |bytes_read| is > 0, this indicates
379 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| 359 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read|
380 // is 0, this indicates that there is no additional data to read. |error| 360 // is 0, this indicates that there is no additional data to read.
381 // specifies whether an error occurred and no bytes were read. 361 // If |bytes_read| is negative, no bytes were read.
382 void GatherRawReadStats(Error error, int bytes_read); 362 void GatherRawReadStats(int bytes_read);
383 363
384 // Updates the profiling info and notifies observers that an additional 364 // Updates the profiling info and notifies observers that an additional
385 // |bytes_read| unfiltered bytes have been read for this job. 365 // |bytes_read| unfiltered bytes have been read for this job.
386 void RecordBytesRead(int bytes_read); 366 void RecordBytesRead(int bytes_read);
387 367
388 // Called to query whether there is data available in the filter to be read
389 // out.
390 bool FilterHasData();
391
392 // NotifyDone marks that request is done. It is really a glorified 368 // NotifyDone marks that request is done. It is really a glorified
393 // set_status, but also does internal state checking and job tracking. It 369 // set_status, but also does internal state checking and job tracking. It
394 // should be called once per request, when the job is finished doing all IO. 370 // should be called once per request, when the job is finished doing all IO.
395 void NotifyDone(const URLRequestStatus& status); 371 void NotifyDone(const URLRequestStatus& status);
396 372
397 // Some work performed by NotifyDone must be completed asynchronously so 373 // Some work performed by NotifyDone must be completed asynchronously so
398 // as to avoid re-entering URLRequest::Delegate. This method performs that 374 // as to avoid re-entering URLRequest::Delegate. This method performs that
399 // work. 375 // work.
400 void CompleteNotifyDone(); 376 void CompleteNotifyDone();
401 377
402 // Subclasses may implement this method to record packet arrival times. 378 // Subclasses may implement this method to record packet arrival times.
403 // The default implementation does nothing. Only invoked when bytes have been 379 // The default implementation does nothing. Only invoked when bytes have been
404 // read since the last invocation. 380 // read since the last invocation.
405 virtual void UpdatePacketReadTimes(); 381 virtual void UpdatePacketReadTimes();
406 382
407 // Computes a new RedirectInfo based on receiving a redirect response of 383 // Computes a new RedirectInfo based on receiving a redirect response of
408 // |location| and |http_status_code|. 384 // |location| and |http_status_code|.
409 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code); 385 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code);
410 386
411 // Notify the network delegate that more bytes have been received or sent over 387 // Notify the network delegate that more bytes have been received or sent over
412 // the network, if bytes have been received or sent since the previous 388 // the network, if bytes have been received or sent since the previous
413 // notification. 389 // notification.
414 void MaybeNotifyNetworkBytes(); 390 void MaybeNotifyNetworkBytes();
415 391
416 // Indicates that the job is done producing data, either it has completed 392 // Indicates that the job is done producing data, either it has completed
417 // all the data or an error has been encountered. Set exclusively by 393 // all the data or an error has been encountered. Set exclusively by
418 // NotifyDone so that it is kept in sync with the request. 394 // NotifyDone so that it is kept in sync with the request.
419 bool done_; 395 bool done_;
420 396
397 // Number of raw network bytes read from job subclass.
421 int64_t prefilter_bytes_read_; 398 int64_t prefilter_bytes_read_;
399
400 // Number of bytes after applying |source_stream_| filters.
422 int64_t postfilter_bytes_read_; 401 int64_t postfilter_bytes_read_;
423 402
424 // The data stream filter which is enabled on demand. 403 // The first SourceStream of the SourceStream chain used.
425 std::unique_ptr<Filter> filter_; 404 std::unique_ptr<SourceStream> source_stream_;
426 405
427 // If the filter filled its output buffer, then there is a change that it 406 // Keep a reference to the buffer passed in via URLRequestJob::Read() so it
428 // still has internal data to emit, and this flag is set. 407 // doesn't get destroyed when the read has not completed.
429 bool filter_needs_more_output_space_; 408 scoped_refptr<IOBuffer> pending_read_buffer_;
430
431 // When we filter data, we receive data into the filter buffers. After
432 // processing the filtered data, we return the data in the caller's buffer.
433 // While the async IO is in progress, we save the user buffer here, and
434 // when the IO completes, we fill this in.
435 scoped_refptr<IOBuffer> filtered_read_buffer_;
436 int filtered_read_buffer_len_;
437 409
438 // We keep a pointer to the read buffer while asynchronous reads are 410 // We keep a pointer to the read buffer while asynchronous reads are
439 // in progress, so we are able to pass those bytes to job observers. 411 // in progress, so we are able to pass those bytes to job observers.
440 scoped_refptr<IOBuffer> raw_read_buffer_; 412 scoped_refptr<IOBuffer> raw_read_buffer_;
441 413
442 // Used by HandleResponseIfNecessary to track whether we've sent the 414 // Used by HandleResponseIfNecessary to track whether we've sent the
443 // OnResponseStarted callback and potentially redirect callbacks as well. 415 // OnResponseStarted callback and potentially redirect callbacks as well.
444 bool has_handled_response_; 416 bool has_handled_response_;
445 417
446 // Expected content size 418 // Expected content size
447 int64_t expected_content_size_; 419 int64_t expected_content_size_;
448 420
449 // Set when a redirect is deferred. 421 // Set when a redirect is deferred.
450 RedirectInfo deferred_redirect_info_; 422 RedirectInfo deferred_redirect_info_;
451 423
452 // The network delegate to use with this request, if any. 424 // The network delegate to use with this request, if any.
453 NetworkDelegate* network_delegate_; 425 NetworkDelegate* network_delegate_;
454 426
455 // The value from GetTotalReceivedBytes() the last time 427 // The value from GetTotalReceivedBytes() the last time
456 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been 428 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been
457 // newly received since the last notification. 429 // newly received since the last notification.
458 int64_t last_notified_total_received_bytes_; 430 int64_t last_notified_total_received_bytes_;
459 431
460 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes() 432 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes()
461 // was called. Used to calculate how bytes have been newly sent since the last 433 // was called. Used to calculate how bytes have been newly sent since the last
462 // notification. 434 // notification.
463 int64_t last_notified_total_sent_bytes_; 435 int64_t last_notified_total_sent_bytes_;
464 436
437 // Non-null if ReadRawData() returned ERR_IO_PENDING, and the read has not
438 // completed.
439 CompletionCallback read_raw_callback_;
440
465 base::WeakPtrFactory<URLRequestJob> weak_factory_; 441 base::WeakPtrFactory<URLRequestJob> weak_factory_;
466 442
467 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); 443 DISALLOW_COPY_AND_ASSIGN(URLRequestJob);
468 }; 444 };
469 445
470 } // namespace net 446 } // namespace net
471 447
472 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ 448 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_
OLDNEW
« no previous file with comments | « net/url_request/url_request_http_job_unittest.cc ('k') | net/url_request/url_request_job.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698