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