1 /*
2  * Copyright (c) 2022-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 #ifndef OHOS_ABILITY_RUNTIME_EVENT_REPORT_H
17 #define OHOS_ABILITY_RUNTIME_EVENT_REPORT_H
18 
19 #include <string>
20 
21 #include "hisysevent.h"
22 
23 using HiSysEventType = OHOS::HiviewDFX::HiSysEvent::EventType;
24 using HiSysEvent = OHOS::HiviewDFX::HiSysEvent;
25 
26 namespace OHOS {
27 namespace AAFwk {
28 struct EventInfo {
29     int32_t pid = -1;
30     int32_t userId = -1;
31     int32_t extensionType = -1;
32     uint32_t versionCode = 0;
33     int32_t errCode = -1;
34     std::string versionName;
35     std::string bundleName;
36     std::string moduleName;
37     std::string abilityName;
38     std::string processName;
39     std::string callerProcessName;
40     std::string callerBundleName;
41     int32_t abilityNumber = 0;
42     int32_t abilityType = -1;
43     int64_t time = 0;
44     int32_t callerUid = -1;
45     int32_t exitResult = -1;
46     int32_t bundleType = -1;
47     int32_t startType = 0;
48     int32_t appUid = -1;
49     int32_t callerState = -1;
50     uint32_t callerVersionCode = -1;
51     std::string callerVersionName;
52     std::string uri;
53     int32_t processType = -1;
54     int32_t callerPid = -1;
55     int64_t duration = 0;
56     int32_t reason = -1;
57     int32_t subReason = -1;
58 };
59 
60 enum class EventName {
61     // fault event
62     START_ABILITY_ERROR = 0,
63     TERMINATE_ABILITY_ERROR,
64     START_EXTENSION_ERROR,
65     STOP_EXTENSION_ERROR,
66     CONNECT_SERVICE_ERROR,
67     DISCONNECT_SERVICE_ERROR,
68 
69     // ability behavior event
70     START_ABILITY,
71     TERMINATE_ABILITY,
72     CLOSE_ABILITY,
73     ABILITY_ONFOREGROUND,
74     ABILITY_ONBACKGROUND,
75     ABILITY_ONACTIVE,
76     ABILITY_ONINACTIVE,
77     START_ABILITY_BY_APP_LINKING,
78 
79     // serviceExtensionAbility behavior event
80     START_SERVICE,
81     STOP_SERVICE,
82     CONNECT_SERVICE,
83     DISCONNECT_SERVICE,
84     START_ABILITY_OTHER_EXTENSION,
85 
86     // app behavior event
87     APP_ATTACH,
88     APP_LAUNCH,
89     APP_FOREGROUND,
90     APP_BACKGROUND,
91     APP_TERMINATE,
92     PROCESS_START,
93     PROCESS_EXIT,
94     DRAWN_COMPLETED,
95     APP_STARTUP_TYPE,
96     PROCESS_START_FAILED,
97 
98     // key behavior event
99     GRANT_URI_PERMISSION,
100     FA_SHOW_ON_LOCK,
101     START_PRIVATE_ABILITY,
102     RESTART_PROCESS_BY_SAME_APP,
103     START_STANDARD_ABILITIES,
104 
105     // atomic service event
106     CREATE_ATOMIC_SERVICE_PROCESS,
107     ATOMIC_SERVICE_DRAWN_COMPLETE,
108 
109     // uri permission
110     SHARE_UNPRIVILEGED_FILE_URI
111 };
112 
113 class EventReport {
114 public:
115     static void SendAppEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
116     static void SendAbilityEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
117     static void SendAtomicServiceEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
118     static void SendExtensionEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
119     static void SendKeyEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
120     static void SendAppLaunchEvent(const EventName &eventName, const EventInfo &eventInfo);
121     static void SendAppForegroundEvent(const EventName &eventName, const EventInfo &eventInfo);
122     static void SendAppBackgroundEvent(const EventName &eventName, const EventInfo &eventInfo);
123     static void SendProcessStartEvent(const EventName &eventName, const EventInfo &eventInfo);
124     static void SendProcessStartFailedEvent(const EventName &eventName, const EventInfo &eventInfo);
125     static void SendProcessExitEvent(const EventName &eventName, const EventInfo &eventInfo);
126     static void SendStartServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
127     static void SendStopServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
128     static void SendConnectServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
129     static void SendDisconnectServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
130     static void SendStartAbilityOtherExtensionEvent(const EventName &eventName, const EventInfo &eventInfo);
131     static void SendGrantUriPermissionEvent(const EventName &eventName, const EventInfo &eventInfo);
132 
133 private:
134     static std::string ConvertEventName(const EventName &eventName);
135     static void LogErrorEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
136     static void LogStartAbilityEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
137     static void LogTerminateAbilityEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
138     static void LogAbilityOnForegroundEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
139     static void LogAbilityOnBackgroundEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
140     static void LogAbilityOnActiveEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
141     static void LogStartStandardEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
142     static void LogStartAbilityByAppLinking(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
143 };
144 }  // namespace AAFWK
145 }  // namespace OHOS
146 #endif  // OHOS_ABILITY_RUNTIME_EVENT_REPORT_H
147