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
IsInDeviceStandyRestrictlist(const std::string & bundleName)72 bool DataManager::IsInDeviceStandyRestrictlist(const std::string& bundleName)
73 {
74 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
75 return deviceRestrictSet.count(bundleName) > 0;
76 }
77
OnDeviceStandyRestrictlistChanged(const std::string & bundleName,const bool add)78 void DataManager::OnDeviceStandyRestrictlistChanged(const std::string& bundleName, const bool add)
79 {
80 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
81 if (add) {
82 deviceRestrictSet.insert(bundleName);
83 } else {
84 deviceRestrictSet.erase(bundleName);
85 }
86 }
87
AddDeviceStandyRestrictlist(const std::list<std::string> & bundleNames)88 void DataManager::AddDeviceStandyRestrictlist(const std::list<std::string>& bundleNames)
89 {
90 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
91 for (const auto& item : bundleNames) {
92 deviceRestrictSet.insert(item);
93 }
94 }
95
ClearDeviceStandyRestrictlist()96 void DataManager::ClearDeviceStandyRestrictlist()
97 {
98 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
99 deviceRestrictSet.clear();
100 }
101
FindGroup(const std::string & bundleName,const int32_t userId,int32_t & appGroup)102 bool DataManager::FindGroup(const std::string& bundleName, const int32_t userId, int32_t& appGroup)
103 {
104 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
105 std::string key = bundleName + "_" + std::to_string(userId);
106 auto iter = activeGroupMap_.find(key);
107 if (iter != activeGroupMap_.end()) {
108 appGroup = iter->second;
109 return true;
110 } else {
111 return false;
112 }
113 }
114
AddGroup(const std::string & bundleName,const int32_t userId,const int32_t appGroup)115 void DataManager::AddGroup(const std::string& bundleName, const int32_t userId, const int32_t appGroup)
116 {
117 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
118 std::string key = bundleName + "_" + std::to_string(userId);
119 auto iter = activeGroupMap_.find(key);
120 if (iter != activeGroupMap_.end()) {
121 activeGroupMap_.erase(iter);
122 }
123 activeGroupMap_.emplace(key, appGroup);
124 WS_HILOGI("activeGroupMap_ size %{public}d", static_cast<int32_t>(activeGroupMap_.size()));
125 }
126
ClearGroup(const std::string & bundleName,const int32_t userId)127 void DataManager::ClearGroup(const std::string& bundleName, const int32_t userId)
128 {
129 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
130 std::string key = bundleName + "_" + std::to_string(userId);
131 auto iter = activeGroupMap_.find(key);
132 if (iter != activeGroupMap_.end()) {
133 activeGroupMap_.erase(iter);
134 }
135 }
136
ClearAllGroup()137 void DataManager::ClearAllGroup()
138 {
139 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
140 activeGroupMap_.clear();
141 }
142 } // namespace WorkScheduler
143 } // namespace OHOS