• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pkg_delay_suspend_info.h"
17 
18 #include <sstream>
19 
20 #include "transient_task_log.h"
21 #include "time_provider.h"
22 #include "bgtaskmgr_inner_errors.h"
23 #include "errors.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace BackgroundTaskMgr {
29 namespace {
30     constexpr int32_t MAX_REQUEST_ID = 3;
31     constexpr int32_t MIN_ALLOW_QUOTA_TIME = 10 * MSEC_PER_SEC; // 10s
32 }
33 
IsAllowRequest()34 ErrCode PkgDelaySuspendInfo::IsAllowRequest()
35 {
36     if (requestList_.size() >= MAX_REQUEST_ID) {
37         return ERR_BGTASK_EXCEEDS_THRESHOLD;
38     }
39 
40     UpdateQuota();
41     if (quota_ >= MIN_ALLOW_QUOTA_TIME) {
42         return ERR_OK;
43     }
44 
45     return ERR_BGTASK_TIME_INSUFFICIENT;
46 }
47 
AddRequest(const shared_ptr<DelaySuspendInfoEx> & delayInfo,const int32_t delayTime)48 void PkgDelaySuspendInfo::AddRequest(const shared_ptr<DelaySuspendInfoEx>& delayInfo,
49     const int32_t delayTime)
50 {
51     if (delayInfo == nullptr) {
52         BGTASK_LOGE("Invalid delayInfo");
53         return;
54     }
55 
56     delayInfo->SetActualDelayTime((quota_ < delayTime) ? quota_ : delayTime);
57     requestList_.push_back(delayInfo);
58 }
59 
RemoveRequest(const int32_t requestId)60 void PkgDelaySuspendInfo::RemoveRequest(const int32_t requestId)
61 {
62     for (auto iter = requestList_.begin(); iter != requestList_.end(); iter++) {
63         if (!(*iter)->IsSameRequestId(requestId)) {
64             continue;
65         }
66         StopAccounting(requestId);
67         requestList_.erase(iter);
68         if (requestList_.empty()) {
69             UpdateQuota();
70             isCounting_ = false;
71         }
72         break;
73     }
74 }
75 
GetRemainDelayTime(const int32_t requestId)76 int32_t PkgDelaySuspendInfo::GetRemainDelayTime(const int32_t requestId)
77 {
78     for (auto &info : requestList_) {
79         if (info->IsSameRequestId(requestId)) {
80             return info->GetRemainDelayTime();
81         }
82     }
83     return 0;
84 }
85 
StartAccounting(const int32_t requestId)86 void PkgDelaySuspendInfo::StartAccounting(const int32_t requestId)
87 {
88     for (auto &info : requestList_) {
89         if ((requestId != -1) && !info->IsSameRequestId(requestId)) {
90             continue;
91         }
92         if (!isCounting_) {
93             UpdateQuota();
94             isCounting_ = true;
95         }
96         if (info->GetBaseTime() == 0) {
97             info->StartAccounting();
98             timerManager_->AddTimer(info->GetRequestId(), info->GetAdvanceCallbackTime());
99             BGTASK_LOGD("StartAccounting %{public}s, requestId: %{public}d", pkg_.c_str(), info->GetRequestId());
100         }
101     }
102 }
103 
StopAccounting(const int32_t requestId)104 void PkgDelaySuspendInfo::StopAccounting(const int32_t requestId)
105 {
106     for (auto &info : requestList_) {
107         if (!info->IsSameRequestId(requestId) || (info->GetBaseTime() == 0)) {
108             continue;
109         }
110         info->StopAccounting();
111         timerManager_->RemoveTimer(info->GetRequestId());
112         BGTASK_LOGD("StopAccounting %{public}s, requestId: %{public}d", pkg_.c_str(), info->GetRequestId());
113     }
114 }
115 
StopAccountingAll()116 void PkgDelaySuspendInfo::StopAccountingAll()
117 {
118     BGTASK_LOGD("StopAccountingAll %{public}s", pkg_.c_str());
119     for (auto &info : requestList_) {
120         if (info->GetBaseTime() == 0) {
121             continue;
122         }
123         info->StopAccounting();
124         timerManager_->RemoveTimer(info->GetRequestId());
125     }
126     UpdateQuota();
127     isCounting_ = false;
128 }
129 
UpdateQuota(bool reset)130 void PkgDelaySuspendInfo::UpdateQuota(bool reset)
131 {
132     spendTime_ = isCounting_ ? (static_cast<int32_t>(TimeProvider::GetCurrentTime()) - baseTime_) : 0;
133     quota_ -= spendTime_;
134     if (quota_ < 0) {
135         quota_ = 0;
136     }
137     baseTime_ = static_cast<int32_t>(TimeProvider::GetCurrentTime());
138     if (reset) {
139         quota_ = INIT_QUOTA;
140     }
141     BGTASK_LOGD("%{public}s Lastest quota: %{public}d", pkg_.c_str(), quota_);
142 }
143 }  // namespace BackgroundTaskMgr
144 }  // namespace OHOS
145