1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
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 "include/navigation.h"
16 #include <iostream>
17 #include "include/sp_utils.h"
18 #include "include/startup_delay.h"
19 #include "include/sp_log.h"
20 #include "include/common.h"
21
22 namespace OHOS {
23 namespace SmartPerf {
ItemData()24 std::map<std::string, std::string> Navigation::ItemData()
25 {
26 std::map<std::string, std::string> result = Navigation::GetNavInfo();
27 LOGI("Navigation::ItemData map size(%u)", result.size());
28 return result;
29 }
30
SetProcessId(const std::string & pid)31 void Navigation::SetProcessId(const std::string &pid)
32 {
33 processId = pid;
34 }
35
GetNavInfo() const36 std::map<std::string, std::string> Navigation::GetNavInfo() const
37 {
38 std::map<std::string, std::string> navInfo;
39 std::string winId = "";
40 winId = GetWinId(processId);
41 if (winId != "-1") {
42 navInfo = GetNavResult(winId);
43 } else {
44 navInfo["navPathName"] = "No Navigation Info";
45 }
46 return navInfo;
47 }
48
GetNavResult(std::string winId) const49 std::map<std::string, std::string> Navigation::GetNavResult(std::string winId) const
50 {
51 std::map<std::string, std::string> navInfo;
52 std::string nameStr = "No Navigation Info";
53 std::string cmd = HIDUMPER_CMD_MAP.at(HidumperCmd::DUMPER_NAV) + winId + " -navigation'";
54 if (cmd.empty()) {
55 navInfo["navPathName"] = nameStr;
56 return navInfo;
57 }
58 FILE *navfd = popen(cmd.c_str(), "r");
59 if (navfd == nullptr) {
60 navInfo["navPathName"] = nameStr;
61 return navInfo;
62 }
63 char buf[4096] = {'\0'};
64 while ((fgets(buf, sizeof(buf), navfd)) != nullptr) {
65 std::string line = buf;
66 if (line.find("]{ name:") != std::string::npos) {
67 size_t pos = line.find("]{ name:");
68 size_t pos2 = line.find_last_of("}");
69 if (line.find(",") != std::string::npos) {
70 size_t pos1 = line.find(",");
71 nameStr = line.substr(pos + paramTen, pos1 - pos - paramEleven);
72 } else {
73 nameStr = line.substr(pos + paramTen, pos2 - pos - paramTwelve);
74 }
75 }
76 }
77 pclose(navfd);
78 navInfo["navPathName"] = nameStr;
79 return navInfo;
80 }
81
GetWinId(std::string navPid) const82 std::string Navigation::GetWinId(std::string navPid) const
83 {
84 std::string wid;
85 const std::string cmd = HIDUMPER_CMD_MAP.at(HidumperCmd::DUMPER_A_A);
86 FILE *fd = popen(cmd.c_str(), "r");
87 if (fd == nullptr) {
88 return wid = -1;
89 }
90 char buf[1024] = {'\0'};
91 while ((fgets(buf, sizeof(buf), fd)) != nullptr) {
92 std::string line = buf;
93 if (line.find("---") != std::string::npos || line.length() <= 1 ||
94 line.find("WindowName") != std::string::npos) {
95 continue;
96 }
97 std::vector<std::string> params;
98 SPUtils::StrSplit(line, " ", params);
99 if (static_cast<int>(params.size()) > paramThree) {
100 if (params[paramTwo] == navPid) {
101 wid = params[paramThree];
102 break;
103 }
104 }
105 }
106 pclose(fd);
107 return wid;
108 }
109 }
110 }
111