morrita | d95714f | 2014-10-01 02:37:24 | [diff] [blame] | 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 | namespace content { |
| 8 | |
| 9 | // static |
| 10 | scoped_ptr<FileDescriptorInfo> FileDescriptorInfoImpl::Create() { |
| 11 | return scoped_ptr<FileDescriptorInfo>(new FileDescriptorInfoImpl()); |
| 12 | } |
| 13 | |
| 14 | FileDescriptorInfoImpl::FileDescriptorInfoImpl() { |
| 15 | } |
| 16 | |
| 17 | FileDescriptorInfoImpl::~FileDescriptorInfoImpl() { |
| 18 | } |
| 19 | |
| 20 | void FileDescriptorInfoImpl::Share(int id, base::PlatformFile fd) { |
| 21 | AddToMapping(id, fd); |
| 22 | } |
| 23 | |
| 24 | void FileDescriptorInfoImpl::Transfer(int id, base::ScopedFD fd) { |
| 25 | AddToMapping(id, fd.get()); |
| 26 | owned_descriptors_.push_back(new base::ScopedFD(fd.Pass())); |
| 27 | } |
| 28 | |
| 29 | base::PlatformFile FileDescriptorInfoImpl::GetFDAt(size_t i) const { |
| 30 | return mapping_[i].first; |
| 31 | } |
| 32 | |
| 33 | int FileDescriptorInfoImpl::GetIDAt(size_t i) const { |
| 34 | return mapping_[i].second; |
| 35 | } |
| 36 | |
| 37 | size_t FileDescriptorInfoImpl::GetMappingSize() const { |
| 38 | return mapping_.size(); |
| 39 | } |
| 40 | |
| 41 | bool FileDescriptorInfoImpl::HasID(int id) const { |
| 42 | for (unsigned i = 0; i < mapping_.size(); ++i) { |
| 43 | if (mapping_[i].second == id) |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
morrita | f7302eb7 | 2015-02-09 18:53:21 | [diff] [blame] | 50 | bool FileDescriptorInfoImpl::OwnsFD(base::PlatformFile file) const { |
| 51 | return owned_descriptors_.end() != |
| 52 | std::find_if( |
| 53 | owned_descriptors_.begin(), owned_descriptors_.end(), |
| 54 | [file](const base::ScopedFD* fd) { return fd->get() == file; }); |
| 55 | } |
| 56 | |
| 57 | base::ScopedFD FileDescriptorInfoImpl::ReleaseFD(base::PlatformFile file) { |
| 58 | DCHECK(OwnsFD(file)); |
| 59 | |
| 60 | base::ScopedFD fd; |
| 61 | auto found = std::find_if( |
| 62 | owned_descriptors_.begin(), owned_descriptors_.end(), |
| 63 | [file](const base::ScopedFD* fd) { return fd->get() == file; }); |
| 64 | |
| 65 | (*found)->swap(fd); |
| 66 | owned_descriptors_.erase(found); |
| 67 | |
| 68 | return fd.Pass(); |
| 69 | } |
| 70 | |
morrita | d95714f | 2014-10-01 02:37:24 | [diff] [blame] | 71 | void FileDescriptorInfoImpl::AddToMapping(int id, base::PlatformFile fd) { |
| 72 | DCHECK(!HasID(id)); |
| 73 | mapping_.push_back(std::make_pair(fd, id)); |
| 74 | } |
| 75 | |
| 76 | const base::FileHandleMappingVector& FileDescriptorInfoImpl::GetMapping() |
| 77 | const { |
| 78 | return mapping_; |
| 79 | } |
| 80 | |
| 81 | base::FileHandleMappingVector |
| 82 | FileDescriptorInfoImpl::GetMappingWithIDAdjustment(int delta) const { |
| 83 | base::FileHandleMappingVector result = mapping_; |
| 84 | // Adding delta to each ID. |
| 85 | for (unsigned i = 0; i < mapping_.size(); ++i) |
| 86 | result[i].second += delta; |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | } // namespace content |