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