chromium/ui/gfx/x/xproto_types.h

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/354829279): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#ifndef UI_GFX_X_XPROTO_TYPES_H_
#define UI_GFX_X_XPROTO_TYPES_H_

#include <cstdint>
#include <memory>

#include "base/component_export.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/free_deleter.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"

namespace x11 {

class Error;

// A memory buffer where the size of the memory buffer is unknown because its
// given as `void*` from a C api which expects us to dynamically cast it to
// another type later. Use of this type is not sound as a mistake will cause
// Undefined Behaviour.
class COMPONENT_EXPORT(X11) UnsizedRefCountedMemory
    : public base::RefCountedThreadSafe<UnsizedRefCountedMemory> {};

// Convert from a sized memory buffer to an unsized one, in order to use the
// buffer in void* APIs that pass the size separately.
class COMPONENT_EXPORT(X11) ThrowAwaySizeRefCountedMemory final
    : public UnsizedRefCountedMemory {};

// Convert from an unsized memory buffer to a sized one, by specifying the size.
class COMPONENT_EXPORT(X11) SizedRefCountedMemory final
    : public base::RefCountedMemory {};

RawReply;
RawError;
ResponseCallback;

// xcb returns unsigned int when making requests.  This may be updated to
// uint16_t if/when we stop using xcb for socket IO.
SequenceType;

constexpr uint8_t kSendEventMask =;

// Constants from the X11 protocol documentation:
// https://www.x.org/releases/X11R7.5/doc/x11proto/proto.html
inline constexpr size_t kMinimumErrorSize =;
inline constexpr size_t kMinimumEventSize =;

namespace detail {

template <typename T>
void VerifyAlignment(T* t, size_t offset) {}

}  // namespace detail

// Wraps data read from the connection.
struct COMPONENT_EXPORT(X11) ReadBuffer {};

// Wraps data to write to the connection.
class COMPONENT_EXPORT(X11) WriteBuffer {};

namespace detail {

template <typename Reply>
std::unique_ptr<Reply> ReadReply(ReadBuffer* buffer);

}  // namespace detail

template <class Reply>
class Future;

template <typename T>
T Read(ReadBuffer* buf);

template <typename T>
WriteBuffer Write(const T& t);

template <typename T>
void ReadEvent(T* event, ReadBuffer* buf);

template <typename Reply>
struct Response {};

template <>
struct Response<void> {};

}  // namespace x11

#endif  // UI_GFX_X_XPROTO_TYPES_H_