1 /*
2  * Copyright (c) 2024 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 "adapter/preview/external/ability/stage/stage_pkg_context_info.h"
17 
18 #include <memory>
19 
20 #include "frameworks/base/json/json_util.h"
21 #include "frameworks/base/log/log_wrapper.h"
22 
23 namespace OHOS::Ace {
SetPkgNameList(const std::map<std::string,std::string> & map)24 void StagePkgContextInfo::SetPkgNameList(const std::map<std::string, std::string>& map)
25 {
26     pkgNameMap_ = map;
27 }
28 
SetPkgContextInfoAndAliasMap(const std::map<std::string,std::string> & map)29 void StagePkgContextInfo::SetPkgContextInfoAndAliasMap(const std::map<std::string, std::string>& map)
30 {
31     if (map.empty()) {
32         LOGW("pkgcontextinfo map is empty");
33         return;
34     }
35     for (auto it = map.begin(); it != map.end(); it++) {
36         std::string moduleName = it->first;
37         std::string pkgInfoStr = it->second;
38         if (pkgInfoStr.empty()) {
39             return;
40         }
41         std::unique_ptr<JsonValue> json = JsonUtil::ParseJsonString(pkgInfoStr);
42         if (!json) {
43             return;
44         }
45         std::vector<std::vector<std::string>> pkgContextInfoList;
46         std::unique_ptr<JsonValue> item = json->GetChild();
47         AliasMap(item, pkgContextInfoList, moduleName);
48     }
49 }
AliasMap(std::unique_ptr<JsonValue> & item,std::vector<std::vector<std::string>> & pkgContextInfoList,const std::string & moduleName)50 void StagePkgContextInfo::AliasMap(std::unique_ptr<JsonValue>& item,
51     std::vector<std::vector<std::string>>& pkgContextInfoList, const std::string& moduleName)
52 {
53     while (item && !item->GetKey().empty()) {
54         std::vector<std::string> sonMap;
55         std::string key = item->GetKey();
56         sonMap.push_back(key);
57         std::unique_ptr<JsonValue> item1 = item->GetChild();
58         while (item1 && !item1->GetKey().empty()) {
59             std::string key1 = item1->GetKey();
60             sonMap.push_back(key1);
61             if ("dependencyAlias" == key1) {
62                 bool value = item1->GetBool();
63                 std::string val1 = value ? "true" : "false";
64                 sonMap.push_back(val1);
65                 pkgAliasMap_.emplace(val1, key);
66             } else {
67                 std::string val1 = item1->GetString();
68                 sonMap.push_back(val1);
69             }
70             item1 = item1->GetNext();
71         }
72         pkgContextInfoList.push_back(sonMap);
73         item = item->GetNext();
74     }
75     pkgContextInfoMap_[moduleName] = pkgContextInfoList;
76 }
GetPkgNameMap() const77 const std::map<std::string, std::string>& StagePkgContextInfo::GetPkgNameMap() const
78 {
79     return pkgNameMap_;
80 }
GetPkgAliasMap() const81 const std::map<std::string, std::string>& StagePkgContextInfo::GetPkgAliasMap() const
82 {
83     return pkgAliasMap_;
84 }
GetPkgContextInfoMap()85 const std::map<std::string, std::vector<std::vector<std::string>>>& StagePkgContextInfo::GetPkgContextInfoMap()
86 {
87     return pkgContextInfoMap_;
88 }
89 }
90