[M94 Dev][Tizen] Fix for errors for generating ninja files
[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
14 namespace base {
15
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 {
20  public:
21   FileDescriptorStore(const FileDescriptorStore&) = delete;
22   FileDescriptorStore& operator=(const FileDescriptorStore&) = delete;
23   struct Descriptor {
24     Descriptor(const std::string& key, base::ScopedFD fd);
25     Descriptor(const std::string& key,
26                base::ScopedFD fd,
27                base::MemoryMappedFile::Region region);
28     Descriptor(Descriptor&& other);
29     ~Descriptor();
30
31     Descriptor& operator=(Descriptor&& other) = default;
32
33     // Globally unique key.
34     std::string key;
35     // Actual FD.
36     base::ScopedFD fd;
37     // Optional region, defaults to kWholeFile.
38     base::MemoryMappedFile::Region region;
39   };
40   using Mapping = std::map<std::string, Descriptor>;
41
42   // Returns the singleton instance of FileDescriptorStore.
43   static FileDescriptorStore& GetInstance();
44
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);
49
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);
53
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);
57
58   // Sets the descriptor and |region| for the given |key|.
59   void Set(const std::string& key,
60            base::ScopedFD fd,
61            base::MemoryMappedFile::Region region);
62
63  private:
64   FileDescriptorStore();
65   ~FileDescriptorStore();
66
67   Mapping descriptors_;
68 };
69
70 }  // namespace base
71
72 #endif  // BASE_FILE_DESCRIPTOR_STORE_H_