1 /* 2 * Copyright (c) 2021-2022 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 #ifndef OHOS_ABILITY_BASE_WANT_PARAMS_H 16 #define OHOS_ABILITY_BASE_WANT_PARAMS_H 17 18 #include <iostream> 19 #include <map> 20 #include <set> 21 #include <unistd.h> 22 #include <vector> 23 #include "base_interfaces.h" 24 #include "refbase.h" 25 #include "parcel.h" 26 #include "foundation/communication/ipc/interfaces/innerkits/ipc_core/include/message_parcel.h" 27 28 namespace OHOS { 29 namespace AAFwk { 30 extern const char* FD; 31 extern const char* REMOTE_OBJECT; 32 extern const char* TYPE_PROPERTY; 33 extern const char* VALUE_PROPERTY; 34 class UnsupportedData { 35 public: 36 std::u16string key; 37 int type = 0; 38 int size = 0; 39 uint8_t *buffer = nullptr; 40 41 ~UnsupportedData(); 42 43 UnsupportedData(); 44 UnsupportedData(const UnsupportedData &other); 45 UnsupportedData(UnsupportedData &&other); 46 47 UnsupportedData &operator=(const UnsupportedData &other); 48 UnsupportedData &operator=(UnsupportedData &&other); 49 }; 50 51 class WantParams final : public Parcelable { 52 public: 53 WantParams() = default; 54 WantParams(const WantParams &wantParams); 55 WantParams(WantParams &&other) noexcept; ~WantParams()56 inline ~WantParams() 57 {} 58 WantParams &operator=(const WantParams &other); 59 WantParams &operator=(WantParams &&other) noexcept; 60 bool operator==(const WantParams &other); 61 62 static sptr<IInterface> GetInterfaceByType(int typeId, const std::string &value); 63 64 static bool CompareInterface(const sptr<IInterface> iIt1, const sptr<IInterface> iIt2, int typeId); 65 66 static int GetDataType(const sptr<IInterface> iIt); 67 68 static std::string GetStringByType(const sptr<IInterface> iIt, int typeId); 69 70 void SetParam(const std::string &key, IInterface *value); 71 72 sptr<IInterface> GetParam(const std::string &key) const; 73 74 WantParams GetWantParams(const std::string& key) const; 75 76 std::string GetStringParam(const std::string& key) const; 77 78 int GetIntParam(const std::string& key, const int defaultValue) const; 79 80 const std::map<std::string, sptr<IInterface>> &GetParams() const; 81 82 const std::set<std::string> KeySet() const; 83 84 void Remove(const std::string &key); 85 86 bool HasParam(const std::string &key) const; 87 88 int Size() const; 89 90 bool IsEmpty() const; 91 92 virtual bool Marshalling(Parcel &parcel) const; 93 94 static WantParams *Unmarshalling(Parcel &parcel, int depth = 1); 95 96 void DumpInfo(int level) const; 97 98 void CloseAllFd(); 99 100 private: 101 enum { 102 VALUE_TYPE_NULL = -1, 103 VALUE_TYPE_BOOLEAN = 1, 104 VALUE_TYPE_BYTE = 2, 105 VALUE_TYPE_CHAR = 3, 106 VALUE_TYPE_SHORT = 4, 107 VALUE_TYPE_INT = 5, 108 VALUE_TYPE_LONG = 6, 109 VALUE_TYPE_FLOAT = 7, 110 VALUE_TYPE_DOUBLE = 8, 111 VALUE_TYPE_STRING = 9, 112 VALUE_TYPE_CHARSEQUENCE = 10, 113 VALUE_TYPE_BOOLEANARRAY = 11, 114 VALUE_TYPE_BYTEARRAY = 12, 115 VALUE_TYPE_CHARARRAY = 13, 116 VALUE_TYPE_SHORTARRAY = 14, 117 VALUE_TYPE_INTARRAY = 15, 118 VALUE_TYPE_LONGARRAY = 16, 119 VALUE_TYPE_FLOATARRAY = 17, 120 VALUE_TYPE_DOUBLEARRAY = 18, 121 VALUE_TYPE_STRINGARRAY = 19, 122 VALUE_TYPE_CHARSEQUENCEARRAY = 20, 123 124 VALUE_TYPE_PARCELABLE = 21, 125 VALUE_TYPE_PARCELABLEARRAY = 22, 126 VALUE_TYPE_SERIALIZABLE = 23, 127 VALUE_TYPE_WANTPARAMSARRAY = 24, 128 VALUE_TYPE_LIST = 50, 129 130 VALUE_TYPE_WANTPARAMS = 101, 131 VALUE_TYPE_ARRAY = 102, 132 VALUE_TYPE_FD = 103, 133 VALUE_TYPE_REMOTE_OBJECT = 104 134 }; 135 136 bool WriteArrayToParcel(Parcel &parcel, IArray *ao, int depth) const; 137 bool ReadArrayToParcel(Parcel &parcel, int type, sptr<IArray> &ao, int depth); 138 bool ReadFromParcel(Parcel &parcel, int depth = 1); 139 bool ReadFromParcelParam(Parcel &parcel, const std::string &key, int type, int depth); 140 bool ReadFromParcelString(Parcel &parcel, const std::string &key); 141 bool ReadFromParcelBool(Parcel &parcel, const std::string &key); 142 bool ReadFromParcelInt8(Parcel &parcel, const std::string &key); 143 bool ReadFromParcelChar(Parcel &parcel, const std::string &key); 144 bool ReadFromParcelShort(Parcel &parcel, const std::string &key); 145 bool ReadFromParcelInt(Parcel &parcel, const std::string &key); 146 bool ReadFromParcelLong(Parcel &parcel, const std::string &key); 147 bool ReadFromParcelFloat(Parcel &parcel, const std::string &key); 148 bool ReadFromParcelDouble(Parcel &parcel, const std::string &key); 149 150 bool ReadFromParcelArrayString(Parcel &parcel, sptr<IArray> &ao); 151 bool ReadFromParcelArrayBool(Parcel &parcel, sptr<IArray> &ao); 152 bool ReadFromParcelArrayByte(Parcel &parcel, sptr<IArray> &ao); 153 bool ReadFromParcelArrayChar(Parcel &parcel, sptr<IArray> &ao); 154 bool ReadFromParcelArrayShort(Parcel &parcel, sptr<IArray> &ao); 155 156 bool ReadFromParcelArrayInt(Parcel &parcel, sptr<IArray> &ao); 157 bool ReadFromParcelArrayLong(Parcel &parcel, sptr<IArray> &ao); 158 bool ReadFromParcelArrayFloat(Parcel &parcel, sptr<IArray> &ao); 159 bool ReadFromParcelArrayDouble(Parcel &parcel, sptr<IArray> &ao); 160 bool ReadFromParcelArrayWantParams(Parcel &parcel, sptr<IArray> &ao, int depth); 161 bool ReadFromParcelWantParamWrapper(Parcel &parcel, const std::string &key, int type, int depth); 162 bool ReadFromParcelFD(Parcel &parcel, const std::string &key); 163 bool ReadFromParcelRemoteObject(Parcel &parcel, const std::string &key); 164 165 bool WriteArrayToParcelString(Parcel &parcel, IArray *ao) const; 166 bool WriteArrayToParcelBool(Parcel &parcel, IArray *ao) const; 167 bool WriteArrayToParcelByte(Parcel &parcel, IArray *ao) const; 168 bool WriteArrayToParcelChar(Parcel &parcel, IArray *ao) const; 169 bool WriteArrayToParcelShort(Parcel &parcel, IArray *ao) const; 170 bool WriteArrayToParcelInt(Parcel &parcel, IArray *ao) const; 171 bool WriteArrayToParcelLong(Parcel &parcel, IArray *ao) const; 172 bool WriteArrayToParcelFloat(Parcel &parcel, IArray *ao) const; 173 bool WriteArrayToParcelDouble(Parcel &parcel, IArray *ao) const; 174 bool WriteArrayToParcelWantParams(Parcel &parcel, IArray *ao, int depth) const; 175 176 bool WriteMarshalling(Parcel &parcel, sptr<IInterface> &o, int depth) const; 177 bool WriteToParcelString(Parcel &parcel, sptr<IInterface> &o) const; 178 bool WriteToParcelBool(Parcel &parcel, sptr<IInterface> &o) const; 179 bool WriteToParcelByte(Parcel &parcel, sptr<IInterface> &o) const; 180 bool WriteToParcelChar(Parcel &parcel, sptr<IInterface> &o) const; 181 bool WriteToParcelShort(Parcel &parcel, sptr<IInterface> &o) const; 182 bool WriteToParcelInt(Parcel &parcel, sptr<IInterface> &o) const; 183 bool WriteToParcelLong(Parcel &parcel, sptr<IInterface> &o) const; 184 bool WriteToParcelFloat(Parcel &parcel, sptr<IInterface> &o) const; 185 bool WriteToParcelDouble(Parcel &parcel, sptr<IInterface> &o) const; 186 bool WriteToParcelWantParams(Parcel &parcel, sptr<IInterface> &o, int depth) const; 187 bool WriteToParcelFD(Parcel &parcel, const WantParams &value) const; 188 bool WriteToParcelRemoteObject(Parcel &parcel, const WantParams &value) const; 189 190 bool DoMarshalling(Parcel &parcel, int depth = 1) const; 191 bool ReadUnsupportedData(Parcel &parcel, const std::string &key, int type); 192 193 friend class WantParamWrapper; 194 // inner use function 195 bool NewArrayData(IArray *source, sptr<IArray> &dest); 196 bool NewParams(const WantParams &source, WantParams &dest); 197 bool NewFds(const WantParams &source, WantParams &dest); 198 std::map<std::string, sptr<IInterface>> params_; 199 std::map<std::string, int> fds_; 200 std::vector<UnsupportedData> cachedUnsupportedData_; 201 }; 202 } // namespace AAFwk 203 } // namespace OHOS 204 205 #endif // OHOS_ABILITY_BASE_WANT_PARAMS_H 206