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