user_message.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2017 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. #ifndef MOJO_CORE_PORTS_USER_MESSAGE_H_
  5. #define MOJO_CORE_PORTS_USER_MESSAGE_H_
  6. #include <stddef.h>
  7. #include "base/component_export.h"
  8. #include "base/memory/raw_ptr_exclusion.h"
  9. namespace mojo {
  10. namespace core {
  11. namespace ports {
  12. // Base type to use for any embedder-defined user message implementation. This
  13. // class is intentionally empty.
  14. //
  15. // Provides a bit of type-safety help to subclasses since by design downcasting
  16. // from this type is a common operation in embedders.
  17. //
  18. // Each subclass should define a static const instance of TypeInfo named
  19. // |kUserMessageTypeInfo| and pass its address down to the UserMessage
  20. // constructor. The type of a UserMessage can then be dynamically inspected by
  21. // comparing |type_info()| to any subclass's |&kUserMessageTypeInfo|.
  22. class COMPONENT_EXPORT(MOJO_CORE_PORTS) UserMessage {
  23. public:
  24. struct TypeInfo {};
  25. explicit UserMessage(const TypeInfo* type_info);
  26. UserMessage(const UserMessage&) = delete;
  27. UserMessage& operator=(const UserMessage&) = delete;
  28. virtual ~UserMessage();
  29. const TypeInfo* type_info() const { return type_info_; }
  30. // Invoked immediately before the system asks the embedder to forward this
  31. // message to an external node.
  32. //
  33. // Returns |true| if the message is OK to route externally, or |false|
  34. // otherwise. Returning |false| implies an unrecoverable condition, and the
  35. // message event will be destroyed without further routing.
  36. virtual bool WillBeRoutedExternally();
  37. // Returns the size in bytes of this message iff it's serialized. Zero
  38. // otherwise.
  39. virtual size_t GetSizeIfSerialized() const;
  40. private:
  41. // `type_info_` is not a raw_ptr<...> for performance reasons (based on
  42. // analysis of sampling profiler data and tab_search:top100:2020).
  43. RAW_PTR_EXCLUSION const TypeInfo* const type_info_;
  44. };
  45. } // namespace ports
  46. } // namespace core
  47. } // namespace mojo
  48. #endif // MOJO_CORE_PORTS_USER_MESSAGE_H_