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 "AppLaunchMonitor.h"
17 #include "AppLaunchMonitorConverter.h"
18 #include "ActionId.h"
19 #include "JlogId.h"
20 #include "hiview_logger.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 DEFINE_LOG_LABEL(0xD002D66, "Hiview-XPerformance");
25 
26 using AppStartCheckPointData = IAppLaunchSceneDataProcessor::AppStartCheckPointData;
27 
AppLaunchMonitor(IMonitorRegistry* registry, IAppThrExecutor* exec, IAppStartReporter* reporter, IAppLaunchSceneDataProcessor* scene)28 AppLaunchMonitor::AppLaunchMonitor(IMonitorRegistry* registry, IAppThrExecutor* exec, IAppStartReporter* reporter,
29                                    IAppLaunchSceneDataProcessor* scene)
30 {
31     this->registry = registry;
32     this->exec = exec;
33     this->reporter = reporter;
34     this->scene = scene;
35     this->actionId = APP_START;
36 }
37 
HandleEvt(std::shared_ptr<XperfEvt> evt)38 void AppLaunchMonitor::HandleEvt(std::shared_ptr<XperfEvt> evt)
39 {
40     HIVIEW_LOGI("AppLaunchMonitor::HandleEvt");
41     if (exec != nullptr) {
42         IAppThrExecutor::AppEvtData appEvtData = AppLaunchMonitorConverter::ConvertXperfEvtToAppEvtData(*evt.get());
43         exec->ExecuteHandleEvtInMainThr(this, appEvtData);
44     } else {
45         HIVIEW_LOGE("[AppLaunchMonitor::HandleEvt] exec is null");
46     }
47 }
48 
ExecuteProcessAppEvtTaskInMainThr(const IAppThrExecutor::AppEvtData& data)49 void AppLaunchMonitor::ExecuteProcessAppEvtTaskInMainThr(const IAppThrExecutor::AppEvtData& data)
50 {
51     HIVIEW_LOGI("AppLaunchMonitor::ExecuteProcessAppEvtTaskInMainThr");
52     try {
53         AppStartCheckPointData cpData = AppLaunchMonitorConverter::ConvertAppEvtDataToCheckPointData(data);
54         if (scene != nullptr) {
55             scene->ProcessSceneData(cpData);
56         } else {
57             HIVIEW_LOGE("[AppLaunchMonitor::ExecuteProcessAppEvtTaskInMainThr] scene is null");
58         }
59     } catch (std::logic_error& ex) {
60         HIVIEW_LOGD("exception error:%{public}s", std::string(ex.what()).c_str());
61     }
62 }
63 
ReportMetrics(const AppStartMetrics& metrics)64 void AppLaunchMonitor::ReportMetrics(const AppStartMetrics& metrics)
65 {
66     HIVIEW_LOGI("AppLaunchMonitor::ReportMetrics");
67     try {
68         ReportNormal(metrics);
69     } catch (std::logic_error& ex) {
70         HIVIEW_LOGE("ReportMetrics error: %{public}s", std::string(ex.what()).c_str());
71     }
72 }
73 
ReportNormal(const AppStartMetrics& metrics)74 void AppLaunchMonitor::ReportNormal(const AppStartMetrics& metrics)
75 {
76     HIVIEW_LOGI("AppLaunchMonitor::ReportNormal");
77     if (reporter == nullptr) {
78         HIVIEW_LOGE("reporter is null.");
79         return;
80     }
81     AppStartReportEvent reportEvent = ConstructReportEvent(metrics);
82     reporter->ReportNormal(reportEvent);
83 }
84 
85 
ConstructReportEvent(const AppStartMetrics& metrics)86 AppStartReportEvent AppLaunchMonitor::ConstructReportEvent(const AppStartMetrics& metrics)
87 {
88     AppStartReportEvent re = AppLaunchMonitorConverter::ConvertMetricToReportEvent(metrics);
89     return re;
90 }
91 } // HiviewDFX
92 } // OHOS