| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/ipc_message.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "build/build_config.h" | |
| 9 | |
| 10 #if defined(OS_POSIX) | |
| 11 #include "chrome/common/file_descriptor_set_posix.h" | |
| 12 #endif | |
| 13 | |
| 14 namespace IPC { | |
| 15 | |
| 16 //------------------------------------------------------------------------------ | |
| 17 | |
| 18 Message::~Message() { | |
| 19 } | |
| 20 | |
| 21 Message::Message() | |
| 22 : Pickle(sizeof(Header)) { | |
| 23 header()->routing = header()->type = header()->flags = 0; | |
| 24 #if defined(OS_POSIX) | |
| 25 header()->num_fds = 0; | |
| 26 #endif | |
| 27 InitLoggingVariables(); | |
| 28 } | |
| 29 | |
| 30 Message::Message(int32 routing_id, uint16 type, PriorityValue priority) | |
| 31 : Pickle(sizeof(Header)) { | |
| 32 header()->routing = routing_id; | |
| 33 header()->type = type; | |
| 34 header()->flags = priority; | |
| 35 #if defined(OS_POSIX) | |
| 36 header()->num_fds = 0; | |
| 37 #endif | |
| 38 InitLoggingVariables(); | |
| 39 } | |
| 40 | |
| 41 Message::Message(const char* data, int data_len) : Pickle(data, data_len) { | |
| 42 InitLoggingVariables(); | |
| 43 } | |
| 44 | |
| 45 Message::Message(const Message& other) : Pickle(other) { | |
| 46 InitLoggingVariables(); | |
| 47 #if defined(OS_POSIX) | |
| 48 file_descriptor_set_ = other.file_descriptor_set_; | |
| 49 #endif | |
| 50 } | |
| 51 | |
| 52 void Message::InitLoggingVariables() { | |
| 53 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 54 received_time_ = 0; | |
| 55 dont_log_ = false; | |
| 56 log_data_ = NULL; | |
| 57 #endif | |
| 58 } | |
| 59 | |
| 60 Message& Message::operator=(const Message& other) { | |
| 61 *static_cast<Pickle*>(this) = other; | |
| 62 #if defined(OS_POSIX) | |
| 63 file_descriptor_set_ = other.file_descriptor_set_; | |
| 64 #endif | |
| 65 return *this; | |
| 66 } | |
| 67 | |
| 68 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 69 void Message::set_sent_time(int64 time) { | |
| 70 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); | |
| 71 header()->flags |= HAS_SENT_TIME_BIT; | |
| 72 WriteInt64(time); | |
| 73 } | |
| 74 | |
| 75 int64 Message::sent_time() const { | |
| 76 if ((header()->flags & HAS_SENT_TIME_BIT) == 0) | |
| 77 return 0; | |
| 78 | |
| 79 const char* data = end_of_payload(); | |
| 80 data -= sizeof(int64); | |
| 81 return *(reinterpret_cast<const int64*>(data)); | |
| 82 } | |
| 83 | |
| 84 void Message::set_received_time(int64 time) const { | |
| 85 received_time_ = time; | |
| 86 } | |
| 87 #endif | |
| 88 | |
| 89 #if defined(OS_POSIX) | |
| 90 bool Message::WriteFileDescriptor(const base::FileDescriptor& descriptor) { | |
| 91 // We write the index of the descriptor so that we don't have to | |
| 92 // keep the current descriptor as extra decoding state when deserialising. | |
| 93 WriteInt(file_descriptor_set()->size()); | |
| 94 if (descriptor.auto_close) { | |
| 95 return file_descriptor_set()->AddAndAutoClose(descriptor.fd); | |
| 96 } else { | |
| 97 return file_descriptor_set()->Add(descriptor.fd); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 bool Message::ReadFileDescriptor(void** iter, | |
| 102 base::FileDescriptor* descriptor) const { | |
| 103 int descriptor_index; | |
| 104 if (!ReadInt(iter, &descriptor_index)) | |
| 105 return false; | |
| 106 | |
| 107 FileDescriptorSet* file_descriptor_set = file_descriptor_set_.get(); | |
| 108 if (!file_descriptor_set) | |
| 109 return false; | |
| 110 | |
| 111 descriptor->fd = file_descriptor_set->GetDescriptorAt(descriptor_index); | |
| 112 descriptor->auto_close = true; | |
| 113 | |
| 114 return descriptor->fd >= 0; | |
| 115 } | |
| 116 | |
| 117 void Message::EnsureFileDescriptorSet() { | |
| 118 if (file_descriptor_set_.get() == NULL) | |
| 119 file_descriptor_set_ = new FileDescriptorSet; | |
| 120 } | |
| 121 | |
| 122 #endif | |
| 123 | |
| 124 } // namespace IPC | |
| OLD | NEW |