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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_
6 #define CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/timer/timer.h"
11 #include "content/browser/loader/resource_handler.h"
12 #include "content/browser/ssl/ssl_client_auth_handler.h"
13 #include "content/browser/ssl/ssl_error_handler.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/resource_controller.h"
16 #include "content/public/common/signed_certificate_timestamp_id_and_status.h"
17 #include "net/url_request/url_request.h"
20 class X509Certificate
;
24 class ResourceDispatcherHostLoginDelegate
;
25 class ResourceLoaderDelegate
;
26 class ResourceRequestInfoImpl
;
28 // This class is responsible for driving the URLRequest (i.e., calling Start,
29 // Read, and servicing events). It has a ResourceHandler, which is typically a
30 // chain of ResourceHandlers, and is the ResourceController for its handler.
31 class CONTENT_EXPORT ResourceLoader
: public net::URLRequest::Delegate
,
32 public SSLErrorHandler::Delegate
,
33 public SSLClientAuthHandler::Delegate
,
34 public ResourceController
{
36 ResourceLoader(scoped_ptr
<net::URLRequest
> request
,
37 scoped_ptr
<ResourceHandler
> handler
,
38 ResourceLoaderDelegate
* delegate
);
39 ~ResourceLoader() override
;
42 void CancelRequest(bool from_renderer
);
44 void ReportUploadProgress();
46 bool is_transferring() const { return is_transferring_
; }
47 void MarkAsTransferring();
48 void CompleteTransfer();
50 net::URLRequest
* request() { return request_
.get(); }
51 ResourceRequestInfoImpl
* GetRequestInfo();
53 void ClearLoginDelegate();
55 // IPC message handlers:
56 void OnUploadProgressACK();
59 // net::URLRequest::Delegate implementation:
60 void OnReceivedRedirect(net::URLRequest
* request
,
61 const net::RedirectInfo
& redirect_info
,
62 bool* defer
) override
;
63 void OnAuthRequired(net::URLRequest
* request
,
64 net::AuthChallengeInfo
* info
) override
;
65 void OnCertificateRequested(net::URLRequest
* request
,
66 net::SSLCertRequestInfo
* info
) override
;
67 void OnSSLCertificateError(net::URLRequest
* request
,
68 const net::SSLInfo
& info
,
70 void OnBeforeNetworkStart(net::URLRequest
* request
, bool* defer
) override
;
71 void OnResponseStarted(net::URLRequest
* request
) override
;
72 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
74 // SSLErrorHandler::Delegate implementation:
75 void CancelSSLRequest(int error
, const net::SSLInfo
* ssl_info
) override
;
76 void ContinueSSLRequest() override
;
78 // SSLClientAuthHandler::Delegate implementation.
79 void ContinueWithCertificate(net::X509Certificate
* cert
) override
;
80 void CancelCertificateSelection() override
;
82 // ResourceController implementation:
83 void Resume() override
;
84 void Cancel() override
;
85 void CancelAndIgnore() override
;
86 void CancelWithError(int error_code
) override
;
88 void StartRequestInternal();
89 void CancelRequestInternal(int error
, bool from_renderer
);
90 // Stores the SignedCertificateTimestamps held in |sct_list| in the
91 // SignedCertificateTimestampStore singleton, associated with |process_id|.
92 // On return, |sct_ids| contains the assigned ID and verification status of
93 // each SignedCertificateTimestamp.
94 void StoreSignedCertificateTimestamps(
95 const net::SignedCertificateTimestampAndStatusList
& sct_list
,
97 SignedCertificateTimestampIDStatusList
* sct_ids
);
98 void CompleteResponseStarted();
99 void StartReading(bool is_continuation
);
100 void ResumeReading();
101 void ReadMore(int* bytes_read
);
102 // Passes a read result to the handler.
103 void CompleteRead(int bytes_read
);
104 void ResponseCompleted();
105 void CallDidFinishLoading();
106 void RecordHistograms();
108 bool is_deferred() const { return deferred_stage_
!= DEFERRED_NONE
; }
110 // Used for categorizing loading of prefetches for reporting in histograms.
111 // NOTE: This enumeration is used in histograms, so please do not add entries
113 enum PrefetchStatus
{
115 STATUS_SUCCESS_FROM_CACHE
,
116 STATUS_SUCCESS_FROM_NETWORK
,
124 DEFERRED_NETWORK_START
,
127 DEFERRED_RESPONSE_COMPLETE
,
130 DeferredStage deferred_stage_
;
132 scoped_ptr
<net::URLRequest
> request_
;
133 scoped_ptr
<ResourceHandler
> handler_
;
134 ResourceLoaderDelegate
* delegate_
;
136 scoped_refptr
<ResourceDispatcherHostLoginDelegate
> login_delegate_
;
137 scoped_ptr
<SSLClientAuthHandler
> ssl_client_auth_handler_
;
139 uint64 last_upload_position_
;
140 bool waiting_for_upload_progress_ack_
;
141 base::TimeTicks last_upload_ticks_
;
142 base::TimeTicks read_deferral_start_time_
;
144 // Indicates that we are in a state of being transferred to a new downstream
145 // consumer. We are waiting for a notification to complete the transfer, at
146 // which point we'll receive a new ResourceHandler.
147 bool is_transferring_
;
149 // Instrumentation add to investigate http://crbug.com/503306.
150 // TODO(mmenke): Remove once bug is fixed.
151 int times_cancelled_before_request_start_
;
152 bool started_request_
;
153 int times_cancelled_after_request_start_
;
155 base::RepeatingTimer
<ResourceLoader
> progress_timer_
;
157 base::WeakPtrFactory
<ResourceLoader
> weak_ptr_factory_
;
159 DISALLOW_COPY_AND_ASSIGN(ResourceLoader
);
162 } // namespace content
164 #endif // CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_