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 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_DATA_MANAGER_H
16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_DATA_MANAGER_H
17 
18 #include <map>
19 #include <mutex>
20 #include <set>
21 #include <string>
22 #include <list>
23 #include "singleton.h"
24 #include "ffrt.h"
25 
26 namespace OHOS {
27 namespace WorkScheduler {
28 class DataManager {
29 DECLARE_DELAYED_SINGLETON(DataManager);
30 public:
31     // device sleep
32     void SetDeviceSleep(const bool isSleep);
33     bool GetDeviceSleep() const;
34     bool IsInDeviceStandyWhitelist(const std::string& bundleName);
35     void OnDeviceStandyWhitelistChanged(const std::string& bundleName, const bool add);
36     void AddDeviceStandyWhitelist(const std::list<std::string>& bundleNames);
37     void ClearDeviceStandyWhitelist();
38     bool IsDeviceStandyWhitelistEmpty();
39     // group
40     void AddGroup(const std::string& bundleName, const int32_t userId, const int32_t appGroup);
41     bool FindGroup(const std::string& bundleName, const int32_t userId, int32_t& appGroup);
42     void ClearGroup(const std::string& bundleName, const int32_t userId);
43     void ClearAllGroup();
44 private:
45     std::atomic<bool> deviceSleep_ = false;
46     ffrt::mutex deviceStandySetMutex_;
47     // <bundle>
48     std::set<std::string> deviceStandySet {};
49     ffrt::mutex activeGroupMapMutex_;
50     // <bundle_userId, group>
51     std::unordered_map<std::string, int32_t> activeGroupMap_;
52 };
53 } // namespace WorkScheduler
54 } // namespace OHOS
55 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_DATA_MANAGER_H