OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/fileapi/sandbox_file_writer.h" | |
6 | |
7 #include "base/file_util_proxy.h" | |
8 #include "base/platform_file.h" | |
9 #include "base/sequenced_task_runner.h" | |
10 #include "net/base/io_buffer.h" | |
11 #include "net/base/net_errors.h" | |
12 #include "webkit/blob/local_file_reader.h" | |
13 #include "webkit/fileapi/file_system_context.h" | |
14 #include "webkit/fileapi/file_system_operation_interface.h" | |
15 #include "webkit/fileapi/file_system_quota_util.h" | |
16 #include "webkit/fileapi/file_system_util.h" | |
17 #include "webkit/fileapi/local_file_writer.h" | |
18 #include "webkit/quota/quota_manager.h" | |
19 | |
20 namespace fileapi { | |
21 | |
22 namespace { | |
23 | |
24 int PlatformFileErrorToNetError(base::PlatformFileError error) { | |
25 // TODO(kinuko): Move this static method to more convenient place. | |
26 return webkit_blob::LocalFileReader::PlatformFileErrorToNetError(error); | |
27 } | |
28 | |
29 // Adjust the |quota| value in overwriting case (i.e. |file_size| > 0 and | |
30 // |file_offset| < |file_size|) to make the remaining quota calculation easier. | |
31 // Specifically this widens the quota for overlapping range (so that we can | |
32 // simply compare written bytes against the adjusted quota). | |
33 int64 AdjustQuotaWithFileRange(int64 quota, | |
34 int64 file_offset, | |
35 int64 file_size) { | |
36 DCHECK_LE(file_offset, file_size); | |
37 if (quota < 0) | |
38 quota = 0; | |
39 int64 overlap = file_size - file_offset; | |
40 if (kint64max - overlap > quota) | |
41 quota += overlap; | |
42 return quota; | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 SandboxFileWriter::SandboxFileWriter( | |
48 FileSystemContext* file_system_context, | |
49 const GURL& url, | |
50 int64 initial_offset) | |
51 : file_system_context_(file_system_context), | |
52 url_(url), | |
53 initial_offset_(initial_offset), | |
54 file_size_(0), | |
55 total_bytes_written_(0), | |
56 allowed_bytes_to_write_(0), | |
57 has_pending_operation_(false), | |
58 default_quota_(kint64max), | |
59 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
60 const bool result = CrackFileSystemURL( | |
61 url_, &origin_, &file_system_type_, &virtual_path_); | |
62 DCHECK(result); | |
63 } | |
64 | |
65 SandboxFileWriter::~SandboxFileWriter() { | |
66 if (quota_util()) | |
67 quota_util()->proxy()->EndUpdateOrigin(origin_, file_system_type_); | |
68 } | |
69 | |
70 int SandboxFileWriter::Write( | |
71 net::IOBuffer* buf, int buf_len, | |
72 const net::CompletionCallback& callback) { | |
73 has_pending_operation_ = true; | |
74 if (local_file_writer_.get()) | |
75 return WriteInternal(buf, buf_len, callback); | |
76 | |
77 FileSystemOperationInterface* operation = | |
78 file_system_context_->CreateFileSystemOperation(url_); | |
79 DCHECK(operation); | |
80 net::CompletionCallback write_task = | |
81 base::Bind(&SandboxFileWriter::DidInitializeForWrite, | |
82 weak_factory_.GetWeakPtr(), | |
83 make_scoped_refptr(buf), buf_len, callback); | |
84 operation->GetMetadata( | |
85 url_, base::Bind(&SandboxFileWriter::DidGetFileInfo, | |
86 weak_factory_.GetWeakPtr(), write_task)); | |
87 return net::ERR_IO_PENDING; | |
88 } | |
89 | |
90 int SandboxFileWriter::Cancel(const net::CompletionCallback& callback) { | |
91 if (!has_pending_operation_) | |
92 return net::ERR_UNEXPECTED; | |
93 | |
94 DCHECK(!callback.is_null()); | |
95 cancel_callback_ = callback; | |
96 return net::ERR_IO_PENDING; | |
97 } | |
98 | |
99 int SandboxFileWriter::WriteInternal( | |
100 net::IOBuffer* buf, int buf_len, | |
101 const net::CompletionCallback& callback) { | |
102 // allowed_bytes_to_write could be negative if the file size is | |
103 // greater than the current (possibly new) quota. | |
104 DCHECK(total_bytes_written_ <= allowed_bytes_to_write_ || | |
105 allowed_bytes_to_write_ < 0); | |
106 if (total_bytes_written_ >= allowed_bytes_to_write_) { | |
107 has_pending_operation_ = false; | |
108 return net::ERR_FILE_NO_SPACE; | |
109 } | |
110 | |
111 if (buf_len > allowed_bytes_to_write_ - total_bytes_written_) | |
112 buf_len = allowed_bytes_to_write_ - total_bytes_written_; | |
113 | |
114 DCHECK(local_file_writer_.get()); | |
115 const int result = local_file_writer_->Write( | |
116 buf, buf_len, | |
117 base::Bind(&SandboxFileWriter::DidWrite, weak_factory_.GetWeakPtr(), | |
118 callback)); | |
119 if (result != net::ERR_IO_PENDING) | |
120 has_pending_operation_ = false; | |
121 return result; | |
122 } | |
123 | |
124 void SandboxFileWriter::DidGetFileInfo( | |
125 const net::CompletionCallback& callback, | |
126 base::PlatformFileError file_error, | |
127 const base::PlatformFileInfo& file_info, | |
128 const FilePath& platform_path) { | |
129 if (CancelIfRequested()) | |
130 return; | |
131 if (file_error != base::PLATFORM_FILE_OK) { | |
132 callback.Run(PlatformFileErrorToNetError(file_error)); | |
133 return; | |
134 } | |
135 if (file_info.is_directory) { | |
136 // We should not be writing to a directory. | |
137 callback.Run(net::ERR_ACCESS_DENIED); | |
138 return; | |
139 } | |
140 file_size_ = file_info.size; | |
141 if (initial_offset_ > file_size_) { | |
142 LOG(ERROR) << initial_offset_ << ", " << file_size_; | |
143 // This shouldn't happen as long as we check offset in the renderer. | |
144 NOTREACHED(); | |
145 initial_offset_ = file_size_; | |
146 } | |
147 DCHECK(!local_file_writer_.get()); | |
148 local_file_writer_.reset(new LocalFileWriter(platform_path, initial_offset_)); | |
149 | |
150 quota::QuotaManagerProxy* quota_manager_proxy = | |
151 file_system_context_->quota_manager_proxy(); | |
152 if (!quota_manager_proxy || !quota_util()) { | |
153 // If we don't have the quota manager or the requested filesystem type | |
154 // does not support quota, we should be able to let it go. | |
155 allowed_bytes_to_write_ = default_quota_; | |
156 callback.Run(net::OK); | |
157 return; | |
158 } | |
159 | |
160 quota_util()->proxy()->StartUpdateOrigin(origin_, file_system_type_); | |
161 DCHECK(quota_manager_proxy->quota_manager()); | |
162 quota_manager_proxy->quota_manager()->GetUsageAndQuota( | |
163 origin_, | |
164 FileSystemTypeToQuotaStorageType(file_system_type_), | |
165 base::Bind(&SandboxFileWriter::DidGetUsageAndQuota, | |
166 weak_factory_.GetWeakPtr(), callback)); | |
167 } | |
168 | |
169 void SandboxFileWriter::DidGetUsageAndQuota( | |
170 const net::CompletionCallback& callback, | |
171 quota::QuotaStatusCode status, | |
172 int64 usage, int64 quota) { | |
ericu
2012/05/17 00:04:13
This looks like it needs a call to CancelIfRequest
kinuko
2012/05/17 04:45:27
Done.
| |
173 if (status != quota::kQuotaStatusOk) { | |
174 LOG(WARNING) << "Got unexpected quota error : " << status; | |
175 callback.Run(net::ERR_FAILED); | |
176 return; | |
177 } | |
178 | |
179 allowed_bytes_to_write_ = quota - usage; | |
180 callback.Run(net::OK); | |
181 } | |
182 | |
183 void SandboxFileWriter::DidInitializeForWrite( | |
184 net::IOBuffer* buf, int buf_len, | |
185 const net::CompletionCallback& callback, | |
186 int init_status) { | |
ericu
2012/05/17 00:04:13
This looks like it needs a call to CancelIfRequest
kinuko
2012/05/17 04:45:27
Done.
| |
187 if (init_status != net::OK) { | |
188 has_pending_operation_ = false; | |
189 callback.Run(init_status); | |
190 return; | |
191 } | |
192 allowed_bytes_to_write_ = AdjustQuotaWithFileRange( | |
193 allowed_bytes_to_write_, initial_offset_, file_size_); | |
194 const int result = WriteInternal(buf, buf_len, callback); | |
195 if (result != net::ERR_IO_PENDING) | |
196 callback.Run(result); | |
197 } | |
198 | |
199 void SandboxFileWriter::DidWrite( | |
200 const net::CompletionCallback& callback, | |
201 int write_response) { | |
202 DCHECK(has_pending_operation_); | |
203 has_pending_operation_ = false; | |
204 | |
205 if (write_response <= 0) { | |
206 if (CancelIfRequested()) | |
207 return; | |
208 callback.Run(write_response); | |
209 return; | |
210 } | |
211 | |
212 if (quota_util() && | |
213 total_bytes_written_ + write_response + initial_offset_ > file_size_) { | |
214 int overlapped = file_size_ - total_bytes_written_ - initial_offset_; | |
215 if (overlapped < 0) | |
216 overlapped = 0; | |
217 quota_util()->proxy()->UpdateOriginUsage( | |
218 file_system_context_->quota_manager_proxy(), | |
219 origin_, file_system_type_, write_response - overlapped); | |
220 } | |
221 total_bytes_written_ += write_response; | |
222 | |
223 if (CancelIfRequested()) | |
224 return; | |
225 callback.Run(write_response); | |
226 } | |
227 | |
228 bool SandboxFileWriter::CancelIfRequested() { | |
229 if (cancel_callback_.is_null()) | |
230 return false; | |
231 | |
232 net::CompletionCallback pending_cancel = cancel_callback_; | |
233 has_pending_operation_ = false; | |
234 cancel_callback_.Reset(); | |
235 pending_cancel.Run(net::OK); | |
236 return true; | |
237 } | |
238 | |
239 FileSystemQuotaUtil* SandboxFileWriter::quota_util() const { | |
240 DCHECK(file_system_context_.get()); | |
241 return file_system_context_->GetQuotaUtil(file_system_type_); | |
242 } | |
243 | |
244 } // namespace fileapi | |
OLD | NEW |