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