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