• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hap_module_info.h"
17 
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20 
21 #include "app_log_wrapper.h"
22 #include "parcel_macro.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 
ReadFromParcel(Parcel & parcel)27 bool HapModuleInfo::ReadFromParcel(Parcel &parcel)
28 {
29     name = Str16ToStr8(parcel.ReadString16());
30     moduleName = Str16ToStr8(parcel.ReadString16());
31     description = Str16ToStr8(parcel.ReadString16());
32     iconPath = Str16ToStr8(parcel.ReadString16());
33     label = Str16ToStr8(parcel.ReadString16());
34     backgroundImg = Str16ToStr8(parcel.ReadString16());
35     mainAbility = Str16ToStr8(parcel.ReadString16());
36     supportedModes = parcel.ReadInt32();
37 
38     int32_t reqCapabilitiesSize;
39     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, reqCapabilitiesSize);
40     for (int32_t i = 0; i < reqCapabilitiesSize; i++) {
41         reqCapabilities.emplace_back(Str16ToStr8(parcel.ReadString16()));
42     }
43 
44     int32_t deviceTypesSize;
45     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, deviceTypesSize);
46     for (int32_t i = 0; i < deviceTypesSize; i++) {
47         deviceTypes.emplace_back(Str16ToStr8(parcel.ReadString16()));
48     }
49 
50     int32_t abilityInfosSize = parcel.ReadInt32();
51     for (int32_t i = 0; i < abilityInfosSize; i++) {
52         std::unique_ptr<AbilityInfo> abilityInfo(parcel.ReadParcelable<AbilityInfo>());
53         if (!abilityInfo) {
54             APP_LOGE("ReadParcelable<AbilityInfo> failed");
55             return false;
56         }
57         abilityInfos.emplace_back(*abilityInfo);
58     }
59 
60     int32_t colorModeData;
61     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, colorModeData);
62     colorMode = static_cast<ModuleColorMode>(colorModeData);
63     return true;
64 }
65 
Unmarshalling(Parcel & parcel)66 HapModuleInfo *HapModuleInfo::Unmarshalling(Parcel &parcel)
67 {
68     HapModuleInfo *info = new (std::nothrow) HapModuleInfo();
69     if (info && !info->ReadFromParcel(parcel)) {
70         APP_LOGW("read from parcel failed");
71         delete info;
72         info = nullptr;
73     }
74     return info;
75 }
76 
Marshalling(Parcel & parcel) const77 bool HapModuleInfo::Marshalling(Parcel &parcel) const
78 {
79     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
80     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
81     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(description));
82     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(iconPath));
83     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
84     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(backgroundImg));
85     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(mainAbility));
86     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, supportedModes);
87     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, reqCapabilities.size());
88     for (auto &reqCapability : reqCapabilities) {
89         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(reqCapability));
90     }
91     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, deviceTypes.size());
92     for (auto &deviceType : deviceTypes) {
93         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceType));
94     }
95     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilityInfos.size());
96     for (auto &abilityInfo : abilityInfos) {
97         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &abilityInfo);
98     }
99     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(colorMode));
100     return true;
101 }
102 
103 }  // namespace AppExecFwk
104 }  // namespace OHOS
105