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 0 : Message::~Message() {
19 0 : }
20 :
21 1 : Message::Message()
22 1 : : Pickle(sizeof(Header)) {
23 1 : header()->routing = header()->type = header()->flags = 0;
24 : #if defined(OS_POSIX)
25 1 : header()->num_fds = 0;
26 : #endif
27 1 : InitLoggingVariables();
28 1 : }
29 :
30 1 : Message::Message(int32 routing_id, msgid_t type, PriorityValue priority,
31 : const char* const name)
32 1 : : Pickle(sizeof(Header)) {
33 1 : header()->routing = routing_id;
34 1 : header()->type = type;
35 1 : header()->flags = priority;
36 : #if defined(OS_POSIX)
37 1 : header()->num_fds = 0;
38 : #endif
39 1 : header()->rpc_remote_stack_depth_guess = static_cast<uint32>(-1);
40 1 : header()->rpc_local_stack_depth = static_cast<uint32>(-1);
41 1 : header()->seqno = 0;
42 1 : InitLoggingVariables(name);
43 1 : }
44 :
45 0 : Message::Message(const char* data, int data_len) : Pickle(data, data_len) {
46 0 : InitLoggingVariables();
47 0 : }
48 :
49 0 : Message::Message(const Message& other) : Pickle(other) {
50 0 : InitLoggingVariables(other.name_);
51 : #if defined(OS_POSIX)
52 0 : file_descriptor_set_ = other.file_descriptor_set_;
53 : #endif
54 0 : }
55 :
56 2 : void Message::InitLoggingVariables(const char* const name) {
57 2 : name_ = name;
58 : #ifdef IPC_MESSAGE_LOG_ENABLED
59 2 : received_time_ = 0;
60 2 : dont_log_ = false;
61 2 : log_data_ = NULL;
62 : #endif
63 2 : }
64 :
65 0 : Message& Message::operator=(const Message& other) {
66 0 : *static_cast<Pickle*>(this) = other;
67 0 : InitLoggingVariables(other.name_);
68 : #if defined(OS_POSIX)
69 0 : file_descriptor_set_ = other.file_descriptor_set_;
70 : #endif
71 0 : return *this;
72 : }
73 :
74 : #ifdef IPC_MESSAGE_LOG_ENABLED
75 0 : void Message::set_sent_time(int64 time) {
76 0 : DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
77 0 : header()->flags |= HAS_SENT_TIME_BIT;
78 0 : WriteInt64(time);
79 0 : }
80 :
81 0 : int64 Message::sent_time() const {
82 0 : if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
83 0 : return 0;
84 :
85 0 : const char* data = end_of_payload();
86 0 : data -= sizeof(int64);
87 0 : return *(reinterpret_cast<const int64*>(data));
88 : }
89 :
90 0 : void Message::set_received_time(int64 time) const {
91 0 : received_time_ = time;
92 0 : }
93 : #endif
94 :
95 : #if defined(OS_POSIX)
96 0 : bool Message::WriteFileDescriptor(const base::FileDescriptor& descriptor) {
97 : // We write the index of the descriptor so that we don't have to
98 : // keep the current descriptor as extra decoding state when deserialising.
99 0 : WriteInt(file_descriptor_set()->size());
100 0 : if (descriptor.auto_close) {
101 0 : return file_descriptor_set()->AddAndAutoClose(descriptor.fd);
102 : } else {
103 0 : return file_descriptor_set()->Add(descriptor.fd);
104 : }
105 : }
106 :
107 0 : bool Message::ReadFileDescriptor(void** iter,
108 : base::FileDescriptor* descriptor) const {
109 : int descriptor_index;
110 0 : if (!ReadInt(iter, &descriptor_index))
111 0 : return false;
112 :
113 0 : FileDescriptorSet* file_descriptor_set = file_descriptor_set_.get();
114 0 : if (!file_descriptor_set)
115 0 : return false;
116 :
117 0 : descriptor->fd = file_descriptor_set->GetDescriptorAt(descriptor_index);
118 0 : descriptor->auto_close = false;
119 :
120 0 : return descriptor->fd >= 0;
121 : }
122 :
123 0 : void Message::EnsureFileDescriptorSet() {
124 0 : if (file_descriptor_set_.get() == NULL)
125 0 : file_descriptor_set_ = new FileDescriptorSet;
126 0 : }
127 :
128 : #endif
129 :
130 : } // namespace IPC
|