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 "profile_change_notification.h"
17 
18 #include <iosfwd>
19 #include <string>
20 #include <type_traits>
21 #include <vector>
22 
23 #include "device_profile_log.h"
24 #include "parcel.h"
25 #include "parcel_helper.h"
26 
27 namespace OHOS {
28 namespace DeviceProfile {
29 namespace {
30 const std::string TAG = "ProfileChangeNotification";
31 constexpr int32_t MAX_ENTRY_LEN = 1000000;
32 }
33 
GetProfileEntries() const34 const std::vector<ProfileEntry>& ProfileChangeNotification::GetProfileEntries() const
35 {
36     return profileEntries_;
37 }
38 
GetDeviceId() const39 const std::string& ProfileChangeNotification::GetDeviceId() const
40 {
41     return deviceId_;
42 }
43 
IsLocal() const44 bool ProfileChangeNotification::IsLocal() const
45 {
46     return isLocal_;
47 }
48 
Marshalling(Parcel & parcel) const49 bool ProfileChangeNotification::Marshalling(Parcel& parcel) const
50 {
51     int32_t entrySize = static_cast<int32_t>(profileEntries_.size());
52     PARCEL_WRITE_HELPER_RET(parcel, Int32, entrySize, false);
53     for (const auto& profileEntry : profileEntries_) {
54         if (!profileEntry.Marshalling(parcel)) {
55             return false;
56         }
57     }
58     PARCEL_WRITE_HELPER_RET(parcel, String, deviceId_, false);
59 
60     return true;
61 }
62 
Unmarshalling(Parcel & parcel)63 bool ProfileChangeNotification::Unmarshalling(Parcel& parcel)
64 {
65     int32_t entrySize = parcel.ReadInt32();
66     if (entrySize < 0 || entrySize > MAX_ENTRY_LEN) {
67         HILOGE("invalid entrySize = %{public}d", entrySize);
68         return false;
69     }
70     profileEntries_.reserve(entrySize);
71     for (int32_t i = 0; i < entrySize; i++) {
72         ProfileEntry profileEntry;
73         if (!profileEntry.Unmarshalling(parcel)) {
74             return false;
75         }
76         profileEntries_.emplace_back(std::move(profileEntry));
77     }
78     PARCEL_READ_HELPER_RET(parcel, String, deviceId_, false);
79     return true;
80 }
81 
Marshalling(Parcel & parcel) const82 bool ProfileEntry::Marshalling(Parcel& parcel) const
83 {
84     PARCEL_WRITE_HELPER_RET(parcel, String, key, false);
85     PARCEL_WRITE_HELPER_RET(parcel, String, value, false);
86     PARCEL_WRITE_HELPER_RET(parcel, Uint8, static_cast<uint8_t>(changeType), false);
87     return true;
88 }
89 
Unmarshalling(Parcel & parcel)90 bool ProfileEntry::Unmarshalling(Parcel& parcel)
91 {
92     PARCEL_READ_HELPER_RET(parcel, String, key, false);
93     PARCEL_READ_HELPER_RET(parcel, String, value, false);
94     uint8_t type = 0;
95     PARCEL_READ_HELPER_RET(parcel, Uint8, type, false);
96     changeType = static_cast<ProfileChangeType>(type);
97     return true;
98 }
99 }  // namespace DeviceProfile
100 }  // namespace OHOS
101