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
GetDeepIdle() const36 bool DataManager::GetDeepIdle() const
37 {
38 return deepIdle_;
39 }
40
SetDeepIdle(const bool isDeepIdle)41 void DataManager::SetDeepIdle(const bool isDeepIdle)
42 {
43 deepIdle_ = isDeepIdle;
44 }
45
IsInDeviceStandyWhitelist(const std::string & bundleName)46 bool DataManager::IsInDeviceStandyWhitelist(const std::string& bundleName)
47 {
48 std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
49 return deviceStandySet.count(bundleName) > 0;
50 }
51
OnDeviceStandyWhitelistChanged(const std::string & bundleName,const bool add)52 void DataManager::OnDeviceStandyWhitelistChanged(const std::string& bundleName, const bool add)
53 {
54 std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
55 if (add) {
56 deviceStandySet.insert(bundleName);
57 } else {
58 deviceStandySet.erase(bundleName);
59 }
60 }
61
AddDeviceStandyWhitelist(const std::list<std::string> & bundleNames)62 void DataManager::AddDeviceStandyWhitelist(const std::list<std::string>& bundleNames)
63 {
64 std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
65 for (const auto& item : bundleNames) {
66 deviceStandySet.insert(item);
67 }
68 }
69
ClearDeviceStandyWhitelist()70 void DataManager::ClearDeviceStandyWhitelist()
71 {
72 std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
73 deviceStandySet.clear();
74 }
75
IsDeviceStandyWhitelistEmpty()76 bool DataManager::IsDeviceStandyWhitelistEmpty()
77 {
78 std::lock_guard<ffrt::mutex> lock(deviceStandySetMutex_);
79 return deviceStandySet.empty();
80 }
81
IsInDeviceStandyRestrictlist(const std::string & bundleName)82 bool DataManager::IsInDeviceStandyRestrictlist(const std::string& bundleName)
83 {
84 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
85 return deviceRestrictSet.count(bundleName) > 0;
86 }
87
OnDeviceStandyRestrictlistChanged(const std::string & bundleName,const bool add)88 void DataManager::OnDeviceStandyRestrictlistChanged(const std::string& bundleName, const bool add)
89 {
90 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
91 if (add) {
92 deviceRestrictSet.insert(bundleName);
93 } else {
94 deviceRestrictSet.erase(bundleName);
95 }
96 }
97
AddDeviceStandyRestrictlist(const std::list<std::string> & bundleNames)98 void DataManager::AddDeviceStandyRestrictlist(const std::list<std::string>& bundleNames)
99 {
100 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
101 for (const auto& item : bundleNames) {
102 deviceRestrictSet.insert(item);
103 }
104 }
105
ClearDeviceStandyRestrictlist()106 void DataManager::ClearDeviceStandyRestrictlist()
107 {
108 std::lock_guard<ffrt::mutex> lock(deviceRestrictSetMutex_);
109 deviceRestrictSet.clear();
110 }
111
FindGroup(const std::string & bundleName,const int32_t userId,int32_t & appGroup)112 bool DataManager::FindGroup(const std::string& bundleName, const int32_t userId, int32_t& appGroup)
113 {
114 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
115 std::string key = bundleName + "_" + std::to_string(userId);
116 auto iter = activeGroupMap_.find(key);
117 if (iter != activeGroupMap_.end()) {
118 appGroup = iter->second;
119 return true;
120 } else {
121 return false;
122 }
123 }
124
AddGroup(const std::string & bundleName,const int32_t userId,const int32_t appGroup)125 void DataManager::AddGroup(const std::string& bundleName, const int32_t userId, const int32_t appGroup)
126 {
127 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
128 std::string key = bundleName + "_" + std::to_string(userId);
129 auto iter = activeGroupMap_.find(key);
130 if (iter != activeGroupMap_.end()) {
131 activeGroupMap_.erase(iter);
132 }
133 activeGroupMap_.emplace(key, appGroup);
134 WS_HILOGI("activeGroupMap_ size %{public}d", static_cast<int32_t>(activeGroupMap_.size()));
135 }
136
ClearGroup(const std::string & bundleName,const int32_t userId)137 void DataManager::ClearGroup(const std::string& bundleName, const int32_t userId)
138 {
139 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
140 std::string key = bundleName + "_" + std::to_string(userId);
141 auto iter = activeGroupMap_.find(key);
142 if (iter != activeGroupMap_.end()) {
143 activeGroupMap_.erase(iter);
144 }
145 }
146
ClearAllGroup()147 void DataManager::ClearAllGroup()
148 {
149 std::lock_guard<ffrt::mutex> lock(activeGroupMapMutex_);
150 activeGroupMap_.clear();
151 }
152 } // namespace WorkScheduler
153 } // namespace OHOS