Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "chrome/common/ipc_message.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/logging.h" |
michael@0 | 8 | #include "build/build_config.h" |
michael@0 | 9 | |
michael@0 | 10 | #if defined(OS_POSIX) |
michael@0 | 11 | #include "chrome/common/file_descriptor_set_posix.h" |
michael@0 | 12 | #endif |
michael@0 | 13 | #ifdef MOZ_TASK_TRACER |
michael@0 | 14 | #include "GeckoTaskTracer.h" |
michael@0 | 15 | #endif |
michael@0 | 16 | |
michael@0 | 17 | #ifdef MOZ_TASK_TRACER |
michael@0 | 18 | using namespace mozilla::tasktracer; |
michael@0 | 19 | #endif |
michael@0 | 20 | |
michael@0 | 21 | namespace IPC { |
michael@0 | 22 | |
michael@0 | 23 | //------------------------------------------------------------------------------ |
michael@0 | 24 | |
michael@0 | 25 | Message::~Message() { |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | Message::Message() |
michael@0 | 29 | : Pickle(sizeof(Header)) { |
michael@0 | 30 | header()->routing = header()->type = header()->flags = 0; |
michael@0 | 31 | #if defined(OS_POSIX) |
michael@0 | 32 | header()->num_fds = 0; |
michael@0 | 33 | #endif |
michael@0 | 34 | #ifdef MOZ_TASK_TRACER |
michael@0 | 35 | header()->source_event_id = 0; |
michael@0 | 36 | header()->parent_task_id = 0; |
michael@0 | 37 | header()->source_event_type = SourceEventType::UNKNOWN; |
michael@0 | 38 | #endif |
michael@0 | 39 | InitLoggingVariables(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | Message::Message(int32_t routing_id, msgid_t type, PriorityValue priority, |
michael@0 | 43 | MessageCompression compression, const char* const name) |
michael@0 | 44 | : Pickle(sizeof(Header)) { |
michael@0 | 45 | header()->routing = routing_id; |
michael@0 | 46 | header()->type = type; |
michael@0 | 47 | header()->flags = priority; |
michael@0 | 48 | if (compression == COMPRESSION_ENABLED) |
michael@0 | 49 | header()->flags |= COMPRESS_BIT; |
michael@0 | 50 | #if defined(OS_POSIX) |
michael@0 | 51 | header()->num_fds = 0; |
michael@0 | 52 | #endif |
michael@0 | 53 | header()->interrupt_remote_stack_depth_guess = static_cast<uint32_t>(-1); |
michael@0 | 54 | header()->interrupt_local_stack_depth = static_cast<uint32_t>(-1); |
michael@0 | 55 | header()->seqno = 0; |
michael@0 | 56 | #if defined(OS_MACOSX) |
michael@0 | 57 | header()->cookie = 0; |
michael@0 | 58 | #endif |
michael@0 | 59 | #ifdef MOZ_TASK_TRACER |
michael@0 | 60 | header()->source_event_id = 0; |
michael@0 | 61 | header()->parent_task_id = 0; |
michael@0 | 62 | header()->source_event_type = SourceEventType::UNKNOWN; |
michael@0 | 63 | #endif |
michael@0 | 64 | InitLoggingVariables(name); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | Message::Message(const char* data, int data_len) : Pickle(data, data_len) { |
michael@0 | 68 | InitLoggingVariables(); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | Message::Message(const Message& other) : Pickle(other) { |
michael@0 | 72 | InitLoggingVariables(other.name_); |
michael@0 | 73 | #if defined(OS_POSIX) |
michael@0 | 74 | file_descriptor_set_ = other.file_descriptor_set_; |
michael@0 | 75 | #endif |
michael@0 | 76 | #ifdef MOZ_TASK_TRACER |
michael@0 | 77 | header()->source_event_id = other.header()->source_event_id; |
michael@0 | 78 | header()->parent_task_id = other.header()->parent_task_id; |
michael@0 | 79 | header()->source_event_type = other.header()->source_event_type; |
michael@0 | 80 | #endif |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | void Message::InitLoggingVariables(const char* const name) { |
michael@0 | 84 | name_ = name; |
michael@0 | 85 | #ifdef IPC_MESSAGE_LOG_ENABLED |
michael@0 | 86 | received_time_ = 0; |
michael@0 | 87 | dont_log_ = false; |
michael@0 | 88 | log_data_ = NULL; |
michael@0 | 89 | #endif |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | Message& Message::operator=(const Message& other) { |
michael@0 | 93 | *static_cast<Pickle*>(this) = other; |
michael@0 | 94 | InitLoggingVariables(other.name_); |
michael@0 | 95 | #if defined(OS_POSIX) |
michael@0 | 96 | file_descriptor_set_ = other.file_descriptor_set_; |
michael@0 | 97 | #endif |
michael@0 | 98 | #ifdef MOZ_TASK_TRACER |
michael@0 | 99 | header()->source_event_id = other.header()->source_event_id; |
michael@0 | 100 | header()->parent_task_id = other.header()->parent_task_id; |
michael@0 | 101 | header()->source_event_type = other.header()->source_event_type; |
michael@0 | 102 | #endif |
michael@0 | 103 | return *this; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | #ifdef IPC_MESSAGE_LOG_ENABLED |
michael@0 | 107 | void Message::set_sent_time(int64_t time) { |
michael@0 | 108 | DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); |
michael@0 | 109 | header()->flags |= HAS_SENT_TIME_BIT; |
michael@0 | 110 | WriteInt64(time); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | int64_t Message::sent_time() const { |
michael@0 | 114 | if ((header()->flags & HAS_SENT_TIME_BIT) == 0) |
michael@0 | 115 | return 0; |
michael@0 | 116 | |
michael@0 | 117 | const char* data = end_of_payload(); |
michael@0 | 118 | data -= sizeof(int64_t); |
michael@0 | 119 | return *(reinterpret_cast<const int64_t*>(data)); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | void Message::set_received_time(int64_t time) const { |
michael@0 | 123 | received_time_ = time; |
michael@0 | 124 | } |
michael@0 | 125 | #endif |
michael@0 | 126 | |
michael@0 | 127 | #if defined(OS_POSIX) |
michael@0 | 128 | bool Message::WriteFileDescriptor(const base::FileDescriptor& descriptor) { |
michael@0 | 129 | // We write the index of the descriptor so that we don't have to |
michael@0 | 130 | // keep the current descriptor as extra decoding state when deserialising. |
michael@0 | 131 | WriteInt(file_descriptor_set()->size()); |
michael@0 | 132 | if (descriptor.auto_close) { |
michael@0 | 133 | return file_descriptor_set()->AddAndAutoClose(descriptor.fd); |
michael@0 | 134 | } else { |
michael@0 | 135 | return file_descriptor_set()->Add(descriptor.fd); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | bool Message::ReadFileDescriptor(void** iter, |
michael@0 | 140 | base::FileDescriptor* descriptor) const { |
michael@0 | 141 | int descriptor_index; |
michael@0 | 142 | if (!ReadInt(iter, &descriptor_index)) |
michael@0 | 143 | return false; |
michael@0 | 144 | |
michael@0 | 145 | FileDescriptorSet* file_descriptor_set = file_descriptor_set_.get(); |
michael@0 | 146 | if (!file_descriptor_set) |
michael@0 | 147 | return false; |
michael@0 | 148 | |
michael@0 | 149 | descriptor->fd = file_descriptor_set->GetDescriptorAt(descriptor_index); |
michael@0 | 150 | descriptor->auto_close = false; |
michael@0 | 151 | |
michael@0 | 152 | return descriptor->fd >= 0; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | void Message::EnsureFileDescriptorSet() { |
michael@0 | 156 | if (file_descriptor_set_.get() == NULL) |
michael@0 | 157 | file_descriptor_set_ = new FileDescriptorSet; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | #endif |
michael@0 | 161 | |
michael@0 | 162 | } // namespace IPC |