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(const RestartAppKeyType & key,time_t time)31 bool RestartAppManager::IsRestartAppFrequent(const RestartAppKeyType &key, 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(key);
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(const RestartAppKeyType & key,time_t time)43 void RestartAppManager::AddRestartAppHistory(const RestartAppKeyType &key, time_t time)
44 {
45 std::lock_guard<ffrt::mutex> lock(restartAppMapLock_);
46 TAG_LOGD(AAFwkTag::ABILITYMGR, "Refresh uid=%{public}d, instanceKey:%{public}s", key.uid, key.instanceKey.c_str());
47 restartAppHistory_[key] = time;
48 }
49 } // namespace AAFwk
50 } // namespace OHOS
51