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 28 matching lines...) Expand all Loading... | |
| 39 if (allowed_bytes_growth != QuotaFileUtil::kNoLimit && | 39 if (allowed_bytes_growth != QuotaFileUtil::kNoLimit && |
| 40 growth_local > 0 && growth_local > allowed_bytes_growth) | 40 growth_local > 0 && growth_local > allowed_bytes_growth) |
| 41 return false; | 41 return false; |
| 42 if (growth != NULL) | 42 if (growth != NULL) |
| 43 *growth = growth_local; | 43 *growth = growth_local; |
| 44 | 44 |
| 45 return true; | 45 return true; |
| 46 } | 46 } |
| 47 | 47 |
| 48 // A helper class to hook quota_util() methods before and after modifications. | 48 // A helper class to hook quota_util() methods before and after modifications. |
| 49 class ScopedOriginUpdateHelper { | 49 class OriginUpdateHelper { |
| 50 public: | 50 public: |
| 51 explicit ScopedOriginUpdateHelper( | 51 explicit OriginUpdateHelper( |
| 52 FileSystemOperationContext* operation_context, | 52 FileSystemOperationContext* operation_context, |
| 53 const GURL& origin_url, | 53 const GURL& origin_url, |
| 54 FileSystemType type) | 54 FileSystemType type) |
| 55 : operation_context_(operation_context), | 55 : operation_context_(operation_context), |
| 56 origin_url_(origin_url), | 56 origin_url_(origin_url), |
| 57 type_(type) { | 57 type_(type) { |
| 58 DCHECK(operation_context_); | 58 DCHECK(operation_context_); |
| 59 DCHECK(operation_context_->file_system_context()); | 59 DCHECK(operation_context_->file_system_context()); |
| 60 DCHECK(type != kFileSystemTypeUnknown); | 60 DCHECK(type != kFileSystemTypeUnknown); |
| 61 quota_util_ = | 61 quota_util_ = |
| 62 operation_context_->file_system_context()->GetQuotaUtil(type_); | 62 operation_context_->file_system_context()->GetQuotaUtil(type_); |
| 63 quota_manager_proxy_ = | 63 quota_manager_proxy_ = |
| 64 operation_context_->file_system_context()->quota_manager_proxy(); | 64 operation_context_->file_system_context()->quota_manager_proxy(); |
| 65 if (quota_util_) | |
| 66 quota_util_->StartUpdateOriginOnFileThread(origin_url_, type_); | |
| 67 } | 65 } |
| 68 | 66 |
| 69 ~ScopedOriginUpdateHelper() { | 67 ~OriginUpdateHelper() { |
| 70 if (quota_util_) | |
| 71 quota_util_->EndUpdateOriginOnFileThread(origin_url_, type_); | |
| 72 } | 68 } |
| 73 | 69 |
| 74 void NotifyUpdate(int64 growth) { | 70 void NotifyUpdate(int64 growth) { |
| 75 operation_context_->set_allowed_bytes_growth( | 71 operation_context_->set_allowed_bytes_growth( |
| 76 operation_context_->allowed_bytes_growth() - growth); | 72 operation_context_->allowed_bytes_growth() - growth); |
| 77 if (quota_util_) | 73 if (quota_util_) |
| 78 quota_util_->UpdateOriginUsageOnFileThread( | 74 quota_util_->UpdateOriginUsageOnFileThread( |
| 79 quota_manager_proxy_, origin_url_, type_, growth); | 75 quota_manager_proxy_, origin_url_, type_, growth); |
| 80 } | 76 } |
| 81 | 77 |
| 82 private: | 78 private: |
| 83 FileSystemOperationContext* operation_context_; | 79 FileSystemOperationContext* operation_context_; |
| 84 FileSystemQuotaUtil* quota_util_; | 80 FileSystemQuotaUtil* quota_util_; |
| 85 QuotaManagerProxy* quota_manager_proxy_; | 81 QuotaManagerProxy* quota_manager_proxy_; |
| 86 const GURL& origin_url_; | 82 const GURL& origin_url_; |
| 87 FileSystemType type_; | 83 FileSystemType type_; |
| 88 DISALLOW_COPY_AND_ASSIGN(ScopedOriginUpdateHelper); | 84 DISALLOW_COPY_AND_ASSIGN(OriginUpdateHelper); |
| 89 }; | 85 }; |
| 90 | 86 |
| 91 } // namespace (anonymous) | 87 } // namespace (anonymous) |
| 92 | 88 |
| 93 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil* underlying_file_util) | 89 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil* underlying_file_util) |
| 94 : underlying_file_util_(underlying_file_util) { | 90 : underlying_file_util_(underlying_file_util) { |
| 95 } | 91 } |
| 96 | 92 |
| 97 QuotaFileUtil::~QuotaFileUtil() { | 93 QuotaFileUtil::~QuotaFileUtil() { |
| 98 } | 94 } |
| 99 | 95 |
| 100 // static | 96 // static |
| 101 QuotaFileUtil* QuotaFileUtil::CreateDefault() { | 97 QuotaFileUtil* QuotaFileUtil::CreateDefault() { |
| 102 return new QuotaFileUtil(new FileSystemFileUtil()); | 98 return new QuotaFileUtil(new FileSystemFileUtil()); |
| 103 } | 99 } |
| 104 | 100 |
| 105 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( | 101 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( |
| 106 FileSystemOperationContext* fs_context, | 102 FileSystemOperationContext* fs_context, |
| 107 const FilePath& src_file_path, | 103 const FilePath& src_file_path, |
| 108 const FilePath& dest_file_path, | 104 const FilePath& dest_file_path, |
| 109 bool copy) { | 105 bool copy) { |
| 110 DCHECK(fs_context); | 106 DCHECK(fs_context); |
| 111 | 107 |
| 112 // TODO(kinuko): For cross-filesystem move case we need 2 helpers, one for | 108 // TODO(kinuko): For cross-filesystem move case we need 2 helpers, one for |
| 113 // src and one for dest. | 109 // src and one for dest. |
| 114 ScopedOriginUpdateHelper helper( | 110 OriginUpdateHelper helper( |
|
kinuko
2011/08/25 09:12:00
If all we do is calling NotifyUpdate() it looks li
Dai Mikurube (NOT FULLTIME)
2011/08/25 09:19:46
Ah, exactly. I'll modify it in the next change.
| |
| 115 fs_context, | 111 fs_context, |
| 116 fs_context->dest_origin_url(), | 112 fs_context->dest_origin_url(), |
| 117 fs_context->dest_type()); | 113 fs_context->dest_type()); |
| 118 | 114 |
| 119 int64 growth = 0; | 115 int64 growth = 0; |
| 120 | 116 |
| 121 // It assumes copy/move operations are always in the same fs currently. | 117 // It assumes copy/move operations are always in the same fs currently. |
| 122 // TODO(dmikurube): Do quota check if moving between different fs. | 118 // TODO(dmikurube): Do quota check if moving between different fs. |
| 123 if (copy) { | 119 if (copy) { |
| 124 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); | 120 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 141 helper.NotifyUpdate(growth); | 137 helper.NotifyUpdate(growth); |
| 142 } | 138 } |
| 143 | 139 |
| 144 return error; | 140 return error; |
| 145 } | 141 } |
| 146 | 142 |
| 147 base::PlatformFileError QuotaFileUtil::DeleteFile( | 143 base::PlatformFileError QuotaFileUtil::DeleteFile( |
| 148 FileSystemOperationContext* fs_context, | 144 FileSystemOperationContext* fs_context, |
| 149 const FilePath& file_path) { | 145 const FilePath& file_path) { |
| 150 DCHECK(fs_context); | 146 DCHECK(fs_context); |
| 151 ScopedOriginUpdateHelper helper( | 147 OriginUpdateHelper helper( |
| 152 fs_context, | 148 fs_context, |
| 153 fs_context->src_origin_url(), | 149 fs_context->src_origin_url(), |
| 154 fs_context->src_type()); | 150 fs_context->src_type()); |
| 155 | 151 |
| 156 int64 growth = 0; | 152 int64 growth = 0; |
| 157 base::PlatformFileInfo file_info; | 153 base::PlatformFileInfo file_info; |
| 158 if (!file_util::GetFileInfo(file_path, &file_info)) | 154 if (!file_util::GetFileInfo(file_path, &file_info)) |
| 159 file_info.size = 0; | 155 file_info.size = 0; |
| 160 growth = -file_info.size; | 156 growth = -file_info.size; |
| 161 | 157 |
| 162 base::PlatformFileError error = underlying_file_util_->DeleteFile( | 158 base::PlatformFileError error = underlying_file_util_->DeleteFile( |
| 163 fs_context, file_path); | 159 fs_context, file_path); |
| 164 | 160 |
| 165 if (error == base::PLATFORM_FILE_OK) | 161 if (error == base::PLATFORM_FILE_OK) |
| 166 helper.NotifyUpdate(growth); | 162 helper.NotifyUpdate(growth); |
| 167 | 163 |
| 168 return error; | 164 return error; |
| 169 } | 165 } |
| 170 | 166 |
| 171 base::PlatformFileError QuotaFileUtil::Truncate( | 167 base::PlatformFileError QuotaFileUtil::Truncate( |
| 172 FileSystemOperationContext* fs_context, | 168 FileSystemOperationContext* fs_context, |
| 173 const FilePath& path, | 169 const FilePath& path, |
| 174 int64 length) { | 170 int64 length) { |
| 175 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); | 171 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); |
| 176 ScopedOriginUpdateHelper helper( | 172 OriginUpdateHelper helper( |
| 177 fs_context, | 173 fs_context, |
| 178 fs_context->src_origin_url(), | 174 fs_context->src_origin_url(), |
| 179 fs_context->src_type()); | 175 fs_context->src_type()); |
| 180 | 176 |
| 181 int64 growth = 0; | 177 int64 growth = 0; |
| 182 base::PlatformFileInfo file_info; | 178 base::PlatformFileInfo file_info; |
| 183 if (!file_util::GetFileInfo(path, &file_info)) | 179 if (!file_util::GetFileInfo(path, &file_info)) |
| 184 return base::PLATFORM_FILE_ERROR_FAILED; | 180 return base::PLATFORM_FILE_ERROR_FAILED; |
| 185 | 181 |
| 186 growth = length - file_info.size; | 182 growth = length - file_info.size; |
| 187 if (allowed_bytes_growth != kNoLimit && | 183 if (allowed_bytes_growth != kNoLimit && |
| 188 growth > 0 && growth > allowed_bytes_growth) | 184 growth > 0 && growth > allowed_bytes_growth) |
| 189 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 185 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 190 | 186 |
| 191 base::PlatformFileError error = underlying_file_util_->Truncate( | 187 base::PlatformFileError error = underlying_file_util_->Truncate( |
| 192 fs_context, path, length); | 188 fs_context, path, length); |
| 193 | 189 |
| 194 if (error == base::PLATFORM_FILE_OK) | 190 if (error == base::PLATFORM_FILE_OK) |
| 195 helper.NotifyUpdate(growth); | 191 helper.NotifyUpdate(growth); |
| 196 | 192 |
| 197 return error; | 193 return error; |
| 198 } | 194 } |
| 199 | 195 |
| 200 } // namespace fileapi | 196 } // namespace fileapi |
| OLD | NEW |