Chromium Code Reviews
|
| 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 obfuscated_file_util { | |
|
michaeln
2011/02/11 00:03:34
not sure we want a new namespace for this, can we
ericu
2011/02/11 01:57:47
Can do, but see the other comment about encapsulat
| |
| 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 { | |
|
michaeln
2011/02/11 00:03:34
extra scope?
ericu
2011/02/11 01:57:47
Will remove.
| |
| 64 PlatformFileError error = | |
| 65 from_map_.PrepareAdd(clear_path.BaseName().value(), | |
| 66 obfuscated_path.BaseName().value()); | |
| 67 if (error != base::PLATFORM_FILE_OK) | |
| 68 return error; | |
| 69 } | |
| 70 from_map_valid_ = true; | |
| 71 return base::PLATFORM_FILE_OK; | |
| 72 } | |
| 73 | |
| 74 PlatformFileError PathObfuscator::PrepareDelete( | |
| 75 const FilePath& clear_path, | |
| 76 const FilePath& obfuscated_path) { | |
| 77 CHECK(!from_map_valid_); | |
| 78 #ifndef NDEBUG | |
| 79 FilePath test_path(ObfuscatedPathFromClearPath(clear_path)); | |
| 80 CHECK(test_path == obfuscated_path); | |
| 81 #endif | |
| 82 FilePath dict_dir = root_path_.Append(obfuscated_path).DirName(); | |
| 83 from_map_.Init(dict_dir); | |
| 84 { | |
| 85 PlatformFileError error = | |
| 86 from_map_.PrepareDelete(clear_path.BaseName().value(), | |
| 87 obfuscated_path.BaseName().value()); | |
| 88 if (error != base::PLATFORM_FILE_OK) | |
| 89 return error; | |
| 90 } | |
| 91 from_map_valid_ = true; | |
| 92 return base::PLATFORM_FILE_OK; | |
| 93 } | |
| 94 | |
| 95 PlatformFileError PathObfuscator::PrepareMove( | |
| 96 const FilePath& old_clear_path, | |
| 97 const FilePath& old_obfuscated_path, | |
| 98 const FilePath& new_clear_path, | |
| 99 const FilePath& new_obfuscated_path) { | |
| 100 CHECK(!from_map_valid_); | |
| 101 #ifndef NDEBUG | |
| 102 FilePath test_path(ObfuscatedPathFromClearPath(old_clear_path)); | |
| 103 CHECK(test_path == old_obfuscated_path); | |
| 104 test_path = ObfuscatedPathFromClearPath(new_clear_path); | |
| 105 CHECK(test_path == new_obfuscated_path); | |
| 106 #endif | |
| 107 FilePath dict_dir = root_path_.Append(old_obfuscated_path).DirName(); | |
| 108 from_map_.Init(dict_dir); | |
| 109 if (old_obfuscated_path.DirName() == new_obfuscated_path.DirName()) { | |
| 110 PlatformFileError error = | |
| 111 from_map_.PrepareMove( | |
| 112 old_clear_path.BaseName().value(), | |
| 113 old_obfuscated_path.BaseName().value(), | |
| 114 new_clear_path.BaseName().value(), | |
| 115 new_obfuscated_path.BaseName().value()); | |
| 116 if (error != base::PLATFORM_FILE_OK) | |
| 117 return error; | |
| 118 from_map_valid_ = true; | |
| 119 } else { | |
| 120 dict_dir = root_path_.Append(new_obfuscated_path).DirName(); | |
| 121 to_map_.Init(new_obfuscated_path); | |
| 122 PlatformFileError error = | |
| 123 from_map_.PrepareDelete(old_clear_path.BaseName().value(), | |
| 124 old_obfuscated_path.BaseName().value()); | |
| 125 if (error != base::PLATFORM_FILE_OK) | |
| 126 return error; | |
| 127 error = to_map_.PrepareAdd(new_clear_path.BaseName().value(), | |
| 128 new_obfuscated_path.BaseName().value()); | |
| 129 if (error != base::PLATFORM_FILE_OK) { | |
| 130 from_map_.RollBack(); | |
| 131 return error; | |
| 132 } | |
| 133 from_map_valid_ = true; | |
| 134 to_map_valid_ = true; | |
| 135 } | |
| 136 return base::PLATFORM_FILE_OK; | |
| 137 } | |
| 138 | |
| 139 PlatformFileError PathObfuscator::Commit() { | |
| 140 CHECK(from_map_valid_); | |
| 141 PlatformFileError error = from_map_.Commit(); | |
| 142 if (error != base::PLATFORM_FILE_OK) | |
| 143 return error; | |
| 144 if (to_map_valid_) | |
| 145 error = to_map_.Commit(); | |
| 146 from_map_valid_ = false; | |
| 147 to_map_valid_ = false; | |
| 148 return error; | |
| 149 } | |
| 150 | |
| 151 void PathObfuscator::RollBack() { | |
| 152 if (from_map_valid_) | |
| 153 from_map_.RollBack(); | |
| 154 if (to_map_valid_) | |
| 155 to_map_.RollBack(); | |
|
michaeln
2011/02/11 00:03:34
do you want to reset the flags here?
ericu
2011/02/11 01:57:47
Yes--thanks.
| |
| 156 } | |
| 157 | |
| 158 } // namespace obfuscated_file_util | |
| OLD | NEW |