Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: content/browser/loader/resource_loader_unittest.cc

Issue 267563004: Avoid issuing a Read() after draining a request. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Re-upload to see if it would make bots happy. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/test/data/download/empty.bin » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 #include "content/browser/loader/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/file.h" 8 #include "base/files/file.h"
9 #include "base/message_loop/message_loop_proxy.h" 9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/platform_file.h" 10 #include "base/platform_file.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 // initialize ResourceLoader. 85 // initialize ResourceLoader.
86 class ResourceHandlerStub : public ResourceHandler { 86 class ResourceHandlerStub : public ResourceHandler {
87 public: 87 public:
88 explicit ResourceHandlerStub(net::URLRequest* request) 88 explicit ResourceHandlerStub(net::URLRequest* request)
89 : ResourceHandler(request), 89 : ResourceHandler(request),
90 read_buffer_(new net::IOBuffer(kReadBufSize)), 90 read_buffer_(new net::IOBuffer(kReadBufSize)),
91 defer_request_on_will_start_(false), 91 defer_request_on_will_start_(false),
92 expect_reads_(true), 92 expect_reads_(true),
93 cancel_on_read_completed_(false), 93 cancel_on_read_completed_(false),
94 defer_eof_(false), 94 defer_eof_(false),
95 received_on_will_read_(false),
96 received_eof_(false),
95 received_response_completed_(false), 97 received_response_completed_(false),
96 total_bytes_downloaded_(0) { 98 total_bytes_downloaded_(0) {
97 } 99 }
98 100
99 // If true, defers the resource load in OnWillStart. 101 // If true, defers the resource load in OnWillStart.
100 void set_defer_request_on_will_start(bool defer_request_on_will_start) { 102 void set_defer_request_on_will_start(bool defer_request_on_will_start) {
101 defer_request_on_will_start_ = defer_request_on_will_start; 103 defer_request_on_will_start_ = defer_request_on_will_start;
102 } 104 }
103 105
104 // If true, expect OnWillRead / OnReadCompleted pairs for handling 106 // If true, expect OnWillRead / OnReadCompleted pairs for handling
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 virtual bool OnBeforeNetworkStart(int request_id, 163 virtual bool OnBeforeNetworkStart(int request_id,
162 const GURL& url, 164 const GURL& url,
163 bool* defer) OVERRIDE { 165 bool* defer) OVERRIDE {
164 return true; 166 return true;
165 } 167 }
166 168
167 virtual bool OnWillRead(int request_id, 169 virtual bool OnWillRead(int request_id,
168 scoped_refptr<net::IOBuffer>* buf, 170 scoped_refptr<net::IOBuffer>* buf,
169 int* buf_size, 171 int* buf_size,
170 int min_size) OVERRIDE { 172 int min_size) OVERRIDE {
171 if (!expect_reads_) { 173 EXPECT_TRUE(expect_reads_);
172 ADD_FAILURE(); 174 EXPECT_FALSE(received_on_will_read_);
173 return false; 175 EXPECT_FALSE(received_eof_);
174 } 176 EXPECT_FALSE(received_response_completed_);
175 177
176 *buf = read_buffer_; 178 *buf = read_buffer_;
177 *buf_size = kReadBufSize; 179 *buf_size = kReadBufSize;
180 received_on_will_read_ = true;
178 return true; 181 return true;
179 } 182 }
180 183
181 virtual bool OnReadCompleted(int request_id, 184 virtual bool OnReadCompleted(int request_id,
182 int bytes_read, 185 int bytes_read,
183 bool* defer) OVERRIDE { 186 bool* defer) OVERRIDE {
184 if (!expect_reads_) { 187 EXPECT_TRUE(received_on_will_read_);
185 ADD_FAILURE(); 188 EXPECT_TRUE(expect_reads_);
186 return false; 189 EXPECT_FALSE(received_response_completed_);
190
191 if (bytes_read == 0) {
192 received_eof_ = true;
193 if (defer_eof_) {
194 defer_eof_ = false;
195 *defer = true;
196 }
187 } 197 }
188 198
189 if (bytes_read == 0 && defer_eof_) { 199 // Need another OnWillRead() call before seeing an OnReadCompleted().
190 // Only defer it once; on resumption there will be another EOF. 200 received_on_will_read_ = false;
191 defer_eof_ = false;
192 *defer = true;
193 }
194 201
195 return !cancel_on_read_completed_; 202 return !cancel_on_read_completed_;
196 } 203 }
197 204
198 virtual void OnResponseCompleted(int request_id, 205 virtual void OnResponseCompleted(int request_id,
199 const net::URLRequestStatus& status, 206 const net::URLRequestStatus& status,
200 const std::string& security_info, 207 const std::string& security_info,
201 bool* defer) OVERRIDE { 208 bool* defer) OVERRIDE {
202 EXPECT_FALSE(received_response_completed_); 209 EXPECT_FALSE(received_response_completed_);
210 if (status.is_success() && expect_reads_)
211 EXPECT_TRUE(received_eof_);
212
203 received_response_completed_ = true; 213 received_response_completed_ = true;
204 status_ = status; 214 status_ = status;
205 } 215 }
206 216
207 virtual void OnDataDownloaded(int request_id, 217 virtual void OnDataDownloaded(int request_id,
208 int bytes_downloaded) OVERRIDE { 218 int bytes_downloaded) OVERRIDE {
209 if (expect_reads_) 219 EXPECT_FALSE(expect_reads_);
210 ADD_FAILURE();
211 total_bytes_downloaded_ += bytes_downloaded; 220 total_bytes_downloaded_ += bytes_downloaded;
212 } 221 }
213 222
214 private: 223 private:
215 scoped_refptr<net::IOBuffer> read_buffer_; 224 scoped_refptr<net::IOBuffer> read_buffer_;
216 225
217 bool defer_request_on_will_start_; 226 bool defer_request_on_will_start_;
218 bool expect_reads_; 227 bool expect_reads_;
219 bool cancel_on_read_completed_; 228 bool cancel_on_read_completed_;
220 bool defer_eof_; 229 bool defer_eof_;
221 230
222 GURL start_url_; 231 GURL start_url_;
223 scoped_refptr<ResourceResponse> response_; 232 scoped_refptr<ResourceResponse> response_;
233 bool received_on_will_read_;
234 bool received_eof_;
224 bool received_response_completed_; 235 bool received_response_completed_;
225 net::URLRequestStatus status_; 236 net::URLRequestStatus status_;
226 int total_bytes_downloaded_; 237 int total_bytes_downloaded_;
227 }; 238 };
228 239
229 // Test browser client that captures calls to SelectClientCertificates and 240 // Test browser client that captures calls to SelectClientCertificates and
230 // records the arguments of the most recent call for later inspection. 241 // records the arguments of the most recent call for later inspection.
231 class SelectCertificateBrowserClient : public TestContentBrowserClient { 242 class SelectCertificateBrowserClient : public TestContentBrowserClient {
232 public: 243 public:
233 SelectCertificateBrowserClient() : call_count_(0) {} 244 SelectCertificateBrowserClient() : call_count_(0) {}
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 735 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
725 EXPECT_EQ(test_data(), contents); 736 EXPECT_EQ(test_data(), contents);
726 737
727 // Release the loader. The file should be gone now. 738 // Release the loader. The file should be gone now.
728 ReleaseLoader(); 739 ReleaseLoader();
729 base::RunLoop().RunUntilIdle(); 740 base::RunLoop().RunUntilIdle();
730 EXPECT_FALSE(base::PathExists(temp_path())); 741 EXPECT_FALSE(base::PathExists(temp_path()));
731 } 742 }
732 743
733 } // namespace content 744 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/test/data/download/empty.bin » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698