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