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

Side by Side Diff: ipc/ipc_message.cc

Issue 835873004: IPC: Generalize FileDescriptorSet to MessageAttachmentSet (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Landing Created 5 years, 11 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.h ('k') | ipc/ipc_message_attachment_set.h » ('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) 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 "ipc/ipc_message.h" 5 #include "ipc/ipc_message.h"
6 6
7 #include "base/atomic_sequence_num.h" 7 #include "base/atomic_sequence_num.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "ipc/ipc_message_attachment_set.h"
10 11
11 #if defined(OS_POSIX) 12 #if defined(OS_POSIX)
12 #include "base/file_descriptor_posix.h" 13 #include "base/file_descriptor_posix.h"
13 #include "ipc/file_descriptor_set_posix.h"
14 #endif 14 #endif
15 15
16 namespace { 16 namespace {
17 17
18 base::StaticAtomicSequenceNumber g_ref_num; 18 base::StaticAtomicSequenceNumber g_ref_num;
19 19
20 // Create a reference number for identifying IPC messages in traces. The return 20 // Create a reference number for identifying IPC messages in traces. The return
21 // values has the reference number stored in the upper 24 bits, leaving the low 21 // values has the reference number stored in the upper 24 bits, leaving the low
22 // 8 bits set to 0 for use as flags. 22 // 8 bits set to 0 for use as flags.
23 inline uint32 GetRefNumUpper24() { 23 inline uint32 GetRefNumUpper24() {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 Init(); 64 Init();
65 } 65 }
66 66
67 Message::Message(const char* data, int data_len) : Pickle(data, data_len) { 67 Message::Message(const char* data, int data_len) : Pickle(data, data_len) {
68 Init(); 68 Init();
69 } 69 }
70 70
71 Message::Message(const Message& other) : Pickle(other) { 71 Message::Message(const Message& other) : Pickle(other) {
72 Init(); 72 Init();
73 #if defined(OS_POSIX) 73 #if defined(OS_POSIX)
74 file_descriptor_set_ = other.file_descriptor_set_; 74 attachment_set_ = other.attachment_set_;
75 #endif 75 #endif
76 } 76 }
77 77
78 void Message::Init() { 78 void Message::Init() {
79 dispatch_error_ = false; 79 dispatch_error_ = false;
80 #ifdef IPC_MESSAGE_LOG_ENABLED 80 #ifdef IPC_MESSAGE_LOG_ENABLED
81 received_time_ = 0; 81 received_time_ = 0;
82 dont_log_ = false; 82 dont_log_ = false;
83 log_data_ = NULL; 83 log_data_ = NULL;
84 #endif 84 #endif
85 } 85 }
86 86
87 Message& Message::operator=(const Message& other) { 87 Message& Message::operator=(const Message& other) {
88 *static_cast<Pickle*>(this) = other; 88 *static_cast<Pickle*>(this) = other;
89 #if defined(OS_POSIX) 89 #if defined(OS_POSIX)
90 file_descriptor_set_ = other.file_descriptor_set_; 90 attachment_set_ = other.attachment_set_;
91 #endif 91 #endif
92 return *this; 92 return *this;
93 } 93 }
94 94
95 void Message::SetHeaderValues(int32 routing, uint32 type, uint32 flags) { 95 void Message::SetHeaderValues(int32 routing, uint32 type, uint32 flags) {
96 // This should only be called when the message is already empty. 96 // This should only be called when the message is already empty.
97 DCHECK(payload_size() == 0); 97 DCHECK(payload_size() == 0);
98 98
99 header()->routing = routing; 99 header()->routing = routing;
100 header()->type = type; 100 header()->type = type;
101 header()->flags = flags; 101 header()->flags = flags;
102 } 102 }
103 103
104 void Message::EnsureMessageAttachmentSet() {
105 if (attachment_set_.get() == NULL)
106 attachment_set_ = new MessageAttachmentSet;
107 }
108
104 #ifdef IPC_MESSAGE_LOG_ENABLED 109 #ifdef IPC_MESSAGE_LOG_ENABLED
105 void Message::set_sent_time(int64 time) { 110 void Message::set_sent_time(int64 time) {
106 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); 111 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
107 header()->flags |= HAS_SENT_TIME_BIT; 112 header()->flags |= HAS_SENT_TIME_BIT;
108 WriteInt64(time); 113 WriteInt64(time);
109 } 114 }
110 115
111 int64 Message::sent_time() const { 116 int64 Message::sent_time() const {
112 if ((header()->flags & HAS_SENT_TIME_BIT) == 0) 117 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
113 return 0; 118 return 0;
114 119
115 const char* data = end_of_payload(); 120 const char* data = end_of_payload();
116 data -= sizeof(int64); 121 data -= sizeof(int64);
117 return *(reinterpret_cast<const int64*>(data)); 122 return *(reinterpret_cast<const int64*>(data));
118 } 123 }
119 124
120 void Message::set_received_time(int64 time) const { 125 void Message::set_received_time(int64 time) const {
121 received_time_ = time; 126 received_time_ = time;
122 } 127 }
123 #endif 128 #endif
124 129
125 #if defined(OS_POSIX) 130 #if defined(OS_POSIX)
126 bool Message::WriteFile(base::ScopedFD descriptor) { 131 bool Message::WriteFile(base::ScopedFD descriptor) {
127 // We write the index of the descriptor so that we don't have to 132 // We write the index of the descriptor so that we don't have to
128 // keep the current descriptor as extra decoding state when deserialising. 133 // keep the current descriptor as extra decoding state when deserialising.
129 WriteInt(file_descriptor_set()->size()); 134 WriteInt(attachment_set()->size());
130 return file_descriptor_set()->AddToOwn(descriptor.Pass()); 135 return attachment_set()->AddToOwn(descriptor.Pass());
131 } 136 }
132 137
133 bool Message::WriteBorrowingFile(const base::PlatformFile& descriptor) { 138 bool Message::WriteBorrowingFile(const base::PlatformFile& descriptor) {
134 // We write the index of the descriptor so that we don't have to 139 // We write the index of the descriptor so that we don't have to
135 // keep the current descriptor as extra decoding state when deserialising. 140 // keep the current descriptor as extra decoding state when deserialising.
136 WriteInt(file_descriptor_set()->size()); 141 WriteInt(attachment_set()->size());
137 return file_descriptor_set()->AddToBorrow(descriptor); 142 return attachment_set()->AddToBorrow(descriptor);
138 } 143 }
139 144
140 bool Message::ReadFile(PickleIterator* iter, base::ScopedFD* descriptor) const { 145 bool Message::ReadFile(PickleIterator* iter, base::ScopedFD* descriptor) const {
141 int descriptor_index; 146 int descriptor_index;
142 if (!iter->ReadInt(&descriptor_index)) 147 if (!iter->ReadInt(&descriptor_index))
143 return false; 148 return false;
144 149
145 FileDescriptorSet* file_descriptor_set = file_descriptor_set_.get(); 150 MessageAttachmentSet* attachment_set = attachment_set_.get();
146 if (!file_descriptor_set) 151 if (!attachment_set)
147 return false; 152 return false;
148 153
149 base::PlatformFile file = 154 base::PlatformFile file = attachment_set->TakeDescriptorAt(descriptor_index);
150 file_descriptor_set->TakeDescriptorAt(descriptor_index);
151 if (file < 0) 155 if (file < 0)
152 return false; 156 return false;
153 157
154 descriptor->reset(file); 158 descriptor->reset(file);
155 return true; 159 return true;
156 } 160 }
157 161
158 bool Message::HasFileDescriptors() const { 162 bool Message::HasFileDescriptors() const {
159 return file_descriptor_set_.get() && !file_descriptor_set_->empty(); 163 return attachment_set_.get() && !attachment_set_->empty();
160 }
161
162 void Message::EnsureFileDescriptorSet() {
163 if (file_descriptor_set_.get() == NULL)
164 file_descriptor_set_ = new FileDescriptorSet;
165 } 164 }
166 165
167 #endif 166 #endif
168 167
169 } // namespace IPC 168 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/ipc_message.h ('k') | ipc/ipc_message_attachment_set.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698