1 /*
2  * Copyright (c) 2021 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 _AMS_ST_PAGE_ABILITY_EVENT_H_
16 #define _AMS_ST_PAGE_ABILITY_EVENT_H_
17 
18 #include <vector>
19 #include <string>
20 #include <memory>
21 #include "common_event.h"
22 #include "common_event_manager.h"
23 #include "ability_loader.h"
24 #include "app_log_wrapper.h"
25 #include "running_process_info.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace STEventName {
30 const std::string g_eventName = "resp_st_page_ability_callback";
31 const std::string g_eventNameProcessMemory = "resp_st_process_memory_info";
32 const std::string g_pidEventName = "resp_st_page_ability_pid_callback";
33 const std::string g_abilityStateInit = ":Init";
34 const std::string g_abilityStateOnStart = ":OnStart";
35 const std::string g_abilityStateOnStop = ":OnStop";
36 const std::string g_abilityStateOnActive = ":OnActive";
37 const std::string g_abilityStateOnInactive = ":OnInactive";
38 const std::string g_abilityStateOnBackground = ":OnBackground";
39 const std::string g_abilityStateOnForeground = ":OnForeground";
40 const std::string g_abilityStateOnNewWant = ":OnNewWant";
41 const std::string g_abilityStateOnCommand = ":OnCommand";
42 const std::string g_abilityStateOnConnect = ":OnConnect";
43 const std::string g_abilityStateOnDisconnect = ":OnDisconnect";
44 const std::string g_abilityStateInsert = ":Insert";
45 const std::string g_abilityStateDelete = ":Delete";
46 const std::string g_abilityStateUpdate = ":Update";
47 const std::string g_abilityStateQuery = ":Query";
48 const std::string g_abilityStateGetFileTypes = ":GetFileTypes";
49 const std::string g_abilityStateOpenFile = ":OpenFile";
50 const std::string g_getWantAgentState = ":GetWantAgentFail";
51 const std::string g_triggerWantAgentState = ":TriggerWantAgentSuccess";
52 const int g_defeventCode = 0;
53 const std::vector<std::string> g_eventList = {
54     "requ_page_ability_terminate",
55     "requ_get_process_memory_info",
56     "requ_disconnect_service",
57     "requ_page_ability_terminate_caller",
58     "requ_page_ability_terminate_result",
59 };
60 
61 static constexpr uint32_t LOAD_TIMEOUT = 3000;            // ms
62 static constexpr uint32_t ACTIVE_TIMEOUT = 5000;          // ms
63 static constexpr uint32_t INACTIVE_TIMEOUT = 500;         // ms
64 static constexpr uint32_t BACKGROUND_TIMEOUT = 10000;     // ms
65 static constexpr uint32_t TERMINATE_TIMEOUT = 10000;      // ms
66 static constexpr uint32_t CONNECT_TIMEOUT = 500;          // ms
67 static constexpr uint32_t DISCONNECT_TIMEOUT = 500;       // ms
68 static constexpr uint32_t COMMAND_TIMEOUT = 5000;         // ms
69 static constexpr uint32_t SYSTEM_UI_TIMEOUT = 5000;       // ms
70 static constexpr uint32_t RESTART_TIMEOUT = 5000;         // ms
71 static constexpr uint32_t RESTART_ABILITY_TIMEOUT = 500;  // ms
72 }  // namespace STEventName
73 
74 class STPageAbilityEventSubscriber : public EventFwk::CommonEventSubscriber {
75 public:
STPageAbilityEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp,std::shared_ptr<Ability> ability,sptr<AAFwk::IAbilityConnection> & stub)76     STPageAbilityEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp, std::shared_ptr<Ability> ability,
77         sptr<AAFwk::IAbilityConnection> &stub)
78         : CommonEventSubscriber(sp), ability_(ability), stub_(stub)
79     {}
~STPageAbilityEventSubscriber()80     ~STPageAbilityEventSubscriber()
81     {}
82     virtual void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
83     std::string RunningProcessInfoToString(std::vector<RunningProcessInfo> &infos);
84     bool PublishEvent(const std::string &eventName, const int &code, const std::string &data);
85 
86 private:
87     std::weak_ptr<Ability> ability_;
88     wptr<AAFwk::IAbilityConnection> stub_;
89 };
90 
91 class STPageAbilityEvent {
92 public:
93     STPageAbilityEvent() = default;
94     explicit STPageAbilityEvent(const std::string &className);
95     ~STPageAbilityEvent() = default;
96 
97     static bool PublishEvent(const std::string &eventName, const int &code, const std::string &data);
98     void SubscribeEvent(std::vector<std::string> eventList, std::shared_ptr<Ability> ability,
99         sptr<AAFwk::IAbilityConnection> stub = nullptr);
100     void UnsubscribeEvent();
101     std::string GetEventDate(const std::string &stateCallbackCount);
102     std::string GetCallBackPath(const std::string &callBackPath);
103     std::string GetAbilityStatus(const std::string &abilityStatus);
104 
105     int GetOnStartCount();
106     int GetOnStopCount();
107     int GetOnActiveCount();
108     int GetOnInactiveCount();
109     int GetOnBackgroundCount();
110     int GetOnForegroundCount();
111     int GetOnNewWantCount();
112     int GetOnCommandCount();
113     int GetOnConnectCount();
114     int GetOnDisconnectCount();
115 
116 private:
117     std::shared_ptr<STPageAbilityEventSubscriber> subscriber_;
118     int onStartCount_ = 0;
119     int onStopCount_ = 0;
120     int onActiveCount_ = 0;
121     int onInactiveCount_ = 0;
122     int onBackgroundCount_ = 0;
123     int onForegroundCount_ = 0;
124     int onNewWantCount_ = 0;
125     int onCommandCount_ = 0;
126     int onConnectCount_ = 0;
127     int onDisconnectCount_ = 0;
128     std::string className_;
129     std::string callBackPath_;
130     std::string abilityStatus_;
131 };
132 }  // namespace AppExecFwk
133 }  // namespace OHOS
134 #endif  // _AMS_ST_PAGE_ABILITY_EVENT_H_