license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/common/ipc_message.h" |
| 6 | |
| 7 | #include "base/logging.h" |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 8 | #include "build/build_config.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame^] | 10 | #if defined(OS_POSIX) |
| 11 | #include "chrome/common/descriptor_set_posix.h" |
| 12 | #endif |
| 13 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 24 | #if defined(OS_POSIX) |
| 25 | header()->num_fds = 0; |
| 26 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 35 | #if defined(OS_POSIX) |
| 36 | header()->num_fds = 0; |
| 37 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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(); |
[email protected] | b7273e01 | 2009-02-10 08:21:50 | [diff] [blame] | 47 | #if defined(OS_POSIX) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame^] | 48 | descriptor_set_ = other.descriptor_set_; |
[email protected] | b7273e01 | 2009-02-10 08:21:50 | [diff] [blame] | 49 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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; |
[email protected] | 6f0f242 | 2009-02-11 01:33:34 | [diff] [blame] | 62 | #if defined(OS_POSIX) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame^] | 63 | descriptor_set_ = other.descriptor_set_; |
[email protected] | 6f0f242 | 2009-02-11 01:33:34 | [diff] [blame] | 64 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame^] | 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(descriptor_set()->size()); |
| 94 | if (descriptor.auto_close) { |
| 95 | return descriptor_set()->AddAndAutoClose(descriptor.fd); |
| 96 | } else { |
| 97 | return 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 | DescriptorSet* descriptor_set = descriptor_set_.get(); |
| 108 | if (!descriptor_set) |
| 109 | return false; |
| 110 | |
| 111 | descriptor->fd = descriptor_set->GetDescriptorAt(descriptor_index); |
| 112 | descriptor->auto_close = false; |
| 113 | |
| 114 | return descriptor->fd >= 0; |
| 115 | } |
| 116 | |
| 117 | void Message::EnsureDescriptorSet() { |
| 118 | if (descriptor_set_.get() == NULL) |
| 119 | descriptor_set_ = new DescriptorSet; |
| 120 | } |
| 121 | |
| 122 | #endif |
| 123 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | } // namespace IPC |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 125 | |