• 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 "module_info.h"
17 
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20 
21 #include "json_util.h"
22 #include "parcel_macro.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string MODULE_INFO_MODULE_SOURCE_DIR = "moduleSourceDir";
28 const std::string MODULE_INFO_PRELOADS = "preloads";
29 }
30 
ReadFromParcel(Parcel & parcel)31 bool ModuleInfo::ReadFromParcel(Parcel &parcel)
32 {
33     moduleName = Str16ToStr8(parcel.ReadString16());
34     moduleSourceDir = Str16ToStr8(parcel.ReadString16());
35     int32_t preloadsSize;
36     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloadsSize);
37     CONTAINER_SECURITY_VERIFY(parcel, preloadsSize, &preloads);
38     for (auto i = 0; i < preloadsSize; i++) {
39         preloads.emplace_back(Str16ToStr8(parcel.ReadString16()));
40     }
41     return true;
42 }
43 
Unmarshalling(Parcel & parcel)44 ModuleInfo *ModuleInfo::Unmarshalling(Parcel &parcel)
45 {
46     ModuleInfo *info = new (std::nothrow) ModuleInfo();
47     if (info && !info->ReadFromParcel(parcel)) {
48         APP_LOGW("read from parcel failed");
49         delete info;
50         info = nullptr;
51     }
52     return info;
53 }
54 
Marshalling(Parcel & parcel) const55 bool ModuleInfo::Marshalling(Parcel &parcel) const
56 {
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
58     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleSourceDir));
59     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloads.size());
60     for (auto &preload : preloads) {
61         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(preload));
62     }
63     return true;
64 }
65 
to_json(nlohmann::json & jsonObject,const ModuleInfo & moduleInfo)66 void to_json(nlohmann::json &jsonObject, const ModuleInfo &moduleInfo)
67 {
68     jsonObject = nlohmann::json {
69         {Constants::MODULE_NAME, moduleInfo.moduleName},
70         {MODULE_INFO_MODULE_SOURCE_DIR, moduleInfo.moduleSourceDir},
71         {MODULE_INFO_PRELOADS, moduleInfo.preloads}
72     };
73 }
74 
from_json(const nlohmann::json & jsonObject,ModuleInfo & moduleInfo)75 void from_json(const nlohmann::json &jsonObject, ModuleInfo &moduleInfo)
76 {
77     const auto &jsonObjectEnd = jsonObject.end();
78     int32_t parseResult = ERR_OK;
79     GetValueIfFindKey<std::string>(jsonObject,
80         jsonObjectEnd,
81         Constants::MODULE_NAME,
82         moduleInfo.moduleName,
83         JsonType::STRING,
84         false,
85         parseResult,
86         ArrayType::NOT_ARRAY);
87     GetValueIfFindKey<std::string>(jsonObject,
88         jsonObjectEnd,
89         MODULE_INFO_MODULE_SOURCE_DIR,
90         moduleInfo.moduleSourceDir,
91         JsonType::STRING,
92         false,
93         parseResult,
94         ArrayType::NOT_ARRAY);
95     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
96         jsonObjectEnd,
97         MODULE_INFO_PRELOADS,
98         moduleInfo.preloads,
99         JsonType::ARRAY,
100         false,
101         parseResult,
102         ArrayType::STRING);
103 }
104 }  // namespace AppExecFwk
105 }  // namespace OHOS
106