| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/fileapi/path_obfuscator.h" |
| 6 |
| 7 #include <vector> |
| 8 #include "base/logging.h" |
| 9 |
| 10 namespace fileapi { |
| 11 |
| 12 PathObfuscator::PathObfuscator(const FilePath& root_path) : |
| 13 from_map_valid_(false), to_map_valid_(false), root_path_(root_path) { |
| 14 } |
| 15 |
| 16 FilePath PathObfuscator::ObfuscatedPathFromClearPath( |
| 17 const FilePath& virtual_path) { |
| 18 std::vector<StringType> components; |
| 19 virtual_path.GetComponents(&components); |
| 20 |
| 21 FilePath obfuscated_path; |
| 22 std::vector<StringType>::const_iterator iter; |
| 23 for (iter = components.begin(); iter != components.end(); ++iter) |
| 24 obfuscated_path = obfuscated_path.Append( |
| 25 ObfuscatedNameMap::ObfuscatedNameFromClearName(*iter)); |
| 26 return obfuscated_path; |
| 27 } |
| 28 |
| 29 PlatformFileError PathObfuscator::ClearPathFromObfuscatedPath( |
| 30 const FilePath& obfuscated_path, |
| 31 FilePath* clear_path) { |
| 32 std::vector<StringType> components; |
| 33 obfuscated_path.GetComponents(&components); |
| 34 |
| 35 FilePath dict_path = root_path_; |
| 36 FilePath path; |
| 37 std::vector<StringType>::const_iterator iter; |
| 38 ObfuscatedNameMap map; |
| 39 for (iter = components.begin(); iter != components.end(); ++iter) { |
| 40 StringType clear_name; |
| 41 map.Init(dict_path); |
| 42 PlatformFileError error = |
| 43 map.ClearNameFromObfuscatedName(*iter, &clear_name); |
| 44 if (error != base::PLATFORM_FILE_OK) |
| 45 return error; |
| 46 path = path.Append(clear_name); |
| 47 dict_path.Append(*iter); |
| 48 } |
| 49 *clear_path = path; |
| 50 return base::PLATFORM_FILE_OK; |
| 51 } |
| 52 |
| 53 PlatformFileError PathObfuscator::PrepareAdd( |
| 54 const FilePath& clear_path, |
| 55 const FilePath& obfuscated_path) { |
| 56 CHECK(!from_map_valid_); |
| 57 #ifndef NDEBUG |
| 58 FilePath test_path(ObfuscatedPathFromClearPath(clear_path)); |
| 59 CHECK(test_path == obfuscated_path); |
| 60 #endif |
| 61 FilePath dict_dir = root_path_.Append(obfuscated_path).DirName(); |
| 62 from_map_.Init(dict_dir); |
| 63 PlatformFileError error = |
| 64 from_map_.PrepareAdd(clear_path.BaseName().value(), |
| 65 obfuscated_path.BaseName().value()); |
| 66 if (error != base::PLATFORM_FILE_OK) |
| 67 return error; |
| 68 from_map_valid_ = true; |
| 69 return base::PLATFORM_FILE_OK; |
| 70 } |
| 71 |
| 72 PlatformFileError PathObfuscator::PrepareDelete( |
| 73 const FilePath& clear_path, |
| 74 const FilePath& obfuscated_path) { |
| 75 CHECK(!from_map_valid_); |
| 76 #ifndef NDEBUG |
| 77 FilePath test_path(ObfuscatedPathFromClearPath(clear_path)); |
| 78 CHECK(test_path == obfuscated_path); |
| 79 #endif |
| 80 FilePath dict_dir = root_path_.Append(obfuscated_path).DirName(); |
| 81 from_map_.Init(dict_dir); |
| 82 { |
| 83 PlatformFileError error = |
| 84 from_map_.PrepareDelete(clear_path.BaseName().value(), |
| 85 obfuscated_path.BaseName().value()); |
| 86 if (error != base::PLATFORM_FILE_OK) |
| 87 return error; |
| 88 } |
| 89 from_map_valid_ = true; |
| 90 return base::PLATFORM_FILE_OK; |
| 91 } |
| 92 |
| 93 PlatformFileError PathObfuscator::PrepareMove( |
| 94 const FilePath& old_clear_path, |
| 95 const FilePath& old_obfuscated_path, |
| 96 const FilePath& new_clear_path, |
| 97 const FilePath& new_obfuscated_path) { |
| 98 CHECK(!from_map_valid_); |
| 99 #ifndef NDEBUG |
| 100 FilePath test_path(ObfuscatedPathFromClearPath(old_clear_path)); |
| 101 CHECK(test_path == old_obfuscated_path); |
| 102 test_path = ObfuscatedPathFromClearPath(new_clear_path); |
| 103 CHECK(test_path == new_obfuscated_path); |
| 104 #endif |
| 105 FilePath dict_dir = root_path_.Append(old_obfuscated_path).DirName(); |
| 106 from_map_.Init(dict_dir); |
| 107 if (old_obfuscated_path.DirName() == new_obfuscated_path.DirName()) { |
| 108 PlatformFileError error = |
| 109 from_map_.PrepareMove( |
| 110 old_clear_path.BaseName().value(), |
| 111 old_obfuscated_path.BaseName().value(), |
| 112 new_clear_path.BaseName().value(), |
| 113 new_obfuscated_path.BaseName().value()); |
| 114 if (error != base::PLATFORM_FILE_OK) |
| 115 return error; |
| 116 from_map_valid_ = true; |
| 117 } else { |
| 118 dict_dir = root_path_.Append(new_obfuscated_path).DirName(); |
| 119 to_map_.Init(new_obfuscated_path); |
| 120 PlatformFileError error = |
| 121 from_map_.PrepareDelete(old_clear_path.BaseName().value(), |
| 122 old_obfuscated_path.BaseName().value()); |
| 123 if (error != base::PLATFORM_FILE_OK) |
| 124 return error; |
| 125 error = to_map_.PrepareAdd(new_clear_path.BaseName().value(), |
| 126 new_obfuscated_path.BaseName().value()); |
| 127 if (error != base::PLATFORM_FILE_OK) { |
| 128 from_map_.Rollback(); |
| 129 return error; |
| 130 } |
| 131 from_map_valid_ = true; |
| 132 to_map_valid_ = true; |
| 133 } |
| 134 return base::PLATFORM_FILE_OK; |
| 135 } |
| 136 |
| 137 PlatformFileError PathObfuscator::Commit() { |
| 138 CHECK(from_map_valid_); |
| 139 PlatformFileError error = from_map_.Commit(); |
| 140 if (error != base::PLATFORM_FILE_OK) |
| 141 return error; |
| 142 if (to_map_valid_) |
| 143 error = to_map_.Commit(); |
| 144 from_map_valid_ = false; |
| 145 to_map_valid_ = false; |
| 146 return error; |
| 147 } |
| 148 |
| 149 void PathObfuscator::Rollback() { |
| 150 if (from_map_valid_) { |
| 151 from_map_.Rollback(); |
| 152 from_map_valid_ = false; |
| 153 } |
| 154 if (to_map_valid_) { |
| 155 to_map_.Rollback(); |
| 156 to_map_valid_ = false; |
| 157 } |
| 158 } |
| 159 |
| 160 } // namespace fileapi |
| OLD | NEW |