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