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