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     bool isPreload = false;
59     int32_t preloadMode = 0;
60 };
61 
62 enum class EventName {
63     // fault event
64     START_ABILITY_ERROR = 0,
65     TERMINATE_ABILITY_ERROR,
66     START_EXTENSION_ERROR,
67     STOP_EXTENSION_ERROR,
68     CONNECT_SERVICE_ERROR,
69     DISCONNECT_SERVICE_ERROR,
70 
71     // ability behavior event
72     START_ABILITY,
73     TERMINATE_ABILITY,
74     CLOSE_ABILITY,
75     ABILITY_ONFOREGROUND,
76     ABILITY_ONBACKGROUND,
77     ABILITY_ONACTIVE,
78     ABILITY_ONINACTIVE,
79     START_ABILITY_BY_APP_LINKING,
80 
81     // serviceExtensionAbility behavior event
82     START_SERVICE,
83     STOP_SERVICE,
84     CONNECT_SERVICE,
85     DISCONNECT_SERVICE,
86     START_ABILITY_OTHER_EXTENSION,
87 
88     // app behavior event
89     APP_ATTACH,
90     APP_LAUNCH,
91     APP_FOREGROUND,
92     APP_BACKGROUND,
93     APP_TERMINATE,
94     PROCESS_START,
95     PROCESS_EXIT,
96     DRAWN_COMPLETED,
97     APP_STARTUP_TYPE,
98     PROCESS_START_FAILED,
99 
100     // key behavior event
101     GRANT_URI_PERMISSION,
102     FA_SHOW_ON_LOCK,
103     START_PRIVATE_ABILITY,
104     RESTART_PROCESS_BY_SAME_APP,
105     START_STANDARD_ABILITIES,
106 
107     // atomic service event
108     CREATE_ATOMIC_SERVICE_PROCESS,
109     ATOMIC_SERVICE_DRAWN_COMPLETE,
110 
111     // uri permission
112     SHARE_UNPRIVILEGED_FILE_URI
113 };
114 
115 class EventReport {
116 public:
117     static void SendAppEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
118     static void SendAbilityEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
119     static void SendAtomicServiceEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
120     static void SendExtensionEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
121     static void SendKeyEvent(const EventName &eventName, HiSysEventType type, const EventInfo &eventInfo);
122     static void SendAppLaunchEvent(const EventName &eventName, const EventInfo &eventInfo);
123     static void SendAppForegroundEvent(const EventName &eventName, const EventInfo &eventInfo);
124     static void SendAppBackgroundEvent(const EventName &eventName, const EventInfo &eventInfo);
125     static void SendProcessStartEvent(const EventName &eventName, const EventInfo &eventInfo);
126     static void SendProcessStartFailedEvent(const EventName &eventName, const EventInfo &eventInfo);
127     static void SendProcessExitEvent(const EventName &eventName, const EventInfo &eventInfo);
128     static void SendStartServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
129     static void SendStopServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
130     static void SendConnectServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
131     static void SendDisconnectServiceEvent(const EventName &eventName, const EventInfo &eventInfo);
132     static void SendStartAbilityOtherExtensionEvent(const EventName &eventName, const EventInfo &eventInfo);
133     static void SendGrantUriPermissionEvent(const EventName &eventName, const EventInfo &eventInfo);
134 
135 private:
136     static std::string ConvertEventName(const EventName &eventName);
137     static void LogErrorEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
138     static void LogStartAbilityEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
139     static void LogTerminateAbilityEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
140     static void LogAbilityOnForegroundEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
141     static void LogAbilityOnBackgroundEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
142     static void LogAbilityOnActiveEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
143     static void LogStartStandardEvent(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
144     static void LogStartAbilityByAppLinking(const std::string &name, HiSysEventType type, const EventInfo &eventInfo);
145 };
146 }  // namespace AAFWK
147 }  // namespace OHOS
148 #endif  // OHOS_ABILITY_RUNTIME_EVENT_REPORT_H
149