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 #ifndef APP_LAUNCH_SCENE_DATA_PROCESSOR_H
16 #define APP_LAUNCH_SCENE_DATA_PROCESSOR_H
17 
18 #include <string>
19 #include <map>
20 #include "IAppLaunchSceneDataProcessor.h"
21 #include "IAppLaunchSceneDb.h"
22 #include "SceneDataMapDbAdapter.h"
23 #include "ITimeoutExecutor.h"
24 #include "IAppTimer.h"
25 
26 using AppStartRecord = IAppLaunchSceneDb::AppStartRecord;
27 using InteractionResponse = IAppLaunchSceneDb::InteractionResponse;
28 using StartAbility = IAppLaunchSceneDb::StartAbility;
29 using AppStartupType = IAppLaunchSceneDb::AppStartupType;
30 using ProcessStart = IAppLaunchSceneDb::ProcessStart;
31 using AppAttach = IAppLaunchSceneDb::AppAttach;
32 using AppForeground = IAppLaunchSceneDb::AppForeground;
33 using AbilityForeground = IAppLaunchSceneDb::AbilityForeground;
34 using StartWindow = IAppLaunchSceneDb::StartWindow;
35 using DrawnCompleted = IAppLaunchSceneDb::DrawnCompleted;
36 using FirstFrameDrawn = IAppLaunchSceneDb::FirstFrameDrawn;
37 using InteractionCompleted = IAppLaunchSceneDb::InteractionCompleted;
38 using AppStartMetrics = IAppLaunchSceneDataProcessor::AppStartMetrics;
39 using ITimeoutHandler = ITimeoutExecutor::ITimeoutHandler;
40 
41 class AppLaunchSceneDataProcessor : public IAppLaunchSceneDataProcessor, public IAppTimer::ICb, public ITimeoutHandler,
42         public SceneDataMapDbAdapter {
43 public:
44     AppLaunchSceneDataProcessor(IAppLaunchSceneDb* db, ITimeoutExecutor* exec,
45                                IAppLaunchSceneDataProcessor::MetricReporter* metricReporter,
46                                IAppTimer* timer);
47     AppLaunchSceneDataProcessor(IAppLaunchSceneDb* db, ITimeoutExecutor* exec, IAppTimer* timer);
48     void ProcessSceneData(const AppStartCheckPointData& data) override;
49     void SetMetricReporter(MetricReporter* metricReporter) override;
50     void Expired(std::string key) override;
51     void HandleTimeoutInMainThr(std::string name) override;
52 private:
53     IAppLaunchSceneDataProcessor::MetricReporter* metricReporter;
54     IAppLaunchSceneDb* db;
55     ITimeoutExecutor* exec;
56     IAppTimer* timer;
57 
58     bool CheckValidCheckPoint(const AppStartCheckPointData& data);
59     void ValidateDuplication(const AppStartCheckPointData& data);
60     void SaveCheckPoint(const std::string& bundleName, const AppStartCheckPointData& data);
61     void SaveCheckPoint(AppStartRecord& record, const AppStartCheckPointData& data);
62     bool IsStartPoint(const AppStartCheckPointData& data);
63     bool AllPointsReceived(const std::string& bundleName);
64     AppStartMetrics CalcMetrics(const std::string& bundleName);
65     void Report(const AppStartMetrics& metrics);
66     bool ReportConditionMet(const std::string& bundleName);
67     void CreateRecord(const AppStartCheckPointData& data);
68     void CreateRecord(const std::string& bundleName, const AppStartCheckPointData& data);
69     AppStartRecord GetRecord(const std::string& bundleName);
70     void DeleteRecord(const std::string& bundleName);
71     bool RecordExist(const std::string& bundleName);
72     void StartTimer(std::string bundle);
73     void StopTimer(std::string bundle);
74     bool HaveStartPoint(const AppStartRecord& record);
75     bool HaveAllEndPoints(const AppStartRecord& record);
76     bool HaveEndPoint(const AppStartRecord& record);
77     bool HaveResponseOrCompletedPoint(const AppStartRecord& record);
78     std::string GetProcessName(const AppStartRecord& record);
79     int32_t GetAppPid(const AppStartRecord& record);
80     uint64_t GetInputTime(const AppStartRecord& record);
81     std::string GetBundleName(const AppStartCheckPointData& data);
82     // The second application starts settlement points that already exist.
83     void CheckOutExistStartPoint(const AppStartCheckPointData& data);
84     void CalcLatency(AppStartMetrics& appStartMetrics, const AppStartRecord& record, const uint64_t inputTime);
85 };
86 #endif