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