1 /*
2  * Copyright (c) 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 "ipc/extract_param.h"
17 
18 #include "parcel_macro.h"
19 #include "string_ex.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 constexpr const char* TYPE_ALL = "All";
25 constexpr const char* TYPE_SO = "So";
26 constexpr const char* TYPE_AN = "An";
27 constexpr const char* TYPE_PATCH = "Patch";
28 constexpr const char* TYPE_AP = "Ap";
29 constexpr const char* TYPE_RESOURCE = "Resource";
30 constexpr const char* TYPE_RES_FILE = "ResFile";
31 constexpr const char* TYPE_HNPS_FILE = "HnpsFile";
32 constexpr const char* TYPE_OTHER = "Other";
33 const ExtractFileType ARGS_MAP_KEY[] = {
34     ExtractFileType::ALL, ExtractFileType::SO, ExtractFileType::AN, ExtractFileType::PATCH, ExtractFileType::AP,
35     ExtractFileType::RESOURCE, ExtractFileType::RES_FILE, ExtractFileType::HNPS_FILE,
36 };
37 constexpr const char* ARGS_MAP_VALUE[] = {
38     TYPE_ALL, TYPE_SO, TYPE_AN, TYPE_PATCH, TYPE_AP, TYPE_RESOURCE, TYPE_RES_FILE, TYPE_HNPS_FILE,
39 };
40 
GetExtractFileTypeStrVal(const ExtractFileType &extractFileType)41 std::string GetExtractFileTypeStrVal(const ExtractFileType &extractFileType)
42 {
43     std::string typeStr = TYPE_OTHER;
44     size_t len = sizeof(ARGS_MAP_KEY) / sizeof(ARGS_MAP_KEY[0]);
45     for (size_t i = 0; i < len; i++) {
46         if (extractFileType == ARGS_MAP_KEY[i]) {
47             typeStr = ARGS_MAP_VALUE[i];
48             break;
49         }
50     }
51     return typeStr;
52 }
53 }
ReadFromParcel(Parcel &parcel)54 bool ExtractParam::ReadFromParcel(Parcel &parcel)
55 {
56     srcPath = Str16ToStr8(parcel.ReadString16());
57     targetPath = Str16ToStr8(parcel.ReadString16());
58     cpuAbi = Str16ToStr8(parcel.ReadString16());
59     extractFileType = static_cast<ExtractFileType>(parcel.ReadInt32());
60     return true;
61 }
62 
Marshalling(Parcel &parcel) const63 bool ExtractParam::Marshalling(Parcel &parcel) const
64 {
65     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(srcPath));
66     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(targetPath));
67     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(cpuAbi));
68     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(extractFileType));
69     return true;
70 }
71 
Unmarshalling(Parcel &parcel)72 ExtractParam *ExtractParam::Unmarshalling(Parcel &parcel)
73 {
74     ExtractParam *info = new (std::nothrow) ExtractParam();
75     if (info) {
76         info->ReadFromParcel(parcel);
77     }
78     return info;
79 }
80 
ToString() const81 std::string ExtractParam::ToString() const
82 {
83     return "[ srcPath :" +  srcPath
84             + ", targetPath = " + targetPath
85             + ", cpuAbi = " + cpuAbi
86             + ", extractFileType = " + GetExtractFileTypeStrVal(extractFileType) + "]";
87 }
88 }  // namespace AppExecFwk
89 }  // namespace OHOS
90