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