| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/fileapi/sandbox_prioritized_origin_database.h" | 5 #include "storage/browser/fileapi/sandbox_prioritized_origin_database.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 12 #include "webkit/browser/fileapi/sandbox_isolated_origin_database.h" | 12 #include "storage/browser/fileapi/sandbox_isolated_origin_database.h" |
| 13 #include "webkit/browser/fileapi/sandbox_origin_database.h" | 13 #include "storage/browser/fileapi/sandbox_origin_database.h" |
| 14 | 14 |
| 15 namespace fileapi { | 15 namespace storage { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const base::FilePath::CharType kPrimaryDirectory[] = | 19 const base::FilePath::CharType kPrimaryDirectory[] = |
| 20 FILE_PATH_LITERAL("primary"); | 20 FILE_PATH_LITERAL("primary"); |
| 21 const base::FilePath::CharType kPrimaryOriginFile[] = | 21 const base::FilePath::CharType kPrimaryOriginFile[] = |
| 22 FILE_PATH_LITERAL("primary.origin"); | 22 FILE_PATH_LITERAL("primary.origin"); |
| 23 | 23 |
| 24 bool WritePrimaryOriginFile(const base::FilePath& path, | 24 bool WritePrimaryOriginFile(const base::FilePath& path, |
| 25 const std::string& origin) { | 25 const std::string& origin) { |
| 26 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); | 26 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
| 27 if (!file.IsValid()) | 27 if (!file.IsValid()) |
| 28 return false; | 28 return false; |
| 29 if (!file.created()) | 29 if (!file.created()) |
| 30 file.SetLength(0); | 30 file.SetLength(0); |
| 31 Pickle pickle; | 31 Pickle pickle; |
| 32 pickle.WriteString(origin); | 32 pickle.WriteString(origin); |
| 33 file.Write(0, static_cast<const char*>(pickle.data()), pickle.size()); | 33 file.Write(0, static_cast<const char*>(pickle.data()), pickle.size()); |
| 34 file.Flush(); | 34 file.Flush(); |
| 35 return true; | 35 return true; |
| 36 } | 36 } |
| 37 | 37 |
| 38 bool ReadPrimaryOriginFile(const base::FilePath& path, | 38 bool ReadPrimaryOriginFile(const base::FilePath& path, std::string* origin) { |
| 39 std::string* origin) { | |
| 40 std::string buffer; | 39 std::string buffer; |
| 41 if (!base::ReadFileToString(path, &buffer)) | 40 if (!base::ReadFileToString(path, &buffer)) |
| 42 return false; | 41 return false; |
| 43 Pickle pickle(buffer.data(), buffer.size()); | 42 Pickle pickle(buffer.data(), buffer.size()); |
| 44 PickleIterator iter(pickle); | 43 PickleIterator iter(pickle); |
| 45 return pickle.ReadString(&iter, origin) && !origin->empty(); | 44 return pickle.ReadString(&iter, origin) && !origin->empty(); |
| 46 } | 45 } |
| 47 | 46 |
| 48 } // namespace | 47 } // namespace |
| 49 | 48 |
| 50 SandboxPrioritizedOriginDatabase::SandboxPrioritizedOriginDatabase( | 49 SandboxPrioritizedOriginDatabase::SandboxPrioritizedOriginDatabase( |
| 51 const base::FilePath& file_system_directory, | 50 const base::FilePath& file_system_directory, |
| 52 leveldb::Env* env_override) | 51 leveldb::Env* env_override) |
| 53 : file_system_directory_(file_system_directory), | 52 : file_system_directory_(file_system_directory), |
| 54 env_override_(env_override), | 53 env_override_(env_override), |
| 55 primary_origin_file_( | 54 primary_origin_file_(file_system_directory_.Append(kPrimaryOriginFile)) { |
| 56 file_system_directory_.Append(kPrimaryOriginFile)) { | |
| 57 } | 55 } |
| 58 | 56 |
| 59 SandboxPrioritizedOriginDatabase::~SandboxPrioritizedOriginDatabase() { | 57 SandboxPrioritizedOriginDatabase::~SandboxPrioritizedOriginDatabase() { |
| 60 } | 58 } |
| 61 | 59 |
| 62 bool SandboxPrioritizedOriginDatabase::InitializePrimaryOrigin( | 60 bool SandboxPrioritizedOriginDatabase::InitializePrimaryOrigin( |
| 63 const std::string& origin) { | 61 const std::string& origin) { |
| 64 if (!primary_origin_database_) { | 62 if (!primary_origin_database_) { |
| 65 if (!MaybeLoadPrimaryOrigin() && ResetPrimaryOrigin(origin)) { | 63 if (!MaybeLoadPrimaryOrigin() && ResetPrimaryOrigin(origin)) { |
| 66 MaybeMigrateDatabase(origin); | 64 MaybeMigrateDatabase(origin); |
| 67 primary_origin_database_.reset( | 65 primary_origin_database_.reset(new SandboxIsolatedOriginDatabase( |
| 68 new SandboxIsolatedOriginDatabase( | 66 origin, file_system_directory_, base::FilePath(kPrimaryDirectory))); |
| 69 origin, | |
| 70 file_system_directory_, | |
| 71 base::FilePath(kPrimaryDirectory))); | |
| 72 return true; | 67 return true; |
| 73 } | 68 } |
| 74 } | 69 } |
| 75 | 70 |
| 76 if (primary_origin_database_) | 71 if (primary_origin_database_) |
| 77 return primary_origin_database_->HasOriginPath(origin); | 72 return primary_origin_database_->HasOriginPath(origin); |
| 78 | 73 |
| 79 return false; | 74 return false; |
| 80 } | 75 } |
| 81 | 76 |
| 82 std::string SandboxPrioritizedOriginDatabase::GetPrimaryOrigin() { | 77 std::string SandboxPrioritizedOriginDatabase::GetPrimaryOrigin() { |
| 83 MaybeLoadPrimaryOrigin(); | 78 MaybeLoadPrimaryOrigin(); |
| 84 if (primary_origin_database_) | 79 if (primary_origin_database_) |
| 85 return primary_origin_database_->origin(); | 80 return primary_origin_database_->origin(); |
| 86 return std::string(); | 81 return std::string(); |
| 87 } | 82 } |
| 88 | 83 |
| 89 bool SandboxPrioritizedOriginDatabase::HasOriginPath( | 84 bool SandboxPrioritizedOriginDatabase::HasOriginPath( |
| 90 const std::string& origin) { | 85 const std::string& origin) { |
| 91 MaybeInitializeDatabases(false); | 86 MaybeInitializeDatabases(false); |
| 92 if (primary_origin_database_ && | 87 if (primary_origin_database_ && |
| 93 primary_origin_database_->HasOriginPath(origin)) | 88 primary_origin_database_->HasOriginPath(origin)) |
| 94 return true; | 89 return true; |
| 95 if (origin_database_) | 90 if (origin_database_) |
| 96 return origin_database_->HasOriginPath(origin); | 91 return origin_database_->HasOriginPath(origin); |
| 97 return false; | 92 return false; |
| 98 } | 93 } |
| 99 | 94 |
| 100 bool SandboxPrioritizedOriginDatabase::GetPathForOrigin( | 95 bool SandboxPrioritizedOriginDatabase::GetPathForOrigin( |
| 101 const std::string& origin, base::FilePath* directory) { | 96 const std::string& origin, |
| 97 base::FilePath* directory) { |
| 102 MaybeInitializeDatabases(true); | 98 MaybeInitializeDatabases(true); |
| 103 if (primary_origin_database_ && | 99 if (primary_origin_database_ && |
| 104 primary_origin_database_->GetPathForOrigin(origin, directory)) | 100 primary_origin_database_->GetPathForOrigin(origin, directory)) |
| 105 return true; | 101 return true; |
| 106 DCHECK(origin_database_); | 102 DCHECK(origin_database_); |
| 107 return origin_database_->GetPathForOrigin(origin, directory); | 103 return origin_database_->GetPathForOrigin(origin, directory); |
| 108 } | 104 } |
| 109 | 105 |
| 110 bool SandboxPrioritizedOriginDatabase::RemovePathForOrigin( | 106 bool SandboxPrioritizedOriginDatabase::RemovePathForOrigin( |
| 111 const std::string& origin) { | 107 const std::string& origin) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 138 primary_origin_database_.reset(); | 134 primary_origin_database_.reset(); |
| 139 origin_database_.reset(); | 135 origin_database_.reset(); |
| 140 } | 136 } |
| 141 | 137 |
| 142 bool SandboxPrioritizedOriginDatabase::MaybeLoadPrimaryOrigin() { | 138 bool SandboxPrioritizedOriginDatabase::MaybeLoadPrimaryOrigin() { |
| 143 if (primary_origin_database_) | 139 if (primary_origin_database_) |
| 144 return true; | 140 return true; |
| 145 std::string saved_origin; | 141 std::string saved_origin; |
| 146 if (!ReadPrimaryOriginFile(primary_origin_file_, &saved_origin)) | 142 if (!ReadPrimaryOriginFile(primary_origin_file_, &saved_origin)) |
| 147 return false; | 143 return false; |
| 148 primary_origin_database_.reset( | 144 primary_origin_database_.reset(new SandboxIsolatedOriginDatabase( |
| 149 new SandboxIsolatedOriginDatabase( | 145 saved_origin, file_system_directory_, base::FilePath(kPrimaryDirectory))); |
| 150 saved_origin, | |
| 151 file_system_directory_, | |
| 152 base::FilePath(kPrimaryDirectory))); | |
| 153 return true; | 146 return true; |
| 154 } | 147 } |
| 155 | 148 |
| 156 bool SandboxPrioritizedOriginDatabase::ResetPrimaryOrigin( | 149 bool SandboxPrioritizedOriginDatabase::ResetPrimaryOrigin( |
| 157 const std::string& origin) { | 150 const std::string& origin) { |
| 158 DCHECK(!primary_origin_database_); | 151 DCHECK(!primary_origin_database_); |
| 159 if (!WritePrimaryOriginFile(primary_origin_file_, origin)) | 152 if (!WritePrimaryOriginFile(primary_origin_file_, origin)) |
| 160 return false; | 153 return false; |
| 161 // We reset the primary origin directory too. | 154 // We reset the primary origin directory too. |
| 162 // (This means the origin file corruption causes data loss | 155 // (This means the origin file corruption causes data loss |
| (...skipping 25 matching lines...) Expand all Loading... |
| 188 } | 181 } |
| 189 | 182 |
| 190 std::vector<OriginRecord> origins; | 183 std::vector<OriginRecord> origins; |
| 191 origin_database_->ListAllOrigins(&origins); | 184 origin_database_->ListAllOrigins(&origins); |
| 192 if (origins.empty()) { | 185 if (origins.empty()) { |
| 193 origin_database_->RemoveDatabase(); | 186 origin_database_->RemoveDatabase(); |
| 194 origin_database_.reset(); | 187 origin_database_.reset(); |
| 195 } | 188 } |
| 196 } | 189 } |
| 197 | 190 |
| 198 void SandboxPrioritizedOriginDatabase::MaybeInitializeDatabases( | 191 void SandboxPrioritizedOriginDatabase::MaybeInitializeDatabases(bool create) { |
| 199 bool create) { | |
| 200 MaybeLoadPrimaryOrigin(); | 192 MaybeLoadPrimaryOrigin(); |
| 201 MaybeInitializeNonPrimaryDatabase(create); | 193 MaybeInitializeNonPrimaryDatabase(create); |
| 202 } | 194 } |
| 203 | 195 |
| 204 void SandboxPrioritizedOriginDatabase::MaybeInitializeNonPrimaryDatabase( | 196 void SandboxPrioritizedOriginDatabase::MaybeInitializeNonPrimaryDatabase( |
| 205 bool create) { | 197 bool create) { |
| 206 if (origin_database_) | 198 if (origin_database_) |
| 207 return; | 199 return; |
| 208 | 200 |
| 209 origin_database_.reset(new SandboxOriginDatabase(file_system_directory_, | 201 origin_database_.reset( |
| 210 env_override_)); | 202 new SandboxOriginDatabase(file_system_directory_, env_override_)); |
| 211 if (!create && !base::DirectoryExists(origin_database_->GetDatabasePath())) { | 203 if (!create && !base::DirectoryExists(origin_database_->GetDatabasePath())) { |
| 212 origin_database_.reset(); | 204 origin_database_.reset(); |
| 213 return; | 205 return; |
| 214 } | 206 } |
| 215 } | 207 } |
| 216 | 208 |
| 217 SandboxOriginDatabase* | 209 SandboxOriginDatabase* |
| 218 SandboxPrioritizedOriginDatabase::GetSandboxOriginDatabase() { | 210 SandboxPrioritizedOriginDatabase::GetSandboxOriginDatabase() { |
| 219 MaybeInitializeNonPrimaryDatabase(true); | 211 MaybeInitializeNonPrimaryDatabase(true); |
| 220 return origin_database_.get(); | 212 return origin_database_.get(); |
| 221 } | 213 } |
| 222 | 214 |
| 223 } // namespace fileapi | 215 } // namespace storage |
| OLD | NEW |