blob: 9a15f1c653795ab02829fbb688e9cd6d834bed3b [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
5#include "chrome/common/ipc_message.h"
6
7#include "base/logging.h"
[email protected]526776c2009-02-07 00:39:268#include "build/build_config.h"
initial.commit09911bf2008-07-26 23:55:299
[email protected]7135bb042009-02-12 04:05:2810#if defined(OS_POSIX)
11#include "chrome/common/descriptor_set_posix.h"
12#endif
13
initial.commit09911bf2008-07-26 23:55:2914namespace IPC {
15
16//------------------------------------------------------------------------------
17
18Message::~Message() {
19}
20
21Message::Message()
22 : Pickle(sizeof(Header)) {
23 header()->routing = header()->type = header()->flags = 0;
[email protected]526776c2009-02-07 00:39:2624#if defined(OS_POSIX)
25 header()->num_fds = 0;
26#endif
initial.commit09911bf2008-07-26 23:55:2927 InitLoggingVariables();
28}
29
30Message::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]526776c2009-02-07 00:39:2635#if defined(OS_POSIX)
36 header()->num_fds = 0;
37#endif
initial.commit09911bf2008-07-26 23:55:2938 InitLoggingVariables();
39}
40
41Message::Message(const char* data, int data_len) : Pickle(data, data_len) {
42 InitLoggingVariables();
43}
44
45Message::Message(const Message& other) : Pickle(other) {
46 InitLoggingVariables();
[email protected]b7273e012009-02-10 08:21:5047#if defined(OS_POSIX)
[email protected]7135bb042009-02-12 04:05:2848 descriptor_set_ = other.descriptor_set_;
[email protected]b7273e012009-02-10 08:21:5049#endif
initial.commit09911bf2008-07-26 23:55:2950}
51
52void Message::InitLoggingVariables() {
53#ifdef IPC_MESSAGE_LOG_ENABLED
54 received_time_ = 0;
55 dont_log_ = false;
56 log_data_ = NULL;
57#endif
58}
59
60Message& Message::operator=(const Message& other) {
61 *static_cast<Pickle*>(this) = other;
[email protected]6f0f2422009-02-11 01:33:3462#if defined(OS_POSIX)
[email protected]7135bb042009-02-12 04:05:2863 descriptor_set_ = other.descriptor_set_;
[email protected]6f0f2422009-02-11 01:33:3464#endif
initial.commit09911bf2008-07-26 23:55:2965 return *this;
66}
67
68#ifdef IPC_MESSAGE_LOG_ENABLED
69void 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
75int64 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
84void Message::set_received_time(int64 time) const {
85 received_time_ = time;
86}
87#endif
88
[email protected]7135bb042009-02-12 04:05:2889#if defined(OS_POSIX)
90bool 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
101bool 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
117void Message::EnsureDescriptorSet() {
118 if (descriptor_set_.get() == NULL)
119 descriptor_set_ = new DescriptorSet;
120}
121
122#endif
123
initial.commit09911bf2008-07-26 23:55:29124} // namespace IPC
license.botbf09a502008-08-24 00:55:55125