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 #ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_PKG_DELAY_SUSPEND_INFO_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_PKG_DELAY_SUSPEND_INFO_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "bgtask_common.h"
23 #include "delay_suspend_info_ex.h"
24 #include "timer_manager.h"
25 
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
28 using std::shared_ptr;
29 using std::vector;
30 using std::string;
31 
32 class PkgDelaySuspendInfo {
33 public:
PkgDelaySuspendInfo(const string & pkg,const int32_t & uid,const shared_ptr<TimerManager> & timerManager)34     PkgDelaySuspendInfo(const string& pkg, const int32_t& uid, const shared_ptr<TimerManager>& timerManager)
35         : pkg_(pkg), uid_(uid), timerManager_(timerManager) {}
36     ~PkgDelaySuspendInfo() = default;
37     ErrCode IsAllowRequest();
38     void AddRequest(const shared_ptr<DelaySuspendInfoEx>& delayInfo, const int32_t delayTime,
39         const bool needSetTime = false);
40     void RemoveRequest(const int32_t requestId);
41     int32_t GetRemainDelayTime(const int32_t requestId);
42     void StartAccounting(const int32_t requestId = -1);
43     void StopAccounting(const int32_t requestId);
44     void StopAccountingAll();
45     void UpdateQuota(bool reset = false);
46 
GetPkg()47     inline const string& GetPkg() const
48     {
49         return pkg_;
50     }
51 
GetUid()52     inline int32_t GetUid() const
53     {
54         return uid_;
55     }
56 
IsRequestEmpty()57     inline bool IsRequestEmpty() const
58     {
59         return requestList_.empty();
60     }
61 
GetRequestSize()62     inline size_t GetRequestSize() const
63     {
64         return requestList_.size();
65     }
66 
GetQuota()67     inline int32_t GetQuota() const
68     {
69         return quota_;
70     }
71 
72 private:
73     int32_t GetModifiedTime();
74 
75 private:
76     string pkg_ {""};
77     int32_t uid_ {-1};
78     int32_t quota_ {INIT_QUOTA};
79     int32_t spendTime_ {0};
80     int32_t baseTime_ {0};
81     bool isCounting_ {false};
82     shared_ptr<TimerManager> timerManager_ {nullptr};
83     vector<shared_ptr<DelaySuspendInfoEx>> requestList_;
84 };
85 }  // namespace BackgroundTaskMgr
86 }  // namespace OHOS
87 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_PKG_DELAY_SUSPEND_INFO_H