1 // Copyright 2017 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.
5 #ifndef BASE_FILE_DESCRIPTOR_STORE_H_
6 #define BASE_FILE_DESCRIPTOR_STORE_H_
11 #include "base/files/memory_mapped_file.h"
12 #include "base/files/scoped_file.h"
13 #include "base/macros.h"
17 // The file descriptor store is used to associate file descriptors with keys
18 // that must be unique.
19 // It is used to share file descriptors from a process to its child.
20 class BASE_EXPORT FileDescriptorStore {
23 Descriptor(const std::string& key, base::ScopedFD fd);
24 Descriptor(const std::string& key,
26 base::MemoryMappedFile::Region region);
27 Descriptor(Descriptor&& other);
30 Descriptor& operator=(Descriptor&& other) = default;
32 // Globally unique key.
36 // Optional region, defaults to kWholeFile.
37 base::MemoryMappedFile::Region region;
39 using Mapping = std::map<std::string, Descriptor>;
41 // Returns the singleton instance of FileDescriptorStore.
42 static FileDescriptorStore& GetInstance();
44 // Gets a descriptor given a key and also populates |region|.
45 // It is a fatal error if the key is not known.
46 base::ScopedFD TakeFD(const std::string& key,
47 base::MemoryMappedFile::Region* region);
49 // Gets a descriptor given a key. Returns an empty ScopedFD on error.
50 base::ScopedFD MaybeTakeFD(const std::string& key,
51 base::MemoryMappedFile::Region* region);
53 // Sets the descriptor for the given |key|. This sets the region associated
54 // with |key| to kWholeFile.
55 void Set(const std::string& key, base::ScopedFD fd);
57 // Sets the descriptor and |region| for the given |key|.
58 void Set(const std::string& key,
60 base::MemoryMappedFile::Region region);
63 FileDescriptorStore();
64 ~FileDescriptorStore();
68 DISALLOW_COPY_AND_ASSIGN(FileDescriptorStore);
73 #endif // BASE_FILE_DESCRIPTOR_STORE_H_