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/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // OnReadComplete. | 26 // OnReadComplete. |
27 class TestURLRequestFileJob : public URLRequestFileJob { | 27 class TestURLRequestFileJob : public URLRequestFileJob { |
28 public: | 28 public: |
29 // |seek_position| will be set to the value passed in to OnSeekComplete. | 29 // |seek_position| will be set to the value passed in to OnSeekComplete. |
30 // |observed_content| will be set to the concatenated data from all calls to | 30 // |observed_content| will be set to the concatenated data from all calls to |
31 // OnReadComplete. | 31 // OnReadComplete. |
32 TestURLRequestFileJob(URLRequest* request, | 32 TestURLRequestFileJob(URLRequest* request, |
33 NetworkDelegate* network_delegate, | 33 NetworkDelegate* network_delegate, |
34 const base::FilePath& file_path, | 34 const base::FilePath& file_path, |
35 const scoped_refptr<base::TaskRunner>& file_task_runner, | 35 const scoped_refptr<base::TaskRunner>& file_task_runner, |
36 int64* seek_position, | 36 int64_t* seek_position, |
37 std::string* observed_content) | 37 std::string* observed_content) |
38 : URLRequestFileJob(request, | 38 : URLRequestFileJob(request, |
39 network_delegate, | 39 network_delegate, |
40 file_path, | 40 file_path, |
41 file_task_runner), | 41 file_task_runner), |
42 seek_position_(seek_position), | 42 seek_position_(seek_position), |
43 observed_content_(observed_content) { | 43 observed_content_(observed_content) { |
44 *seek_position_ = 0; | 44 *seek_position_ = 0; |
45 observed_content_->clear(); | 45 observed_content_->clear(); |
46 } | 46 } |
47 | 47 |
48 ~TestURLRequestFileJob() override {} | 48 ~TestURLRequestFileJob() override {} |
49 | 49 |
50 protected: | 50 protected: |
51 void OnSeekComplete(int64 result) override { | 51 void OnSeekComplete(int64_t result) override { |
52 ASSERT_EQ(*seek_position_, 0); | 52 ASSERT_EQ(*seek_position_, 0); |
53 *seek_position_ = result; | 53 *seek_position_ = result; |
54 } | 54 } |
55 | 55 |
56 void OnReadComplete(IOBuffer* buf, int result) override { | 56 void OnReadComplete(IOBuffer* buf, int result) override { |
57 observed_content_->append(std::string(buf->data(), result)); | 57 observed_content_->append(std::string(buf->data(), result)); |
58 } | 58 } |
59 | 59 |
60 int64* const seek_position_; | 60 int64_t* const seek_position_; |
61 std::string* const observed_content_; | 61 std::string* const observed_content_; |
62 }; | 62 }; |
63 | 63 |
64 // A URLRequestJobFactory that will return TestURLRequestFileJob instances for | 64 // A URLRequestJobFactory that will return TestURLRequestFileJob instances for |
65 // file:// scheme URLs. Can only be used to create a single job. | 65 // file:// scheme URLs. Can only be used to create a single job. |
66 class TestJobFactory : public URLRequestJobFactory { | 66 class TestJobFactory : public URLRequestJobFactory { |
67 public: | 67 public: |
68 TestJobFactory(const base::FilePath& path, | 68 TestJobFactory(const base::FilePath& path, |
69 int64* seek_position, | 69 int64_t* seek_position, |
70 std::string* observed_content) | 70 std::string* observed_content) |
71 : path_(path), | 71 : path_(path), |
72 seek_position_(seek_position), | 72 seek_position_(seek_position), |
73 observed_content_(observed_content) { | 73 observed_content_(observed_content) { |
74 CHECK(seek_position_); | 74 CHECK(seek_position_); |
75 CHECK(observed_content_); | 75 CHECK(observed_content_); |
76 } | 76 } |
77 | 77 |
78 ~TestJobFactory() override {} | 78 ~TestJobFactory() override {} |
79 | 79 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 112 } |
113 | 113 |
114 bool IsSafeRedirectTarget(const GURL& location) const override { | 114 bool IsSafeRedirectTarget(const GURL& location) const override { |
115 return false; | 115 return false; |
116 } | 116 } |
117 | 117 |
118 private: | 118 private: |
119 const base::FilePath path_; | 119 const base::FilePath path_; |
120 | 120 |
121 // These are mutable because MaybeCreateJobWithProtocolHandler is const. | 121 // These are mutable because MaybeCreateJobWithProtocolHandler is const. |
122 mutable int64* seek_position_; | 122 mutable int64_t* seek_position_; |
123 mutable std::string* observed_content_; | 123 mutable std::string* observed_content_; |
124 }; | 124 }; |
125 | 125 |
126 // Helper function to create a file in |directory| filled with | 126 // Helper function to create a file in |directory| filled with |
127 // |content|. Returns true on succes and fills in |path| with the full path to | 127 // |content|. Returns true on succes and fills in |path| with the full path to |
128 // the file. | 128 // the file. |
129 bool CreateTempFileWithContent(const std::string& content, | 129 bool CreateTempFileWithContent(const std::string& content, |
130 const base::ScopedTempDir& directory, | 130 const base::ScopedTempDir& directory, |
131 base::FilePath* path) { | 131 base::FilePath* path) { |
132 if (!directory.IsValid()) | 132 if (!directory.IsValid()) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} | 176 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} |
177 | 177 |
178 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, | 178 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, |
179 const Range* range) { | 179 const Range* range) { |
180 base::ScopedTempDir directory; | 180 base::ScopedTempDir directory; |
181 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 181 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
182 base::FilePath path; | 182 base::FilePath path; |
183 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); | 183 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); |
184 | 184 |
185 { | 185 { |
186 int64 seek_position; | 186 int64_t seek_position; |
187 std::string observed_content; | 187 std::string observed_content; |
188 TestJobFactory factory(path, &seek_position, &observed_content); | 188 TestJobFactory factory(path, &seek_position, &observed_content); |
189 context_.set_job_factory(&factory); | 189 context_.set_job_factory(&factory); |
190 | 190 |
191 scoped_ptr<URLRequest> request(context_.CreateRequest( | 191 scoped_ptr<URLRequest> request(context_.CreateRequest( |
192 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); | 192 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); |
193 if (range) { | 193 if (range) { |
194 ASSERT_GE(range->start, 0); | 194 ASSERT_GE(range->start, 0); |
195 ASSERT_GE(range->end, 0); | 195 ASSERT_GE(range->end, 0); |
196 ASSERT_LE(range->start, range->end); | 196 ASSERT_LE(range->start, range->end); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 // Use a 15KB content file and read a range chosen somewhat arbitrarily but | 252 // Use a 15KB content file and read a range chosen somewhat arbitrarily but |
253 // not aligned on any likely page boundaries. | 253 // not aligned on any likely page boundaries. |
254 int size = 15 * 1024; | 254 int size = 15 * 1024; |
255 Range range(1701, (6 * 1024) + 3); | 255 Range range(1701, (6 * 1024) + 3); |
256 RunRequest(MakeContentOfSize(size), &range); | 256 RunRequest(MakeContentOfSize(size), &range); |
257 } | 257 } |
258 | 258 |
259 } // namespace | 259 } // namespace |
260 | 260 |
261 } // namespace net | 261 } // namespace net |
OLD | NEW |