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