#ifndef IPC_IPC_MESSAGE_H_
#define IPC_IPC_MESSAGE_H_
#include <stddef.h>
#include <stdint.h>
#include <string>
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/pickle.h"
#include "build/build_config.h"
#include "ipc/ipc_buildflags.h"
#include "ipc/ipc_message_support_export.h"
namespace mojo {
namespace internal {
struct UnmappedNativeStructSerializerImpl;
}
}
namespace IPC {
namespace internal {
class ChannelReader;
}
struct LogData;
class MessageAttachmentSet;
class IPC_MESSAGE_SUPPORT_EXPORT Message : public base::Pickle {
public:
enum PriorityValue {
PRIORITY_LOW = 1,
PRIORITY_NORMAL,
PRIORITY_HIGH
};
enum {
PRIORITY_MASK = 0x03,
SYNC_BIT = 0x04,
REPLY_BIT = 0x08,
REPLY_ERROR_BIT = 0x10,
UNBLOCK_BIT = 0x20,
PUMPING_MSGS_BIT = 0x40,
HAS_SENT_TIME_BIT = 0x80,
};
~Message() override;
Message();
Message(int32_t routing_id, uint32_t type, PriorityValue priority);
Message(const char* data, size_t data_len);
Message(const Message& other);
Message& operator=(const Message& other);
bool IsValid() const { return header_size() == sizeof(Header) && header(); }
PriorityValue priority() const {
return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK);
}
void set_sync() {
header()->flags |= SYNC_BIT;
}
bool is_sync() const {
return (header()->flags & SYNC_BIT) != 0;
}
void set_reply() {
header()->flags |= REPLY_BIT;
}
bool is_reply() const {
return (header()->flags & REPLY_BIT) != 0;
}
void set_reply_error() {
header()->flags |= REPLY_ERROR_BIT;
}
bool is_reply_error() const {
return (header()->flags & REPLY_ERROR_BIT) != 0;
}
void set_unblock(bool unblock) {
if (unblock) {
header()->flags |= UNBLOCK_BIT;
} else {
header()->flags &= static_cast<uint32_t>(~UNBLOCK_BIT);
}
}
bool should_unblock() const {
return (header()->flags & UNBLOCK_BIT) != 0;
}
void set_dispatch_error() const {
dispatch_error_ = true;
}
bool dispatch_error() const {
return dispatch_error_;
}
uint32_t type() const {
return header()->type;
}
int32_t routing_id() const {
return header()->routing;
}
void set_routing_id(int32_t new_id) {
header()->routing = new_id;
}
uint32_t flags() const {
return header()->flags;
}
void SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags);
template<class T, class S, class P>
static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
void (T::*func)()) {
(obj->*func)();
return true;
}
template<class T, class S, class P>
static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
void (T::*func)(P*)) {
(obj->*func)(parameter);
return true;
}
static void Log(std::string* name, const Message* msg, std::string* l) {
}
struct IPC_MESSAGE_SUPPORT_EXPORT NextMessageInfo {
NextMessageInfo();
~NextMessageInfo();
size_t message_size;
bool message_found;
const char* pickle_end;
const char* message_end;
};
static void FindNext(const char* range_start,
const char* range_end,
NextMessageInfo* info);
bool WriteAttachment(
scoped_refptr<base::Pickle::Attachment> attachment) override;
bool ReadAttachment(
base::PickleIterator* iter,
scoped_refptr<base::Pickle::Attachment>* attachment) const override;
bool HasAttachments() const override;
#if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
void set_sent_time(int64_t time);
int64_t sent_time() const;
void set_received_time(int64_t time) const;
int64_t received_time() const { return received_time_; }
void set_output_params(const std::string& op) const { output_params_ = op; }
const std::string& output_params() const { return output_params_; }
void set_sync_log_data(LogData* data) const { log_data_ = data; }
LogData* sync_log_data() const { return log_data_; }
void set_dont_log() const { dont_log_ = true; }
bool dont_log() const { return dont_log_; }
#endif
protected:
friend class Channel;
friend class ChannelMojo;
friend class ChannelNacl;
friend class ChannelPosix;
friend class ChannelWin;
friend class internal::ChannelReader;
friend class MessageReplyDeserializer;
friend class SyncMessage;
friend struct mojo::internal::UnmappedNativeStructSerializerImpl;
#pragma pack(push, 4)
struct Header : base::Pickle::Header {
int32_t routing;
uint32_t type;
uint32_t flags;
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
uint16_t num_fds;
uint16_t pad;
#endif
};
#pragma pack(pop)
Header* header() {
return headerT<Header>();
}
const Header* header() const {
return headerT<Header>();
}
void Init();
mutable bool dispatch_error_;
scoped_refptr<MessageAttachmentSet> attachment_set_;
void EnsureMessageAttachmentSet();
MessageAttachmentSet* attachment_set() {
EnsureMessageAttachmentSet();
return attachment_set_.get();
}
const MessageAttachmentSet* attachment_set() const {
return attachment_set_.get();
}
#if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
mutable int64_t received_time_;
mutable std::string output_params_;
mutable raw_ptr<LogData> log_data_;
mutable bool dont_log_;
#endif
FRIEND_TEST_ALL_PREFIXES(IPCMessageTest, FindNext);
FRIEND_TEST_ALL_PREFIXES(IPCMessageTest, FindNextOverflow);
};
}
enum SpecialRoutingIDs {
MSG_ROUTING_NONE = -2,
MSG_ROUTING_CONTROL = INT32_MAX,
};
#define IPC_REPLY_ID 0xFFFFFFF0
#define IPC_LOGGING_ID 0xFFFFFFF1
#endif