Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / base / file_descriptor_store.h
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.
4
5 #ifndef BASE_FILE_DESCRIPTOR_STORE_H_
6 #define BASE_FILE_DESCRIPTOR_STORE_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/files/memory_mapped_file.h"
12 #include "base/files/scoped_file.h"
13 #include "base/macros.h"
14
15 namespace base {
16
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 {
21  public:
22   struct Descriptor {
23     Descriptor(const std::string& key, base::ScopedFD fd);
24     Descriptor(const std::string& key,
25                base::ScopedFD fd,
26                base::MemoryMappedFile::Region region);
27     Descriptor(Descriptor&& other);
28     ~Descriptor();
29
30     Descriptor& operator=(Descriptor&& other) = default;
31
32     // Globally unique key.
33     std::string key;
34     // Actual FD.
35     base::ScopedFD fd;
36     // Optional region, defaults to kWholeFile.
37     base::MemoryMappedFile::Region region;
38   };
39   using Mapping = std::map<std::string, Descriptor>;
40
41   // Returns the singleton instance of FileDescriptorStore.
42   static FileDescriptorStore& GetInstance();
43
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);
48
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);
52
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);
56
57   // Sets the descriptor and |region| for the given |key|.
58   void Set(const std::string& key,
59            base::ScopedFD fd,
60            base::MemoryMappedFile::Region region);
61
62  private:
63   FileDescriptorStore();
64   ~FileDescriptorStore();
65
66   Mapping descriptors_;
67
68   DISALLOW_COPY_AND_ASSIGN(FileDescriptorStore);
69 };
70
71 }  // namespace base
72
73 #endif  // BASE_FILE_DESCRIPTOR_STORE_H_