Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: ipc/ipc_message_attachment_set.cc

Issue 866223005: IPC: Add MojoHandleAttachment (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing windows build again... Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ipc/ipc_message_attachment_set.h ('k') | ipc/mojo/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/ipc_message_attachment.h" 10 #include "ipc/ipc_message_attachment.h"
(...skipping 27 matching lines...) Expand all
38 << consumed_descriptor_highwater_ << "/" << size(); 38 << consumed_descriptor_highwater_ << "/" << size();
39 } 39 }
40 40
41 unsigned MessageAttachmentSet::num_descriptors() const { 41 unsigned MessageAttachmentSet::num_descriptors() const {
42 return std::count_if(attachments_.begin(), attachments_.end(), 42 return std::count_if(attachments_.begin(), attachments_.end(),
43 [](scoped_refptr<MessageAttachment> i) { 43 [](scoped_refptr<MessageAttachment> i) {
44 return i->GetType() == MessageAttachment::TYPE_PLATFORM_FILE; 44 return i->GetType() == MessageAttachment::TYPE_PLATFORM_FILE;
45 }); 45 });
46 } 46 }
47 47
48 unsigned MessageAttachmentSet::num_mojo_handles() const {
49 return std::count_if(attachments_.begin(), attachments_.end(),
50 [](scoped_refptr<MessageAttachment> i) {
51 return i->GetType() == MessageAttachment::TYPE_MOJO_HANDLE;
52 });
53 }
54
48 unsigned MessageAttachmentSet::size() const { 55 unsigned MessageAttachmentSet::size() const {
49 return static_cast<unsigned>(attachments_.size()); 56 return static_cast<unsigned>(attachments_.size());
50 } 57 }
51 58
52 bool MessageAttachmentSet::AddAttachment( 59 bool MessageAttachmentSet::AddAttachment(
53 scoped_refptr<MessageAttachment> attachment) { 60 scoped_refptr<MessageAttachment> attachment) {
54 #if defined(OS_POSIX) 61 #if defined(OS_POSIX)
55 if (attachment->GetType() != MessageAttachment::TYPE_PLATFORM_FILE || 62 if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE &&
56 num_descriptors() == kMaxDescriptorsPerMessage) { 63 num_descriptors() == kMaxDescriptorsPerMessage) {
57 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full."; 64 DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
58 return false; 65 return false;
59 } 66 }
60 #endif 67 #endif
61 68
62 attachments_.push_back(attachment); 69 attachments_.push_back(attachment);
63 return true; 70 return true;
64 } 71 }
65 72
(...skipping 29 matching lines...) Expand all
95 consumed_descriptor_highwater_ = 0; 102 consumed_descriptor_highwater_ = 0;
96 103
97 if (index != consumed_descriptor_highwater_) 104 if (index != consumed_descriptor_highwater_)
98 return scoped_refptr<MessageAttachment>(); 105 return scoped_refptr<MessageAttachment>();
99 106
100 consumed_descriptor_highwater_ = index + 1; 107 consumed_descriptor_highwater_ = index + 1;
101 108
102 return attachments_[index]; 109 return attachments_[index];
103 } 110 }
104 111
112 void MessageAttachmentSet::CommitAll() {
113 attachments_.clear();
114 consumed_descriptor_highwater_ = 0;
115 }
116
105 #if defined(OS_POSIX) 117 #if defined(OS_POSIX)
106 118
107 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const { 119 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const {
108 for (size_t i = 0; i != attachments_.size(); ++i) 120 for (size_t i = 0; i != attachments_.size(); ++i)
109 buffer[i] = internal::GetPlatformFile(attachments_[i]); 121 buffer[i] = internal::GetPlatformFile(attachments_[i]);
110 } 122 }
111 123
112 bool MessageAttachmentSet::ContainsDirectoryDescriptor() const { 124 bool MessageAttachmentSet::ContainsDirectoryDescriptor() const {
113 struct stat st; 125 struct stat st;
114 126
115 for (auto i = attachments_.begin(); i != attachments_.end(); ++i) { 127 for (auto i = attachments_.begin(); i != attachments_.end(); ++i) {
116 if (fstat(internal::GetPlatformFile(*i), &st) == 0 && S_ISDIR(st.st_mode)) 128 if (fstat(internal::GetPlatformFile(*i), &st) == 0 && S_ISDIR(st.st_mode))
117 return true; 129 return true;
118 } 130 }
119 131
120 return false; 132 return false;
121 } 133 }
122 134
123 void MessageAttachmentSet::CommitAll() {
124 attachments_.clear();
125 consumed_descriptor_highwater_ = 0;
126 }
127
128 void MessageAttachmentSet::ReleaseFDsToClose( 135 void MessageAttachmentSet::ReleaseFDsToClose(
129 std::vector<base::PlatformFile>* fds) { 136 std::vector<base::PlatformFile>* fds) {
130 for (size_t i = 0; i < attachments_.size(); ++i) { 137 for (size_t i = 0; i < attachments_.size(); ++i) {
131 internal::PlatformFileAttachment* file = 138 internal::PlatformFileAttachment* file =
132 static_cast<internal::PlatformFileAttachment*>(attachments_[i].get()); 139 static_cast<internal::PlatformFileAttachment*>(attachments_[i].get());
133 if (file->Owns()) 140 if (file->Owns())
134 fds->push_back(file->TakePlatformFile()); 141 fds->push_back(file->TakePlatformFile());
135 } 142 }
136 143
137 CommitAll(); 144 CommitAll();
138 } 145 }
139 146
140 void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer, 147 void MessageAttachmentSet::AddDescriptorsToOwn(const base::PlatformFile* buffer,
141 unsigned count) { 148 unsigned count) {
142 DCHECK(count <= kMaxDescriptorsPerMessage); 149 DCHECK(count <= kMaxDescriptorsPerMessage);
143 DCHECK_EQ(num_descriptors(), 0u); 150 DCHECK_EQ(num_descriptors(), 0u);
144 DCHECK_EQ(consumed_descriptor_highwater_, 0u); 151 DCHECK_EQ(consumed_descriptor_highwater_, 0u);
145 152
146 attachments_.reserve(count); 153 attachments_.reserve(count);
147 for (unsigned i = 0; i < count; ++i) 154 for (unsigned i = 0; i < count; ++i)
148 AddAttachment( 155 AddAttachment(
149 new internal::PlatformFileAttachment(base::ScopedFD(buffer[i]))); 156 new internal::PlatformFileAttachment(base::ScopedFD(buffer[i])));
150 } 157 }
151 158
152 #endif // OS_POSIX 159 #endif // OS_POSIX
153 160
154 } // namespace IPC 161 } // namespace IPC
155 162
156 163
OLDNEW
« no previous file with comments | « ipc/ipc_message_attachment_set.h ('k') | ipc/mojo/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698