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 #include "webkit/blob/local_file_reader.h" | 5 #include "webkit/blob/local_file_reader.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/file_util_proxy.h" | 8 #include "base/file_util_proxy.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop_proxy.h" | |
12 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "base/task_runner.h" |
13 #include "net/base/file_stream.h" | 13 #include "net/base/file_stream.h" |
14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 | 16 |
17 namespace webkit_blob { | 17 namespace webkit_blob { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const int kOpenFlagsForRead = base::PLATFORM_FILE_OPEN | | 21 const int kOpenFlagsForRead = base::PLATFORM_FILE_OPEN | |
22 base::PLATFORM_FILE_READ | | 22 base::PLATFORM_FILE_READ | |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 case base::PLATFORM_FILE_ERROR_ACCESS_DENIED: | 76 case base::PLATFORM_FILE_ERROR_ACCESS_DENIED: |
77 return net::ERR_ACCESS_DENIED; | 77 return net::ERR_ACCESS_DENIED; |
78 default: | 78 default: |
79 return net::ERR_FAILED; | 79 return net::ERR_FAILED; |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 // A helper class to open, verify and seek a file stream for a given path. | 83 // A helper class to open, verify and seek a file stream for a given path. |
84 class LocalFileReader::OpenFileStreamHelper { | 84 class LocalFileReader::OpenFileStreamHelper { |
85 public: | 85 public: |
86 explicit OpenFileStreamHelper(base::MessageLoopProxy* file_thread_proxy) | 86 explicit OpenFileStreamHelper(base::TaskRunner* task_runner) |
87 : file_thread_proxy_(file_thread_proxy), | 87 : task_runner_(task_runner), |
88 file_handle_(base::kInvalidPlatformFileValue), | 88 file_handle_(base::kInvalidPlatformFileValue), |
89 result_(net::OK) {} | 89 result_(net::OK) {} |
90 ~OpenFileStreamHelper() { | 90 ~OpenFileStreamHelper() { |
91 if (file_handle_ != base::kInvalidPlatformFileValue) { | 91 if (file_handle_ != base::kInvalidPlatformFileValue) { |
92 file_thread_proxy_->PostTask( | 92 task_runner_->PostTask( |
93 FROM_HERE, | 93 FROM_HERE, |
94 base::Bind(base::IgnoreResult(&base::ClosePlatformFile), | 94 base::Bind(base::IgnoreResult(&base::ClosePlatformFile), |
95 file_handle_)); | 95 file_handle_)); |
96 } | 96 } |
97 } | 97 } |
98 | 98 |
99 void OpenAndVerifyOnFileThread(const FilePath& file_path, | 99 void OpenAndVerifyOnFileThread(const FilePath& file_path, |
100 const base::Time& expected_modification_time) { | 100 const base::Time& expected_modification_time) { |
101 base::PlatformFileError file_error = base::PLATFORM_FILE_OK; | 101 base::PlatformFileError file_error = base::PLATFORM_FILE_OK; |
102 file_handle_ = base::CreatePlatformFile( | 102 file_handle_ = base::CreatePlatformFile( |
(...skipping 28 matching lines...) Expand all Loading... |
131 file_handle_ = base::kInvalidPlatformFileValue; | 131 file_handle_ = base::kInvalidPlatformFileValue; |
132 result_ = stream_impl->Seek(net::FROM_BEGIN, initial_offset, | 132 result_ = stream_impl->Seek(net::FROM_BEGIN, initial_offset, |
133 base::Bind(&DidSeekFile, callback, | 133 base::Bind(&DidSeekFile, callback, |
134 base::Passed(&stream_impl), | 134 base::Passed(&stream_impl), |
135 initial_offset)); | 135 initial_offset)); |
136 if (result_ != net::ERR_IO_PENDING) | 136 if (result_ != net::ERR_IO_PENDING) |
137 callback.Run(result_, stream_impl.Pass()); | 137 callback.Run(result_, stream_impl.Pass()); |
138 } | 138 } |
139 | 139 |
140 private: | 140 private: |
141 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; | 141 scoped_refptr<base::TaskRunner> task_runner_; |
142 base::PlatformFile file_handle_; | 142 base::PlatformFile file_handle_; |
143 int result_; | 143 int result_; |
144 DISALLOW_COPY_AND_ASSIGN(OpenFileStreamHelper); | 144 DISALLOW_COPY_AND_ASSIGN(OpenFileStreamHelper); |
145 }; | 145 }; |
146 | 146 |
147 LocalFileReader::LocalFileReader( | 147 LocalFileReader::LocalFileReader( |
148 base::MessageLoopProxy* file_thread_proxy, | 148 base::TaskRunner* task_runner, |
149 const FilePath& file_path, | 149 const FilePath& file_path, |
150 int64 initial_offset, | 150 int64 initial_offset, |
151 const base::Time& expected_modification_time) | 151 const base::Time& expected_modification_time) |
152 : file_thread_proxy_(file_thread_proxy), | 152 : task_runner_(task_runner), |
153 file_path_(file_path), | 153 file_path_(file_path), |
154 initial_offset_(initial_offset), | 154 initial_offset_(initial_offset), |
155 expected_modification_time_(expected_modification_time), | 155 expected_modification_time_(expected_modification_time), |
156 has_pending_open_(false), | 156 has_pending_open_(false), |
157 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | 157 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
158 | 158 |
159 LocalFileReader::~LocalFileReader() { | 159 LocalFileReader::~LocalFileReader() { |
160 if (!stream_impl_.get()) | 160 if (!stream_impl_.get()) |
161 return; | 161 return; |
162 stream_impl_->Close(base::Bind(&EmptyCompletionCallback)); | 162 stream_impl_->Close(base::Bind(&EmptyCompletionCallback)); |
163 } | 163 } |
164 | 164 |
165 int LocalFileReader::Read(net::IOBuffer* buf, int buf_len, | 165 int LocalFileReader::Read(net::IOBuffer* buf, int buf_len, |
166 const net::CompletionCallback& callback) { | 166 const net::CompletionCallback& callback) { |
167 DCHECK(!has_pending_open_); | 167 DCHECK(!has_pending_open_); |
168 if (stream_impl_.get()) | 168 if (stream_impl_.get()) |
169 return stream_impl_->Read(buf, buf_len, callback); | 169 return stream_impl_->Read(buf, buf_len, callback); |
170 return Open(base::Bind(&LocalFileReader::DidOpen, weak_factory_.GetWeakPtr(), | 170 return Open(base::Bind(&LocalFileReader::DidOpen, weak_factory_.GetWeakPtr(), |
171 make_scoped_refptr(buf), buf_len, callback)); | 171 make_scoped_refptr(buf), buf_len, callback)); |
172 } | 172 } |
173 | 173 |
174 int LocalFileReader::GetLength(const net::Int64CompletionCallback& callback) { | 174 int LocalFileReader::GetLength(const net::Int64CompletionCallback& callback) { |
175 const bool posted = base::FileUtilProxy::GetFileInfo( | 175 const bool posted = base::FileUtilProxy::GetFileInfo( |
176 file_thread_proxy_, file_path_, | 176 task_runner_, file_path_, |
177 base::Bind(&DidGetFileInfoForGetLength, callback, | 177 base::Bind(&DidGetFileInfoForGetLength, callback, |
178 expected_modification_time_)); | 178 expected_modification_time_)); |
179 DCHECK(posted); | 179 DCHECK(posted); |
180 return net::ERR_IO_PENDING; | 180 return net::ERR_IO_PENDING; |
181 } | 181 } |
182 | 182 |
183 int LocalFileReader::Open(const OpenFileStreamCallback& callback) { | 183 int LocalFileReader::Open(const OpenFileStreamCallback& callback) { |
184 DCHECK(!has_pending_open_); | 184 DCHECK(!has_pending_open_); |
185 DCHECK(!stream_impl_.get()); | 185 DCHECK(!stream_impl_.get()); |
186 has_pending_open_ = true; | 186 has_pending_open_ = true; |
187 OpenFileStreamHelper* helper = new OpenFileStreamHelper(file_thread_proxy_); | 187 OpenFileStreamHelper* helper = new OpenFileStreamHelper(task_runner_); |
188 const bool posted = file_thread_proxy_->PostTaskAndReply( | 188 const bool posted = task_runner_->PostTaskAndReply( |
189 FROM_HERE, | 189 FROM_HERE, |
190 base::Bind(&OpenFileStreamHelper::OpenAndVerifyOnFileThread, | 190 base::Bind(&OpenFileStreamHelper::OpenAndVerifyOnFileThread, |
191 base::Unretained(helper), file_path_, | 191 base::Unretained(helper), file_path_, |
192 expected_modification_time_), | 192 expected_modification_time_), |
193 base::Bind(&OpenFileStreamHelper::OpenStreamOnCallingThread, | 193 base::Bind(&OpenFileStreamHelper::OpenStreamOnCallingThread, |
194 base::Owned(helper), initial_offset_, callback)); | 194 base::Owned(helper), initial_offset_, callback)); |
195 DCHECK(posted); | 195 DCHECK(posted); |
196 return net::ERR_IO_PENDING; | 196 return net::ERR_IO_PENDING; |
197 } | 197 } |
198 | 198 |
(...skipping 10 matching lines...) Expand all Loading... |
209 return; | 209 return; |
210 } | 210 } |
211 DCHECK(stream_impl.get()); | 211 DCHECK(stream_impl.get()); |
212 stream_impl_ = stream_impl.Pass(); | 212 stream_impl_ = stream_impl.Pass(); |
213 const int read_error = stream_impl_->Read(buf, buf_len, callback); | 213 const int read_error = stream_impl_->Read(buf, buf_len, callback); |
214 if (read_error != net::ERR_IO_PENDING) | 214 if (read_error != net::ERR_IO_PENDING) |
215 callback.Run(read_error); | 215 callback.Run(read_error); |
216 } | 216 } |
217 | 217 |
218 } // namespace webkit_blob | 218 } // namespace webkit_blob |
OLD | NEW |