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 "webkit/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_path.h" | 9 #include "base/files/file_path.h" |
9 #include "base/files/scoped_platform_file_closer.h" | |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
12 #include "base/platform_file.h" | |
13 #include "webkit/browser/fileapi/sandbox_isolated_origin_database.h" | 12 #include "webkit/browser/fileapi/sandbox_isolated_origin_database.h" |
14 #include "webkit/browser/fileapi/sandbox_origin_database.h" | 13 #include "webkit/browser/fileapi/sandbox_origin_database.h" |
15 | 14 |
16 namespace fileapi { | 15 namespace fileapi { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 const base::FilePath::CharType kPrimaryDirectory[] = | 19 const base::FilePath::CharType kPrimaryDirectory[] = |
21 FILE_PATH_LITERAL("primary"); | 20 FILE_PATH_LITERAL("primary"); |
22 const base::FilePath::CharType kPrimaryOriginFile[] = | 21 const base::FilePath::CharType kPrimaryOriginFile[] = |
23 FILE_PATH_LITERAL("primary.origin"); | 22 FILE_PATH_LITERAL("primary.origin"); |
24 | 23 |
25 bool WritePrimaryOriginFile(const base::FilePath& path, | 24 bool WritePrimaryOriginFile(const base::FilePath& path, |
26 const std::string& origin) { | 25 const std::string& origin) { |
27 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | 26 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
28 bool created; | 27 if (!file.IsValid()) |
29 base::PlatformFile file = base::CreatePlatformFile( | |
30 path, | |
31 base::PLATFORM_FILE_OPEN_ALWAYS | | |
32 base::PLATFORM_FILE_WRITE, | |
33 &created, &error); | |
34 base::ScopedPlatformFileCloser closer(&file); | |
35 if (error != base::PLATFORM_FILE_OK || | |
36 file == base::kInvalidPlatformFileValue) | |
37 return false; | 28 return false; |
38 base::TruncatePlatformFile(file, 0); | 29 if (!file.created()) |
| 30 file.SetLength(0); |
39 Pickle pickle; | 31 Pickle pickle; |
40 pickle.WriteString(origin); | 32 pickle.WriteString(origin); |
41 base::WritePlatformFile(file, 0, static_cast<const char*>(pickle.data()), | 33 file.Write(0, static_cast<const char*>(pickle.data()), pickle.size()); |
42 pickle.size()); | 34 file.Flush(); |
43 base::FlushPlatformFile(file); | |
44 return true; | 35 return true; |
45 } | 36 } |
46 | 37 |
47 bool ReadPrimaryOriginFile(const base::FilePath& path, | 38 bool ReadPrimaryOriginFile(const base::FilePath& path, |
48 std::string* origin) { | 39 std::string* origin) { |
49 std::string buffer; | 40 std::string buffer; |
50 if (!base::ReadFileToString(path, &buffer)) | 41 if (!base::ReadFileToString(path, &buffer)) |
51 return false; | 42 return false; |
52 Pickle pickle(buffer.data(), buffer.size()); | 43 Pickle pickle(buffer.data(), buffer.size()); |
53 PickleIterator iter(pickle); | 44 PickleIterator iter(pickle); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 214 } |
224 } | 215 } |
225 | 216 |
226 SandboxOriginDatabase* | 217 SandboxOriginDatabase* |
227 SandboxPrioritizedOriginDatabase::GetSandboxOriginDatabase() { | 218 SandboxPrioritizedOriginDatabase::GetSandboxOriginDatabase() { |
228 MaybeInitializeNonPrimaryDatabase(true); | 219 MaybeInitializeNonPrimaryDatabase(true); |
229 return origin_database_.get(); | 220 return origin_database_.get(); |
230 } | 221 } |
231 | 222 |
232 } // namespace fileapi | 223 } // namespace fileapi |
OLD | NEW |