OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/obfuscated_file_util.h" | 5 #include "webkit/fileapi/obfuscated_file_util.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/metrics/histogram.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
17 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
18 #include "base/sys_string_conversions.h" | 19 #include "base/sys_string_conversions.h" |
19 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
20 #include "webkit/fileapi/file_system_context.h" | 21 #include "webkit/fileapi/file_system_context.h" |
21 #include "webkit/fileapi/file_system_operation_context.h" | 22 #include "webkit/fileapi/file_system_operation_context.h" |
22 #include "webkit/fileapi/file_system_path.h" | 23 #include "webkit/fileapi/file_system_path.h" |
23 #include "webkit/fileapi/file_system_quota_util.h" | 24 #include "webkit/fileapi/file_system_quota_util.h" |
24 #include "webkit/fileapi/file_system_util.h" | 25 #include "webkit/fileapi/file_system_util.h" |
(...skipping 802 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 if (!db->RemoveFileInfo(file_id)) | 828 if (!db->RemoveFileInfo(file_id)) |
828 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | 829 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
829 int64 growth = -UsageForPath(file_info.name.size()); | 830 int64 growth = -UsageForPath(file_info.name.size()); |
830 AllocateQuota(context, growth); | 831 AllocateQuota(context, growth); |
831 UpdateUsage(context, virtual_path.origin(), virtual_path.type(), growth); | 832 UpdateUsage(context, virtual_path.origin(), virtual_path.type(), growth); |
832 TouchDirectory(db, file_info.parent_id); | 833 TouchDirectory(db, file_info.parent_id); |
833 return base::PLATFORM_FILE_OK; | 834 return base::PLATFORM_FILE_OK; |
834 } | 835 } |
835 | 836 |
836 FilePath ObfuscatedFileUtil::GetDirectoryForOriginAndType( | 837 FilePath ObfuscatedFileUtil::GetDirectoryForOriginAndType( |
837 const GURL& origin, FileSystemType type, bool create) { | 838 const GURL& origin, |
838 FilePath origin_dir = GetDirectoryForOrigin(origin, create); | 839 FileSystemType type, |
| 840 bool create, |
| 841 base::PlatformFileError* error_code) { |
| 842 FilePath origin_dir = GetDirectoryForOrigin(origin, create, error_code); |
839 if (origin_dir.empty()) | 843 if (origin_dir.empty()) |
840 return FilePath(); | 844 return FilePath(); |
841 FilePath::StringType type_string = GetDirectoryNameForType(type); | 845 FilePath::StringType type_string = GetDirectoryNameForType(type); |
842 if (type_string.empty()) { | 846 if (type_string.empty()) { |
843 LOG(WARNING) << "Unknown filesystem type requested:" << type; | 847 LOG(WARNING) << "Unknown filesystem type requested:" << type; |
| 848 |
| 849 if (error_code) |
| 850 *error_code = base::PLATFORM_FILE_ERROR_INVALID_URL; |
844 return FilePath(); | 851 return FilePath(); |
845 } | 852 } |
846 FilePath path = origin_dir.Append(type_string); | 853 FilePath path = origin_dir.Append(type_string); |
847 if (!file_util::DirectoryExists(path) && | 854 if (!file_util::DirectoryExists(path) && |
848 (!create || !file_util::CreateDirectory(path))) | 855 (!create || !file_util::CreateDirectory(path))) { |
| 856 if (error_code) { |
| 857 *error_code = create ? |
| 858 base::PLATFORM_FILE_ERROR_FAILED : |
| 859 base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 860 } |
849 return FilePath(); | 861 return FilePath(); |
| 862 } |
| 863 |
| 864 if (error_code) |
| 865 *error_code = base::PLATFORM_FILE_OK; |
850 return path; | 866 return path; |
851 } | 867 } |
852 | 868 |
853 bool ObfuscatedFileUtil::DeleteDirectoryForOriginAndType( | 869 bool ObfuscatedFileUtil::DeleteDirectoryForOriginAndType( |
854 const GURL& origin, FileSystemType type) { | 870 const GURL& origin, FileSystemType type) { |
855 FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false); | 871 FilePath origin_type_path = GetDirectoryForOriginAndType(origin, type, false); |
856 if (!file_util::PathExists(origin_type_path)) | 872 if (!file_util::PathExists(origin_type_path)) |
857 return true; | 873 return true; |
858 | 874 |
859 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. | 875 // TODO(dmikurube): Consider the return value of DestroyDirectoryDatabase. |
860 // We ignore its error now since 1) it doesn't matter the final result, and | 876 // We ignore its error now since 1) it doesn't matter the final result, and |
861 // 2) it always returns false in Windows because of LevelDB's implementation. | 877 // 2) it always returns false in Windows because of LevelDB's implementation. |
862 // Information about failure would be useful for debugging. | 878 // Information about failure would be useful for debugging. |
863 DestroyDirectoryDatabase(origin, type); | 879 DestroyDirectoryDatabase(origin, type); |
864 if (!file_util::Delete(origin_type_path, true /* recursive */)) | 880 if (!file_util::Delete(origin_type_path, true /* recursive */)) |
865 return false; | 881 return false; |
866 | 882 |
867 FilePath origin_path = origin_type_path.DirName(); | 883 FilePath origin_path = origin_type_path.DirName(); |
868 DCHECK_EQ(origin_path.value(), GetDirectoryForOrigin(origin, false).value()); | 884 DCHECK_EQ(origin_path.value(), |
| 885 GetDirectoryForOrigin(origin, false, NULL).value()); |
869 | 886 |
870 // Delete the origin directory if the deleted one was the last remaining | 887 // Delete the origin directory if the deleted one was the last remaining |
871 // type for the origin. | 888 // type for the origin. |
872 if (file_util::Delete(origin_path, false /* recursive */)) { | 889 if (file_util::Delete(origin_path, false /* recursive */)) { |
873 InitOriginDatabase(false); | 890 InitOriginDatabase(false); |
874 if (origin_database_.get()) | 891 if (origin_database_.get()) |
875 origin_database_->RemovePathForOrigin(GetOriginIdentifierFromURL(origin)); | 892 origin_database_->RemovePathForOrigin(GetOriginIdentifierFromURL(origin)); |
876 } | 893 } |
877 | 894 |
878 // At this point we are sure we had successfully deleted the origin/type | 895 // At this point we are sure we had successfully deleted the origin/type |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1205 return NULL; | 1222 return NULL; |
1206 } | 1223 } |
1207 } | 1224 } |
1208 MarkUsed(); | 1225 MarkUsed(); |
1209 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); | 1226 FileSystemDirectoryDatabase* database = new FileSystemDirectoryDatabase(path); |
1210 directories_[key] = database; | 1227 directories_[key] = database; |
1211 return database; | 1228 return database; |
1212 } | 1229 } |
1213 | 1230 |
1214 FilePath ObfuscatedFileUtil::GetDirectoryForOrigin( | 1231 FilePath ObfuscatedFileUtil::GetDirectoryForOrigin( |
1215 const GURL& origin, bool create) { | 1232 const GURL& origin, bool create, base::PlatformFileError* error_code) { |
1216 if (!InitOriginDatabase(create)) | 1233 if (!InitOriginDatabase(create)) { |
| 1234 if (error_code) { |
| 1235 *error_code = create ? |
| 1236 base::PLATFORM_FILE_ERROR_FAILED : |
| 1237 base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 1238 } |
1217 return FilePath(); | 1239 return FilePath(); |
| 1240 } |
1218 FilePath directory_name; | 1241 FilePath directory_name; |
1219 std::string id = GetOriginIdentifierFromURL(origin); | 1242 std::string id = GetOriginIdentifierFromURL(origin); |
1220 | 1243 |
1221 bool exists_in_db = origin_database_->HasOriginPath(id); | 1244 bool exists_in_db = origin_database_->HasOriginPath(id); |
1222 if (!exists_in_db && !create) | 1245 if (!exists_in_db && !create) { |
| 1246 if (error_code) |
| 1247 *error_code = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
1223 return FilePath(); | 1248 return FilePath(); |
1224 if (!origin_database_->GetPathForOrigin(id, &directory_name)) | 1249 } |
| 1250 if (!origin_database_->GetPathForOrigin(id, &directory_name)) { |
| 1251 if (error_code) |
| 1252 *error_code = base::PLATFORM_FILE_ERROR_FAILED; |
1225 return FilePath(); | 1253 return FilePath(); |
| 1254 } |
1226 | 1255 |
1227 FilePath path = file_system_directory_.Append(directory_name); | 1256 FilePath path = file_system_directory_.Append(directory_name); |
1228 bool exists_in_fs = file_util::DirectoryExists(path); | 1257 bool exists_in_fs = file_util::DirectoryExists(path); |
1229 if (!exists_in_db && exists_in_fs) { | 1258 if (!exists_in_db && exists_in_fs) { |
1230 if (!file_util::Delete(path, true)) | 1259 if (!file_util::Delete(path, true)) { |
| 1260 if (error_code) |
| 1261 *error_code = base::PLATFORM_FILE_ERROR_FAILED; |
1231 return FilePath(); | 1262 return FilePath(); |
| 1263 } |
1232 exists_in_fs = false; | 1264 exists_in_fs = false; |
1233 } | 1265 } |
1234 | 1266 |
1235 if (!exists_in_fs) { | 1267 if (!exists_in_fs) { |
1236 if (!create || !file_util::CreateDirectory(path)) | 1268 if (!create || !file_util::CreateDirectory(path)) { |
| 1269 if (error_code) |
| 1270 *error_code = create ? |
| 1271 base::PLATFORM_FILE_ERROR_FAILED : |
| 1272 base::PLATFORM_FILE_ERROR_NOT_FOUND; |
1237 return FilePath(); | 1273 return FilePath(); |
| 1274 } |
1238 } | 1275 } |
1239 | 1276 |
| 1277 if (error_code) |
| 1278 *error_code = base::PLATFORM_FILE_OK; |
| 1279 |
1240 return path; | 1280 return path; |
1241 } | 1281 } |
1242 | 1282 |
1243 void ObfuscatedFileUtil::MarkUsed() { | 1283 void ObfuscatedFileUtil::MarkUsed() { |
1244 if (timer_.IsRunning()) | 1284 if (timer_.IsRunning()) |
1245 timer_.Reset(); | 1285 timer_.Reset(); |
1246 else | 1286 else |
1247 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kFlushDelaySeconds), | 1287 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kFlushDelaySeconds), |
1248 this, &ObfuscatedFileUtil::DropDatabases); | 1288 this, &ObfuscatedFileUtil::DropDatabases); |
1249 } | 1289 } |
(...skipping 14 matching lines...) Expand all Loading... |
1264 file_system_directory_.value(); | 1304 file_system_directory_.value(); |
1265 return false; | 1305 return false; |
1266 } | 1306 } |
1267 origin_database_.reset( | 1307 origin_database_.reset( |
1268 new FileSystemOriginDatabase(file_system_directory_)); | 1308 new FileSystemOriginDatabase(file_system_directory_)); |
1269 } | 1309 } |
1270 return true; | 1310 return true; |
1271 } | 1311 } |
1272 | 1312 |
1273 } // namespace fileapi | 1313 } // namespace fileapi |
OLD | NEW |