1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17 #ifndef MOCK_NATIVE_INCLUDE_PARCEL_H_ 18 #define MOCK_NATIVE_INCLUDE_PARCEL_H_ 19 20 #include <string> 21 #include <vector> 22 #include "nocopyable.h" 23 #include "refbase.h" 24 25 namespace OHOS { 26 class Parcel; 27 28 class Parcelable : public virtual RefBase { 29 public: 30 virtual ~Parcelable() = default; 31 32 Parcelable(); 33 explicit Parcelable(bool asRemote); 34 35 // Write a parcelable object to the given parcel. 36 // The object position is saved into Parcel if set asRemote_ to 37 // true, and this intends to use in kernel data transaction. 38 // Returns true being written on success or false if any error occur. 39 virtual bool Marshalling(Parcel &parcel) const = 0; 40 41 // NOTICE! A static Unmarshalling function must also be implemented, so 42 // that you can get data from the given parcel into this parcelable object. 43 // See "static TestParcelable *Unmarshalling(Parcel &parcel)" as an example. 44 45 enum BehaviorFlag { IPC = 0x01, RPC = 0x02, HOLD_OBJECT = 0x10 }; 46 SetBehavior(BehaviorFlag b)47 inline void SetBehavior(BehaviorFlag b) const 48 { 49 behavior_ |= static_cast<uint8_t>(b); 50 } 51 ClearBehavior(BehaviorFlag b)52 inline void ClearBehavior(BehaviorFlag b) const 53 { 54 behavior_ &= ~(static_cast<uint8_t>(b)); 55 } 56 TestBehavior(BehaviorFlag b)57 inline bool TestBehavior(BehaviorFlag b) const 58 { 59 return behavior_ & (static_cast<uint8_t>(b)); 60 } 61 62 public: 63 bool asRemote_; 64 mutable uint8_t behavior_; 65 }; 66 67 class Allocator { 68 public: 69 virtual ~Allocator() = default; 70 71 virtual void *Realloc(void *data, size_t newSize) = 0; 72 73 virtual void *Alloc(size_t size) = 0; 74 75 virtual void Dealloc(void *data) = 0; 76 }; 77 78 class Parcel { 79 public: 80 Parcel(); 81 explicit Parcel(Allocator *allocator); 82 83 virtual ~Parcel(); 84 85 size_t GetWritableBytes() const; 86 87 size_t GetReadableBytes() const; 88 89 size_t GetDataCapacity() const; 90 91 bool SetDataCapacity(size_t newCapacity); 92 93 bool SetDataSize(size_t dataSize); 94 95 bool SetMaxCapacity(size_t maxCapacity); 96 97 bool WriteInt32(int32_t value); 98 99 bool WriteUint32(uint32_t value); 100 101 bool WriteUnpadBuffer(const void *data, size_t size); 102 103 bool WriteParcelable(const Parcelable *object); 104 105 bool WriteRemoteObject(const Parcelable *object); 106 107 bool ParseFrom(uintptr_t data, size_t size); 108 109 int32_t ReadInt32(); 110 111 uint32_t ReadUint32(); 112 113 bool ReadInt32(int32_t &value); 114 115 bool ReadUint32(uint32_t &value); 116 117 const uint8_t *ReadBuffer(size_t length); 118 119 const uint8_t *ReadUnpadBuffer(size_t length); 120 121 size_t GetReadPosition(); 122 123 bool CheckOffsets(); 124 125 bool SetAllocator(Allocator *allocator); 126 127 private: 128 DISALLOW_COPY_AND_MOVE(Parcel); 129 template <typename T> 130 bool Write(T value); 131 132 template <typename T> 133 bool Read(T &value); 134 135 template <typename T> 136 T Read(); 137 138 bool WriteDataBytes(const void *data, size_t size); 139 140 void WritePadBytes(size_t padded); 141 }; 142 } // namespace OHOS 143 #endif // MOCK_NATIVE_INCLUDE_PARCEL_H_ 144