• 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 "plugin_module_info.h"
17 
18 #include "app_log_wrapper.h"
19 #include "json_util.h"
20 #include "nlohmann/json.hpp"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23 
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const char* PLUGIN_MODULE_INFO_MODULENAME = "moduleName";
29 const char* PLUGIN_MODULE_INFO_DESCRIPTION = "description";
30 const char* PLUGIN_MODULE_INFO_DESCRIPTION_ID = "descriptionId";
31 const char* PLUGIN_MODULE_INFO_HAP_PATH = "hapPath";
32 const char* PLUGIN_MODULE_INFO_CPU_ABI = "cpuAbi";
33 const char* PLUGIN_MODULE_INFO_NATIVE_LIBRARY_PATH = "nativeLibraryPath";
34 const char* PLUGIN_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES = "nativeLibraryFileNames";
35 }
36 
ReadFromParcel(Parcel & parcel)37 bool PluginModuleInfo::ReadFromParcel(Parcel &parcel)
38 {
39     descriptionId = parcel.ReadUint32();
40     moduleName = parcel.ReadString();
41     description = parcel.ReadString();
42     hapPath = parcel.ReadString();
43     cpuAbi = parcel.ReadString();
44     nativeLibraryPath = parcel.ReadString();
45     int32_t nativeLibraryFileNamesSize;
46     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, nativeLibraryFileNamesSize);
47     CONTAINER_SECURITY_VERIFY(parcel, nativeLibraryFileNamesSize, &nativeLibraryFileNames);
48     for (int32_t i = 0; i < nativeLibraryFileNamesSize; ++i) {
49         nativeLibraryFileNames.emplace_back(parcel.ReadString());
50     }
51     return true;
52 }
53 
Marshalling(Parcel & parcel) const54 bool PluginModuleInfo::Marshalling(Parcel &parcel) const
55 {
56     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, descriptionId);
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, moduleName);
58     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, description);
59     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, hapPath);
60     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, cpuAbi);
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, nativeLibraryPath);
62     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, nativeLibraryFileNames.size());
63     for (auto &fileName : nativeLibraryFileNames) {
64         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, fileName);
65     }
66     return true;
67 }
68 
Unmarshalling(Parcel & parcel)69 PluginModuleInfo *PluginModuleInfo::Unmarshalling(Parcel &parcel)
70 {
71     PluginModuleInfo *info = new (std::nothrow) PluginModuleInfo();
72     if (info && !info->ReadFromParcel(parcel)) {
73         APP_LOGW("read from parcel failed");
74         delete info;
75         info = nullptr;
76     }
77     return info;
78 }
79 
to_json(nlohmann::json & jsonObject,const PluginModuleInfo & pluginModuleInfo)80 void to_json(nlohmann::json &jsonObject, const PluginModuleInfo &pluginModuleInfo)
81 {
82     jsonObject = nlohmann::json {
83         {PLUGIN_MODULE_INFO_MODULENAME, pluginModuleInfo.moduleName},
84         {PLUGIN_MODULE_INFO_DESCRIPTION, pluginModuleInfo.description},
85         {PLUGIN_MODULE_INFO_DESCRIPTION_ID, pluginModuleInfo.descriptionId},
86         {PLUGIN_MODULE_INFO_HAP_PATH, pluginModuleInfo.hapPath},
87         {PLUGIN_MODULE_INFO_CPU_ABI, pluginModuleInfo.cpuAbi},
88         {PLUGIN_MODULE_INFO_NATIVE_LIBRARY_PATH, pluginModuleInfo.nativeLibraryPath},
89         {PLUGIN_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES, pluginModuleInfo.nativeLibraryFileNames}
90     };
91 }
92 
from_json(const nlohmann::json & jsonObject,PluginModuleInfo & pluginModuleInfo)93 void from_json(const nlohmann::json &jsonObject, PluginModuleInfo &pluginModuleInfo)
94 {
95     const auto &jsonObjectEnd = jsonObject.end();
96     int32_t parseResult = ERR_OK;
97     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
98         PLUGIN_MODULE_INFO_MODULENAME,
99         pluginModuleInfo.moduleName, false, parseResult);
100     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
101         PLUGIN_MODULE_INFO_DESCRIPTION,
102         pluginModuleInfo.description, false, parseResult);
103     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd,
104         PLUGIN_MODULE_INFO_DESCRIPTION_ID,
105         pluginModuleInfo.descriptionId, JsonType::NUMBER, false, parseResult,
106         ArrayType::NOT_ARRAY);
107     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
108         PLUGIN_MODULE_INFO_HAP_PATH,
109         pluginModuleInfo.hapPath, false, parseResult);
110     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
111         PLUGIN_MODULE_INFO_CPU_ABI,
112         pluginModuleInfo.cpuAbi, false, parseResult);
113     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
114         PLUGIN_MODULE_INFO_NATIVE_LIBRARY_PATH,
115         pluginModuleInfo.nativeLibraryPath, false, parseResult);
116     GetValueIfFindKey<std::vector<std::string>>(jsonObject, jsonObjectEnd,
117         PLUGIN_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES,
118         pluginModuleInfo.nativeLibraryFileNames, JsonType::ARRAY, false, parseResult,
119         ArrayType::STRING);
120     if (parseResult != ERR_OK) {
121         APP_LOGE("read pluginModuleInfo error : %{public}d", parseResult);
122     }
123 }
124 } // AppExecFwk
125 } // OHOS
126