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 "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); 80 81 /** 82 * @brief Removes data of an image with the specified {@code picName} from this {@code FormProviderData} instance. 83 * @param picName Indicates the name of the image to remove. 84 */ 85 void RemoveImageData(std::string picName); 86 87 /** 88 * @brief Obtains the add/remove state stored in this {@code FormProviderData} object. 89 * @return Returns the add/remove state of shared image data. 90 */ 91 int32_t GetImageDataState(); 92 93 /** 94 * @brief Updates imageDataState in this {@code FormProviderData} object. 95 * @param imageDataState Indicates the imageDataState to update. 96 */ 97 void SetImageDataState(int32_t imageDataState); 98 99 /** 100 * @brief Obtains the imageDataMap stored in this {@code FormProviderData} object. 101 * @return Returns the map that contains shared image data. 102 */ 103 std::map<std::string, std::pair<sptr<Ashmem>, int32_t>> GetImageDataMap(); 104 105 /** 106 * @brief Updates imageDataMap in this {@code FormProviderData} object. 107 * @param imageDataMap Indicates the imageDataMap to update. 108 */ 109 void SetImageDataMap(std::map<std::string, std::pair<sptr<Ashmem>, int32_t>> imageDataMap); 110 111 /** 112 * @brief Obtains the form data stored in this {@code FormProviderData} object. 113 * @return Returns json data 114 */ 115 nlohmann::json GetData() const; 116 /** 117 * @brief Set the form data stored from string string. 118 * @param Returns string string. 119 */ 120 void SetDataString(std::string &jsonDataString); 121 /** 122 * @brief Merge new data to FormProviderData. 123 * @param addJsonData data to merge to FormProviderData 124 */ 125 void MergeData(nlohmann::json &addJsonData); 126 127 /** 128 * Read this {@code FormProviderData} object from a Parcel. 129 * @param parcel the parcel 130 * eturn Returns {@code true} if the marshalling is successful; returns {@code false} otherwise. 131 */ 132 bool ReadFromParcel(Parcel &parcel); 133 /** 134 * @brief Marshals this {@code FormProviderData} object into a {@link ohos.utils.Parcel} object. 135 * @param parcel Indicates the {@code Parcel} object for marshalling. 136 * @return Returns {@code true} if the marshalling is successful; returns {@code false} otherwise. 137 */ 138 virtual bool Marshalling(Parcel &parcel) const override; 139 140 /** 141 * @brief Unmarshals this {@code FormProviderData} object from a {@link ohos.utils.Parcel} object. 142 * @param parcel Indicates the {@code Parcel} object for unmarshalling. 143 * @return Returns FormProviderData. 144 */ 145 static FormProviderData* Unmarshalling(Parcel &parcel); 146 147 /** 148 * @brief Clear imageDataMap, rawImageBytesMap, imageDataState and jsonFormProviderData. 149 */ 150 void ClearData(); 151 152 private: 153 bool WriteImageDataToParcel(Parcel &parcel, std::string picName, char *data) const; 154 155 private: 156 nlohmann::json jsonFormProviderData_; 157 std::map<std::string, std::pair<sptr<Ashmem>, int32_t>> imageDataMap_; 158 std::map<std::string, char *> rawImageBytesMap_; 159 int32_t imageDataState_; 160 }; 161 } // namespace AppExecFwk 162 } // namespace OHOS 163 164 #endif // FOUNDATION_APPEXECFWK_OHOS_FORM_PROVIDER_DATA_H 165