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_MIME_SNIFFING_RESOURCE_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
6 #define CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ | 6 #define CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // Starting state of the MimeSniffingResourceHandler. In this state, it is | 59 // Starting state of the MimeSniffingResourceHandler. In this state, it is |
60 // acting as a blind pass-through ResourceHandler until the response is | 60 // acting as a blind pass-through ResourceHandler until the response is |
61 // received. | 61 // received. |
62 STATE_STARTING, | 62 STATE_STARTING, |
63 | 63 |
64 // In this state, the MimeSniffingResourceHandler is buffering the response | 64 // In this state, the MimeSniffingResourceHandler is buffering the response |
65 // data in read_buffer_, waiting to sniff the mime type and make a choice | 65 // data in read_buffer_, waiting to sniff the mime type and make a choice |
66 // about request interception. | 66 // about request interception. |
67 STATE_BUFFERING, | 67 STATE_BUFFERING, |
68 | 68 |
| 69 // In these states, the MimeSniffingResourceHandler is calling OnWillRead on |
| 70 // the downstream ResourceHandler and then waiting for the response. |
| 71 STATE_CALLING_ON_WILL_READ, |
| 72 STATE_WAITING_FOR_BUFFER, |
| 73 |
69 // In this state, the MimeSniffingResourceHandler has identified the mime | 74 // In this state, the MimeSniffingResourceHandler has identified the mime |
70 // type and made a decision on whether the request should be intercepted or | 75 // type and made a decision on whether the request should be intercepted or |
71 // not. It is nows attempting to replay the response to downstream | 76 // not. It is nows attempting to replay the response to downstream |
72 // handlers. | 77 // handlers. |
73 STATE_INTERCEPTION_CHECK_DONE, | 78 STATE_INTERCEPTION_CHECK_DONE, |
74 | 79 |
75 // In this state, the MimeSniffingResourceHandler is replaying the buffered | 80 // In this state, the MimeSniffingResourceHandler is replaying the buffered |
76 // OnResponseStarted event to the downstream ResourceHandlers. | 81 // OnResponseStarted event to the downstream ResourceHandlers. |
77 STATE_REPLAYING_RESPONSE_RECEIVED, | 82 STATE_REPLAYING_RESPONSE_RECEIVED, |
78 | 83 |
79 // In this state, the MimeSniffingResourceHandler is just a blind | 84 // In this state, the MimeSniffingResourceHandler is just a blind |
80 // pass-through | 85 // pass-through |
81 // ResourceHandler. | 86 // ResourceHandler. |
82 STATE_STREAMING, | 87 STATE_STREAMING, |
83 }; | 88 }; |
84 | 89 |
85 // ResourceHandler implementation: | 90 // ResourceHandler implementation: |
86 void OnWillStart(const GURL&, | 91 void OnWillStart(const GURL&, |
87 std::unique_ptr<ResourceController> controller) override; | 92 std::unique_ptr<ResourceController> controller) override; |
88 void OnResponseStarted( | 93 void OnResponseStarted( |
89 ResourceResponse* response, | 94 ResourceResponse* response, |
90 std::unique_ptr<ResourceController> controller) override; | 95 std::unique_ptr<ResourceController> controller) override; |
91 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 96 void OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
92 int* buf_size) override; | 97 int* buf_size, |
| 98 std::unique_ptr<ResourceController> controller) override; |
93 void OnReadCompleted(int bytes_read, | 99 void OnReadCompleted(int bytes_read, |
94 std::unique_ptr<ResourceController> controller) override; | 100 std::unique_ptr<ResourceController> controller) override; |
95 void OnResponseCompleted( | 101 void OnResponseCompleted( |
96 const net::URLRequestStatus& status, | 102 const net::URLRequestStatus& status, |
97 std::unique_ptr<ResourceController> controller) override; | 103 std::unique_ptr<ResourceController> controller) override; |
98 | 104 |
99 void ResumeInternal(); | 105 void ResumeInternal(); |
100 | 106 |
101 // -------------------------------------------------------------------------- | 107 // -------------------------------------------------------------------------- |
102 // The following methods replay the buffered data to the downstream | 108 // The following methods replay the buffered data to the downstream |
103 // ResourceHandlers. They return false if the request should be cancelled, | 109 // ResourceHandlers. They return false if the request should be cancelled, |
104 // true otherwise. Each of them will set |defer| to true if the request will | 110 // true otherwise. Each of them will set |defer| to true if the request will |
105 // proceed to the next stage asynchronously. | 111 // proceed to the next stage asynchronously. |
106 | 112 |
107 // Used to advance through the states of the state machine. | 113 // Used to advance through the states of the state machine. |
108 void AdvanceState(); | 114 void AdvanceState(); |
109 | 115 |
110 // Intercepts the request as a stream/download if needed. | 116 // Intercepts the request as a stream/download if needed. |
111 void MaybeIntercept(); | 117 void MaybeIntercept(); |
112 | 118 |
| 119 // Calls OnWillRead on the downstream handlers. |
| 120 void CallOnWillRead(); |
| 121 |
| 122 // Copies received buffer to parent. |
| 123 void BufferReceived(); |
| 124 |
113 // Replays OnResponseStarted on the downstream handlers. | 125 // Replays OnResponseStarted on the downstream handlers. |
114 void ReplayResponseReceived(); | 126 void ReplayResponseReceived(); |
115 | 127 |
116 // Replays OnReadCompleted on the downstreams handlers. | 128 // Replays OnReadCompleted on the downstreams handlers. |
117 void ReplayReadCompleted(); | 129 void ReplayReadCompleted(); |
118 | 130 |
119 // -------------------------------------------------------------------------- | 131 // -------------------------------------------------------------------------- |
120 | 132 |
121 // Whether the response body should be sniffed in order to determine the MIME | 133 // Whether the response body should be sniffed in order to determine the MIME |
122 // type of the response. | 134 // type of the response. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 172 |
161 bool must_download_; | 173 bool must_download_; |
162 bool must_download_is_set_; | 174 bool must_download_is_set_; |
163 | 175 |
164 // Used to buffer the reponse received until replay. | 176 // Used to buffer the reponse received until replay. |
165 scoped_refptr<ResourceResponse> response_; | 177 scoped_refptr<ResourceResponse> response_; |
166 scoped_refptr<net::IOBuffer> read_buffer_; | 178 scoped_refptr<net::IOBuffer> read_buffer_; |
167 int read_buffer_size_; | 179 int read_buffer_size_; |
168 int bytes_read_; | 180 int bytes_read_; |
169 | 181 |
| 182 // Pointers to parent-owned read buffer and its size. Only used for first |
| 183 // OnWillRead call. |
| 184 scoped_refptr<net::IOBuffer>* parent_read_buffer_; |
| 185 int* parent_read_buffer_size_; |
| 186 |
170 // The InterceptingResourceHandler that will perform ResourceHandler swap if | 187 // The InterceptingResourceHandler that will perform ResourceHandler swap if |
171 // needed. | 188 // needed. |
172 InterceptingResourceHandler* intercepting_handler_; | 189 InterceptingResourceHandler* intercepting_handler_; |
173 | 190 |
174 RequestContextType request_context_type_; | 191 RequestContextType request_context_type_; |
175 | 192 |
176 // True if current in an AdvanceState loop. Used to prevent re-entrancy and | 193 // True if current in an AdvanceState loop. Used to prevent re-entrancy and |
177 // avoid an extra PostTask. | 194 // avoid an extra PostTask. |
178 bool in_state_loop_; | 195 bool in_state_loop_; |
179 // Set to true if Resume() is called while |in_state_loop_| is true. When | 196 // Set to true if Resume() is called while |in_state_loop_| is true. When |
180 // returning to the parent AdvanceState loop, will synchronously advance to | 197 // returning to the parent AdvanceState loop, will synchronously advance to |
181 // the next state when control returns to the AdvanceState loop. | 198 // the next state when control returns to the AdvanceState loop. |
182 bool advance_state_; | 199 bool advance_state_; |
183 | 200 |
184 base::WeakPtrFactory<MimeSniffingResourceHandler> weak_ptr_factory_; | 201 base::WeakPtrFactory<MimeSniffingResourceHandler> weak_ptr_factory_; |
185 | 202 |
186 DISALLOW_COPY_AND_ASSIGN(MimeSniffingResourceHandler); | 203 DISALLOW_COPY_AND_ASSIGN(MimeSniffingResourceHandler); |
187 }; | 204 }; |
188 | 205 |
189 } // namespace content | 206 } // namespace content |
190 | 207 |
191 #endif // CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ | 208 #endif // CONTENT_BROWSER_LOADER_MIME_SNIFFING_RESOURCE_HANDLER_H_ |
OLD | NEW |