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 "base/files/memory_mapped_file.h" | 5 #include "base/files/memory_mapped_file.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 | 11 |
12 MemoryMappedFile::~MemoryMappedFile() { | 12 MemoryMappedFile::~MemoryMappedFile() { |
13 CloseHandles(); | 13 CloseHandles(); |
14 } | 14 } |
15 | 15 |
16 bool MemoryMappedFile::Initialize(const FilePath& file_name) { | 16 bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
17 if (IsValid()) | 17 if (IsValid()) |
18 return false; | 18 return false; |
19 | 19 |
20 if (!MapFileToMemory(file_name)) { | 20 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
| 21 |
| 22 if (!file_.IsValid()) { |
| 23 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); |
| 24 return false; |
| 25 } |
| 26 |
| 27 if (!MapFileToMemory()) { |
21 CloseHandles(); | 28 CloseHandles(); |
22 return false; | 29 return false; |
23 } | 30 } |
24 | 31 |
25 return true; | 32 return true; |
26 } | 33 } |
27 | 34 |
28 bool MemoryMappedFile::Initialize(PlatformFile file) { | 35 bool MemoryMappedFile::Initialize(File file) { |
29 if (IsValid()) | 36 if (IsValid()) |
30 return false; | 37 return false; |
31 | 38 |
32 file_ = file; | 39 file_ = file.Pass(); |
33 | 40 |
34 if (!MapFileToMemoryInternal()) { | 41 if (!MapFileToMemory()) { |
35 CloseHandles(); | 42 CloseHandles(); |
36 return false; | 43 return false; |
37 } | 44 } |
38 | 45 |
39 return true; | 46 return true; |
40 } | 47 } |
41 | 48 |
42 bool MemoryMappedFile::IsValid() const { | 49 bool MemoryMappedFile::IsValid() const { |
43 return data_ != NULL; | 50 return data_ != NULL; |
44 } | 51 } |
45 | 52 |
46 bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { | |
47 file_ = CreatePlatformFile(file_name, PLATFORM_FILE_OPEN | PLATFORM_FILE_READ, | |
48 NULL, NULL); | |
49 | |
50 if (file_ == kInvalidPlatformFileValue) { | |
51 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); | |
52 return false; | |
53 } | |
54 | |
55 return MapFileToMemoryInternal(); | |
56 } | |
57 | |
58 } // namespace base | 53 } // namespace base |
OLD | NEW |