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