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 #include "form_provider_info.h"
17 
18 #include "string_ex.h"
19 
20 #include "fms_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
ReadFromParcel(Parcel & parcel)24 bool FormProviderInfo::ReadFromParcel(Parcel &parcel)
25 {
26     std::unique_ptr<FormProviderData> bindingData(parcel.ReadParcelable<FormProviderData>());
27     if (bindingData == nullptr) {
28         HILOG_ERROR("null bindingData");
29         return false;
30     }
31     jsBindingData_ = *bindingData;
32 
33     auto number = parcel.ReadInt32();
34     if (number < 0 || number > INT16_MAX) {
35         HILOG_ERROR("proxies number over limit:%{public}d", number);
36         return false;
37     }
38     HILOG_DEBUG("proxies number:%{public}d", number);
39     for (int32_t i = 0; i < number; i++) {
40         FormDataProxy formDataProxy("", "");
41         formDataProxy.key = Str16ToStr8(parcel.ReadString16());
42         formDataProxy.subscribeId = Str16ToStr8(parcel.ReadString16());
43         formDataProxies_.push_back(formDataProxy);
44     }
45 
46     return true;
47 }
48 
Unmarshalling(Parcel & parcel)49 FormProviderInfo *FormProviderInfo::Unmarshalling(Parcel &parcel)
50 {
51     std::unique_ptr<FormProviderInfo> formProviderInfo = std::make_unique<FormProviderInfo>();
52     if (formProviderInfo && !formProviderInfo->ReadFromParcel(parcel)) {
53         formProviderInfo = nullptr;
54     }
55     return formProviderInfo.release();
56 }
57 
Marshalling(Parcel & parcel) const58 bool FormProviderInfo::Marshalling(Parcel &parcel) const
59 {
60     if (!parcel.WriteParcelable(&jsBindingData_)) {
61         return false;
62     }
63     HILOG_DEBUG("proxies size:%{public}zu", formDataProxies_.size());
64     if (!parcel.WriteInt32(formDataProxies_.size())) {
65         HILOG_ERROR("fail marshalling form data proxies size");
66         return false;
67     }
68     for (const auto &formDataProxy : formDataProxies_) {
69         // write key
70         if (!parcel.WriteString16(Str8ToStr16(formDataProxy.key))) {
71             HILOG_ERROR("fail marshalling form data proxies key:%{public}s", formDataProxy.key.c_str());
72             return false;
73         }
74         if (!parcel.WriteString16(Str8ToStr16(formDataProxy.subscribeId))) {
75             HILOG_ERROR("fail marshalling form data proxies subscribeId:%{public}s",
76                 formDataProxy.subscribeId.c_str());
77             return false;
78         }
79     }
80     return true;
81 }
SetFormDataString(std::string & dataString)82 void FormProviderInfo::SetFormDataString(std::string &dataString)
83 {
84     jsBindingData_.SetDataString(dataString);
85 }
86 /**
87  * @brief Updates imageDataMap in this {@code FormProviderData} object.
88  * @param imageDataMap Indicates the imageDataMap to update.
89  */
SetImageDataMap(std::map<std::string,std::pair<sptr<FormAshmem>,int32_t>> imageDataMap)90 void FormProviderInfo::SetImageDataMap(std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap)
91 {
92     jsBindingData_.SetImageDataMap(imageDataMap);
93 }
94 
95 /**
96  * @brief Obtains the imageDataMap stored in this {@code FormProviderData} object.
97  * @return Returns the map that contains shared image data.
98  */
GetImageDataMap() const99 std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> FormProviderInfo::GetImageDataMap() const
100 {
101     return jsBindingData_.GetImageDataMap();
102 }
103 /**
104  * @brief Merge new data to FormProviderData.
105  * @param addJsonData data to merge to FormProviderData
106  */
MergeData(nlohmann::json & addJsonData)107 void FormProviderInfo::MergeData(nlohmann::json &addJsonData)
108 {
109     jsBindingData_.MergeData(addJsonData);
110 }
111 
NeedCache() const112 bool FormProviderInfo::NeedCache() const
113 {
114     return jsBindingData_.NeedCache();
115 }
116 }  // namespace AppExecFwk
117 }  // namespace OHOS
118