1 /* 2 * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_OHOS_FORM_PROVIDER_DATA_H 17 #define FOUNDATION_APPEXECFWK_OHOS_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 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 FormProviderData(std::string jsonDataString); 56 57 /** 58 * @brief Destructor. 59 */ ~FormProviderData()60 virtual ~FormProviderData(){}; 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 data Indicates the binary data of the image content. 78 */ 79 void AddImageData(std::string picName, char *data, int32_t size); 80 81 /** 82 * @brief Adds an image to this {@code FormProviderData} instance. 83 * @param picName Indicates the name of the image to add. 84 * @param fd Indicates the file descriptor of the image content. 85 */ 86 void AddImageData(std::string picName, int fd); 87 88 /** 89 * @brief Parse images in jsonFormProviderData_. The images data is in the format of 90 * {"images": {"key": fd, "key": fd}} 91 */ 92 void ParseImagesData(); 93 94 /** 95 * @brief Removes data of an image with the specified {@code picName} from this {@code FormProviderData} instance. 96 * @param picName Indicates the name of the image to remove. 97 */ 98 void RemoveImageData(std::string picName); 99 100 /** 101 * @brief Obtains the add/remove state stored in this {@code FormProviderData} object. 102 * @return Returns the add/remove state of shared image data. 103 */ 104 int32_t GetImageDataState() const; 105 106 /** 107 * @brief Updates imageDataState in this {@code FormProviderData} object. 108 * @param imageDataState Indicates the imageDataState to update. 109 */ 110 void SetImageDataState(int32_t imageDataState); 111 112 /** 113 * @brief Obtains the imageDataMap stored in this {@code FormProviderData} object. 114 * @return Returns the map that contains shared image data. 115 */ 116 std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> GetImageDataMap() const; 117 118 /** 119 * @brief Updates imageDataMap in this {@code FormProviderData} object. 120 * @param imageDataMap Indicates the imageDataMap to update. 121 */ 122 void SetImageDataMap(std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap); 123 124 /** 125 * @brief Obtains the form data stored in this {@code FormProviderData} object. 126 * @return Returns json data 127 */ 128 nlohmann::json GetData() const; 129 /** 130 * @brief Set the form data stored from string string. 131 * @param Returns string string. 132 */ 133 void SetDataString(std::string &jsonDataString); 134 /** 135 * @brief Merge new data to FormProviderData. 136 * @param addJsonData data to merge to FormProviderData 137 */ 138 void MergeData(nlohmann::json &addJsonData); 139 140 /** 141 * Read this {@code FormProviderData} object from a Parcel. 142 * @param parcel the parcel 143 * eturn Returns {@code true} if the marshalling is successful; returns {@code false} otherwise. 144 */ 145 bool ReadFromParcel(Parcel &parcel); 146 /** 147 * @brief Marshals this {@code FormProviderData} object into a {@link ohos.utils.Parcel} object. 148 * @param parcel Indicates the {@code Parcel} object for marshalling. 149 * @return Returns {@code true} if the marshalling is successful; returns {@code false} otherwise. 150 */ 151 virtual bool Marshalling(Parcel &parcel) const override; 152 153 /** 154 * @brief Unmarshals this {@code FormProviderData} object from a {@link ohos.utils.Parcel} object. 155 * @param parcel Indicates the {@code Parcel} object for unmarshalling. 156 * @return Returns FormProviderData. 157 */ 158 static FormProviderData* Unmarshalling(Parcel &parcel); 159 160 /** 161 * @brief Clear imageDataMap, rawImageBytesMap, imageDataState and jsonFormProviderData. 162 */ 163 void ClearData(); 164 165 public: 166 static constexpr int IMAGE_DATA_STATE_REMOVED = -1; 167 static constexpr int IMAGE_DATA_STATE_NO_OPERATION = 0; 168 static constexpr int IMAGE_DATA_STATE_ADDED = 1; 169 170 private: 171 bool WriteImageDataToParcel(Parcel &parcel, std::string picName, char *data, int32_t size) const; 172 173 private: 174 nlohmann::json jsonFormProviderData_; 175 std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap_; 176 std::map<std::string, std::pair<char *, int32_t>> rawImageBytesMap_; 177 int32_t imageDataState_ = 0; 178 }; 179 } // namespace AppExecFwk 180 } // namespace OHOS 181 182 #endif // FOUNDATION_APPEXECFWK_OHOS_FORM_PROVIDER_DATA_H 183