Home | History | Annotate | Download | only in files
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/files/memory_mapped_file.h"
      6 
      7 #include "base/files/file_path.h"
      8 #include "base/logging.h"
      9 
     10 namespace base {
     11 
     12 MemoryMappedFile::~MemoryMappedFile() {
     13   CloseHandles();
     14 }
     15 
     16 bool MemoryMappedFile::Initialize(const FilePath& file_name) {
     17   if (IsValid())
     18     return false;
     19 
     20   if (!MapFileToMemory(file_name)) {
     21     CloseHandles();
     22     return false;
     23   }
     24 
     25   return true;
     26 }
     27 
     28 bool MemoryMappedFile::Initialize(PlatformFile file) {
     29   if (IsValid())
     30     return false;
     31 
     32   file_ = file;
     33 
     34   if (!MapFileToMemoryInternal()) {
     35     CloseHandles();
     36     return false;
     37   }
     38 
     39   return true;
     40 }
     41 
     42 bool MemoryMappedFile::IsValid() const {
     43   return data_ != NULL;
     44 }
     45 
     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
     59