1 /*
2  * Copyright (c) 2022-2023 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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_AGING_REQUEST_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_AGING_REQUEST_H
18 
19 #include <mutex>
20 #include <vector>
21 
22 #include "aging_bundle_info.h"
23 #include "aging_util.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 enum class AgingCleanType {
28     CLEAN_CACHE = 0,
29     CLEAN_OTHERS,
30 };
31 
32 class AgingRequest {
33 public:
34     AgingRequest();
35     bool IsReachStartAgingThreshold() const;
36     bool IsReachEndAgingThreshold() const;
37     size_t SortAgingBundles();
38     void ResetRequest();
39     void Dump();
40     void AddAgingBundle(AgingBundleInfo &bundleInfo);
41 
GetAgingBundles()42     const std::vector<AgingBundleInfo> GetAgingBundles() const
43     {
44         std::lock_guard<std::mutex> lock(mutex_);
45         return agingBundles_;
46     };
47 
UpdateTotalDataBytesAfterUninstalled(const int64_t dataBytes)48     void UpdateTotalDataBytesAfterUninstalled(const int64_t dataBytes)
49     {
50         totalDataBytes_ -= dataBytes;
51     };
52 
GetTotalDataBytes()53     int64_t GetTotalDataBytes() const
54     {
55         return totalDataBytes_;
56     };
57 
SetTotalDataBytes(const int64_t allBundleDataBytes)58     void SetTotalDataBytes(const int64_t allBundleDataBytes)
59     {
60         totalDataBytes_ = allBundleDataBytes;
61     };
62 
SetAgingCleanType(const AgingCleanType agingCleanType)63     void SetAgingCleanType(const AgingCleanType agingCleanType)
64     {
65         agingCleanType_ = agingCleanType;
66     };
67 
GetAgingCleanType()68     AgingCleanType GetAgingCleanType() const
69     {
70         return agingCleanType_;
71     };
72 
GetTotalDataBytesThreshold()73     static int64_t GetTotalDataBytesThreshold()
74     {
75         return totalDataBytesThreshold_;
76     };
77 
GetOneDayTimeMs()78     static int64_t GetOneDayTimeMs()
79     {
80         return oneDayTimeMs_;
81     };
82 
83 private:
84     void InitAgingPolicySystemParameters();
85     void InitAgingDatasizeThreshold();
86     void InitAgingOneDayTimeMs();
87 
88     mutable std::mutex mutex_;
89     std::vector<AgingBundleInfo> agingBundles_;
90     int64_t totalDataBytes_ = 0;
91     AgingCleanType agingCleanType_ = AgingCleanType::CLEAN_CACHE;
92 
93     static int64_t totalDataBytesThreshold_;
94     static int64_t oneDayTimeMs_;
95 };
96 }  //  namespace AppExecFwk
97 }  //  namespace OHOS
98 
99 #endif  //  FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_AGING_REQUEST_H
100