| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/url_request/url_request_file_job.h" | 5 #include "net/url_request/url_request_file_job.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 public: | 21 public: |
| 22 URLRequestFileJobWithCallbacks( | 22 URLRequestFileJobWithCallbacks( |
| 23 net::URLRequest* request, | 23 net::URLRequest* request, |
| 24 net::NetworkDelegate* network_delegate, | 24 net::NetworkDelegate* network_delegate, |
| 25 const base::FilePath& file_path, | 25 const base::FilePath& file_path, |
| 26 const scoped_refptr<base::TaskRunner>& file_task_runner) | 26 const scoped_refptr<base::TaskRunner>& file_task_runner) |
| 27 : URLRequestFileJob(request, | 27 : URLRequestFileJob(request, |
| 28 network_delegate, | 28 network_delegate, |
| 29 file_path, | 29 file_path, |
| 30 file_task_runner), | 30 file_task_runner), |
| 31 seek_position_(0) { | 31 seek_position_(0) {} |
| 32 } | |
| 33 | 32 |
| 34 int64 seek_position() { return seek_position_; } | 33 int64 seek_position() { return seek_position_; } |
| 35 const std::vector<std::string>& data_chunks() { return data_chunks_; } | 34 const std::vector<std::string>& data_chunks() { return data_chunks_; } |
| 36 | 35 |
| 37 protected: | 36 protected: |
| 38 virtual ~URLRequestFileJobWithCallbacks() {} | 37 virtual ~URLRequestFileJobWithCallbacks() {} |
| 39 | 38 |
| 40 virtual void OnSeekComplete(int64 result) OVERRIDE { | 39 virtual void OnSeekComplete(int64 result) OVERRIDE { |
| 41 ASSERT_EQ(seek_position_, 0); | 40 ASSERT_EQ(seek_position_, 0); |
| 42 seek_position_ = result; | 41 seek_position_ = result; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 53 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks | 52 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks |
| 54 // instances for file:// scheme URLs. | 53 // instances for file:// scheme URLs. |
| 55 class CallbacksJobFactory : public net::URLRequestJobFactory { | 54 class CallbacksJobFactory : public net::URLRequestJobFactory { |
| 56 public: | 55 public: |
| 57 class JobObserver { | 56 class JobObserver { |
| 58 public: | 57 public: |
| 59 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0; | 58 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0; |
| 60 }; | 59 }; |
| 61 | 60 |
| 62 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) | 61 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) |
| 63 : path_(path), observer_(observer) { | 62 : path_(path), observer_(observer) {} |
| 64 } | |
| 65 | 63 |
| 66 virtual ~CallbacksJobFactory() {} | 64 virtual ~CallbacksJobFactory() {} |
| 67 | 65 |
| 68 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 66 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 69 const std::string& scheme, | 67 const std::string& scheme, |
| 70 net::URLRequest* request, | 68 net::URLRequest* request, |
| 71 net::NetworkDelegate* network_delegate) const OVERRIDE { | 69 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 72 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks( | 70 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks( |
| 73 request, | 71 request, |
| 74 network_delegate, | 72 network_delegate, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // is provided, this function will add the appropriate Range http header to | 151 // is provided, this function will add the appropriate Range http header to |
| 154 // the request and verify that only the bytes in that range (inclusive) were | 152 // the request and verify that only the bytes in that range (inclusive) were |
| 155 // observed. | 153 // observed. |
| 156 void RunRequest(const std::string& content, const Range* range); | 154 void RunRequest(const std::string& content, const Range* range); |
| 157 | 155 |
| 158 JobObserverImpl observer_; | 156 JobObserverImpl observer_; |
| 159 net::TestURLRequestContext context_; | 157 net::TestURLRequestContext context_; |
| 160 net::TestDelegate delegate_; | 158 net::TestDelegate delegate_; |
| 161 }; | 159 }; |
| 162 | 160 |
| 163 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} | 161 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() { |
| 162 } |
| 164 | 163 |
| 165 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, | 164 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, |
| 166 const Range* range) { | 165 const Range* range) { |
| 167 base::ScopedTempDir directory; | 166 base::ScopedTempDir directory; |
| 168 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 167 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
| 169 base::FilePath path; | 168 base::FilePath path; |
| 170 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); | 169 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); |
| 171 CallbacksJobFactory factory(path, &observer_); | 170 CallbacksJobFactory factory(path, &observer_); |
| 172 context_.set_job_factory(&factory); | 171 context_.set_job_factory(&factory); |
| 173 | 172 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 RunRequest(MakeContentOfSize(3 * 1024 * 1024), NULL); | 243 RunRequest(MakeContentOfSize(3 * 1024 * 1024), NULL); |
| 245 } | 244 } |
| 246 | 245 |
| 247 TEST_F(URLRequestFileJobEventsTest, Range) { | 246 TEST_F(URLRequestFileJobEventsTest, Range) { |
| 248 // Use a 15KB content file and read a range chosen somewhat arbitrarily but | 247 // Use a 15KB content file and read a range chosen somewhat arbitrarily but |
| 249 // not aligned on any likely page boundaries. | 248 // not aligned on any likely page boundaries. |
| 250 int size = 15 * 1024; | 249 int size = 15 * 1024; |
| 251 Range range(1701, (6 * 1024) + 3); | 250 Range range(1701, (6 * 1024) + 3); |
| 252 RunRequest(MakeContentOfSize(size), &range); | 251 RunRequest(MakeContentOfSize(size), &range); |
| 253 } | 252 } |
| OLD | NEW |