| 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 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); | 20 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
| 21 | 21 |
| 22 if (!file_.IsValid()) { | 22 if (!file_.IsValid()) { |
| 23 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); | 23 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); |
| 24 return false; | 24 return false; |
| 25 } | 25 } |
| 26 | 26 |
| 27 if (!MapFileToMemory()) { | 27 if (!MapFileToMemory(base::File::Region(file_))) { |
| 28 CloseHandles(); | 28 CloseHandles(); |
| 29 return false; | 29 return false; |
| 30 } | 30 } |
| 31 | 31 |
| 32 return true; | 32 return true; |
| 33 } | 33 } |
| 34 | 34 |
| 35 bool MemoryMappedFile::Initialize(File file) { | 35 bool MemoryMappedFile::Initialize(File file) { |
| 36 const base::File::Region whole_file(file); |
| 37 return Initialize(file.Pass(), whole_file); |
| 38 } |
| 39 |
| 40 bool MemoryMappedFile::Initialize(File file, const base::File::Region& region) { |
| 36 if (IsValid()) | 41 if (IsValid()) |
| 37 return false; | 42 return false; |
| 38 | 43 |
| 39 file_ = file.Pass(); | 44 file_ = file.Pass(); |
| 40 | 45 |
| 41 if (!MapFileToMemory()) { | 46 if (!MapFileToMemory(region)) { |
| 42 CloseHandles(); | 47 CloseHandles(); |
| 43 return false; | 48 return false; |
| 44 } | 49 } |
| 45 | 50 |
| 46 return true; | 51 return true; |
| 47 } | 52 } |
| 48 | 53 |
| 49 bool MemoryMappedFile::IsValid() const { | 54 bool MemoryMappedFile::IsValid() const { |
| 50 return data_ != NULL; | 55 return data_ != NULL; |
| 51 } | 56 } |
| 52 | 57 |
| 53 } // namespace base | 58 } // namespace base |
| OLD | NEW |