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_FORM_FWK_FORM_PROVIDER_DATA_H
17 #define OHOS_FORM_FWK_FORM_PROVIDER_DATA_H
18 
19 #include <cstddef>
20 #include <map>
21 #include <string>
22 
23 #include "form_ashmem.h"
24 #include "message_parcel.h"
25 #include "nlohmann/json.hpp"
26 #include "parcel.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 /**
31  * @class FormProviderData
32  * Defines form provider data.
33  */
34 class FormProviderData : public Parcelable {
35 public:
36     /**
37      * @brief Constructor.
38      */
39     FormProviderData();
40 
41     /**
42      * @brief A constructor used to create a {@code FormProviderData} instance with data of
43      * the {@code nlohmann::json} type specified.
44      * @param jsonData Indicates the data to be carried in the new {@code FormProviderData} instance,
45      * in {@code nlohmann::json} format.
46      */
47     explicit FormProviderData(nlohmann::json &jsonData);
48 
49     /**
50      * @brief A constructor used to create a {@code FormProviderData} instance with data of the {@code String} type
51      * specified.
52      * @param jsonDataString Indicates the data to be carried in the new {@code FormProviderData} instance, in JSON
53      * string format.
54      */
55     explicit FormProviderData(std::string jsonDataString);
56 
57     /**
58      * @brief A constructor used to create a {@code FormProviderData} instance with data of the {@code String} type
59      * specified.
60      * @param jsonDataString Indicates the data to be carried in the new {@code FormProviderData} instance, in JSON
61      * string format.
62      * @param isUsedInFRS Indicates is used in frs
63      */
64     explicit FormProviderData(std::string jsonDataString, bool isUsedInFRS);
65 
66     /**
67      * @brief Destructor.
68      */
69     ~FormProviderData() override = default;
70 
71     /**
72      * @brief Updates form data in this {@code FormProviderData} object.
73      * @param jsonData Indicates the new data to use, in {@code nlohmann::json} format.
74      */
75     void UpdateData(nlohmann::json &jsonData);
76 
77     /**
78      * @brief Obtains the form data stored in this {@code FormProviderData} object.
79      * @return Returns json string format
80      */
81     std::string GetDataString() const;
82 
83     /**
84      * @brief Adds an image to this {@code FormProviderData} instance.
85      * @param picName Indicates the name of the image to add.
86      * @param fd Indicates the file descriptor of the image content.
87      */
88     void AddImageData(const std::string &picName, int fd);
89 
90     /**
91      * @brief Parse images in jsonFormProviderData_. The images data is in the format of
92      * {"images": {"key": fd, "key": fd}}
93      */
94     void ParseImagesData();
95 
96     /**
97      * @brief Removes data of an image with the specified {@code picName} from this {@code FormProviderData} instance.
98      * @param picName Indicates the name of the image to remove.
99      */
100     void RemoveImageData(std::string picName);
101 
102     /**
103      * @brief Obtains the add/remove state stored in this {@code FormProviderData} object.
104      * @return Returns the add/remove state of shared image data.
105      */
106     int32_t GetImageDataState() const;
107 
108     /**
109      * @brief Updates imageDataState in this {@code FormProviderData} object.
110      * @param imageDataState Indicates the imageDataState to update.
111      */
112     void SetImageDataState(int32_t imageDataState);
113 
114     /**
115      * @brief Obtains the imageDataMap stored in this {@code FormProviderData} object.
116      * @return Returns the map that contains shared image data.
117      */
118     std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> GetImageDataMap() const;
119 
120     /**
121      * @brief Updates imageDataMap in this {@code FormProviderData} object.
122      * @param imageDataMap Indicates the imageDataMap to update.
123      */
124     void SetImageDataMap(std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap);
125 
126     /**
127      * @brief Obtains the form data stored in this {@code FormProviderData} object.
128      * @return Returns json data
129      */
130     nlohmann::json GetData() const;
131     /**
132      * @brief Set the form data stored from string string.
133      * @param Returns string string.
134      */
135     void SetDataString(std::string &jsonDataString);
136     /**
137      * @brief Merge new data to FormProviderData.
138      * @param addJsonData data to merge to FormProviderData
139      */
140     void MergeData(nlohmann::json &addJsonData);
141 
142     /**
143      * Read this {@code FormProviderData} object from a Parcel.
144      * @param parcel the parcel
145      * eturn Returns {@code true} if the marshalling is successful; returns {@code false} otherwise.
146      */
147     bool ReadFromParcel(Parcel &parcel);
148      /**
149      * @brief Marshals this {@code FormProviderData} object into a {@link ohos.utils.Parcel} object.
150      * @param parcel Indicates the {@code Parcel} object for marshalling.
151      * @return Returns {@code true} if the marshalling is successful; returns {@code false} otherwise.
152      */
153     virtual bool Marshalling(Parcel &parcel) const override;
154 
155     /**
156      * @brief Unmarshals this {@code FormProviderData} object from a {@link ohos.utils.Parcel} object.
157      * @param parcel Indicates the {@code Parcel} object for unmarshalling.
158      * @return Returns FormProviderData.
159      */
160     static FormProviderData* Unmarshalling(Parcel &parcel);
161 
162     /**
163      * @brief Clear imageDataMap, rawImageBytesMap, imageDataState and jsonFormProviderData.
164      */
165     void ClearData();
166 
167     /**
168      * @brief Whether the form provider data needs to be cached
169      * @return Returns {@code true} if the data needs to be cached; returns {@code false} otherwise.
170      */
171     bool NeedCache() const;
172 
173     /**
174      * @brief Convert raw image data to shmem image data
175      * @return Returns {@code true} if the image data converted successfully; returns {@code false} otherwise.
176      */
177     bool ConvertRawImageData();
178 public:
179     static constexpr int IMAGE_DATA_STATE_REMOVED = -1;
180     static constexpr int IMAGE_DATA_STATE_NO_OPERATION = 0;
181     static constexpr int IMAGE_DATA_STATE_ADDED = 1;
182 
183 private:
184     bool WriteImageDataToParcel(Parcel &parcel, const std::string &picName, const std::shared_ptr<char> &data,
185         int32_t size) const;
186 
187     /**
188      * @brief Adds an image to this {@code FormProviderData} instance.
189      * @param picName Indicates the name of the image to add.
190      * @param data Indicates the binary data of the image content.
191      */
192     void AddImageData(const std::string &picName, const std::shared_ptr<char> &data, int32_t size);
193 private:
194     struct DeleteBytes {
operatorDeleteBytes195         void operator()(char* bytes) const
196         {
197             delete[] bytes;
198         }
199     };
200     nlohmann::json jsonFormProviderData_;
201     std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap_;
202     std::map<std::string, std::pair<std::shared_ptr<char>, int32_t>> rawImageBytesMap_;
203     int32_t imageDataState_ = 0;
204 };
205 }  // namespace AppExecFwk
206 }  // namespace OHOS
207 
208 #endif  // OHOS_FORM_FWK_FORM_PROVIDER_DATA_H
209