1 /* 2 * Copyright (c) 2021-2025 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 <mutex> 21 #include <set> 22 #include <unistd.h> 23 #include <vector> 24 25 #include "base_interfaces.h" 26 #include "message_parcel.h" 27 #include "nlohmann/json.hpp" 28 #include "parcel.h" 29 #include "refbase.h" 30 31 namespace OHOS { 32 namespace AAFwk { 33 extern const char* FD; 34 extern const char* REMOTE_OBJECT; 35 extern const char* TYPE_PROPERTY; 36 extern const char* VALUE_PROPERTY; 37 38 enum ScreenMode : int8_t { 39 IDLE_SCREEN_MODE = -1, 40 JUMP_SCREEN_MODE = 0, 41 EMBEDDED_FULL_SCREEN_MODE = 1, 42 EMBEDDED_HALF_SCREEN_MODE = 2 43 }; 44 constexpr const char* SCREEN_MODE_KEY = "ohos.extra.param.key.showMode"; 45 46 class UnsupportedData { 47 public: 48 std::u16string key; 49 int type = 0; 50 int size = 0; 51 uint8_t *buffer = nullptr; 52 53 ~UnsupportedData(); 54 55 UnsupportedData(); 56 UnsupportedData(const UnsupportedData &other); 57 UnsupportedData(UnsupportedData &&other); 58 59 UnsupportedData &operator=(const UnsupportedData &other); 60 UnsupportedData &operator=(UnsupportedData &&other); 61 }; 62 63 class WantParams final : public Parcelable { 64 public: 65 WantParams() = default; 66 WantParams(const WantParams &wantParams); 67 WantParams(WantParams &&other) noexcept; ~WantParams()68 ~WantParams() 69 {} 70 WantParams &operator=(const WantParams &other); 71 WantParams &operator=(WantParams &&other) noexcept; 72 bool operator==(const WantParams &other); 73 74 static sptr<IInterface> GetInterfaceByType(int typeId, const std::string &value); 75 76 static bool CompareInterface(const sptr<IInterface> iIt1, const sptr<IInterface> iIt2, int typeId); 77 78 static int GetDataType(const sptr<IInterface> iIt); 79 80 static std::string GetStringByType(const sptr<IInterface> iIt, int typeId); 81 82 void SetParam(const std::string &key, IInterface *value); 83 84 sptr<IInterface> GetParam(const std::string &key) const; 85 86 WantParams GetWantParams(const std::string& key) const; 87 88 std::string GetStringParam(const std::string& key) const; 89 90 int GetIntParam(const std::string& key, const int defaultValue) const; 91 92 const std::map<std::string, sptr<IInterface>> &GetParams() const; 93 94 const std::set<std::string> KeySet() const; 95 96 void Remove(const std::string &key); 97 98 bool HasParam(const std::string &key) const; 99 100 int Size() const; 101 102 bool IsEmpty() const; 103 104 virtual bool Marshalling(Parcel &parcel) const; 105 106 static WantParams *Unmarshalling(Parcel &parcel, int depth = 1); 107 108 void DumpInfo(int level) const; 109 110 void CloseAllFd(); 111 112 void RemoveAllFd(); 113 114 void DupAllFd(); 115 116 void GetCachedUnsupportedData(std::vector<UnsupportedData> &cachedUnsuppertedData) const; 117 118 void SetCachedUnsupportedData(const std::vector<UnsupportedData> &cachedUnsuppertedData); 119 120 void FromJson(const nlohmann::json &jsonObject); 121 void ToJson(nlohmann::json &jsonObject) const; 122 123 private: 124 enum { 125 VALUE_TYPE_NULL = -1, 126 VALUE_TYPE_BOOLEAN = 1, 127 VALUE_TYPE_BYTE = 2, 128 VALUE_TYPE_CHAR = 3, 129 VALUE_TYPE_SHORT = 4, 130 VALUE_TYPE_INT = 5, 131 VALUE_TYPE_LONG = 6, 132 VALUE_TYPE_FLOAT = 7, 133 VALUE_TYPE_DOUBLE = 8, 134 VALUE_TYPE_STRING = 9, 135 VALUE_TYPE_CHARSEQUENCE = 10, 136 VALUE_TYPE_BOOLEANARRAY = 11, 137 VALUE_TYPE_BYTEARRAY = 12, 138 VALUE_TYPE_CHARARRAY = 13, 139 VALUE_TYPE_SHORTARRAY = 14, 140 VALUE_TYPE_INTARRAY = 15, 141 VALUE_TYPE_LONGARRAY = 16, 142 VALUE_TYPE_FLOATARRAY = 17, 143 VALUE_TYPE_DOUBLEARRAY = 18, 144 VALUE_TYPE_STRINGARRAY = 19, 145 VALUE_TYPE_CHARSEQUENCEARRAY = 20, 146 147 VALUE_TYPE_PARCELABLE = 21, 148 VALUE_TYPE_PARCELABLEARRAY = 22, 149 VALUE_TYPE_SERIALIZABLE = 23, 150 VALUE_TYPE_WANTPARAMSARRAY = 24, 151 VALUE_TYPE_LIST = 50, 152 153 VALUE_TYPE_WANTPARAMS = 101, 154 VALUE_TYPE_ARRAY = 102, 155 VALUE_TYPE_FD = 103, 156 VALUE_TYPE_REMOTE_OBJECT = 104, 157 VALUE_TYPE_INVALID_FD = 105, 158 }; 159 160 bool WriteArrayToParcel(Parcel &parcel, IArray *ao, int depth) const; 161 bool ReadArrayToParcel(Parcel &parcel, int type, sptr<IArray> &ao, int depth); 162 bool ReadFromParcel(Parcel &parcel, int depth = 1); 163 bool ReadFromParcelParam(Parcel &parcel, const std::string &key, int type, int depth); 164 bool ReadFromParcelString(Parcel &parcel, const std::string &key); 165 bool ReadFromParcelBool(Parcel &parcel, const std::string &key); 166 bool ReadFromParcelInt8(Parcel &parcel, const std::string &key); 167 bool ReadFromParcelChar(Parcel &parcel, const std::string &key); 168 bool ReadFromParcelShort(Parcel &parcel, const std::string &key); 169 bool ReadFromParcelInt(Parcel &parcel, const std::string &key); 170 bool ReadFromParcelLong(Parcel &parcel, const std::string &key); 171 bool ReadFromParcelFloat(Parcel &parcel, const std::string &key); 172 bool ReadFromParcelDouble(Parcel &parcel, const std::string &key); 173 174 bool ReadFromParcelArrayString(Parcel &parcel, sptr<IArray> &ao); 175 bool ReadFromParcelArrayBool(Parcel &parcel, sptr<IArray> &ao); 176 bool ReadFromParcelArrayByte(Parcel &parcel, sptr<IArray> &ao); 177 bool ReadFromParcelArrayChar(Parcel &parcel, sptr<IArray> &ao); 178 bool ReadFromParcelArrayShort(Parcel &parcel, sptr<IArray> &ao); 179 180 bool ReadFromParcelArrayInt(Parcel &parcel, sptr<IArray> &ao); 181 bool ReadFromParcelArrayLong(Parcel &parcel, sptr<IArray> &ao); 182 bool ReadFromParcelArrayFloat(Parcel &parcel, sptr<IArray> &ao); 183 bool ReadFromParcelArrayDouble(Parcel &parcel, sptr<IArray> &ao); 184 bool ReadFromParcelArrayWantParams(Parcel &parcel, sptr<IArray> &ao, int depth); 185 bool ReadFromParcelWantParamWrapper(Parcel &parcel, const std::string &key, int type, int depth); 186 bool ReadFromParcelFD(Parcel &parcel, const std::string &key); 187 bool ReadFromParcelRemoteObject(Parcel &parcel, const std::string &key); 188 189 bool WriteArrayToParcelString(Parcel &parcel, IArray *ao) const; 190 bool WriteArrayToParcelBool(Parcel &parcel, IArray *ao) const; 191 bool WriteArrayToParcelByte(Parcel &parcel, IArray *ao) const; 192 bool WriteArrayToParcelChar(Parcel &parcel, IArray *ao) const; 193 bool WriteArrayToParcelShort(Parcel &parcel, IArray *ao) const; 194 bool WriteArrayToParcelInt(Parcel &parcel, IArray *ao) const; 195 bool WriteArrayToParcelLong(Parcel &parcel, IArray *ao) const; 196 bool WriteArrayToParcelFloat(Parcel &parcel, IArray *ao) const; 197 bool WriteArrayToParcelDouble(Parcel &parcel, IArray *ao) const; 198 bool WriteArrayToParcelWantParams(Parcel &parcel, IArray *ao, int depth) const; 199 200 bool WriteMarshalling(Parcel &parcel, sptr<IInterface> &o, int depth) const; 201 bool WriteToParcelString(Parcel &parcel, sptr<IInterface> &o) const; 202 bool WriteToParcelBool(Parcel &parcel, sptr<IInterface> &o) const; 203 bool WriteToParcelByte(Parcel &parcel, sptr<IInterface> &o) const; 204 bool WriteToParcelChar(Parcel &parcel, sptr<IInterface> &o) const; 205 bool WriteToParcelShort(Parcel &parcel, sptr<IInterface> &o) const; 206 bool WriteToParcelInt(Parcel &parcel, sptr<IInterface> &o) const; 207 bool WriteToParcelLong(Parcel &parcel, sptr<IInterface> &o) const; 208 bool WriteToParcelFloat(Parcel &parcel, sptr<IInterface> &o) const; 209 bool WriteToParcelDouble(Parcel &parcel, sptr<IInterface> &o) const; 210 bool WriteToParcelWantParams(Parcel &parcel, sptr<IInterface> &o, int depth) const; 211 bool WriteToParcelFD(Parcel &parcel, const WantParams &value) const; 212 bool WriteToParcelRemoteObject(Parcel &parcel, const WantParams &value) const; 213 214 bool DoMarshalling(Parcel &parcel, int depth = 1) const; 215 bool ReadUnsupportedData(Parcel &parcel, const std::string &key, int type); 216 217 void UnwrapWantParamsNumber(const nlohmann::json &jsonObject, const std::string &key); 218 219 void InnerWrapWantParamsByte(nlohmann::json &jsObject, const std::string &key) const; 220 void InnerWrapWantParamsChar(nlohmann::json &jsObject, const std::string &key) const; 221 void InnerWrapWantParamsShort(nlohmann::json &jsObject, const std::string &key) const; 222 void InnerWrapWantParamsBool(nlohmann::json &jsObject, const std::string &key) const; 223 void InnerWrapWantParamsString(nlohmann::json &jsObject, const std::string &key) const; 224 void InnerWrapWantParamsInt32(nlohmann::json &jsObject, const std::string &key) const; 225 void InnerWrapWantParamsInt64(nlohmann::json &jsObject, const std::string &key) const; 226 void InnerWrapWantParamsFloat(nlohmann::json &jsObject, const std::string &key) const; 227 void InnerWrapWantParamsDouble(nlohmann::json &jsObject, const std::string &key) const; 228 void InnerWrapWantParamsWantParams(nlohmann::json &jsObject, const std::string &key) const; 229 230 void InnerWrapWantParamsArrayString(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 231 void InnerWrapWantParamsArrayBool(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 232 void InnerWrapWantParamsArrayShort(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 233 void InnerWrapWantParamsArrayByte(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 234 void InnerWrapWantParamsArrayInt32(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 235 void InnerWrapWantParamsArrayInt64(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 236 void InnerWrapWantParamsArrayFloat(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 237 void InnerWrapWantParamsArrayWantParams(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 238 void InnerWrapWantParamsArrayChar(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 239 void InnerWrapWantParamsArrayDouble(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 240 void InnerWrapWantParamsArray(nlohmann::json &jsObject, const std::string &key, sptr<IArray> &ao) const; 241 242 friend class WantParamWrapper; 243 // inner use function 244 bool NewArrayData(IArray *source, sptr<IArray> &dest); 245 bool NewParams(const WantParams &source, WantParams &dest); 246 bool NewFds(const WantParams &source, WantParams &dest); 247 std::map<std::string, sptr<IInterface>> params_; 248 std::map<std::string, int> fds_; 249 std::vector<UnsupportedData> cachedUnsupportedData_; 250 }; 251 252 void from_json(const nlohmann::json &jsonObject, WantParams &wantParams); 253 void to_json(nlohmann::json &jsonObject, const WantParams &wantParams); 254 } // namespace AAFwk 255 } // namespace OHOS 256 257 #endif // OHOS_ABILITY_BASE_WANT_PARAMS_H 258