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/gtest_prod_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/loader/resource_handler.h"
12 #include "content/browser/ssl/ssl_error_handler.h"
13 #include "content/common/content_export.h"
14 #include "content/public/browser/resource_controller.h"
15 #include "content/public/common/signed_certificate_timestamp_id_and_status.h"
16 #include "net/url_request/url_request.h"
19 class ResourceDispatcherHostLoginDelegate
;
20 class ResourceLoaderDelegate
;
21 class ResourceRequestInfoImpl
;
22 class SSLClientAuthHandler
;
24 // This class is responsible for driving the URLRequest (i.e., calling Start,
25 // Read, and servicing events). It has a ResourceHandler, which is typically a
26 // chain of ResourceHandlers, and is the ResourceController for its handler.
27 class CONTENT_EXPORT ResourceLoader
: public net::URLRequest::Delegate
,
28 public SSLErrorHandler::Delegate
,
29 public ResourceController
{
31 ResourceLoader(scoped_ptr
<net::URLRequest
> request
,
32 scoped_ptr
<ResourceHandler
> handler
,
33 ResourceLoaderDelegate
* delegate
);
34 virtual ~ResourceLoader();
37 void CancelRequest(bool from_renderer
);
39 void ReportUploadProgress();
41 bool is_transferring() const { return is_transferring_
; }
42 void MarkAsTransferring();
43 void CompleteTransfer();
45 net::URLRequest
* request() { return request_
.get(); }
46 ResourceRequestInfoImpl
* GetRequestInfo();
48 void ClearLoginDelegate();
49 void ClearSSLClientAuthHandler();
51 // IPC message handlers:
52 void OnUploadProgressACK();
55 FRIEND_TEST_ALL_PREFIXES(ResourceLoaderTest
, ClientCertStoreLookup
);
56 FRIEND_TEST_ALL_PREFIXES(ResourceLoaderTest
, ClientCertStoreNull
);
58 // net::URLRequest::Delegate implementation:
59 virtual void OnReceivedRedirect(net::URLRequest
* request
,
60 const net::RedirectInfo
& redirect_info
,
61 bool* defer
) OVERRIDE
;
62 virtual void OnAuthRequired(net::URLRequest
* request
,
63 net::AuthChallengeInfo
* info
) OVERRIDE
;
64 virtual void OnCertificateRequested(net::URLRequest
* request
,
65 net::SSLCertRequestInfo
* info
) OVERRIDE
;
66 virtual void OnSSLCertificateError(net::URLRequest
* request
,
67 const net::SSLInfo
& info
,
69 virtual void OnBeforeNetworkStart(net::URLRequest
* request
,
70 bool* defer
) OVERRIDE
;
71 virtual void OnResponseStarted(net::URLRequest
* request
) OVERRIDE
;
72 virtual void OnReadCompleted(net::URLRequest
* request
,
73 int bytes_read
) OVERRIDE
;
75 // SSLErrorHandler::Delegate implementation:
76 virtual void CancelSSLRequest(const GlobalRequestID
& id
,
78 const net::SSLInfo
* ssl_info
) OVERRIDE
;
79 virtual void ContinueSSLRequest(const GlobalRequestID
& id
) OVERRIDE
;
81 // ResourceController implementation:
82 virtual void Resume() OVERRIDE
;
83 virtual void Cancel() OVERRIDE
;
84 virtual void CancelAndIgnore() OVERRIDE
;
85 virtual void CancelWithError(int error_code
) OVERRIDE
;
87 void StartRequestInternal();
88 void CancelRequestInternal(int error
, bool from_renderer
);
89 // Stores the SignedCertificateTimestamps held in |sct_list| in the
90 // SignedCertificateTimestampStore singleton, associated with |process_id|.
91 // On return, |sct_ids| contains the assigned ID and verification status of
92 // each SignedCertificateTimestamp.
93 void StoreSignedCertificateTimestamps(
94 const net::SignedCertificateTimestampAndStatusList
& sct_list
,
96 SignedCertificateTimestampIDStatusList
* sct_ids
);
97 void CompleteResponseStarted();
98 void StartReading(bool is_continuation
);
100 void ReadMore(int* bytes_read
);
101 // Passes a read result to the handler.
102 void CompleteRead(int bytes_read
);
103 void ResponseCompleted();
104 void CallDidFinishLoading();
105 void RecordHistograms();
107 bool is_deferred() const { return deferred_stage_
!= DEFERRED_NONE
; }
109 // Used for categorizing loading of prefetches for reporting in histograms.
110 // NOTE: This enumeration is used in histograms, so please do not add entries
112 enum PrefetchStatus
{
114 STATUS_SUCCESS_FROM_CACHE
,
115 STATUS_SUCCESS_FROM_NETWORK
,
123 DEFERRED_NETWORK_START
,
126 DEFERRED_RESPONSE_COMPLETE
,
129 DeferredStage deferred_stage_
;
131 scoped_ptr
<net::URLRequest
> request_
;
132 scoped_ptr
<ResourceHandler
> handler_
;
133 ResourceLoaderDelegate
* delegate_
;
135 scoped_refptr
<ResourceDispatcherHostLoginDelegate
> login_delegate_
;
136 scoped_refptr
<SSLClientAuthHandler
> ssl_client_auth_handler_
;
138 uint64 last_upload_position_
;
139 bool waiting_for_upload_progress_ack_
;
140 base::TimeTicks last_upload_ticks_
;
141 base::TimeTicks read_deferral_start_time_
;
143 // Indicates that we are in a state of being transferred to a new downstream
144 // consumer. We are waiting for a notification to complete the transfer, at
145 // which point we'll receive a new ResourceHandler.
146 bool is_transferring_
;
148 base::WeakPtrFactory
<ResourceLoader
> weak_ptr_factory_
;
150 DISALLOW_COPY_AND_ASSIGN(ResourceLoader
);
153 } // namespace content
155 #endif // CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_