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 #include "buffer_extra_data_impl.h"
17
18 #include <message_parcel.h>
19
20 #include "buffer_log.h"
21
22 namespace OHOS {
23 namespace {
24 constexpr int32_t BUFFER_EXTRA_DATA_MAGIC = 0x4567;
25 } // namespace
26
ReadFromParcel(MessageParcel & parcel)27 GSError BufferExtraDataImpl::ReadFromParcel(MessageParcel &parcel)
28 {
29 int32_t magic;
30 if (parcel.ReadInt32(magic) == false || magic != BUFFER_EXTRA_DATA_MAGIC) {
31 BLOGW("read failed, magic is error");
32 return GSERROR_INTERNAL;
33 }
34
35 int32_t size = parcel.ReadInt32();
36 for (int32_t i = 0; i < size; i++) {
37 auto key = parcel.ReadString();
38 auto type = static_cast<ExtraDataType>(parcel.ReadInt32());
39 switch (type) {
40 case ExtraDataType::i32: {
41 ExtraSet(key, type, parcel.ReadInt32());
42 break;
43 }
44 case ExtraDataType::i64: {
45 ExtraSet(key, type, parcel.ReadInt64());
46 break;
47 }
48 case ExtraDataType::f64: {
49 ExtraSet(key, type, parcel.ReadDouble());
50 break;
51 }
52 case ExtraDataType::string: {
53 ExtraSet(key, type, parcel.ReadString());
54 break;
55 }
56 default: break;
57 }
58 }
59 return GSERROR_OK;
60 }
61
WriteToParcel(MessageParcel & parcel)62 GSError BufferExtraDataImpl::WriteToParcel(MessageParcel &parcel)
63 {
64 parcel.WriteInt32(BUFFER_EXTRA_DATA_MAGIC);
65 parcel.WriteInt32(datas.size());
66 for (const auto &[key, data] : datas) {
67 parcel.WriteString(key);
68 parcel.WriteInt32(static_cast<int32_t>(data.type));
69 switch (data.type) {
70 case ExtraDataType::i32: {
71 int32_t i32 = -1;
72 auto pVal = std::any_cast<int32_t>(&data.val);
73 if (pVal != nullptr) {
74 i32 = *pVal;
75 }
76 parcel.WriteInt32(i32);
77 break;
78 }
79 case ExtraDataType::i64: {
80 int64_t i64 = -1;
81 auto pVal = std::any_cast<int64_t>(&data.val);
82 if (pVal != nullptr) {
83 i64 = *pVal;
84 }
85 parcel.WriteInt64(i64);
86 break;
87 }
88 case ExtraDataType::f64: {
89 double f64 = -1;
90 auto pVal = std::any_cast<double>(&data.val);
91 if (pVal != nullptr) {
92 f64 = *pVal;
93 }
94 parcel.WriteDouble(f64);
95 break;
96 }
97 case ExtraDataType::string: {
98 std::string string = "-1";
99 auto pVal = std::any_cast<std::string>(&data.val);
100 if (pVal != nullptr) {
101 string = *pVal;
102 }
103 parcel.WriteString(string);
104 break;
105 }
106 default:
107 break;
108 }
109 }
110 return GSERROR_OK;
111 }
112
ExtraGet(const std::string & key,int32_t & value) const113 GSError BufferExtraDataImpl::ExtraGet(const std::string &key, int32_t &value) const
114 {
115 return ExtraGet<int32_t>(key, ExtraDataType::i32, value);
116 }
117
ExtraGet(const std::string & key,int64_t & value) const118 GSError BufferExtraDataImpl::ExtraGet(const std::string &key, int64_t &value) const
119 {
120 return ExtraGet<int64_t>(key, ExtraDataType::i64, value);
121 }
122
ExtraGet(const std::string & key,double & value) const123 GSError BufferExtraDataImpl::ExtraGet(const std::string &key, double &value) const
124 {
125 return ExtraGet<double>(key, ExtraDataType::f64, value);
126 }
127
ExtraGet(const std::string & key,std::string & value) const128 GSError BufferExtraDataImpl::ExtraGet(const std::string &key, std::string &value) const
129 {
130 return ExtraGet<std::string>(key, ExtraDataType::string, value);
131 }
132
ExtraSet(const std::string & key,int32_t value)133 GSError BufferExtraDataImpl::ExtraSet(const std::string &key, int32_t value)
134 {
135 return ExtraSet(key, ExtraDataType::i32, value);
136 }
137
ExtraSet(const std::string & key,int64_t value)138 GSError BufferExtraDataImpl::ExtraSet(const std::string &key, int64_t value)
139 {
140 return ExtraSet(key, ExtraDataType::i64, value);
141 }
142
ExtraSet(const std::string & key,double value)143 GSError BufferExtraDataImpl::ExtraSet(const std::string &key, double value)
144 {
145 return ExtraSet(key, ExtraDataType::f64, value);
146 }
147
ExtraSet(const std::string & key,const std::string & value)148 GSError BufferExtraDataImpl::ExtraSet(const std::string &key, const std::string& value)
149 {
150 return ExtraSet(key, ExtraDataType::string, value);
151 }
152
153 template<class T>
ExtraGet(const std::string & key,ExtraDataType type,T & value) const154 GSError BufferExtraDataImpl::ExtraGet(const std::string &key, ExtraDataType type, T &value) const
155 {
156 auto it = datas.find(key);
157 if (it == datas.end()) {
158 return GSERROR_NO_ENTRY;
159 }
160 if (it->second.type != type) {
161 return GSERROR_TYPE_ERROR;
162 }
163 auto pVal = std::any_cast<T>(&it->second.val);
164 if (pVal == nullptr) {
165 return GSERROR_TYPE_ERROR;
166 }
167 value = *pVal;
168 return GSERROR_OK;
169 }
170
ExtraSet(const std::string & key,ExtraDataType type,const std::any & val)171 GSError BufferExtraDataImpl::ExtraSet(const std::string &key, ExtraDataType type, const std::any& val)
172 {
173 auto it = datas.find(key);
174 if (it == datas.end() && datas.size() > SURFACE_MAX_USER_DATA_COUNT) {
175 BLOGW("SurfaceBuffer has too many extra data, cannot save one more!!!");
176 return GSERROR_OUT_OF_RANGE;
177 }
178 datas[key].type = type;
179 datas[key].val = val;
180 return GSERROR_OK;
181 }
182 } // namespace OHOS
183