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_PUBLIC_CPP_BINDINGS_LIB_UNSERIALIZED_MESSAGE_CONTEXT_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_UNSERIALIZED_MESSAGE_CONTEXT_H_ 7 8 #include <stdint.h> 9 10 #include "base/component_export.h" 11 #include "base/macros.h" 12 #include "base/optional.h" 13 #include "mojo/public/c/system/types.h" 14 #include "mojo/public/cpp/bindings/lib/buffer.h" 15 #include "mojo/public/cpp/bindings/lib/message_internal.h" 16 #include "mojo/public/cpp/bindings/lib/serialization_context.h" 17 18 namespace mojo { 19 namespace internal { 20 COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE)21class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE) UnserializedMessageContext { 22 public: 23 struct Tag {}; 24 25 UnserializedMessageContext(const Tag* tag, 26 uint32_t message_name, 27 uint32_t message_flags); 28 virtual ~UnserializedMessageContext(); 29 30 template <typename MessageType> 31 MessageType* SafeCast() { 32 if (&MessageType::kMessageTag != tag_) 33 return nullptr; 34 return static_cast<MessageType*>(this); 35 } 36 37 const Tag* tag() const { return tag_; } 38 uint32_t message_name() const { return header_.name; } 39 uint32_t message_flags() const { return header_.flags; } 40 41 MessageHeaderV1* header() { return &header_; } 42 43 virtual void Serialize(SerializationContext* serialization_context, 44 Buffer* buffer) = 0; 45 46 private: 47 // The |tag_| is used for run-time type identification of specific 48 // unserialized message types, e.g. messages generated by mojom bindings. This 49 // allows opaque message objects to be safely downcast once pulled off a pipe. 50 const Tag* const tag_; 51 52 // We store message metadata in a serialized header structure to simplify 53 // Message implementation which needs to query such metadata for both 54 // serialized and unserialized message objects. 55 MessageHeaderV1 header_; 56 57 DISALLOW_COPY_AND_ASSIGN(UnserializedMessageContext); 58 }; 59 60 } // namespace internal 61 } // namespace mojo 62 63 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_UNSERIALIZED_MESSAGE_CONTEXT_H_ 64