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
16 #include "page_history_manager.h"
17
18 #include "hiview_logger.h"
19 #include "sys_event.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 DEFINE_LOG_LABEL(0xD002D11, "Faultlogger");
24 namespace {
25 constexpr const char* const INTERACTION_COMPLETED_LATENCY = "INTERACTION_COMPLETED_LATENCY";
26 constexpr const char* const ABILITY_OR_PAGE_SWITCH = "ABILITY_OR_PAGE_SWITCH";
27 constexpr const char* const ABILITY_ONFOREGROUND = "ABILITY_ONFOREGROUND";
28 constexpr const char* const ABILITY_ONBACKGROUND = "ABILITY_ONBACKGROUND";
29 }
30
GetInstance()31 PageHistoryManager& PageHistoryManager::GetInstance()
32 {
33 static PageHistoryManager instance;
34 return instance;
35 }
36
HandleEvent(const Event & event)37 void PageHistoryManager::HandleEvent(const Event& event)
38 {
39 if (event.eventName_ != INTERACTION_COMPLETED_LATENCY &&
40 event.eventName_ != ABILITY_ONFOREGROUND &&
41 event.eventName_ != ABILITY_ONBACKGROUND) {
42 HIVIEW_LOGE("event(%{public}s) is not match.", event.eventName_.c_str());
43 return;
44 }
45 Event& eventRef = const_cast<Event&>(event);
46 SysEvent& sysEvent = static_cast<SysEvent&>(eventRef);
47
48 PageTraceNode node;
49 node.pid_ = sysEvent.GetPid();
50 node.timestamp_ = sysEvent.happenTime_;
51 if (sysEvent.eventName_ == INTERACTION_COMPLETED_LATENCY) {
52 if (sysEvent.GetEventValue("SCENE_ID") != ABILITY_OR_PAGE_SWITCH) {
53 return;
54 }
55 node.SetPageInfo(sysEvent.GetEventValue("PAGE_URL"), sysEvent.GetEventValue("PAGE_NAME"));
56 } else if (sysEvent.eventName_ == ABILITY_ONFOREGROUND) {
57 node.SetPageInfo("", "enters foreground");
58 } else if (sysEvent.eventName_ == ABILITY_ONBACKGROUND) {
59 node.SetPageInfo("", "leaves foreground");
60 } else {
61 return;
62 }
63 auto processName = sysEvent.GetEventValue("BUNDLE_NAME");
64
65 recorder_.PutPageTrace(processName, std::move(node));
66 }
67
GetPageHistory(const std::string & processName,int64_t pid)68 std::string PageHistoryManager::GetPageHistory(const std::string& processName, int64_t pid)
69 {
70 return recorder_.GetPageTrace(processName, pid);
71 }
72 } // HiviewDFX
73 } // OHOS
74