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