1 /*
2  * Copyright (c) 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 #include "aging/aging_handler_chain.h"
17 #include "app_log_wrapper.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
21 namespace {
22 const std::vector<AgingCleanType> SUPPORT_AGING_CLEAN_TYPE = {
23     AgingCleanType::CLEAN_CACHE,
24     AgingCleanType::CLEAN_OTHERS,
25 };
26 }
27 
AgingHandlerChain()28 AgingHandlerChain::AgingHandlerChain() {}
29 
~AgingHandlerChain()30 AgingHandlerChain::~AgingHandlerChain()
31 {
32     handlers_.clear();
33 }
34 
AddHandler(const std::shared_ptr<AgingHandler> & handler)35 void AgingHandlerChain::AddHandler(const std::shared_ptr<AgingHandler> &handler)
36 {
37     if (handler == nullptr) {
38         APP_LOGE("agingHandler: invalid handler");
39         return;
40     }
41 
42     handlers_.emplace_back(handler);
43 }
44 
Process(AgingRequest & request) const45 bool AgingHandlerChain::Process(AgingRequest &request) const
46 {
47     if (!request.IsReachStartAgingThreshold()) {
48         APP_LOGI("Not reach agingThreshold and not need aging");
49         return true;
50     }
51 
52     bool isPassed = false;
53     for (const auto &agingCleanType : SUPPORT_AGING_CLEAN_TYPE) {
54         request.SetAgingCleanType(agingCleanType);
55         isPassed = InnerProcess(request);
56         if (isPassed) {
57             break;
58         }
59     }
60 
61     APP_LOGD("agingHandler: aging handler chain process done");
62     return isPassed;
63 }
64 
InnerProcess(AgingRequest & request) const65 bool AgingHandlerChain::InnerProcess(AgingRequest &request) const
66 {
67     bool isPassed = false;
68     for (auto handler : handlers_) {
69         isPassed = !handler->Process(request);
70         APP_LOGD("agingHandler: passed: %{public}d, type: %{public}d",
71             isPassed, static_cast<AgingCleanType>(request.GetAgingCleanType()));
72         if (isPassed) {
73             break;
74         }
75     }
76 
77     return isPassed;
78 }
79 }  //  namespace AppExecFwk
80 }  //  namespace OHOS