Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/fileapi/quota_file_util.h" | 5 #include "webkit/fileapi/quota_file_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "webkit/fileapi/file_system_context.h" | 9 #include "webkit/fileapi/file_system_context.h" |
| 10 #include "webkit/fileapi/file_system_operation_context.h" | 10 #include "webkit/fileapi/file_system_operation_context.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 FileSystemOperationContext* operation_context_; | 63 FileSystemOperationContext* operation_context_; |
| 64 FileSystemQuotaUtil* quota_util_; | 64 FileSystemQuotaUtil* quota_util_; |
| 65 QuotaManagerProxy* quota_manager_proxy_; | 65 QuotaManagerProxy* quota_manager_proxy_; |
| 66 const GURL& origin_url_; | 66 const GURL& origin_url_; |
| 67 FileSystemType type_; | 67 FileSystemType type_; |
| 68 DISALLOW_COPY_AND_ASSIGN(ScopedOriginUpdateHelper); | 68 DISALLOW_COPY_AND_ASSIGN(ScopedOriginUpdateHelper); |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 } // namespace (anonymous) | 71 } // namespace (anonymous) |
| 72 | 72 |
| 73 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil* underlying_file_util) | |
| 74 : underlying_file_util_(underlying_file_util) { | |
| 75 } | |
| 76 | |
| 77 QuotaFileUtil::~QuotaFileUtil() { | |
| 78 } | |
| 79 | |
| 73 // static | 80 // static |
| 74 QuotaFileUtil* QuotaFileUtil::GetInstance() { | 81 QuotaFileUtil* QuotaFileUtil::GetDefault() { |
|
kinuko
2011/07/26 06:00:35
Please make the method name obvious that it create
Dai Mikurube (NOT FULLTIME)
2011/07/26 06:52:46
Done.
| |
| 75 return Singleton<QuotaFileUtil>::get(); | 82 return new QuotaFileUtil(new FileSystemFileUtil()); |
| 76 } | 83 } |
| 77 | 84 |
| 78 int64 QuotaFileUtil::ComputeFilePathCost(const FilePath& file_path) const { | 85 int64 QuotaFileUtil::ComputeFilePathCost(const FilePath& file_path) const { |
| 79 return kFilePathCostPerFile + | 86 return kFilePathCostPerFile + |
| 80 file_path.BaseName().value().length() * kFilePathCostPerChar; | 87 file_path.BaseName().value().length() * kFilePathCostPerChar; |
| 81 } | 88 } |
| 82 | 89 |
| 83 PlatformFileError QuotaFileUtil::CreateOrOpen( | 90 PlatformFileError QuotaFileUtil::CreateOrOpen( |
| 84 FileSystemOperationContext* fs_context, | 91 FileSystemOperationContext* fs_context, |
| 85 const FilePath& file_path, int file_flags, | 92 const FilePath& file_path, int file_flags, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 96 if (growth > 0) { | 103 if (growth > 0) { |
| 97 helper.reset(new ScopedOriginUpdateHelper( | 104 helper.reset(new ScopedOriginUpdateHelper( |
| 98 fs_context, | 105 fs_context, |
| 99 fs_context->src_origin_url(), | 106 fs_context->src_origin_url(), |
| 100 fs_context->src_type())); | 107 fs_context->src_type())); |
| 101 | 108 |
| 102 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) | 109 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) |
| 103 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 110 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 104 } | 111 } |
| 105 | 112 |
| 106 base::PlatformFileError error = FileSystemFileUtil::GetInstance()-> | 113 base::PlatformFileError error = underlying_file_util_->CreateOrOpen( |
| 107 CreateOrOpen(fs_context, file_path, file_flags, file_handle, created); | 114 fs_context, file_path, file_flags, file_handle, created); |
| 108 | 115 |
| 109 if (growth > 0) { | 116 if (growth > 0) { |
| 110 if (error == base::PLATFORM_FILE_OK) | 117 if (error == base::PLATFORM_FILE_OK) |
| 111 helper->NotifyUpdate(growth); | 118 helper->NotifyUpdate(growth); |
| 112 } | 119 } |
| 113 | 120 |
| 114 return error; | 121 return error; |
| 115 } | 122 } |
| 116 | 123 |
| 117 PlatformFileError QuotaFileUtil::EnsureFileExists( | 124 PlatformFileError QuotaFileUtil::EnsureFileExists( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 130 if (growth > 0) { | 137 if (growth > 0) { |
| 131 helper.reset(new ScopedOriginUpdateHelper( | 138 helper.reset(new ScopedOriginUpdateHelper( |
| 132 fs_context, | 139 fs_context, |
| 133 fs_context->src_origin_url(), | 140 fs_context->src_origin_url(), |
| 134 fs_context->src_type())); | 141 fs_context->src_type())); |
| 135 | 142 |
| 136 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) | 143 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) |
| 137 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 144 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 138 } | 145 } |
| 139 | 146 |
| 140 base::PlatformFileError error = FileSystemFileUtil::GetInstance()-> | 147 base::PlatformFileError error = underlying_file_util_->EnsureFileExists( |
| 141 EnsureFileExists(fs_context, file_path, created); | 148 fs_context, file_path, created); |
| 142 | 149 |
| 143 if (growth > 0 &&error == base::PLATFORM_FILE_OK) | 150 if (growth > 0 &&error == base::PLATFORM_FILE_OK) |
| 144 helper->NotifyUpdate(growth); | 151 helper->NotifyUpdate(growth); |
| 145 | 152 |
| 146 return error; | 153 return error; |
| 147 } | 154 } |
| 148 | 155 |
| 149 PlatformFileError QuotaFileUtil::CreateDirectory( | 156 PlatformFileError QuotaFileUtil::CreateDirectory( |
| 150 FileSystemOperationContext* fs_context, | 157 FileSystemOperationContext* fs_context, |
| 151 const FilePath& file_path, | 158 const FilePath& file_path, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 177 helper.reset(new ScopedOriginUpdateHelper( | 184 helper.reset(new ScopedOriginUpdateHelper( |
| 178 fs_context, | 185 fs_context, |
| 179 fs_context->src_origin_url(), | 186 fs_context->src_origin_url(), |
| 180 fs_context->src_type())); | 187 fs_context->src_type())); |
| 181 | 188 |
| 182 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) | 189 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) |
| 183 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 190 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 184 } | 191 } |
| 185 | 192 |
| 186 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 193 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 187 error = FileSystemFileUtil::GetInstance()-> | 194 error = underlying_file_util_->CreateDirectory( |
| 188 CreateDirectory(fs_context, file_path, exclusive, recursive); | 195 fs_context, file_path, exclusive, recursive); |
| 189 | 196 |
| 190 if (growth > 0 && error == base::PLATFORM_FILE_OK) | 197 if (growth > 0 && error == base::PLATFORM_FILE_OK) |
| 191 helper->NotifyUpdate(growth); | 198 helper->NotifyUpdate(growth); |
| 192 | 199 |
| 193 return error; | 200 return error; |
| 194 } | 201 } |
| 195 | 202 |
| 196 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( | 203 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( |
| 197 FileSystemOperationContext* fs_context, | 204 FileSystemOperationContext* fs_context, |
| 198 const FilePath& src_file_path, | 205 const FilePath& src_file_path, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 224 ComputeFilePathCost(fs_context->src_virtual_path()); | 231 ComputeFilePathCost(fs_context->src_virtual_path()); |
| 225 int64 dest_file_path_cost = | 232 int64 dest_file_path_cost = |
| 226 ComputeFilePathCost(fs_context->dest_virtual_path()); | 233 ComputeFilePathCost(fs_context->dest_virtual_path()); |
| 227 if (!file_util::GetFileInfo(dest_file_path, &dest_file_info)) | 234 if (!file_util::GetFileInfo(dest_file_path, &dest_file_info)) |
| 228 dest_file_info.size = 0; | 235 dest_file_info.size = 0; |
| 229 growth = -dest_file_info.size - src_file_path_cost + dest_file_path_cost; | 236 growth = -dest_file_info.size - src_file_path_cost + dest_file_path_cost; |
| 230 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) | 237 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) |
| 231 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 238 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 232 } | 239 } |
| 233 | 240 |
| 234 base::PlatformFileError error = FileSystemFileUtil::GetInstance()-> | 241 base::PlatformFileError error = underlying_file_util_->CopyOrMoveFile( |
| 235 CopyOrMoveFile(fs_context, src_file_path, dest_file_path, copy); | 242 fs_context, src_file_path, dest_file_path, copy); |
| 236 | 243 |
| 237 if (error == base::PLATFORM_FILE_OK) { | 244 if (error == base::PLATFORM_FILE_OK) { |
| 238 // TODO(kinuko): For cross-filesystem move case, call this with -growth | 245 // TODO(kinuko): For cross-filesystem move case, call this with -growth |
| 239 // for source and growth for dest. | 246 // for source and growth for dest. |
| 240 helper.NotifyUpdate(growth); | 247 helper.NotifyUpdate(growth); |
| 241 } | 248 } |
| 242 | 249 |
| 243 return error; | 250 return error; |
| 244 } | 251 } |
| 245 | 252 |
| 246 base::PlatformFileError QuotaFileUtil::DeleteFile( | 253 base::PlatformFileError QuotaFileUtil::DeleteFile( |
| 247 FileSystemOperationContext* fs_context, | 254 FileSystemOperationContext* fs_context, |
| 248 const FilePath& file_path) { | 255 const FilePath& file_path) { |
| 249 DCHECK(fs_context); | 256 DCHECK(fs_context); |
| 250 ScopedOriginUpdateHelper helper( | 257 ScopedOriginUpdateHelper helper( |
| 251 fs_context, | 258 fs_context, |
| 252 fs_context->src_origin_url(), | 259 fs_context->src_origin_url(), |
| 253 fs_context->src_type()); | 260 fs_context->src_type()); |
| 254 | 261 |
| 255 int64 growth = 0; | 262 int64 growth = 0; |
| 256 base::PlatformFileInfo file_info; | 263 base::PlatformFileInfo file_info; |
| 257 if (file_util::GetFileInfo(file_path, &file_info)) { | 264 if (file_util::GetFileInfo(file_path, &file_info)) { |
| 258 growth -= file_info.size + | 265 growth -= file_info.size + |
| 259 ComputeFilePathCost(fs_context->src_virtual_path()); | 266 ComputeFilePathCost(fs_context->src_virtual_path()); |
| 260 } | 267 } |
| 261 | 268 |
| 262 base::PlatformFileError error = FileSystemFileUtil::GetInstance()-> | 269 base::PlatformFileError error = underlying_file_util_->DeleteFile( |
| 263 DeleteFile(fs_context, file_path); | 270 fs_context, file_path); |
| 264 | 271 |
| 265 if (error == base::PLATFORM_FILE_OK) | 272 if (error == base::PLATFORM_FILE_OK) |
| 266 helper.NotifyUpdate(growth); | 273 helper.NotifyUpdate(growth); |
| 267 | 274 |
| 268 return error; | 275 return error; |
| 269 } | 276 } |
| 270 | 277 |
| 271 base::PlatformFileError QuotaFileUtil::DeleteSingleDirectory( | 278 base::PlatformFileError QuotaFileUtil::DeleteSingleDirectory( |
| 272 FileSystemOperationContext* fs_context, | 279 FileSystemOperationContext* fs_context, |
| 273 const FilePath& file_path) { | 280 const FilePath& file_path) { |
| 274 DCHECK(fs_context); | 281 DCHECK(fs_context); |
| 275 ScopedOriginUpdateHelper helper( | 282 ScopedOriginUpdateHelper helper( |
| 276 fs_context, | 283 fs_context, |
| 277 fs_context->src_origin_url(), | 284 fs_context->src_origin_url(), |
| 278 fs_context->src_type()); | 285 fs_context->src_type()); |
| 279 | 286 |
| 280 int64 growth = 0; | 287 int64 growth = 0; |
| 281 if (file_util::DirectoryExists(file_path)) | 288 if (file_util::DirectoryExists(file_path)) |
| 282 growth -= ComputeFilePathCost(fs_context->src_virtual_path()); | 289 growth -= ComputeFilePathCost(fs_context->src_virtual_path()); |
| 283 | 290 |
| 284 base::PlatformFileError error = FileSystemFileUtil::GetInstance()-> | 291 base::PlatformFileError error = underlying_file_util_->DeleteSingleDirectory( |
| 285 DeleteSingleDirectory(fs_context, file_path); | 292 fs_context, file_path); |
| 286 | 293 |
| 287 if (error == base::PLATFORM_FILE_OK) | 294 if (error == base::PLATFORM_FILE_OK) |
| 288 helper.NotifyUpdate(growth); | 295 helper.NotifyUpdate(growth); |
| 289 | 296 |
| 290 return error; | 297 return error; |
| 291 } | 298 } |
| 292 | 299 |
| 293 base::PlatformFileError QuotaFileUtil::Truncate( | 300 base::PlatformFileError QuotaFileUtil::Truncate( |
| 294 FileSystemOperationContext* fs_context, | 301 FileSystemOperationContext* fs_context, |
| 295 const FilePath& path, | 302 const FilePath& path, |
| 296 int64 length) { | 303 int64 length) { |
| 297 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); | 304 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); |
| 298 ScopedOriginUpdateHelper helper( | 305 ScopedOriginUpdateHelper helper( |
| 299 fs_context, | 306 fs_context, |
| 300 fs_context->src_origin_url(), | 307 fs_context->src_origin_url(), |
| 301 fs_context->src_type()); | 308 fs_context->src_type()); |
| 302 | 309 |
| 303 int64 growth = 0; | 310 int64 growth = 0; |
| 304 base::PlatformFileInfo file_info; | 311 base::PlatformFileInfo file_info; |
| 305 if (!file_util::GetFileInfo(path, &file_info)) | 312 if (!file_util::GetFileInfo(path, &file_info)) |
| 306 return base::PLATFORM_FILE_ERROR_FAILED; | 313 return base::PLATFORM_FILE_ERROR_FAILED; |
| 307 | 314 |
| 308 growth = length - file_info.size; | 315 growth = length - file_info.size; |
| 309 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) | 316 if (allowed_bytes_growth != kNoLimit && growth > allowed_bytes_growth) |
| 310 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 317 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 311 | 318 |
| 312 base::PlatformFileError error = FileSystemFileUtil::GetInstance()->Truncate( | 319 base::PlatformFileError error = underlying_file_util_->Truncate( |
| 313 fs_context, path, length); | 320 fs_context, path, length); |
| 314 | 321 |
| 315 if (error == base::PLATFORM_FILE_OK) | 322 if (error == base::PLATFORM_FILE_OK) |
| 316 helper.NotifyUpdate(growth); | 323 helper.NotifyUpdate(growth); |
| 317 | 324 |
| 318 return error; | 325 return error; |
| 319 } | 326 } |
| 320 | 327 |
| 321 // Checks if copying in the same filesystem can be performed. | 328 // Checks if copying in the same filesystem can be performed. |
| 322 // This method is not called for moving within a single filesystem. | 329 // This method is not called for moving within a single filesystem. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 340 } | 347 } |
| 341 *growth = src_file_info.size - dest_file_info.size + dest_file_path_cost; | 348 *growth = src_file_info.size - dest_file_info.size + dest_file_path_cost; |
| 342 if (allowed_bytes_growth != QuotaFileUtil::kNoLimit && | 349 if (allowed_bytes_growth != QuotaFileUtil::kNoLimit && |
| 343 *growth > allowed_bytes_growth) | 350 *growth > allowed_bytes_growth) |
| 344 return false; | 351 return false; |
| 345 | 352 |
| 346 return true; | 353 return true; |
| 347 } | 354 } |
| 348 | 355 |
| 349 } // namespace fileapi | 356 } // namespace fileapi |
| OLD | NEW |