• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "os_account_constraint_subscribe_info.h"
17 
18 #include "account_log_wrapper.h"
19 #include "os_account_constants.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
OsAccountConstraintSubscribeInfo()23 OsAccountConstraintSubscribeInfo::OsAccountConstraintSubscribeInfo()
24 {}
25 
OsAccountConstraintSubscribeInfo(const std::set<std::string> & constraints)26 OsAccountConstraintSubscribeInfo::OsAccountConstraintSubscribeInfo(const std::set<std::string> &constraints)
27     : constraintSet_(constraints)
28 {}
29 
SetConstraints(const std::set<std::string> & constraints)30 void OsAccountConstraintSubscribeInfo::SetConstraints(const std::set<std::string> &constraints)
31 {
32     constraintSet_ = constraints;
33 }
34 
GetConstraints(std::set<std::string> & constraints) const35 void OsAccountConstraintSubscribeInfo::GetConstraints(std::set<std::string> &constraints) const
36 {
37     constraints = constraintSet_;
38 }
39 
Marshalling(Parcel & parcel) const40 bool OsAccountConstraintSubscribeInfo::Marshalling(Parcel &parcel) const
41 {
42     if (!parcel.WriteUint32(constraintSet_.size())) {
43         ACCOUNT_LOGE("Write constraintSet size failed.");
44         return false;
45     }
46     for (const auto &item : constraintSet_) {
47         if ((!parcel.WriteString(item))) {
48             ACCOUNT_LOGE("Write constraintSet item failed.");
49             return false;
50         }
51     }
52     return true;
53 }
54 
Unmarshalling(Parcel & parcel)55 OsAccountConstraintSubscribeInfo *OsAccountConstraintSubscribeInfo::Unmarshalling(Parcel &parcel)
56 {
57     OsAccountConstraintSubscribeInfo *info = new (std::nothrow) OsAccountConstraintSubscribeInfo();
58     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
59         ACCOUNT_LOGW("Read OsAccountConstraintSubscribeInfo from parcel failed.");
60         delete info;
61         info = nullptr;
62     }
63     return info;
64 }
65 
ReadFromParcel(Parcel & parcel)66 bool OsAccountConstraintSubscribeInfo::ReadFromParcel(Parcel &parcel)
67 {
68     uint32_t size = 0;
69     if (!parcel.ReadUint32(size)) {
70         ACCOUNT_LOGE("Read size failed.");
71         return false;
72     }
73     if (size > Constants::CONSTRAINT_MAX_SIZE) {
74         ACCOUNT_LOGE("Constraint is oversize, the size is %{public}u", size);
75         return false;
76     }
77     std::string constraint;
78     for (uint32_t i = 0; i < size; i++) {
79         if ((!parcel.ReadString(constraint))) {
80             ACCOUNT_LOGE("Read constraint item failed.");
81             return false;
82         }
83         constraintSet_.emplace(constraint);
84     }
85     return true;
86 }
87 }  // namespace AccountSA
88 }  // namespace OHOS