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"
16 // The file descriptor store is used to associate file descriptors with keys
17 // that must be unique.
18 // It is used to share file descriptors from a process to its child.
19 class BASE_EXPORT FileDescriptorStore {
21 FileDescriptorStore(const FileDescriptorStore&) = delete;
22 FileDescriptorStore& operator=(const FileDescriptorStore&) = delete;
24 Descriptor(const std::string& key, base::ScopedFD fd);
25 Descriptor(const std::string& key,
27 base::MemoryMappedFile::Region region);
28 Descriptor(Descriptor&& other);
31 Descriptor& operator=(Descriptor&& other) = default;
33 // Globally unique key.
37 // Optional region, defaults to kWholeFile.
38 base::MemoryMappedFile::Region region;
40 using Mapping = std::map<std::string, Descriptor>;
42 // Returns the singleton instance of FileDescriptorStore.
43 static FileDescriptorStore& GetInstance();
45 // Gets a descriptor given a key and also populates |region|.
46 // It is a fatal error if the key is not known.
47 base::ScopedFD TakeFD(const std::string& key,
48 base::MemoryMappedFile::Region* region);
50 // Gets a descriptor given a key. Returns an empty ScopedFD on error.
51 base::ScopedFD MaybeTakeFD(const std::string& key,
52 base::MemoryMappedFile::Region* region);
54 // Sets the descriptor for the given |key|. This sets the region associated
55 // with |key| to kWholeFile.
56 void Set(const std::string& key, base::ScopedFD fd);
58 // Sets the descriptor and |region| for the given |key|.
59 void Set(const std::string& key,
61 base::MemoryMappedFile::Region region);
64 FileDescriptorStore();
65 ~FileDescriptorStore();
72 #endif // BASE_FILE_DESCRIPTOR_STORE_H_