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 #include "work_sched_data_manager.h"
16 #include "work_sched_hilog.h"
17 #include "work_sched_utils.h"
18 #include "work_scheduler_service.h"
19 
20 namespace OHOS {
21 namespace WorkScheduler {
DataManager()22 DataManager::DataManager() {}
23 
~DataManager()24 DataManager::~DataManager() {}
25 
GetDeviceSleep() const26 bool DataManager::GetDeviceSleep() const
27 {
28     return deviceSleep_;
29 }
30 
SetDeviceSleep(const bool isSleep)31 void DataManager::SetDeviceSleep(const bool isSleep)
32 {
33     deviceSleep_ = isSleep;
34 }
35 
IsInDeviceStandyWhitelist(const std::string & bundleName)36 bool DataManager::IsInDeviceStandyWhitelist(const std::string& bundleName)
37 {
38     std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
39     return deviceStandySet.count(bundleName) > 0;
40 }
41 
OnDeviceStandyWhitelistChanged(const std::string & bundleName,const bool add)42 void DataManager::OnDeviceStandyWhitelistChanged(const std::string& bundleName, const bool add)
43 {
44     std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
45     if (add) {
46         deviceStandySet.insert(bundleName);
47     } else {
48         deviceStandySet.erase(bundleName);
49     }
50 }
51 
AddDeviceStandyWhitelist(const std::list<std::string> & bundleNames)52 void DataManager::AddDeviceStandyWhitelist(const std::list<std::string>& bundleNames)
53 {
54     std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
55     for (const auto& item : bundleNames) {
56         deviceStandySet.insert(item);
57     }
58 }
59 
ClearDeviceStandyWhitelist()60 void DataManager::ClearDeviceStandyWhitelist()
61 {
62     std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
63     deviceStandySet.clear();
64 }
65 
IsDeviceStandyWhitelistEmpty()66 bool DataManager::IsDeviceStandyWhitelistEmpty()
67 {
68     std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
69     return deviceStandySet.empty();
70 }
71 
FindGroup(const std::string & bundleName,const int32_t userId,int32_t & appGroup)72 bool DataManager::FindGroup(const std::string& bundleName, const int32_t userId, int32_t& appGroup)
73 {
74     std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
75     std::string key = bundleName + "_" + std::to_string(userId);
76     auto iter = activeGroupMap_.find(key);
77     if (iter != activeGroupMap_.end()) {
78         appGroup = iter->second;
79         return true;
80     } else {
81         return false;
82     }
83 }
84 
AddGroup(const std::string & bundleName,const int32_t userId,const int32_t appGroup)85 void DataManager::AddGroup(const std::string& bundleName, const int32_t userId, const int32_t appGroup)
86 {
87     std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
88     std::string key = bundleName + "_" + std::to_string(userId);
89     auto iter = activeGroupMap_.find(key);
90     if (iter != activeGroupMap_.end()) {
91         activeGroupMap_.erase(iter);
92     }
93     activeGroupMap_.emplace(key, appGroup);
94     WS_HILOGI("activeGroupMap_ size %{public}d", static_cast<int32_t>(activeGroupMap_.size()));
95 }
96 
ClearGroup(const std::string & bundleName,const int32_t userId)97 void DataManager::ClearGroup(const std::string& bundleName, const int32_t userId)
98 {
99     std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
100     std::string key = bundleName + "_" + std::to_string(userId);
101     auto iter = activeGroupMap_.find(key);
102     if (iter != activeGroupMap_.end()) {
103         activeGroupMap_.erase(iter);
104     }
105 }
106 
ClearAllGroup()107 void DataManager::ClearAllGroup()
108 {
109     std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
110     activeGroupMap_.clear();
111 }
112 } // namespace WorkScheduler
113 } // namespace OHOS