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