| 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 27 matching lines...) Expand all Loading... |
| 38 int64 growth_local = src_file_info.size - dest_file_info.size; | 38 int64 growth_local = src_file_info.size - dest_file_info.size; |
| 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 void NotifyUpdate(FileSystemOperationContext* operation_context, |
| 49 class ScopedOriginUpdateHelper { | 49 const GURL& origin_url, |
| 50 public: | 50 FileSystemType type, |
| 51 explicit ScopedOriginUpdateHelper( | 51 int64 growth) { |
| 52 FileSystemOperationContext* operation_context, | 52 DCHECK(operation_context); |
| 53 const GURL& origin_url, | 53 DCHECK(operation_context->file_system_context()); |
| 54 FileSystemType type) | 54 DCHECK(type != kFileSystemTypeUnknown); |
| 55 : operation_context_(operation_context), | |
| 56 origin_url_(origin_url), | |
| 57 type_(type) { | |
| 58 DCHECK(operation_context_); | |
| 59 DCHECK(operation_context_->file_system_context()); | |
| 60 DCHECK(type != kFileSystemTypeUnknown); | |
| 61 quota_util_ = | |
| 62 operation_context_->file_system_context()->GetQuotaUtil(type_); | |
| 63 quota_manager_proxy_ = | |
| 64 operation_context_->file_system_context()->quota_manager_proxy(); | |
| 65 if (quota_util_) | |
| 66 quota_util_->StartUpdateOriginOnFileThread(origin_url_, type_); | |
| 67 } | |
| 68 | 55 |
| 69 ~ScopedOriginUpdateHelper() { | 56 FileSystemQuotaUtil* quota_util = |
| 70 if (quota_util_) | 57 operation_context->file_system_context()->GetQuotaUtil(type); |
| 71 quota_util_->EndUpdateOriginOnFileThread(origin_url_, type_); | 58 QuotaManagerProxy* quota_manager_proxy = |
| 72 } | 59 operation_context->file_system_context()->quota_manager_proxy(); |
| 73 | 60 |
| 74 void NotifyUpdate(int64 growth) { | 61 operation_context->set_allowed_bytes_growth( |
| 75 operation_context_->set_allowed_bytes_growth( | 62 operation_context->allowed_bytes_growth() - growth); |
| 76 operation_context_->allowed_bytes_growth() - growth); | 63 if (quota_util) |
| 77 if (quota_util_) | 64 quota_util->UpdateOriginUsageOnFileThread( |
| 78 quota_util_->UpdateOriginUsageOnFileThread( | 65 quota_manager_proxy, origin_url, type, growth); |
| 79 quota_manager_proxy_, origin_url_, type_, growth); | 66 } |
| 80 } | |
| 81 | |
| 82 private: | |
| 83 FileSystemOperationContext* operation_context_; | |
| 84 FileSystemQuotaUtil* quota_util_; | |
| 85 QuotaManagerProxy* quota_manager_proxy_; | |
| 86 const GURL& origin_url_; | |
| 87 FileSystemType type_; | |
| 88 DISALLOW_COPY_AND_ASSIGN(ScopedOriginUpdateHelper); | |
| 89 }; | |
| 90 | 67 |
| 91 } // namespace (anonymous) | 68 } // namespace (anonymous) |
| 92 | 69 |
| 93 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil* underlying_file_util) | 70 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil* underlying_file_util) |
| 94 : underlying_file_util_(underlying_file_util) { | 71 : underlying_file_util_(underlying_file_util) { |
| 95 } | 72 } |
| 96 | 73 |
| 97 QuotaFileUtil::~QuotaFileUtil() { | 74 QuotaFileUtil::~QuotaFileUtil() { |
| 98 } | 75 } |
| 99 | 76 |
| 100 // static | 77 // static |
| 101 QuotaFileUtil* QuotaFileUtil::CreateDefault() { | 78 QuotaFileUtil* QuotaFileUtil::CreateDefault() { |
| 102 return new QuotaFileUtil(new FileSystemFileUtil()); | 79 return new QuotaFileUtil(new FileSystemFileUtil()); |
| 103 } | 80 } |
| 104 | 81 |
| 105 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( | 82 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( |
| 106 FileSystemOperationContext* fs_context, | 83 FileSystemOperationContext* fs_context, |
| 107 const FilePath& src_file_path, | 84 const FilePath& src_file_path, |
| 108 const FilePath& dest_file_path, | 85 const FilePath& dest_file_path, |
| 109 bool copy) { | 86 bool copy) { |
| 110 DCHECK(fs_context); | 87 DCHECK(fs_context); |
| 111 | 88 |
| 112 // TODO(kinuko): For cross-filesystem move case we need 2 helpers, one for | |
| 113 // src and one for dest. | |
| 114 ScopedOriginUpdateHelper helper( | |
| 115 fs_context, | |
| 116 fs_context->dest_origin_url(), | |
| 117 fs_context->dest_type()); | |
| 118 | |
| 119 int64 growth = 0; | 89 int64 growth = 0; |
| 120 | 90 |
| 121 // It assumes copy/move operations are always in the same fs currently. | 91 // It assumes copy/move operations are always in the same fs currently. |
| 122 // TODO(dmikurube): Do quota check if moving between different fs. | 92 // TODO(dmikurube): Do quota check if moving between different fs. |
| 123 if (copy) { | 93 if (copy) { |
| 124 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); | 94 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); |
| 125 // The third argument (growth) is not used for now. | 95 // The third argument (growth) is not used for now. |
| 126 if (!CanCopy(src_file_path, dest_file_path, allowed_bytes_growth, &growth)) | 96 if (!CanCopy(src_file_path, dest_file_path, allowed_bytes_growth, &growth)) |
| 127 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 97 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 128 } else { | 98 } else { |
| 129 base::PlatformFileInfo dest_file_info; | 99 base::PlatformFileInfo dest_file_info; |
| 130 if (!file_util::GetFileInfo(dest_file_path, &dest_file_info)) | 100 if (!file_util::GetFileInfo(dest_file_path, &dest_file_info)) |
| 131 dest_file_info.size = 0; | 101 dest_file_info.size = 0; |
| 132 growth = -dest_file_info.size; | 102 growth = -dest_file_info.size; |
| 133 } | 103 } |
| 134 | 104 |
| 135 base::PlatformFileError error = underlying_file_util_->CopyOrMoveFile( | 105 base::PlatformFileError error = underlying_file_util_->CopyOrMoveFile( |
| 136 fs_context, src_file_path, dest_file_path, copy); | 106 fs_context, src_file_path, dest_file_path, copy); |
| 137 | 107 |
| 138 if (error == base::PLATFORM_FILE_OK) { | 108 if (error == base::PLATFORM_FILE_OK) { |
| 139 // TODO(kinuko): For cross-filesystem move case, call this with -growth | 109 // TODO(kinuko): For cross-filesystem move case, call this with -growth |
| 140 // for source and growth for dest. | 110 // for source and growth for dest. |
| 141 helper.NotifyUpdate(growth); | 111 NotifyUpdate(fs_context, |
| 112 fs_context->dest_origin_url(), |
| 113 fs_context->dest_type(), |
| 114 growth); |
| 142 } | 115 } |
| 143 | 116 |
| 144 return error; | 117 return error; |
| 145 } | 118 } |
| 146 | 119 |
| 147 base::PlatformFileError QuotaFileUtil::DeleteFile( | 120 base::PlatformFileError QuotaFileUtil::DeleteFile( |
| 148 FileSystemOperationContext* fs_context, | 121 FileSystemOperationContext* fs_context, |
| 149 const FilePath& file_path) { | 122 const FilePath& file_path) { |
| 150 DCHECK(fs_context); | 123 DCHECK(fs_context); |
| 151 ScopedOriginUpdateHelper helper( | |
| 152 fs_context, | |
| 153 fs_context->src_origin_url(), | |
| 154 fs_context->src_type()); | |
| 155 | 124 |
| 156 int64 growth = 0; | 125 int64 growth = 0; |
| 157 base::PlatformFileInfo file_info; | 126 base::PlatformFileInfo file_info; |
| 158 if (!file_util::GetFileInfo(file_path, &file_info)) | 127 if (!file_util::GetFileInfo(file_path, &file_info)) |
| 159 file_info.size = 0; | 128 file_info.size = 0; |
| 160 growth = -file_info.size; | 129 growth = -file_info.size; |
| 161 | 130 |
| 162 base::PlatformFileError error = underlying_file_util_->DeleteFile( | 131 base::PlatformFileError error = underlying_file_util_->DeleteFile( |
| 163 fs_context, file_path); | 132 fs_context, file_path); |
| 164 | 133 |
| 165 if (error == base::PLATFORM_FILE_OK) | 134 if (error == base::PLATFORM_FILE_OK) |
| 166 helper.NotifyUpdate(growth); | 135 NotifyUpdate(fs_context, |
| 136 fs_context->src_origin_url(), |
| 137 fs_context->src_type(), |
| 138 growth); |
| 167 | 139 |
| 168 return error; | 140 return error; |
| 169 } | 141 } |
| 170 | 142 |
| 171 base::PlatformFileError QuotaFileUtil::Truncate( | 143 base::PlatformFileError QuotaFileUtil::Truncate( |
| 172 FileSystemOperationContext* fs_context, | 144 FileSystemOperationContext* fs_context, |
| 173 const FilePath& path, | 145 const FilePath& path, |
| 174 int64 length) { | 146 int64 length) { |
| 175 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); | 147 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); |
| 176 ScopedOriginUpdateHelper helper( | |
| 177 fs_context, | |
| 178 fs_context->src_origin_url(), | |
| 179 fs_context->src_type()); | |
| 180 | 148 |
| 181 int64 growth = 0; | 149 int64 growth = 0; |
| 182 base::PlatformFileInfo file_info; | 150 base::PlatformFileInfo file_info; |
| 183 if (!file_util::GetFileInfo(path, &file_info)) | 151 if (!file_util::GetFileInfo(path, &file_info)) |
| 184 return base::PLATFORM_FILE_ERROR_FAILED; | 152 return base::PLATFORM_FILE_ERROR_FAILED; |
| 185 | 153 |
| 186 growth = length - file_info.size; | 154 growth = length - file_info.size; |
| 187 if (allowed_bytes_growth != kNoLimit && | 155 if (allowed_bytes_growth != kNoLimit && |
| 188 growth > 0 && growth > allowed_bytes_growth) | 156 growth > 0 && growth > allowed_bytes_growth) |
| 189 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 157 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 190 | 158 |
| 191 base::PlatformFileError error = underlying_file_util_->Truncate( | 159 base::PlatformFileError error = underlying_file_util_->Truncate( |
| 192 fs_context, path, length); | 160 fs_context, path, length); |
| 193 | 161 |
| 194 if (error == base::PLATFORM_FILE_OK) | 162 if (error == base::PLATFORM_FILE_OK) |
| 195 helper.NotifyUpdate(growth); | 163 NotifyUpdate(fs_context, |
| 164 fs_context->src_origin_url(), |
| 165 fs_context->src_type(), |
| 166 growth); |
| 196 | 167 |
| 197 return error; | 168 return error; |
| 198 } | 169 } |
| 199 | 170 |
| 200 } // namespace fileapi | 171 } // namespace fileapi |
| OLD | NEW |