OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ipc/ipc_message_attachment_set.h" | 5 #include "ipc/ipc_message_attachment_set.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/posix/eintr_wrapper.h" | 9 #include "base/posix/eintr_wrapper.h" |
| 10 #include "ipc/brokerable_attachment.h" |
10 #include "ipc/ipc_message_attachment.h" | 11 #include "ipc/ipc_message_attachment.h" |
11 | 12 |
12 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
13 #include <sys/types.h> | 14 #include <sys/types.h> |
14 #include <sys/stat.h> | 15 #include <sys/stat.h> |
15 #include <unistd.h> | 16 #include <unistd.h> |
16 #include "ipc/ipc_platform_file_attachment_posix.h" | 17 #include "ipc/ipc_platform_file_attachment_posix.h" |
17 #endif // OS_POSIX | 18 #endif // OS_POSIX |
18 | 19 |
19 namespace IPC { | 20 namespace IPC { |
20 | 21 |
| 22 namespace { |
| 23 |
| 24 unsigned count_attachments_of_type( |
| 25 const std::vector<scoped_refptr<MessageAttachment>>& attachments, |
| 26 MessageAttachment::Type type) { |
| 27 unsigned count = 0; |
| 28 for (const scoped_refptr<MessageAttachment>& attachment : attachments) { |
| 29 if (attachment->GetType() == type) |
| 30 ++count; |
| 31 } |
| 32 return count; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
21 MessageAttachmentSet::MessageAttachmentSet() | 37 MessageAttachmentSet::MessageAttachmentSet() |
22 : consumed_descriptor_highwater_(0) { | 38 : consumed_descriptor_highwater_(0) { |
23 } | 39 } |
24 | 40 |
25 MessageAttachmentSet::~MessageAttachmentSet() { | 41 MessageAttachmentSet::~MessageAttachmentSet() { |
26 if (consumed_descriptor_highwater_ == size()) | 42 if (consumed_descriptor_highwater_ == size()) |
27 return; | 43 return; |
28 | 44 |
29 // We close all the owning descriptors. If this message should have | 45 // We close all the owning descriptors. If this message should have |
30 // been transmitted, then closing those with close flags set mirrors | 46 // been transmitted, then closing those with close flags set mirrors |
31 // the expected behaviour. | 47 // the expected behaviour. |
32 // | 48 // |
33 // If this message was received with more descriptors than expected | 49 // If this message was received with more descriptors than expected |
34 // (which could a DOS against the browser by a rogue renderer) then all | 50 // (which could a DOS against the browser by a rogue renderer) then all |
35 // the descriptors have their close flag set and we free all the extra | 51 // the descriptors have their close flag set and we free all the extra |
36 // kernel resources. | 52 // kernel resources. |
37 LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: " | 53 LOG(WARNING) << "MessageAttachmentSet destroyed with unconsumed descriptors: " |
38 << consumed_descriptor_highwater_ << "/" << size(); | 54 << consumed_descriptor_highwater_ << "/" << size(); |
39 } | 55 } |
40 | 56 |
41 unsigned MessageAttachmentSet::num_descriptors() const { | 57 unsigned MessageAttachmentSet::num_descriptors() const { |
42 return std::count_if(attachments_.begin(), attachments_.end(), | 58 return count_attachments_of_type(attachments_, |
43 [](scoped_refptr<MessageAttachment> i) { | 59 MessageAttachment::TYPE_PLATFORM_FILE); |
44 return i->GetType() == MessageAttachment::TYPE_PLATFORM_FILE; | |
45 }); | |
46 } | 60 } |
47 | 61 |
48 unsigned MessageAttachmentSet::num_mojo_handles() const { | 62 unsigned MessageAttachmentSet::num_mojo_handles() const { |
49 return std::count_if(attachments_.begin(), attachments_.end(), | 63 return count_attachments_of_type(attachments_, |
50 [](scoped_refptr<MessageAttachment> i) { | 64 MessageAttachment::TYPE_MOJO_HANDLE); |
51 return i->GetType() == MessageAttachment::TYPE_MOJO_HANDLE; | 65 } |
52 }); | 66 |
| 67 unsigned MessageAttachmentSet::num_brokerable_attachments() const { |
| 68 return count_attachments_of_type( |
| 69 attachments_, MessageAttachment::TYPE_BROKERABLE_ATTACHMENT); |
53 } | 70 } |
54 | 71 |
55 unsigned MessageAttachmentSet::size() const { | 72 unsigned MessageAttachmentSet::size() const { |
56 return static_cast<unsigned>(attachments_.size()); | 73 return static_cast<unsigned>(attachments_.size()); |
57 } | 74 } |
58 | 75 |
59 bool MessageAttachmentSet::AddAttachment( | 76 bool MessageAttachmentSet::AddAttachment( |
60 scoped_refptr<MessageAttachment> attachment) { | 77 scoped_refptr<MessageAttachment> attachment) { |
61 #if defined(OS_POSIX) | 78 #if defined(OS_POSIX) |
62 if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE && | 79 if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE && |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 consumed_descriptor_highwater_ = index + 1; | 124 consumed_descriptor_highwater_ = index + 1; |
108 | 125 |
109 return attachments_[index]; | 126 return attachments_[index]; |
110 } | 127 } |
111 | 128 |
112 void MessageAttachmentSet::CommitAll() { | 129 void MessageAttachmentSet::CommitAll() { |
113 attachments_.clear(); | 130 attachments_.clear(); |
114 consumed_descriptor_highwater_ = 0; | 131 consumed_descriptor_highwater_ = 0; |
115 } | 132 } |
116 | 133 |
| 134 std::vector<const BrokerableAttachment*> |
| 135 MessageAttachmentSet::PeekBrokerableAttachments() const { |
| 136 std::vector<const BrokerableAttachment*> output; |
| 137 for (const scoped_refptr<MessageAttachment>& attachment : attachments_) { |
| 138 if (attachment->GetType() == |
| 139 MessageAttachment::TYPE_BROKERABLE_ATTACHMENT) { |
| 140 output.push_back(static_cast<BrokerableAttachment*>(attachment.get())); |
| 141 } |
| 142 } |
| 143 return output; |
| 144 } |
| 145 |
117 #if defined(OS_POSIX) | 146 #if defined(OS_POSIX) |
118 | 147 |
119 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const { | 148 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const { |
120 for (size_t i = 0; i != attachments_.size(); ++i) | 149 for (size_t i = 0; i != attachments_.size(); ++i) |
121 buffer[i] = internal::GetPlatformFile(attachments_[i]); | 150 buffer[i] = internal::GetPlatformFile(attachments_[i]); |
122 } | 151 } |
123 | 152 |
124 bool MessageAttachmentSet::ContainsDirectoryDescriptor() const { | 153 bool MessageAttachmentSet::ContainsDirectoryDescriptor() const { |
125 struct stat st; | 154 struct stat st; |
126 | 155 |
(...skipping 27 matching lines...) Expand all Loading... |
154 for (unsigned i = 0; i < count; ++i) | 183 for (unsigned i = 0; i < count; ++i) |
155 AddAttachment( | 184 AddAttachment( |
156 new internal::PlatformFileAttachment(base::ScopedFD(buffer[i]))); | 185 new internal::PlatformFileAttachment(base::ScopedFD(buffer[i]))); |
157 } | 186 } |
158 | 187 |
159 #endif // OS_POSIX | 188 #endif // OS_POSIX |
160 | 189 |
161 } // namespace IPC | 190 } // namespace IPC |
162 | 191 |
163 | 192 |
OLD | NEW |