1 /* 2 * Copyright (c) 2021-2022 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_APP_MGR_SERVICE_EVENT_HANDLER_H 17 #define OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_EVENT_HANDLER_H 18 19 #include <list> 20 #include <mutex> 21 22 #include "event_handler_wrap.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 class AppMgrServiceInner; 27 28 class AMSEventHandler : public AAFwk::EventHandlerWrap { 29 public: 30 AMSEventHandler(const std::shared_ptr<AAFwk::TaskHandlerWrap> &taskHandler, 31 const std::weak_ptr<AppMgrServiceInner> &appMgr); 32 virtual ~AMSEventHandler() override; 33 34 virtual void ProcessEvent(const AAFwk::EventWrap &event) override; 35 36 static constexpr uint32_t TERMINATE_ABILITY_TIMEOUT_MSG = 0; 37 static constexpr uint32_t TERMINATE_APPLICATION_TIMEOUT_MSG = 1; 38 static constexpr uint32_t ADD_ABILITY_STAGE_INFO_TIMEOUT_MSG = 2; 39 static constexpr uint32_t START_SPECIFIED_ABILITY_TIMEOUT_MSG = 3; 40 static constexpr uint32_t START_PROCESS_SPECIFIED_ABILITY_TIMEOUT_MSG = 4; 41 static constexpr uint32_t START_SPECIFIED_PROCESS_TIMEOUT_MSG = 5; 42 43 static constexpr uint32_t TERMINATE_ABILITY_HALF_TIMEOUT_MSG = 6; 44 static constexpr uint32_t TERMINATE_APPLICATION_HALF_TIMEOUT_MSG = 7; 45 static constexpr uint32_t ADD_ABILITY_STAGE_INFO_HALF_TIMEOUT_MSG = 8; 46 static constexpr uint32_t START_SPECIFIED_ABILITY_HALF_TIMEOUT_MSG = 9; 47 static constexpr uint32_t START_PROCESS_SPECIFIED_ABILITY_HALF_TIMEOUT_MSG = 10; 48 static constexpr uint32_t START_SPECIFIED_PROCESS_HALF_TIMEOUT_MSG = 11; 49 #ifdef SUPPORT_ASAN 50 static constexpr uint32_t TERMINATE_ABILITY_TIMEOUT = 45000; // ms 51 static constexpr uint32_t TERMINATE_APPLICATION_TIMEOUT = 150000; // ms 52 static constexpr uint32_t BACKGROUND_APPLICATION_TIMEOUT = 45000; // ms 53 static constexpr uint32_t ADD_ABILITY_STAGE_INFO_TIMEOUT = 45000; // ms 54 static constexpr uint32_t ADD_ABILITY_STAGE_EMPTY_RESIDENT_TIMEOUT = 100000; // ms 55 static constexpr uint32_t START_SPECIFIED_ABILITY_TIMEOUT = 45000; // ms 56 static constexpr uint32_t START_PROCESS_SPECIFIED_ABILITY_TIMEOUT = 75000; // ms 57 static constexpr uint32_t START_SPECIFIED_PROCESS_TIMEOUT = 45000; // ms 58 static constexpr uint32_t KILL_PROCESS_TIMEOUT = 45000; // ms 59 #else 60 static constexpr uint32_t TERMINATE_ABILITY_TIMEOUT = 3000; // ms 61 static constexpr uint32_t TERMINATE_APPLICATION_TIMEOUT = 10000; // ms 62 static constexpr uint32_t BACKGROUND_APPLICATION_TIMEOUT = 3000; // ms 63 static constexpr uint32_t ADD_ABILITY_STAGE_INFO_TIMEOUT = 3000; // ms 64 static constexpr uint32_t ADD_ABILITY_STAGE_EMPTY_RESIDENT_TIMEOUT = 20000; // ms 65 static constexpr uint32_t START_SPECIFIED_ABILITY_TIMEOUT = 3000; // ms 66 static constexpr uint32_t START_PROCESS_SPECIFIED_ABILITY_TIMEOUT = 5000; // ms 67 static constexpr uint32_t START_SPECIFIED_PROCESS_TIMEOUT = 2000; // ms 68 static constexpr uint32_t KILL_PROCESS_TIMEOUT = 3000; // ms 69 #endif 70 static constexpr uint32_t DELAY_KILL_PROCESS_TIMEOUT = 3000; // ms 71 static constexpr uint32_t DELAY_KILL_EXTENSION_PROCESS_TIMEOUT = 500; // ms 72 static constexpr uint32_t DELAY_NOTIFY_PROCESS_CACHED_STATE = 2000; // ms 73 static constexpr uint32_t DELAY_CHECK_ALL_PROCESSES_EXITED = 5000; // ms 74 private: 75 std::weak_ptr<AppMgrServiceInner> appMgr_; 76 }; 77 78 class AppRunningRecord; 79 struct AppEventData { 80 uint32_t eventId = 0; 81 int64_t param = 0; 82 std::weak_ptr<AppRunningRecord> appRecord; 83 AppEventDataAppEventData84 AppEventData(uint32_t eventId, int64_t param, std::shared_ptr<AppRunningRecord> appRecord) 85 : eventId(eventId), param(param), appRecord(appRecord) {} 86 }; 87 88 class AppEventUtil { 89 public: 90 static AppEventUtil &GetInstance(); 91 92 AppEventUtil() = default; 93 AppEventUtil(AppEventUtil &) = delete; 94 void operator=(AppEventUtil &) = delete; 95 96 void AddEvent(std::shared_ptr<AppRunningRecord> appRecord, uint32_t eventId, int64_t param); 97 bool HasEvent(std::shared_ptr<AppRunningRecord> appRecord, uint32_t eventId); 98 std::shared_ptr<AppRunningRecord> RemoveEvent(uint32_t eventId, int64_t param); 99 std::list<AppEventData> RemoveEvent(std::shared_ptr<AppRunningRecord> appRecord, uint32_t eventId); 100 private: 101 std::mutex appEventListMutex_; 102 std::list<AppEventData> appEventList_; 103 }; 104 } // namespace AppExecFwk 105 } // namespace OHOS 106 #endif // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_EVENT_HANDLER_H 107