|
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 #ifdef MOZ_TASK_TRACER |
|
14 #include "GeckoTaskTracer.h" |
|
15 #endif |
|
16 |
|
17 #ifdef MOZ_TASK_TRACER |
|
18 using namespace mozilla::tasktracer; |
|
19 #endif |
|
20 |
|
21 namespace IPC { |
|
22 |
|
23 //------------------------------------------------------------------------------ |
|
24 |
|
25 Message::~Message() { |
|
26 } |
|
27 |
|
28 Message::Message() |
|
29 : Pickle(sizeof(Header)) { |
|
30 header()->routing = header()->type = header()->flags = 0; |
|
31 #if defined(OS_POSIX) |
|
32 header()->num_fds = 0; |
|
33 #endif |
|
34 #ifdef MOZ_TASK_TRACER |
|
35 header()->source_event_id = 0; |
|
36 header()->parent_task_id = 0; |
|
37 header()->source_event_type = SourceEventType::UNKNOWN; |
|
38 #endif |
|
39 InitLoggingVariables(); |
|
40 } |
|
41 |
|
42 Message::Message(int32_t routing_id, msgid_t type, PriorityValue priority, |
|
43 MessageCompression compression, const char* const name) |
|
44 : Pickle(sizeof(Header)) { |
|
45 header()->routing = routing_id; |
|
46 header()->type = type; |
|
47 header()->flags = priority; |
|
48 if (compression == COMPRESSION_ENABLED) |
|
49 header()->flags |= COMPRESS_BIT; |
|
50 #if defined(OS_POSIX) |
|
51 header()->num_fds = 0; |
|
52 #endif |
|
53 header()->interrupt_remote_stack_depth_guess = static_cast<uint32_t>(-1); |
|
54 header()->interrupt_local_stack_depth = static_cast<uint32_t>(-1); |
|
55 header()->seqno = 0; |
|
56 #if defined(OS_MACOSX) |
|
57 header()->cookie = 0; |
|
58 #endif |
|
59 #ifdef MOZ_TASK_TRACER |
|
60 header()->source_event_id = 0; |
|
61 header()->parent_task_id = 0; |
|
62 header()->source_event_type = SourceEventType::UNKNOWN; |
|
63 #endif |
|
64 InitLoggingVariables(name); |
|
65 } |
|
66 |
|
67 Message::Message(const char* data, int data_len) : Pickle(data, data_len) { |
|
68 InitLoggingVariables(); |
|
69 } |
|
70 |
|
71 Message::Message(const Message& other) : Pickle(other) { |
|
72 InitLoggingVariables(other.name_); |
|
73 #if defined(OS_POSIX) |
|
74 file_descriptor_set_ = other.file_descriptor_set_; |
|
75 #endif |
|
76 #ifdef MOZ_TASK_TRACER |
|
77 header()->source_event_id = other.header()->source_event_id; |
|
78 header()->parent_task_id = other.header()->parent_task_id; |
|
79 header()->source_event_type = other.header()->source_event_type; |
|
80 #endif |
|
81 } |
|
82 |
|
83 void Message::InitLoggingVariables(const char* const name) { |
|
84 name_ = name; |
|
85 #ifdef IPC_MESSAGE_LOG_ENABLED |
|
86 received_time_ = 0; |
|
87 dont_log_ = false; |
|
88 log_data_ = NULL; |
|
89 #endif |
|
90 } |
|
91 |
|
92 Message& Message::operator=(const Message& other) { |
|
93 *static_cast<Pickle*>(this) = other; |
|
94 InitLoggingVariables(other.name_); |
|
95 #if defined(OS_POSIX) |
|
96 file_descriptor_set_ = other.file_descriptor_set_; |
|
97 #endif |
|
98 #ifdef MOZ_TASK_TRACER |
|
99 header()->source_event_id = other.header()->source_event_id; |
|
100 header()->parent_task_id = other.header()->parent_task_id; |
|
101 header()->source_event_type = other.header()->source_event_type; |
|
102 #endif |
|
103 return *this; |
|
104 } |
|
105 |
|
106 #ifdef IPC_MESSAGE_LOG_ENABLED |
|
107 void Message::set_sent_time(int64_t time) { |
|
108 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); |
|
109 header()->flags |= HAS_SENT_TIME_BIT; |
|
110 WriteInt64(time); |
|
111 } |
|
112 |
|
113 int64_t Message::sent_time() const { |
|
114 if ((header()->flags & HAS_SENT_TIME_BIT) == 0) |
|
115 return 0; |
|
116 |
|
117 const char* data = end_of_payload(); |
|
118 data -= sizeof(int64_t); |
|
119 return *(reinterpret_cast<const int64_t*>(data)); |
|
120 } |
|
121 |
|
122 void Message::set_received_time(int64_t time) const { |
|
123 received_time_ = time; |
|
124 } |
|
125 #endif |
|
126 |
|
127 #if defined(OS_POSIX) |
|
128 bool Message::WriteFileDescriptor(const base::FileDescriptor& descriptor) { |
|
129 // We write the index of the descriptor so that we don't have to |
|
130 // keep the current descriptor as extra decoding state when deserialising. |
|
131 WriteInt(file_descriptor_set()->size()); |
|
132 if (descriptor.auto_close) { |
|
133 return file_descriptor_set()->AddAndAutoClose(descriptor.fd); |
|
134 } else { |
|
135 return file_descriptor_set()->Add(descriptor.fd); |
|
136 } |
|
137 } |
|
138 |
|
139 bool Message::ReadFileDescriptor(void** iter, |
|
140 base::FileDescriptor* descriptor) const { |
|
141 int descriptor_index; |
|
142 if (!ReadInt(iter, &descriptor_index)) |
|
143 return false; |
|
144 |
|
145 FileDescriptorSet* file_descriptor_set = file_descriptor_set_.get(); |
|
146 if (!file_descriptor_set) |
|
147 return false; |
|
148 |
|
149 descriptor->fd = file_descriptor_set->GetDescriptorAt(descriptor_index); |
|
150 descriptor->auto_close = false; |
|
151 |
|
152 return descriptor->fd >= 0; |
|
153 } |
|
154 |
|
155 void Message::EnsureFileDescriptorSet() { |
|
156 if (file_descriptor_set_.get() == NULL) |
|
157 file_descriptor_set_ = new FileDescriptorSet; |
|
158 } |
|
159 |
|
160 #endif |
|
161 |
|
162 } // namespace IPC |