OLD | NEW |
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 CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_ |
6 #define CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_ | 6 #define CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "content/browser/ssl/ssl_client_auth_handler.h" | 14 #include "content/browser/ssl/ssl_client_auth_handler.h" |
15 #include "content/browser/ssl/ssl_error_handler.h" | 15 #include "content/browser/ssl/ssl_error_handler.h" |
16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
17 #include "content/public/browser/resource_controller.h" | |
18 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
19 | 18 |
20 namespace net { | 19 namespace net { |
21 class X509Certificate; | 20 class X509Certificate; |
22 } | 21 } |
23 | 22 |
24 namespace content { | 23 namespace content { |
25 class ResourceDispatcherHostLoginDelegate; | 24 class ResourceDispatcherHostLoginDelegate; |
26 class ResourceHandler; | 25 class ResourceHandler; |
27 class ResourceLoaderDelegate; | 26 class ResourceLoaderDelegate; |
28 class ResourceRequestInfoImpl; | 27 class ResourceRequestInfoImpl; |
29 | 28 |
30 // This class is responsible for driving the URLRequest (i.e., calling Start, | 29 // This class is responsible for driving the URLRequest (i.e., calling Start, |
31 // Read, and servicing events). It has a ResourceHandler, which is typically a | 30 // Read, and servicing events). It has a ResourceHandler, which is typically a |
32 // chain of ResourceHandlers, and is the ResourceController for its handler. | 31 // chain of ResourceHandlers, and is the ResourceController for its handler. |
33 class CONTENT_EXPORT ResourceLoader : public net::URLRequest::Delegate, | 32 class CONTENT_EXPORT ResourceLoader : public net::URLRequest::Delegate, |
34 public SSLErrorHandler::Delegate, | 33 public SSLErrorHandler::Delegate, |
35 public SSLClientAuthHandler::Delegate, | 34 public SSLClientAuthHandler::Delegate { |
36 public ResourceController { | |
37 public: | 35 public: |
38 ResourceLoader(std::unique_ptr<net::URLRequest> request, | 36 ResourceLoader(std::unique_ptr<net::URLRequest> request, |
39 std::unique_ptr<ResourceHandler> handler, | 37 std::unique_ptr<ResourceHandler> handler, |
40 ResourceLoaderDelegate* delegate); | 38 ResourceLoaderDelegate* delegate); |
41 ~ResourceLoader() override; | 39 ~ResourceLoader() override; |
42 | 40 |
43 void StartRequest(); | 41 void StartRequest(); |
44 void CancelRequest(bool from_renderer); | 42 void CancelRequest(bool from_renderer); |
45 | 43 |
46 bool is_transferring() const { return is_transferring_; } | 44 bool is_transferring() const { return is_transferring_; } |
47 void MarkAsTransferring(const base::Closure& on_transfer_complete_callback); | 45 void MarkAsTransferring(const base::Closure& on_transfer_complete_callback); |
48 void CompleteTransfer(); | 46 void CompleteTransfer(); |
49 | 47 |
50 net::URLRequest* request() { return request_.get(); } | 48 net::URLRequest* request() { return request_.get(); } |
51 ResourceRequestInfoImpl* GetRequestInfo(); | 49 ResourceRequestInfoImpl* GetRequestInfo(); |
52 | 50 |
53 void ClearLoginDelegate(); | 51 void ClearLoginDelegate(); |
54 | 52 |
55 private: | 53 private: |
| 54 // ResourceController implementation for the ResourceLoader. |
| 55 class Controller; |
| 56 |
56 // net::URLRequest::Delegate implementation: | 57 // net::URLRequest::Delegate implementation: |
57 void OnReceivedRedirect(net::URLRequest* request, | 58 void OnReceivedRedirect(net::URLRequest* request, |
58 const net::RedirectInfo& redirect_info, | 59 const net::RedirectInfo& redirect_info, |
59 bool* defer) override; | 60 bool* defer) override; |
60 void OnAuthRequired(net::URLRequest* request, | 61 void OnAuthRequired(net::URLRequest* request, |
61 net::AuthChallengeInfo* info) override; | 62 net::AuthChallengeInfo* info) override; |
62 void OnCertificateRequested(net::URLRequest* request, | 63 void OnCertificateRequested(net::URLRequest* request, |
63 net::SSLCertRequestInfo* info) override; | 64 net::SSLCertRequestInfo* info) override; |
64 void OnSSLCertificateError(net::URLRequest* request, | 65 void OnSSLCertificateError(net::URLRequest* request, |
65 const net::SSLInfo& info, | 66 const net::SSLInfo& info, |
66 bool fatal) override; | 67 bool fatal) override; |
67 void OnResponseStarted(net::URLRequest* request) override; | 68 void OnResponseStarted(net::URLRequest* request) override; |
68 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | 69 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
69 | 70 |
70 // SSLErrorHandler::Delegate implementation: | 71 // SSLErrorHandler::Delegate implementation: |
71 void CancelSSLRequest(int error, const net::SSLInfo* ssl_info) override; | 72 void CancelSSLRequest(int error, const net::SSLInfo* ssl_info) override; |
72 void ContinueSSLRequest() override; | 73 void ContinueSSLRequest() override; |
73 | 74 |
74 // SSLClientAuthHandler::Delegate implementation. | 75 // SSLClientAuthHandler::Delegate implementation. |
75 void ContinueWithCertificate(net::X509Certificate* cert) override; | 76 void ContinueWithCertificate(net::X509Certificate* cert) override; |
76 void CancelCertificateSelection() override; | 77 void CancelCertificateSelection() override; |
77 | 78 |
78 // ResourceController implementation: | 79 // These correspond to Controller's methods. |
79 void Resume() override; | 80 // TODO(mmenke): Seems like this could be simplified a little. |
80 void Cancel() override; | 81 |
81 void CancelAndIgnore() override; | 82 // |is_continuation| is false if called from a ResourceController, and true if |
82 void CancelWithError(int error_code) override; | 83 // called from ResourceLoader, right after the previous operation succeeded. |
| 84 // In the latter case, the ResourceController's Resume() method was called |
| 85 // synchronously by the ResourceLoader, and the stack has since been unwound, |
| 86 // so the ResourceLoader is at the top of the stack. |
| 87 void Resume(bool is_continuation); |
| 88 void Cancel(); |
| 89 void CancelAndIgnore(); |
| 90 void CancelWithError(int error_code); |
83 | 91 |
84 void StartRequestInternal(); | 92 void StartRequestInternal(); |
85 void CancelRequestInternal(int error, bool from_renderer); | 93 void CancelRequestInternal(int error, bool from_renderer); |
86 void CompleteResponseStarted(); | 94 void CompleteResponseStarted(); |
87 void StartReading(bool is_continuation); | 95 void StartReading(bool is_continuation); |
88 void ResumeReading(); | 96 void ResumeReading(); |
89 void ReadMore(int* bytes_read); | 97 void ReadMore(int* bytes_read); |
90 // Passes a read result to the handler. | 98 // Passes a read result to the handler. |
91 void CompleteRead(int bytes_read); | 99 void CompleteRead(int bytes_read); |
92 void ResponseCompleted(); | 100 void ResponseCompleted(); |
93 void CallDidFinishLoading(); | 101 void CallDidFinishLoading(); |
94 void RecordHistograms(); | 102 void RecordHistograms(); |
95 | 103 |
96 bool is_deferred() const { return deferred_stage_ != DEFERRED_NONE; } | 104 bool is_deferred() const { return deferred_stage_ != DEFERRED_NONE; } |
97 | 105 |
98 // Used for categorizing loading of prefetches for reporting in histograms. | 106 // Used for categorizing loading of prefetches for reporting in histograms. |
99 // NOTE: This enumeration is used in histograms, so please do not add entries | 107 // NOTE: This enumeration is used in histograms, so please do not add entries |
100 // in the middle. | 108 // in the middle. |
101 enum PrefetchStatus { | 109 enum PrefetchStatus { |
102 STATUS_UNDEFINED, | 110 STATUS_UNDEFINED, |
103 STATUS_SUCCESS_FROM_CACHE, | 111 STATUS_SUCCESS_FROM_CACHE, |
104 STATUS_SUCCESS_FROM_NETWORK, | 112 STATUS_SUCCESS_FROM_NETWORK, |
105 STATUS_CANCELED, | 113 STATUS_CANCELED, |
106 STATUS_MAX, | 114 STATUS_MAX, |
107 }; | 115 }; |
108 | 116 |
109 enum DeferredStage { | 117 enum DeferredStage { |
110 DEFERRED_NONE, | 118 DEFERRED_NONE, |
| 119 // Magic deferral "stage" which means that the code is currently in a |
| 120 // recursive call from the ResourceLoader. When in this state, Resume() does |
| 121 // nothing but update the deferral state, and when the stack is unwound back |
| 122 // up to the ResourceLoader, the request will be continued. This is used to |
| 123 // prevent the stack from getting too deep. |
| 124 DEFERRED_SYNC, |
111 DEFERRED_START, | 125 DEFERRED_START, |
112 DEFERRED_REDIRECT, | 126 DEFERRED_REDIRECT, |
113 DEFERRED_READ, | 127 DEFERRED_READ, |
114 DEFERRED_RESPONSE_COMPLETE, | 128 DEFERRED_RESPONSE_COMPLETE, |
115 DEFERRED_FINISH | 129 DEFERRED_FINISH |
116 }; | 130 }; |
117 DeferredStage deferred_stage_; | 131 DeferredStage deferred_stage_; |
118 | 132 |
| 133 class ScopedDeferral; |
| 134 |
119 std::unique_ptr<net::URLRequest> request_; | 135 std::unique_ptr<net::URLRequest> request_; |
120 std::unique_ptr<ResourceHandler> handler_; | 136 std::unique_ptr<ResourceHandler> handler_; |
121 ResourceLoaderDelegate* delegate_; | 137 ResourceLoaderDelegate* delegate_; |
122 | 138 |
123 scoped_refptr<ResourceDispatcherHostLoginDelegate> login_delegate_; | 139 scoped_refptr<ResourceDispatcherHostLoginDelegate> login_delegate_; |
124 std::unique_ptr<SSLClientAuthHandler> ssl_client_auth_handler_; | 140 std::unique_ptr<SSLClientAuthHandler> ssl_client_auth_handler_; |
125 | 141 |
126 base::TimeTicks read_deferral_start_time_; | 142 base::TimeTicks read_deferral_start_time_; |
127 | 143 |
128 // Indicates that we are in a state of being transferred to a new downstream | 144 // Indicates that we are in a state of being transferred to a new downstream |
(...skipping 11 matching lines...) Expand all Loading... |
140 int times_cancelled_after_request_start_; | 156 int times_cancelled_after_request_start_; |
141 | 157 |
142 base::WeakPtrFactory<ResourceLoader> weak_ptr_factory_; | 158 base::WeakPtrFactory<ResourceLoader> weak_ptr_factory_; |
143 | 159 |
144 DISALLOW_COPY_AND_ASSIGN(ResourceLoader); | 160 DISALLOW_COPY_AND_ASSIGN(ResourceLoader); |
145 }; | 161 }; |
146 | 162 |
147 } // namespace content | 163 } // namespace content |
148 | 164 |
149 #endif // CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_ | 165 #endif // CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_ |
OLD | NEW |