1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "restart_app_manager.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include "app_scheduler.h" 19eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h" 20eace7efcSopenharmony_ci#include "ipc_skeleton.h" 21eace7efcSopenharmony_ci 22eace7efcSopenharmony_cinamespace OHOS { 23eace7efcSopenharmony_cinamespace AAFwk { 24eace7efcSopenharmony_ciusing OHOS::AppExecFwk::AppProcessState; 25eace7efcSopenharmony_ciRestartAppManager &RestartAppManager::GetInstance() 26eace7efcSopenharmony_ci{ 27eace7efcSopenharmony_ci static RestartAppManager instance; 28eace7efcSopenharmony_ci return instance; 29eace7efcSopenharmony_ci} 30eace7efcSopenharmony_ci 31eace7efcSopenharmony_cibool RestartAppManager::IsRestartAppFrequent(int32_t uid, time_t time) 32eace7efcSopenharmony_ci{ 33eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(restartAppMapLock_); 34eace7efcSopenharmony_ci constexpr int64_t MIN_RESTART_TIME = 10; 35eace7efcSopenharmony_ci auto it = restartAppHistory_.find(uid); 36eace7efcSopenharmony_ci if ((it != restartAppHistory_.end()) && (it->second + MIN_RESTART_TIME > time)) { 37eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "restart too frequently. try again at least 10s later"); 38eace7efcSopenharmony_ci return true; 39eace7efcSopenharmony_ci } 40eace7efcSopenharmony_ci return false; 41eace7efcSopenharmony_ci} 42eace7efcSopenharmony_ci 43eace7efcSopenharmony_civoid RestartAppManager::AddRestartAppHistory(int32_t uid, time_t time) 44eace7efcSopenharmony_ci{ 45eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(restartAppMapLock_); 46eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "Refresh history, uid=%{public}d", uid); 47eace7efcSopenharmony_ci restartAppHistory_[uid] = time; 48eace7efcSopenharmony_ci} 49eace7efcSopenharmony_ci 50eace7efcSopenharmony_cibool RestartAppManager::IsForegroundToRestartApp() const 51eace7efcSopenharmony_ci{ 52eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); 53eace7efcSopenharmony_ci auto callerPid = IPCSkeleton::GetCallingPid(); 54eace7efcSopenharmony_ci AppExecFwk::RunningProcessInfo processInfo; 55eace7efcSopenharmony_ci DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(callerPid, processInfo); 56eace7efcSopenharmony_ci if (processInfo.isFocused || processInfo.isAbilityForegrounding) { 57eace7efcSopenharmony_ci return true; 58eace7efcSopenharmony_ci } 59eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "IsForegroundToRestartApp, state not foreground"); 60eace7efcSopenharmony_ci return false; 61eace7efcSopenharmony_ci} 62eace7efcSopenharmony_ci} // namespace AAFwk 63eace7efcSopenharmony_ci} // namespace OHOS 64