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 LOGD("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 SubstrNavName(line, nameStr);
67 }
68 if (pclose(navfd) == -1) {
69 LOGE("Error: Failed to close file");
70 navInfo["navPathName"] = nameStr;
71 return navInfo;
72 }
73 navInfo["navPathName"] = nameStr;
74 return navInfo;
75 }
76
SubstrNavName(const std::string & line,std::string & nameStr) const77 void Navigation::SubstrNavName(const std::string &line, std::string &nameStr) const
78 {
79 size_t nameShangPos = line.find("name: ");
80 size_t nameTrunkPos = line.find("Name: ");
81 size_t startPos = line.find("\"", nameTrunkPos + 6);
82 size_t endPos = line.find("\"", startPos + 1);
83 if (line.find("[0]") != std::string::npos) {
84 if (nameTrunkPos != std::string::npos || nameShangPos != std::string::npos) {
85 if (startPos != std::string::npos && endPos != std::string::npos) {
86 nameStr = line.substr(startPos + 1, endPos - startPos - 1);
87 }
88 }
89 }
90 }
91
GetWinId(std::string navPid) const92 std::string Navigation::GetWinId(std::string navPid) const
93 {
94 std::string wid;
95 const std::string cmd = HIDUMPER_CMD_MAP.at(HidumperCmd::DUMPER_A_A);
96 FILE *fd = popen(cmd.c_str(), "r");
97 if (fd == nullptr) {
98 return wid = -1;
99 }
100 char buf[1024] = {'\0'};
101 while ((fgets(buf, sizeof(buf), fd)) != nullptr) {
102 std::string line = buf;
103 if (line.find("---") != std::string::npos || line.length() <= 1 ||
104 line.find("WindowName") != std::string::npos) {
105 continue;
106 }
107 std::vector<std::string> params;
108 SPUtils::StrSplit(line, " ", params);
109 if (static_cast<int>(params.size()) > paramThree) {
110 if (params[paramTwo] == navPid) {
111 wid = params[paramThree];
112 break;
113 }
114 }
115 }
116 if (pclose(fd) == -1) {
117 LOGE("Error: Failed to close file");
118 return wid = -1;
119 }
120 return wid;
121 }
122 }
123 }
124