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