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