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

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

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

Powered by Google App Engine
This is Rietveld 408576698