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_INFO_H
17 #define OHOS_FORM_FWK_FORM_PROVIDER_INFO_H
18 
19 #include <cstdint>
20 #include <utility>
21 #include <vector>
22 
23 #include "form_ashmem.h"
24 #include "form_provider_data.h"
25 #include "form_provider_data_proxy.h"
26 #include "nlohmann/json_fwd.hpp"
27 #include "parcel.h"
28 #include "refbase.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 /**
33  * @struct FormProviderInfo
34  * Defines form provider info.
35  */
36 class FormProviderInfo : public Parcelable {
37 public:
38     FormProviderInfo() = default;
39     ~FormProviderInfo() = default;
40 
41     /**
42      * @brief Set the form data.
43      * @param formProviderData The form data.
44      */
SetFormData(const FormProviderData & formProviderData)45     inline void SetFormData(const FormProviderData &formProviderData)
46     {
47         jsBindingData_ = formProviderData;
48     }
49 
50     /**
51      * @brief Get the form data.
52      * @return the form data.
53      */
GetFormData()54     inline FormProviderData GetFormData() const
55     {
56         return jsBindingData_;
57     }
58     /**
59      * @brief Get the form data.
60      * @return the form data.
61      */
GetFormDataString()62     inline std::string GetFormDataString() const
63     {
64         return jsBindingData_.GetDataString();
65     }
66 
67     /**
68      * @brief Set the upgrade flg.
69      * @param upgradeFlg The upgrade flg.
70      */
SetUpgradeFlg(const bool upgradeFlg)71     inline void SetUpgradeFlg(const bool upgradeFlg)
72     {
73         upgradeFlg_ = upgradeFlg;
74     }
75     /**
76      * @brief Get the upgrade flg.
77      * @return the upgrade flg.
78      */
GetUpgradeFlg()79     inline bool GetUpgradeFlg() const
80     {
81         return upgradeFlg_;
82     }
83 
84     /**
85      * @brief Set form date by string.
86      * @param dataString string json data.
87      */
88     void SetFormDataString(std::string &dataString);
89 
90     /**
91      * @brief Updates imageDataMap in this {@code FormProviderData} object.
92      * @param imageDataMap Indicates the imageDataMap to update.
93      */
94     void SetImageDataMap(std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap);
95 
96     /**
97      * @brief Obtains the imageDataMap stored in this {@code FormProviderData} object.
98      * @return Returns the map that contains shared image data.
99      */
100     std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> GetImageDataMap() const;
101 
102     /**
103      * @brief Merge new data to FormProviderData.
104      * @param addJsonData data to merge to FormProviderData
105      */
106     void MergeData(nlohmann::json &addJsonData);
107 
108     /**
109      * @brief Whether the form provider data needs to be cached
110      * @return Returns {@code true} if the data needs to be cached; returns {@code false} otherwise.
111      */
112     bool NeedCache() const;
113 
SetFormDataProxies(std::vector<FormDataProxy> & formDataProxies)114     void SetFormDataProxies(std::vector<FormDataProxy> &formDataProxies)
115     {
116         formDataProxies_ = formDataProxies;
117     }
118 
GetFormProxies()119     std::vector<FormDataProxy> GetFormProxies() const
120     {
121         return formDataProxies_;
122     }
123 
124     bool ReadFromParcel(Parcel &parcel);
125     virtual bool Marshalling(Parcel &parcel) const override;
126     static FormProviderInfo *Unmarshalling(Parcel &parcel);
127 private:
128     FormProviderData jsBindingData_;
129     bool upgradeFlg_;
130     std::vector<FormDataProxy> formDataProxies_;
131 };
132 }  // namespace AppExecFwk
133 }  // namespace OHOS
134 #endif  // OHOS_FORM_FWK_FORM_PROVIDER_INFO_H
135