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 AdjustQuotaForOverlap(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) { |
| 173 if (CancelIfRequested()) |
| 174 return; |
| 175 if (status != quota::kQuotaStatusOk) { |
| 176 LOG(WARNING) << "Got unexpected quota error : " << status; |
| 177 callback.Run(net::ERR_FAILED); |
| 178 return; |
| 179 } |
| 180 |
| 181 allowed_bytes_to_write_ = quota - usage; |
| 182 callback.Run(net::OK); |
| 183 } |
| 184 |
| 185 void SandboxFileWriter::DidInitializeForWrite( |
| 186 net::IOBuffer* buf, int buf_len, |
| 187 const net::CompletionCallback& callback, |
| 188 int init_status) { |
| 189 if (CancelIfRequested()) |
| 190 return; |
| 191 if (init_status != net::OK) { |
| 192 has_pending_operation_ = false; |
| 193 callback.Run(init_status); |
| 194 return; |
| 195 } |
| 196 allowed_bytes_to_write_ = AdjustQuotaForOverlap( |
| 197 allowed_bytes_to_write_, initial_offset_, file_size_); |
| 198 const int result = WriteInternal(buf, buf_len, callback); |
| 199 if (result != net::ERR_IO_PENDING) |
| 200 callback.Run(result); |
| 201 } |
| 202 |
| 203 void SandboxFileWriter::DidWrite( |
| 204 const net::CompletionCallback& callback, |
| 205 int write_response) { |
| 206 DCHECK(has_pending_operation_); |
| 207 has_pending_operation_ = false; |
| 208 |
| 209 if (write_response <= 0) { |
| 210 if (CancelIfRequested()) |
| 211 return; |
| 212 callback.Run(write_response); |
| 213 return; |
| 214 } |
| 215 |
| 216 if (quota_util() && |
| 217 total_bytes_written_ + write_response + initial_offset_ > file_size_) { |
| 218 int overlapped = file_size_ - total_bytes_written_ - initial_offset_; |
| 219 if (overlapped < 0) |
| 220 overlapped = 0; |
| 221 quota_util()->proxy()->UpdateOriginUsage( |
| 222 file_system_context_->quota_manager_proxy(), |
| 223 origin_, file_system_type_, write_response - overlapped); |
| 224 } |
| 225 total_bytes_written_ += write_response; |
| 226 |
| 227 if (CancelIfRequested()) |
| 228 return; |
| 229 callback.Run(write_response); |
| 230 } |
| 231 |
| 232 bool SandboxFileWriter::CancelIfRequested() { |
| 233 if (cancel_callback_.is_null()) |
| 234 return false; |
| 235 |
| 236 net::CompletionCallback pending_cancel = cancel_callback_; |
| 237 has_pending_operation_ = false; |
| 238 cancel_callback_.Reset(); |
| 239 pending_cancel.Run(net::OK); |
| 240 return true; |
| 241 } |
| 242 |
| 243 FileSystemQuotaUtil* SandboxFileWriter::quota_util() const { |
| 244 DCHECK(file_system_context_.get()); |
| 245 return file_system_context_->GetQuotaUtil(file_system_type_); |
| 246 } |
| 247 |
| 248 } // namespace fileapi |
OLD | NEW |