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 "killing_process_manager.h"
17
18 #include <mutex>
19
20 #include "hilog_tag_wrapper.h"
21 #include "task_handler_wrap.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 constexpr int32_t CLEAR_CALLER_KEY_DELAY_TIME = 5 * 1000; // 5s
27 }
GetInstance()28 KillingProcessManager& KillingProcessManager::GetInstance()
29 {
30 static KillingProcessManager instance;
31 return instance;
32 }
33
IsCallerKilling(const std::string & callerKey) const34 bool KillingProcessManager::IsCallerKilling(const std::string& callerKey) const
35 {
36 std::lock_guard<ffrt::mutex> lock(mutex_);
37 return killingCallerKeySet_.find(callerKey) != killingCallerKeySet_.end();
38 }
39
AddKillingCallerKey(const std::string & callerKey)40 void KillingProcessManager::AddKillingCallerKey(const std::string& callerKey)
41 {
42 auto taskHandler = AAFwk::TaskHandlerWrap::GetFfrtHandler();
43 if (taskHandler == nullptr) {
44 TAG_LOGE(AAFwkTag::APPMGR, "handler is null");
45 return;
46 }
47 if (callerKey.empty()) {
48 TAG_LOGE(AAFwkTag::APPMGR, "invalid callerKey");
49 return;
50 }
51 TAG_LOGI(AAFwkTag::APPMGR, "add caller:%{public}s", callerKey.c_str());
52 {
53 std::lock_guard<ffrt::mutex> lock(mutex_);
54 auto ret = killingCallerKeySet_.insert(callerKey);
55 if (!ret.second) {
56 TAG_LOGI(AAFwkTag::APPMGR, "already inserted");
57 return;
58 }
59 }
60 auto task = [callerKey] () {
61 KillingProcessManager::GetInstance().RemoveKillingCallerKey(callerKey);
62 };
63 taskHandler->SubmitTaskJust(task, "clearCallerKey", CLEAR_CALLER_KEY_DELAY_TIME);
64 }
65
RemoveKillingCallerKey(const std::string & callerKey)66 void KillingProcessManager::RemoveKillingCallerKey(const std::string& callerKey)
67 {
68 if (callerKey.empty()) {
69 TAG_LOGE(AAFwkTag::APPMGR, "invalid callerKey");
70 return;
71 }
72 TAG_LOGI(AAFwkTag::APPMGR, "remove caller:%{public}s", callerKey.c_str());
73 std::lock_guard<ffrt::mutex> lock(mutex_);
74 killingCallerKeySet_.erase(callerKey);
75 }
76 } // namespace AppExecFwk
77 } // namespace OHOS