• 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 #include "plugin_bundle_config.h"
16 
17 #include "cjson_util.h"
18 #include "hiview_logger.h"
19 #include "parameter_ex.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 DEFINE_LOG_TAG("PluginBundleConfig");
24 namespace {
ParseBundleNames(const cJSON * config,std::vector<std::string> & bundleNames)25 bool ParseBundleNames(const cJSON* config, std::vector<std::string>& bundleNames)
26 {
27     cJSON* bundleName = nullptr;
28     cJSON_ArrayForEach(bundleName, config) {
29         if (bundleName == nullptr || !cJSON_IsString(bundleName)) {
30             HIVIEW_LOGW("bundle name is not string");
31             continue;
32         }
33         bundleNames.emplace_back(bundleName->valuestring);
34     }
35     return true;
36 }
37 }
38 
PluginBundleConfig(const std::string & configPath)39 PluginBundleConfig::PluginBundleConfig(const std::string& configPath)
40 {
41     Parse(configPath);
42 }
43 
Parse(const std::string & configPath)44 void PluginBundleConfig::Parse(const std::string& configPath)
45 {
46     auto root = CJsonUtil::ParseJsonRoot(configPath);
47     if (root == nullptr) {
48         HIVIEW_LOGW("failed to parse config, file=%{public}s", configPath.c_str());
49         return;
50     }
51     std::string version = Parameter::GetVersionTypeStr();
52     auto config = CJsonUtil::GetArrayValue(root, version);
53     if (config == nullptr) {
54         HIVIEW_LOGW("failed to parse config, file=%{public}s, version=%{public}s",
55             configPath.c_str(), version.c_str());
56         cJSON_Delete(root);
57         return;
58     }
59     if (!ParseBundleNames(config, bundleNames_)) {
60         HIVIEW_LOGW("failed to parse bundle names, file=%{public}s, version=%{public}s",
61             configPath.c_str(), version.c_str());
62     } else {
63         HIVIEW_LOGI("succ to parse bundle config=%{public}s", configPath.c_str());
64     }
65     cJSON_Delete(root);
66 }
67 } // namespace HiviewDFX
68 } // namespace OHOS
69