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