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
27namespace OHOS {
28namespace AppExecFwk {
29namespace STEventName {
30const std::string g_eventName = "resp_st_page_ability_callback";
31const std::string g_eventNameProcessMemory = "resp_st_process_memory_info";
32const std::string g_pidEventName = "resp_st_page_ability_pid_callback";
33const std::string g_abilityStateInit = ":Init";
34const std::string g_abilityStateOnStart = ":OnStart";
35const std::string g_abilityStateOnStop = ":OnStop";
36const std::string g_abilityStateOnActive = ":OnActive";
37const std::string g_abilityStateOnInactive = ":OnInactive";
38const std::string g_abilityStateOnBackground = ":OnBackground";
39const std::string g_abilityStateOnForeground = ":OnForeground";
40const std::string g_abilityStateOnNewWant = ":OnNewWant";
41const std::string g_abilityStateOnCommand = ":OnCommand";
42const std::string g_abilityStateOnConnect = ":OnConnect";
43const std::string g_abilityStateOnDisconnect = ":OnDisconnect";
44const std::string g_abilityStateInsert = ":Insert";
45const std::string g_abilityStateDelete = ":Delete";
46const std::string g_abilityStateUpdate = ":Update";
47const std::string g_abilityStateQuery = ":Query";
48const std::string g_abilityStateGetFileTypes = ":GetFileTypes";
49const std::string g_abilityStateOpenFile = ":OpenFile";
50const std::string g_getWantAgentState = ":GetWantAgentFail";
51const std::string g_triggerWantAgentState = ":TriggerWantAgentSuccess";
52const int g_defeventCode = 0;
53const 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
61static constexpr uint32_t LOAD_TIMEOUT = 3000;            // ms
62static constexpr uint32_t ACTIVE_TIMEOUT = 5000;          // ms
63static constexpr uint32_t INACTIVE_TIMEOUT = 500;         // ms
64static constexpr uint32_t BACKGROUND_TIMEOUT = 10000;     // ms
65static constexpr uint32_t TERMINATE_TIMEOUT = 10000;      // ms
66static constexpr uint32_t CONNECT_TIMEOUT = 500;          // ms
67static constexpr uint32_t DISCONNECT_TIMEOUT = 500;       // ms
68static constexpr uint32_t COMMAND_TIMEOUT = 5000;         // ms
69static constexpr uint32_t SYSTEM_UI_TIMEOUT = 5000;       // ms
70static constexpr uint32_t RESTART_TIMEOUT = 5000;         // ms
71static constexpr uint32_t RESTART_ABILITY_TIMEOUT = 500;  // ms
72}  // namespace STEventName
73
74class STPageAbilityEventSubscriber : public EventFwk::CommonEventSubscriber {
75public:
76    STPageAbilityEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp, std::shared_ptr<Ability> ability,
77        sptr<AAFwk::IAbilityConnection> &stub)
78        : CommonEventSubscriber(sp), ability_(ability), stub_(stub)
79    {}
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
86private:
87    std::weak_ptr<Ability> ability_;
88    wptr<AAFwk::IAbilityConnection> stub_;
89};
90
91class STPageAbilityEvent {
92public:
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
116private:
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_