OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "content/browser/file_descriptor_info_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/stl_util.h" | |
11 | |
12 namespace content { | |
13 | |
14 // static | |
15 std::unique_ptr<FileDescriptorInfo> FileDescriptorInfoImpl::Create() { | |
16 return std::unique_ptr<FileDescriptorInfo>(new FileDescriptorInfoImpl()); | |
17 } | |
18 | |
19 FileDescriptorInfoImpl::FileDescriptorInfoImpl() { | |
20 } | |
21 | |
22 FileDescriptorInfoImpl::~FileDescriptorInfoImpl() { | |
23 } | |
24 | |
25 void FileDescriptorInfoImpl::Share(int id, base::PlatformFile fd) { | |
26 ShareWithRegion(id, fd, base::MemoryMappedFile::Region::kWholeFile); | |
27 } | |
28 | |
29 void FileDescriptorInfoImpl::ShareWithRegion(int id, base::PlatformFile fd, | |
30 const base::MemoryMappedFile::Region& region) { | |
31 AddToMapping(id, fd, region); | |
32 } | |
33 | |
34 void FileDescriptorInfoImpl::Transfer(int id, base::ScopedFD fd) { | |
35 AddToMapping(id, fd.get(), base::MemoryMappedFile::Region::kWholeFile); | |
36 owned_descriptors_.push_back(std::move(fd)); | |
37 } | |
38 | |
39 base::PlatformFile FileDescriptorInfoImpl::GetFDAt(size_t i) const { | |
40 return mapping_[i].first; | |
41 } | |
42 | |
43 int FileDescriptorInfoImpl::GetIDAt(size_t i) const { | |
44 return mapping_[i].second; | |
45 } | |
46 | |
47 const base::MemoryMappedFile::Region& FileDescriptorInfoImpl::GetRegionAt( | |
48 size_t i) const { | |
49 auto iter = ids_to_regions_.find(GetIDAt(i)); | |
50 return (iter != ids_to_regions_.end()) ? | |
51 iter->second : base::MemoryMappedFile::Region::kWholeFile; | |
52 } | |
53 | |
54 size_t FileDescriptorInfoImpl::GetMappingSize() const { | |
55 return mapping_.size(); | |
56 } | |
57 | |
58 bool FileDescriptorInfoImpl::HasID(int id) const { | |
59 for (unsigned i = 0; i < mapping_.size(); ++i) { | |
60 if (mapping_[i].second == id) | |
61 return true; | |
62 } | |
63 | |
64 return false; | |
65 } | |
66 | |
67 bool FileDescriptorInfoImpl::OwnsFD(base::PlatformFile file) const { | |
68 return base::ContainsValue(owned_descriptors_, file); | |
69 } | |
70 | |
71 base::ScopedFD FileDescriptorInfoImpl::ReleaseFD(base::PlatformFile file) { | |
72 DCHECK(OwnsFD(file)); | |
73 | |
74 base::ScopedFD fd; | |
75 auto found = | |
76 std::find(owned_descriptors_.begin(), owned_descriptors_.end(), file); | |
77 | |
78 std::swap(*found, fd); | |
79 owned_descriptors_.erase(found); | |
80 | |
81 return fd; | |
82 } | |
83 | |
84 void FileDescriptorInfoImpl::AddToMapping(int id, base::PlatformFile fd, | |
85 const base::MemoryMappedFile::Region& region) { | |
86 DCHECK(!HasID(id)); | |
87 mapping_.push_back(std::make_pair(fd, id)); | |
88 if (region != base::MemoryMappedFile::Region::kWholeFile) | |
89 ids_to_regions_[id] = region; | |
90 } | |
91 | |
92 const base::FileHandleMappingVector& FileDescriptorInfoImpl::GetMapping() | |
93 const { | |
94 return mapping_; | |
95 } | |
96 | |
97 std::unique_ptr<base::FileHandleMappingVector> | |
98 FileDescriptorInfoImpl::GetMappingWithIDAdjustment(int delta) const { | |
99 std::unique_ptr<base::FileHandleMappingVector> result = | |
100 base::MakeUnique<base::FileHandleMappingVector>(mapping_); | |
101 // Adding delta to each ID. | |
102 for (unsigned i = 0; i < mapping_.size(); ++i) | |
103 (*result)[i].second += delta; | |
104 return result; | |
105 } | |
106 | |
107 } // namespace content | |
OLD | NEW |