1 // Copyright 2015 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_TESTS_PICKLED_TYPES_BLINK_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_PICKLED_TYPES_BLINK_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 12 #include "base/logging.h" 13 #include "base/macros.h" 14 #include "ipc/ipc_message_macros.h" 15 #include "ipc/ipc_param_traits.h" 16 17 namespace base { 18 class Pickle; 19 class PickleIterator; 20 class PickleSizer; 21 } 22 23 namespace mojo { 24 namespace test { 25 26 // Implementation of types with IPC::ParamTraits for consumers in Blink. 27 28 enum class PickledEnumBlink { VALUE_0, VALUE_1 }; 29 30 // To make things slightly more interesting, this variation of the type doesn't 31 // support negative values. It'll DCHECK if you try to construct it with any, 32 // and it will fail deserialization if negative values are decoded. 33 class PickledStructBlink { 34 public: 35 PickledStructBlink(); 36 PickledStructBlink(int foo, int bar); 37 PickledStructBlink(PickledStructBlink&& other) = default; 38 ~PickledStructBlink(); 39 40 PickledStructBlink& operator=(PickledStructBlink&& other) = default; 41 42 int foo() const { return foo_; } 43 void set_foo(int foo) { 44 DCHECK_GE(foo, 0); 45 foo_ = foo; 46 } 47 48 int bar() const { return bar_; } 49 void set_bar(int bar) { 50 DCHECK_GE(bar, 0); 51 bar_ = bar; 52 } 53 54 private: 55 int foo_ = 0; 56 int bar_ = 0; 57 58 DISALLOW_COPY_AND_ASSIGN(PickledStructBlink); 59 }; 60 61 } // namespace test 62 } // namespace mojo 63 64 namespace IPC { 65 66 template <> 67 struct ParamTraits<mojo::test::PickledStructBlink> { 68 using param_type = mojo::test::PickledStructBlink; 69 70 static void GetSize(base::PickleSizer* sizer, const param_type& p); 71 static void Write(base::Pickle* m, const param_type& p); 72 static bool Read(const base::Pickle* m, 73 base::PickleIterator* iter, 74 param_type* r); 75 static void Log(const param_type& p, std::string* l) {} 76 }; 77 78 } // namespace IPC 79 80 IPC_ENUM_TRAITS_MAX_VALUE(mojo::test::PickledEnumBlink, 81 mojo::test::PickledEnumBlink::VALUE_1) 82 83 #endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_PICKLED_TYPES_BLINK_H_ 84