1 /*
2 * Copyright (c) 2024 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 #include "restart_app_manager.h"
17
18 #include "app_scheduler.h"
19 #include "hilog_tag_wrapper.h"
20 #include "ipc_skeleton.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24 using OHOS::AppExecFwk::AppProcessState;
GetInstance()25 RestartAppManager &RestartAppManager::GetInstance()
26 {
27 static RestartAppManager instance;
28 return instance;
29 }
30
IsRestartAppFrequent(int32_t uid,time_t time)31 bool RestartAppManager::IsRestartAppFrequent(int32_t uid, time_t time)
32 {
33 std::lock_guard<ffrt::mutex> lock(restartAppMapLock_);
34 constexpr int64_t MIN_RESTART_TIME = 3;
35 auto it = restartAppHistory_.find(uid);
36 if ((it != restartAppHistory_.end()) && (it->second + MIN_RESTART_TIME > time)) {
37 TAG_LOGE(AAFwkTag::ABILITYMGR, "Restart too frequently. Try again at least 3s later.");
38 return true;
39 }
40 return false;
41 }
42
AddRestartAppHistory(int32_t uid,time_t time)43 void RestartAppManager::AddRestartAppHistory(int32_t uid, time_t time)
44 {
45 std::lock_guard<ffrt::mutex> lock(restartAppMapLock_);
46 TAG_LOGD(AAFwkTag::ABILITYMGR, "Refresh history, uid=%{public}d", uid);
47 restartAppHistory_[uid] = time;
48 }
49
IsForegroundToRestartApp() const50 bool RestartAppManager::IsForegroundToRestartApp() const
51 {
52 TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
53 auto callerPid = IPCSkeleton::GetCallingPid();
54 AppExecFwk::RunningProcessInfo processInfo;
55 DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(callerPid, processInfo);
56 if (processInfo.isFocused || processInfo.isAbilityForegrounding) {
57 return true;
58 }
59 TAG_LOGE(AAFwkTag::ABILITYMGR, "IsForegroundToRestartApp, app stae is not foreground.");
60 return false;
61 }
62 } // namespace AAFwk
63 } // namespace OHOS
64