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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 file_task_runner), | 34 file_task_runner), |
35 seek_position_(0) { | 35 seek_position_(0) { |
36 } | 36 } |
37 | 37 |
38 int64 seek_position() { return seek_position_; } | 38 int64 seek_position() { return seek_position_; } |
39 const std::vector<std::string>& data_chunks() { return data_chunks_; } | 39 const std::vector<std::string>& data_chunks() { return data_chunks_; } |
40 | 40 |
41 protected: | 41 protected: |
42 virtual ~URLRequestFileJobWithCallbacks() {} | 42 virtual ~URLRequestFileJobWithCallbacks() {} |
43 | 43 |
44 virtual void OnSeekComplete(int64 result) OVERRIDE { | 44 virtual void OnSeekComplete(int64 result) override { |
45 ASSERT_EQ(seek_position_, 0); | 45 ASSERT_EQ(seek_position_, 0); |
46 seek_position_ = result; | 46 seek_position_ = result; |
47 } | 47 } |
48 | 48 |
49 virtual void OnReadComplete(IOBuffer* buf, int result) OVERRIDE { | 49 virtual void OnReadComplete(IOBuffer* buf, int result) override { |
50 data_chunks_.push_back(std::string(buf->data(), result)); | 50 data_chunks_.push_back(std::string(buf->data(), result)); |
51 } | 51 } |
52 | 52 |
53 int64 seek_position_; | 53 int64 seek_position_; |
54 std::vector<std::string> data_chunks_; | 54 std::vector<std::string> data_chunks_; |
55 }; | 55 }; |
56 | 56 |
57 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks | 57 // A URLRequestJobFactory that will return URLRequestFileJobWithCallbacks |
58 // instances for file:// scheme URLs. | 58 // instances for file:// scheme URLs. |
59 class CallbacksJobFactory : public URLRequestJobFactory { | 59 class CallbacksJobFactory : public URLRequestJobFactory { |
60 public: | 60 public: |
61 class JobObserver { | 61 class JobObserver { |
62 public: | 62 public: |
63 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0; | 63 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) = 0; |
64 }; | 64 }; |
65 | 65 |
66 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) | 66 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) |
67 : path_(path), observer_(observer) { | 67 : path_(path), observer_(observer) { |
68 } | 68 } |
69 | 69 |
70 virtual ~CallbacksJobFactory() {} | 70 virtual ~CallbacksJobFactory() {} |
71 | 71 |
72 virtual URLRequestJob* MaybeCreateJobWithProtocolHandler( | 72 virtual URLRequestJob* MaybeCreateJobWithProtocolHandler( |
73 const std::string& scheme, | 73 const std::string& scheme, |
74 URLRequest* request, | 74 URLRequest* request, |
75 NetworkDelegate* network_delegate) const OVERRIDE { | 75 NetworkDelegate* network_delegate) const override { |
76 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks( | 76 URLRequestFileJobWithCallbacks* job = new URLRequestFileJobWithCallbacks( |
77 request, | 77 request, |
78 network_delegate, | 78 network_delegate, |
79 path_, | 79 path_, |
80 base::MessageLoop::current()->message_loop_proxy()); | 80 base::MessageLoop::current()->message_loop_proxy()); |
81 observer_->OnJobCreated(job); | 81 observer_->OnJobCreated(job); |
82 return job; | 82 return job; |
83 } | 83 } |
84 | 84 |
85 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | 85 virtual bool IsHandledProtocol(const std::string& scheme) const override { |
86 return scheme == "file"; | 86 return scheme == "file"; |
87 } | 87 } |
88 | 88 |
89 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | 89 virtual bool IsHandledURL(const GURL& url) const override { |
90 return IsHandledProtocol(url.scheme()); | 90 return IsHandledProtocol(url.scheme()); |
91 } | 91 } |
92 | 92 |
93 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { | 93 virtual bool IsSafeRedirectTarget(const GURL& location) const override { |
94 return false; | 94 return false; |
95 } | 95 } |
96 | 96 |
97 private: | 97 private: |
98 base::FilePath path_; | 98 base::FilePath path_; |
99 JobObserver* observer_; | 99 JobObserver* observer_; |
100 }; | 100 }; |
101 | 101 |
102 // Helper function to create a file in |directory| filled with | 102 // Helper function to create a file in |directory| filled with |
103 // |content|. Returns true on succes and fills in |path| with the full path to | 103 // |content|. Returns true on succes and fills in |path| with the full path to |
104 // the file. | 104 // the file. |
105 bool CreateTempFileWithContent(const std::string& content, | 105 bool CreateTempFileWithContent(const std::string& content, |
106 const base::ScopedTempDir& directory, | 106 const base::ScopedTempDir& directory, |
107 base::FilePath* path) { | 107 base::FilePath* path) { |
108 if (!directory.IsValid()) | 108 if (!directory.IsValid()) |
109 return false; | 109 return false; |
110 | 110 |
111 if (!base::CreateTemporaryFileInDir(directory.path(), path)) | 111 if (!base::CreateTemporaryFileInDir(directory.path(), path)) |
112 return false; | 112 return false; |
113 | 113 |
114 return base::WriteFile(*path, content.c_str(), content.length()); | 114 return base::WriteFile(*path, content.c_str(), content.length()); |
115 } | 115 } |
116 | 116 |
117 class JobObserverImpl : public CallbacksJobFactory::JobObserver { | 117 class JobObserverImpl : public CallbacksJobFactory::JobObserver { |
118 public: | 118 public: |
119 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) OVERRIDE { | 119 virtual void OnJobCreated(URLRequestFileJobWithCallbacks* job) override { |
120 jobs_.push_back(job); | 120 jobs_.push_back(job); |
121 } | 121 } |
122 | 122 |
123 typedef std::vector<scoped_refptr<URLRequestFileJobWithCallbacks> > JobList; | 123 typedef std::vector<scoped_refptr<URLRequestFileJobWithCallbacks> > JobList; |
124 | 124 |
125 const JobList& jobs() { return jobs_; } | 125 const JobList& jobs() { return jobs_; } |
126 | 126 |
127 protected: | 127 protected: |
128 JobList jobs_; | 128 JobList jobs_; |
129 }; | 129 }; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 // 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 |
248 // not aligned on any likely page boundaries. | 248 // not aligned on any likely page boundaries. |
249 int size = 15 * 1024; | 249 int size = 15 * 1024; |
250 Range range(1701, (6 * 1024) + 3); | 250 Range range(1701, (6 * 1024) + 3); |
251 RunRequest(MakeContentOfSize(size), &range); | 251 RunRequest(MakeContentOfSize(size), &range); |
252 } | 252 } |
253 | 253 |
254 } // namespace | 254 } // namespace |
255 | 255 |
256 } // namespace net | 256 } // namespace net |
OLD | NEW |