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 
16 #ifndef OHOS_ABILITY_BASE_PAC_MAP_H
17 #define OHOS_ABILITY_BASE_PAC_MAP_H
18 
19 #include <string>
20 #include <iostream>
21 #include <map>
22 #include <vector>
23 #include <memory>
24 #include <mutex>
25 #include <set>
26 #include "securec.h"
27 #include "json/json.h"
28 #include "parcel.h"
29 #include "base_def.h"
30 #include "base_obj.h"
31 #include "base_interfaces.h"
32 
33 // json key define
34 // base:   0x00000001 ~ 0x000000FF
35 // array:  0x00000100 ~ 0x0000FF00
36 // boject: 0x00010000 ~ 0x00FF0000
37 #define PACMAP_DATA_NONE 0x00
38 // base data
39 #define PACMAP_DATA_SHORT 0x00000001
40 #define PACMAP_DATA_INTEGER 0x00000002
41 #define PACMAP_DATA_LONG 0x00000003
42 #define PACMAP_DATA_CHAR 0x00000004
43 #define PACMAP_DATA_BYTE 0x00000005
44 #define PACMAP_DATA_BOOLEAN 0x00000007
45 #define PACMAP_DATA_FLOAT 0x00000008
46 #define PACMAP_DATA_DOUBLE 0x00000009
47 #define PACMAP_DATA_STRING 0x0000000A
48 // array data
49 #define PACMAP_DATA_ARRAY_SHORT 0x00000100
50 #define PACMAP_DATA_ARRAY_INTEGER 0x00000200
51 #define PACMAP_DATA_ARRAY_LONG 0x00000300
52 #define PACMAP_DATA_ARRAY_CHAR 0x00000400
53 #define PACMAP_DATA_ARRAY_BYTE 0x00000500
54 #define PACMAP_DATA_ARRAY_BOOLEAN 0x00000600
55 #define PACMAP_DATA_ARRAY_FLOAT 0x00000700
56 #define PACMAP_DATA_ARRAY_DOUBLE 0x00000800
57 #define PACMAP_DATA_ARRAY_STRING 0x00000900
58 // object data
59 #define PACMAP_DATA_USEROBJECT 0x00010000
60 #define PACMAP_DATA_PACMAP 0x00020000
61 
62 namespace OHOS {
63 namespace AppExecFwk {
64 namespace PacMapObject {
65 using INTERFACE = sptr<OHOS::AAFwk::IInterface>;
66 };  // namespace PacMapObject
67 
68 using namespace AAFwk;
69 using UserObjectBase = AAFwk::UserObjectBase;
70 using PacMapList = std::map<std::string, PacMapObject::INTERFACE>;
71 
72 class PacMap final : public Parcelable, public Object, public IPacMap {
73 public:
74     /**
75      * @brief Default constructor used to create a PacMap instance, in which the Map object has no key-value pair.
76      */
77     PacMap() = default;
78 
79     /**
80      * @brief A replication structure with deep copy.
81      */
82     PacMap(const PacMap &other);
83 
84     ~PacMap();
85 
86     IINTERFACE_DECL();
87 
88     /**
89      * @brief A overload operation with shallow copy.
90      */
91     PacMap &operator=(const PacMap &other);
92 
93     /**
94      * @brief Clear all key-value pairs and free resources.
95      */
96     void Clear(void);
97 
98     /**
99      * @brief Creates and returns a copy of this object with shallow copy.
100      *
101      * @return A clone of this instance.
102      */
103     PacMap Clone(void);
104 
105     /**
106      * @brief Creates a deep copy of this PacMap object with deep copy.
107      * @param pacMap Returns the constructed object.
108      */
109     PacMap DeepCopy(void);
110     void DeepCopy(PacMap &other);
111 
112     /**
113      * @brief Adds a short value matching a specified key.
114      * @param key A specified key.
115      * @param value The value that matches the specified key.
116      */
117     void PutShortValue(const std::string &key, short value);
118 
119     /**
120      * @brief Adds a integer value matching a specified key.
121      * @param key A specified key.
122      * @param value The value that matches the specified key.
123      */
124     void PutIntValue(const std::string &key, int value);
125 
126     /**
127      * @brief Adds a long value matching a specified key.
128      * @param key A specified key.
129      * @param value The value that matches the specified key.
130      */
131     void PutLongValue(const std::string &key, long value);
132 
133     /**
134      * @brief Adds a boolean value matching a specified key.
135      * @param key A specified key.
136      * @param value The value that matches the specified key.
137      */
138     void PutBooleanValue(const std::string &key, bool value);
139 
140     /**
141      * @brief Adds a char value matching a specified key.
142      * @param key A specified key.
143      * @param value The value that matches the specified key.
144      */
145     void PutCharValue(const std::string &key, char value);
146 
147     /**
148      * @brief Adds a byte value matching a specified key.
149      * @param key A specified key.
150      * @param value The value that matches the specified key.
151      */
152     void PutByteValue(const std::string &key, AAFwk::byte value);
153 
154     /**
155      * @brief Adds a float value matching a specified key.
156      * @param key A specified key.
157      * @param value The value that matches the specified key.
158      */
159     void PutFloatValue(const std::string &key, float value);
160 
161     /**
162      * @brief Adds a double value matching a specified key.
163      * @param key A specified key.
164      * @param value The value that matches the specified key.
165      */
166     void PutDoubleValue(const std::string &key, double value);
167 
168     /**
169      * @brief Adds a string {std::string} value matching a specified key.
170      * @param key A specified key.
171      * @param value The value that matches the specified key.
172      */
173     void PutStringValue(const std::string &key, const std::string &value);
174 
175     /**
176      * @brief Adds an object value matching a specified key. The object must be a subclass of UserObjectBase.
177      * @param key A specified key.
178      * @param value A smart pointer to the object that matches the specified key.
179      */
180     void PutObject(const std::string &key, const std::shared_ptr<UserObjectBase> &value);
181 
182     /**
183      * @brief Adds an PacMap value matching a specified key.
184      * @param key A specified key.
185      * @param value The value that matches the specified key.
186      */
187     bool PutPacMap(const std::string &key, const PacMap &value);
188 
189     /**
190      * @brief Adds some short values matching a specified key.
191      * @param key A specified key.
192      * @param value Store a list of short values.
193      */
194     void PutShortValueArray(const std::string &key, const std::vector<short> &value);
195 
196     /**
197      * @brief Adds some integer values matching a specified key.
198      * @param key A specified key.
199      * @param value Store a list of integer values.
200      */
201     void PutIntValueArray(const std::string &key, const std::vector<int> &value);
202 
203     /**
204      * @brief Adds some long values matching a specified key.
205      * @param key A specified key.
206      * @param value Store a list of long values.
207      */
208     void PutLongValueArray(const std::string &key, const std::vector<long> &value);
209 
210     /**
211      * @brief Adds some boolean values matching a specified key.
212      * @param key A specified key.
213      * @param value Store a list of boolean values.
214      */
215     void PutBooleanValueArray(const std::string &key, const std::vector<bool> &value);
216 
217     /**
218      * @brief Adds some char values matching a specified key.
219      * @param key A specified key.
220      * @param value Store a list of char values.
221      */
222     void PutCharValueArray(const std::string &key, const std::vector<char> &value);
223 
224     /**
225      * @brief Adds some byte values matching a specified key.
226      * @param key A specified key.
227      * @param value Store a list of byte values.
228      */
229     void PutByteValueArray(const std::string &key, const std::vector<AAFwk::byte> &value);
230 
231     /**
232      * @brief Adds some float values matching a specified key.
233      * @param key A specified key.
234      * @param value Store a list of float values.
235      */
236     void PutFloatValueArray(const std::string &key, const std::vector<float> &value);
237 
238     /**
239      * @brief Adds some double values matching a specified key.
240      * @param key A specified key.
241      * @param value Store a list of double values.
242      */
243     void PutDoubleValueArray(const std::string &key, const std::vector<double> &value);
244 
245     /**
246      * @brief Adds some string {std::string} values matching a specified key.
247      * @param key A specified key.
248      * @param value Store a list of string values.
249      */
250     void PutStringValueArray(const std::string &key, const std::vector<std::string> &value);
251 
252     /**
253      * @brief Inserts all key-value pairs of a map object into the built-in data object.
254      * Duplicate key values will be replaced.
255      * @param mapData Store a list of key-value pairs.
256      */
257     void PutAll(std::map<std::string, PacMapObject::INTERFACE> &mapData);
258 
259     /**
260      * @brief Saves the data in a PacMap object to the current object. Duplicate key values will be replaced.
261      * @param pacMap Store the date of PacMap.
262      */
263     void PutAll(PacMap &pacMap);
264 
265     /**
266      * @brief Obtains the int value matching a specified key.
267      * @param key A specified key.
268      * @param defaultValue The return value when the function fails.
269      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
270      */
271     int GetIntValue(const std::string &key, int defaultValue = 0);
272 
273     /**
274      * @brief Obtains the short value matching a specified key.
275      * @param key A specified key.
276      * @param defaultValue The return value when the function fails.
277      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
278      */
279     short GetShortValue(const std::string &key, short defaultValue = 0);
280 
281     /**
282      * @brief Obtains the boolean value matching a specified key.
283      * @param key A specified key.
284      * @param defaultValue The return value when the function fails.
285      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
286      */
287     bool GetBooleanValue(const std::string &key, bool defaultValue = false);
288 
289     /**
290      * @brief Obtains the long value matching a specified key.
291      * @param key A specified key.
292      * @param defaultValue The return value when the function fails.
293      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
294      */
295     long GetLongValue(const std::string &key, long defaultValue = 0);
296 
297     /**
298      * @brief Obtains the char value matching a specified key.
299      * @param key A specified key.
300      * @param defaultValue The return value when the function fails.
301      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
302      */
303     char GetCharValue(const std::string &key, char defaultValue = 0x00);
304 
305     /**
306      * @brief Obtains the byte value matching a specified key.
307      * @param key A specified key.
308      * @param defaultValue The return value when the function fails.
309      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
310      */
311     AAFwk::byte GetByteValue(const std::string &key, AAFwk::byte defaultValue = 0x00);
312 
313     /**
314      * @brief Obtains the float value matching a specified key.
315      * @param key A specified key.
316      * @param defaultValue The return value when the function fails.
317      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
318      */
319     float GetFloatValue(const std::string &key, float defaultValue = 0.0f);
320 
321     /**
322      * @brief Obtains the double value matching a specified key.
323      * @param key A specified key.
324      * @param defaultValue The return value when the function fails.
325      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
326      */
327     double GetDoubleValue(const std::string &key, double defaultValue = 0.0);
328 
329     /**
330      * @brief Obtains the string {std::string} value matching a specified key.
331      * @param key A specified key.
332      * @param defaultValue The return value when the function fails.
333      * @return If the match is successful, return the value matching the key, otherwise return the @a defaultValue.
334      */
335     std::string GetStringValue(const std::string &key, const std::string &defaultValue = "");
336 
337     /**
338      * @brief Obtains some int values matching a specified key.
339      * @param key A specified key.
340      * @param value Save the returned int values.
341      */
342     void GetIntValueArray(const std::string &key, std::vector<int> &value);
343 
344     /**
345      * @brief Obtains some short values matching a specified key.
346      * @param key A specified key.
347      * @param value Save the returned short values.
348      */
349     void GetShortValueArray(const std::string &key, std::vector<short> &value);
350 
351     /**
352      * @brief Obtains some boolean values matching a specified key.
353      * @param key A specified key.
354      * @param value Save the returned boolean values.
355      */
356     void GetBooleanValueArray(const std::string &key, std::vector<bool> &value);
357 
358     /**
359      * @brief Obtains some long values matching a specified key.
360      * @param key A specified key.
361      * @param value Save the returned long values.
362      */
363     void GetLongValueArray(const std::string &key, std::vector<long> &value);
364 
365     /**
366      * @brief Obtains some char values matching a specified key.
367      * @param key A specified key.
368      * @param value Save the returned char values.
369      */
370     void GetCharValueArray(const std::string &key, std::vector<char> &value);
371 
372     /**
373      * @brief Obtains some byte values matching a specified key.
374      * @param key A specified key.
375      * @param value Save the returned byte values.
376      */
377     void GetByteValueArray(const std::string &key, std::vector<AAFwk::byte> &value);
378 
379     /**
380      * @brief Obtains some float values matching a specified key.
381      * @param key A specified key.
382      * @param value Save the returned float values.
383      */
384     void GetFloatValueArray(const std::string &key, std::vector<float> &value);
385 
386     /**
387      * @brief Obtains some double values matching a specified key.
388      * @param key A specified key.
389      * @param value Save the returned double values.
390      */
391     void GetDoubleValueArray(const std::string &key, std::vector<double> &value);
392 
393     /**
394      * @brief Obtains some string {std::string} values matching a specified key.
395      * @param key A specified key.
396      * @param value Save the returned string {std::string} values.
397      */
398     void GetStringValueArray(const std::string &key, std::vector<std::string> &value);
399 
400     /**
401      * @brief Obtains the object matching a specified key.
402      * @param key A specified key.
403      * @return Returns the smart pointer to object that matches the key.
404      */
405     std::shared_ptr<UserObjectBase> GetObject(const std::string &key);
406 
407     /**
408      * @brief Obtains the PacMap matching a specified key.
409      * @param key A specified key.
410      * @return Returns PacMap that matches the key.
411      */
412     PacMap GetPacMap(const std::string &key);
413 
414     /**
415      * @brief Obtains all the data that has been stored with shallow copy.
416      * @return Returns all data in current PacMap. There is no dependency between the returned data and
417      * the original data.
418      */
419     std::map<std::string, PacMapObject::INTERFACE> GetAll(void);
420 
421     /**
422      * @brief Indicates whether some other object is "equal to" this one.
423      * @param pacMap The object with which to compare.
424      * @return Returns true if this object is the same as the pacMap argument; false otherwise.
425      */
426     bool Equals(const PacMap *pacMap);
427     bool Equals(const PacMap &pacMap);
428 
429     /**
430      * @brief Checks whether the current object is empty.
431      * @return If there is no data, return true, otherwise return false.
432      */
433     bool IsEmpty(void) const;
434 
435     /**
436      * @brief Obtains the number of key-value pairs stored in the current object.
437      * @return Returns the number of key-value pairs.
438      */
439     int GetSize(void) const;
440 
441     /**
442      * @brief Obtains all the keys of the current object.
443      */
444     const std::set<std::string> GetKeys(void);
445 
446     /**
447      * @brief Checks whether a specified key is contained.
448      * @param key Indicates the key in String
449      * @return Returns true if the key is contained; returns false otherwise.
450      */
451     bool HasKey(const std::string &key);
452 
453     /**
454      * @brief Deletes a key-value pair with a specified key.
455      * @param key Specifies the key of the deleted data.
456      */
457     void Remove(const std::string &key);
458 
459     /**
460      * @brief Marshals this Sequenceable object to a Parcel.
461      * @param parcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
462      * @return Marshals success returns true, otherwise returns false.
463      */
464     virtual bool Marshalling(Parcel &parcel) const override;
465 
466     /**
467      * @brief Unmarshals this Sequenceable object from a Parcel.
468      * @param parcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
469      * @return Unmarshals success returns a smart pointer to PacMap, otherwise returns nullptr.
470      */
471     static PacMap *Unmarshalling(Parcel &parcel);
472 
473     /**
474      * @brief Save pacmap to string.
475      * @return Returns the string.
476      */
477     virtual std::string ToString() override;
478 
479     /**
480      * @brief Restore pacmap from the string.
481      * @return Return true if successful, otherwise false.
482      */
483     virtual bool FromString(const std::string &str) override;
484 
485     ErrCode GetValue(PacMap &value);
486 
487     virtual bool Equals(IObject &other) override;
488 
489     static sptr<IPacMap> Parse(const std::string &str);
490 
491     static constexpr char SIGNATURE = 'P';
492 
493 private:
494     PacMapList dataList_;
495     std::mutex mapLock_;
496     bool GetBaseJsonValue(PacMapList::const_iterator &it, Json::Value &json) const;
497     bool GetArrayJsonValue(PacMapList::const_iterator &it, Json::Value &json) const;
498     bool GetUserObjectJsonValue(PacMapList::const_iterator &it, Json::Value &json) const;
499     void ShallowCopyData(PacMapList &desPacMap, const PacMapList &srcPacMap);
500     void RemoveData(PacMapList &srcPacMap, const std::string &key);
501     bool EqualPacMapData(const PacMapList &leftPacMapList, const PacMapList &rightPacMapList);
502     bool ReadFromParcel(Parcel &parcel);
503 
504     bool ParseJson(Json::Value &data, PacMapList &mapList);
505     bool ParseJsonItem(PacMapList &mapList, const std::string &key, Json::Value &item);
506     bool ParseJsonItemArray(PacMapList &mapList, const std::string &key, Json::Value &item);
507     bool ParseJsonItemArrayShort(PacMapList &mapList, const std::string &key, Json::Value &item);
508     bool ParseJsonItemArrayInteger(PacMapList &mapList, const std::string &key, Json::Value &item);
509     bool ParseJsonItemArrayLong(PacMapList &mapList, const std::string &key, Json::Value &item);
510     bool ParseJsonItemArrayChar(PacMapList &mapList, const std::string &key, Json::Value &item);
511     bool ParseJsonItemArrayByte(PacMapList &mapList, const std::string &key, Json::Value &item);
512     bool ParseJsonItemArrayBoolean(PacMapList &mapList, const std::string &key, Json::Value &item);
513     bool ParseJsonItemArrayFloat(PacMapList &mapList, const std::string &key, Json::Value &item);
514     bool ParseJsonItemArrayDouble(PacMapList &mapList, const std::string &key, Json::Value &item);
515     bool ParseJsonItemArrayString(PacMapList &mapList, const std::string &key, Json::Value &item);
516 
517     bool InnerPutObjectValue(PacMapList &mapList, const std::string &key, Json::Value &item);
518     bool InnerPutPacMapValue(PacMapList &mapList, const std::string &key, Json::Value &item);
519 
520     bool ToJson(const PacMapList &mapList, Json::Value &dataObject) const;
521 
522     std::string MapListToString(const PacMapList &mapList) const;
523     bool StringToMapList(const std::string &str, PacMapList &mapList);
524 
525     void InnerPutShortValue(PacMapList &mapList, const std::string &key, short value);
526     void InnerPutIntValue(PacMapList &mapList, const std::string &key, int value);
527     void InnerPutLongValue(PacMapList &mapList, const std::string &key, long value);
528     void InnerPutBooleanValue(PacMapList &mapList, const std::string &key, bool value);
529     void InnerPutCharValue(PacMapList &mapList, const std::string &key, char value);
530     void InnerPutByteValue(PacMapList &mapList, const std::string &key, AAFwk::byte value);
531     void InnerPutFloatValue(PacMapList &mapList, const std::string &key, float value);
532     void InnerPutDoubleValue(PacMapList &mapList, const std::string &key, double value);
533     void InnerPutStringValue(PacMapList &mapList, const std::string &key, const std::string &value);
534     void InnerPutObject(PacMapList &mapList, const std::string &key, const std::shared_ptr<UserObjectBase> &value);
535     bool InnerPutPacMap(PacMapList &mapList, const std::string &key, PacMap &value);
536 
537     void InnerPutShortValueArray(PacMapList &mapList, const std::string &key, const std::vector<short> &value);
538     void InnerPutIntValueArray(PacMapList &mapList, const std::string &key, const std::vector<int> &value);
539     void InnerPutLongValueArray(PacMapList &mapList, const std::string &key, const std::vector<long> &value);
540     void InnerPutBooleanValueArray(PacMapList &mapList, const std::string &key, const std::vector<bool> &value);
541     void InnerPutCharValueArray(PacMapList &mapList, const std::string &key, const std::vector<char> &value);
542     void InnerPutByteValueArray(PacMapList &mapList, const std::string &key, const std::vector<AAFwk::byte> &value);
543     void InnerPutFloatValueArray(PacMapList &mapList, const std::string &key, const std::vector<float> &value);
544     void InnerPutDoubleValueArray(PacMapList &mapList, const std::string &key, const std::vector<double> &value);
545     void InnerPutStringValueArray(PacMapList &mapList, const std::string &key, const std::vector<std::string> &value);
546 
547     // char data:none
548     bool ToJsonArrayShort(std::vector<short> &array, Json::Value &item, int type) const;
549     bool ToJsonArrayInt(std::vector<int> &array, Json::Value &item, int type) const;
550     bool ToJsonArrayLong(std::vector<long> &array, Json::Value &item, int type) const;
551     bool ToJsonArrayByte(std::vector<byte> &array, Json::Value &item, int type) const;
552     bool ToJsonArrayBoolean(std::vector<bool> &array, Json::Value &item, int type) const;
553     bool ToJsonArrayFloat(std::vector<float> &array, Json::Value &item, int type) const;
554     bool ToJsonArrayDouble(std::vector<double> &array, Json::Value &item, int type) const;
555     bool ToJsonArrayString(std::vector<std::string> &array, Json::Value &item, int type) const;
556 
557     bool IsNumber(const std::string &str);
558     bool CompareArrayData(AAFwk::IInterface *one_interface, AAFwk::IInterface *two_interface);
559     bool JudgeType(Json::Value &item);
560 };
561 }  // namespace AppExecFwk
562 }  // namespace OHOS
563 
564 #endif