| 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_stream_writer.h" | |
| 6 | |
| 7 #include "base/files/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_stream_reader.h" | |
| 13 #include "webkit/fileapi/file_observers.h" | |
| 14 #include "webkit/fileapi/file_system_context.h" | |
| 15 #include "webkit/fileapi/file_system_operation.h" | |
| 16 #include "webkit/fileapi/file_system_util.h" | |
| 17 #include "webkit/fileapi/local_file_stream_writer.h" | |
| 18 #include "webkit/quota/quota_manager.h" | |
| 19 | |
| 20 namespace fileapi { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Adjust the |quota| value in overwriting case (i.e. |file_size| > 0 and | |
| 25 // |file_offset| < |file_size|) to make the remaining quota calculation easier. | |
| 26 // Specifically this widens the quota for overlapping range (so that we can | |
| 27 // simply compare written bytes against the adjusted quota). | |
| 28 int64 AdjustQuotaForOverlap(int64 quota, | |
| 29 int64 file_offset, | |
| 30 int64 file_size) { | |
| 31 DCHECK_LE(file_offset, file_size); | |
| 32 if (quota < 0) | |
| 33 quota = 0; | |
| 34 int64 overlap = file_size - file_offset; | |
| 35 if (kint64max - overlap > quota) | |
| 36 quota += overlap; | |
| 37 return quota; | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 SandboxFileStreamWriter::SandboxFileStreamWriter( | |
| 43 FileSystemContext* file_system_context, | |
| 44 const FileSystemURL& url, | |
| 45 int64 initial_offset, | |
| 46 const UpdateObserverList& observers) | |
| 47 : file_system_context_(file_system_context), | |
| 48 url_(url), | |
| 49 initial_offset_(initial_offset), | |
| 50 observers_(observers), | |
| 51 file_size_(0), | |
| 52 total_bytes_written_(0), | |
| 53 allowed_bytes_to_write_(0), | |
| 54 has_pending_operation_(false), | |
| 55 default_quota_(kint64max), | |
| 56 weak_factory_(this) { | |
| 57 DCHECK(url_.is_valid()); | |
| 58 } | |
| 59 | |
| 60 SandboxFileStreamWriter::~SandboxFileStreamWriter() {} | |
| 61 | |
| 62 int SandboxFileStreamWriter::Write( | |
| 63 net::IOBuffer* buf, int buf_len, | |
| 64 const net::CompletionCallback& callback) { | |
| 65 has_pending_operation_ = true; | |
| 66 if (local_file_writer_) | |
| 67 return WriteInternal(buf, buf_len, callback); | |
| 68 | |
| 69 base::PlatformFileError error_code; | |
| 70 FileSystemOperation* operation = | |
| 71 file_system_context_->CreateFileSystemOperation(url_, &error_code); | |
| 72 if (error_code != base::PLATFORM_FILE_OK) | |
| 73 return net::PlatformFileErrorToNetError(error_code); | |
| 74 | |
| 75 DCHECK(operation); | |
| 76 net::CompletionCallback write_task = | |
| 77 base::Bind(&SandboxFileStreamWriter::DidInitializeForWrite, | |
| 78 weak_factory_.GetWeakPtr(), | |
| 79 make_scoped_refptr(buf), buf_len, callback); | |
| 80 operation->GetMetadata( | |
| 81 url_, base::Bind(&SandboxFileStreamWriter::DidGetFileInfo, | |
| 82 weak_factory_.GetWeakPtr(), write_task)); | |
| 83 return net::ERR_IO_PENDING; | |
| 84 } | |
| 85 | |
| 86 int SandboxFileStreamWriter::Cancel(const net::CompletionCallback& callback) { | |
| 87 if (!has_pending_operation_) | |
| 88 return net::ERR_UNEXPECTED; | |
| 89 | |
| 90 DCHECK(!callback.is_null()); | |
| 91 cancel_callback_ = callback; | |
| 92 return net::ERR_IO_PENDING; | |
| 93 } | |
| 94 | |
| 95 int SandboxFileStreamWriter::WriteInternal( | |
| 96 net::IOBuffer* buf, int buf_len, | |
| 97 const net::CompletionCallback& callback) { | |
| 98 // allowed_bytes_to_write could be negative if the file size is | |
| 99 // greater than the current (possibly new) quota. | |
| 100 DCHECK(total_bytes_written_ <= allowed_bytes_to_write_ || | |
| 101 allowed_bytes_to_write_ < 0); | |
| 102 if (total_bytes_written_ >= allowed_bytes_to_write_) { | |
| 103 has_pending_operation_ = false; | |
| 104 return net::ERR_FILE_NO_SPACE; | |
| 105 } | |
| 106 | |
| 107 if (buf_len > allowed_bytes_to_write_ - total_bytes_written_) | |
| 108 buf_len = allowed_bytes_to_write_ - total_bytes_written_; | |
| 109 | |
| 110 DCHECK(local_file_writer_.get()); | |
| 111 const int result = local_file_writer_->Write( | |
| 112 buf, buf_len, | |
| 113 base::Bind(&SandboxFileStreamWriter::DidWrite, weak_factory_.GetWeakPtr(), | |
| 114 callback)); | |
| 115 if (result != net::ERR_IO_PENDING) | |
| 116 has_pending_operation_ = false; | |
| 117 return result; | |
| 118 } | |
| 119 | |
| 120 void SandboxFileStreamWriter::DidGetFileInfo( | |
| 121 const net::CompletionCallback& callback, | |
| 122 base::PlatformFileError file_error, | |
| 123 const base::PlatformFileInfo& file_info, | |
| 124 const base::FilePath& platform_path) { | |
| 125 if (CancelIfRequested()) | |
| 126 return; | |
| 127 if (file_error != base::PLATFORM_FILE_OK) { | |
| 128 callback.Run(net::PlatformFileErrorToNetError(file_error)); | |
| 129 return; | |
| 130 } | |
| 131 if (file_info.is_directory) { | |
| 132 // We should not be writing to a directory. | |
| 133 callback.Run(net::ERR_ACCESS_DENIED); | |
| 134 return; | |
| 135 } | |
| 136 file_size_ = file_info.size; | |
| 137 if (initial_offset_ > file_size_) { | |
| 138 LOG(ERROR) << initial_offset_ << ", " << file_size_; | |
| 139 // This shouldn't happen as long as we check offset in the renderer. | |
| 140 NOTREACHED(); | |
| 141 initial_offset_ = file_size_; | |
| 142 } | |
| 143 DCHECK(!local_file_writer_.get()); | |
| 144 local_file_writer_.reset( | |
| 145 new LocalFileStreamWriter(platform_path, initial_offset_)); | |
| 146 | |
| 147 quota::QuotaManagerProxy* quota_manager_proxy = | |
| 148 file_system_context_->quota_manager_proxy(); | |
| 149 if (!quota_manager_proxy) { | |
| 150 // If we don't have the quota manager or the requested filesystem type | |
| 151 // does not support quota, we should be able to let it go. | |
| 152 allowed_bytes_to_write_ = default_quota_; | |
| 153 callback.Run(net::OK); | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 DCHECK(quota_manager_proxy->quota_manager()); | |
| 158 quota_manager_proxy->quota_manager()->GetUsageAndQuota( | |
| 159 url_.origin(), | |
| 160 FileSystemTypeToQuotaStorageType(url_.type()), | |
| 161 base::Bind(&SandboxFileStreamWriter::DidGetUsageAndQuota, | |
| 162 weak_factory_.GetWeakPtr(), callback)); | |
| 163 } | |
| 164 | |
| 165 void SandboxFileStreamWriter::DidGetUsageAndQuota( | |
| 166 const net::CompletionCallback& callback, | |
| 167 quota::QuotaStatusCode status, | |
| 168 int64 usage, int64 quota) { | |
| 169 if (CancelIfRequested()) | |
| 170 return; | |
| 171 if (status != quota::kQuotaStatusOk) { | |
| 172 LOG(WARNING) << "Got unexpected quota error : " << status; | |
| 173 callback.Run(net::ERR_FAILED); | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 allowed_bytes_to_write_ = quota - usage; | |
| 178 callback.Run(net::OK); | |
| 179 } | |
| 180 | |
| 181 void SandboxFileStreamWriter::DidInitializeForWrite( | |
| 182 net::IOBuffer* buf, int buf_len, | |
| 183 const net::CompletionCallback& callback, | |
| 184 int init_status) { | |
| 185 if (CancelIfRequested()) | |
| 186 return; | |
| 187 if (init_status != net::OK) { | |
| 188 has_pending_operation_ = false; | |
| 189 callback.Run(init_status); | |
| 190 return; | |
| 191 } | |
| 192 allowed_bytes_to_write_ = AdjustQuotaForOverlap( | |
| 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 SandboxFileStreamWriter::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 (total_bytes_written_ + write_response + initial_offset_ > file_size_) { | |
| 213 int overlapped = file_size_ - total_bytes_written_ - initial_offset_; | |
| 214 if (overlapped < 0) | |
| 215 overlapped = 0; | |
| 216 observers_.Notify(&FileUpdateObserver::OnUpdate, | |
| 217 MakeTuple(url_, write_response - overlapped)); | |
| 218 } | |
| 219 total_bytes_written_ += write_response; | |
| 220 | |
| 221 if (CancelIfRequested()) | |
| 222 return; | |
| 223 callback.Run(write_response); | |
| 224 } | |
| 225 | |
| 226 bool SandboxFileStreamWriter::CancelIfRequested() { | |
| 227 if (cancel_callback_.is_null()) | |
| 228 return false; | |
| 229 | |
| 230 net::CompletionCallback pending_cancel = cancel_callback_; | |
| 231 has_pending_operation_ = false; | |
| 232 cancel_callback_.Reset(); | |
| 233 pending_cancel.Run(net::OK); | |
| 234 return true; | |
| 235 } | |
| 236 | |
| 237 int SandboxFileStreamWriter::Flush(const net::CompletionCallback& callback) { | |
| 238 DCHECK(!has_pending_operation_); | |
| 239 DCHECK(cancel_callback_.is_null()); | |
| 240 | |
| 241 // Write() is not called yet, so there's nothing to flush. | |
| 242 if (!local_file_writer_) | |
| 243 return net::OK; | |
| 244 | |
| 245 return local_file_writer_->Flush(callback); | |
| 246 } | |
| 247 | |
| 248 } // namespace fileapi | |
| OLD | NEW |