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 
16 #include "work_sched_config.h"
17 #include "work_sched_hilog.h"
18 #include "nlohmann/json.hpp"
19 
20 namespace OHOS {
21 namespace WorkScheduler {
22 namespace {
23 const std::string ACTIVE_GROUP_WHITELIST = "active_group_whitelist";
24 
25 }
InitActiveGroupWhitelist(const std::string & configData)26 void WorkSchedulerConfig::InitActiveGroupWhitelist(const std::string &configData)
27 {
28     std::lock_guard<std::mutex> lock(configMutex_);
29     const nlohmann::json &jsonObj = nlohmann::json::parse(configData, nullptr, false);
30     if (jsonObj.is_discarded()) {
31         WS_HILOGE("jsonObj parse fail");
32         return;
33     }
34     if (jsonObj.is_null() || jsonObj.empty()) {
35         WS_HILOGE("jsonObj null");
36         return;
37     }
38     if (!jsonObj.contains(ACTIVE_GROUP_WHITELIST) ||
39         !jsonObj[ACTIVE_GROUP_WHITELIST].is_array()) {
40         WS_HILOGE("no key %{public}s", ACTIVE_GROUP_WHITELIST.c_str());
41         return;
42     }
43     nlohmann::json appArray = jsonObj[ACTIVE_GROUP_WHITELIST];
44     for (const auto &app : appArray) {
45         activeGroupWhitelist_.insert(app);
46     }
47     for (const auto &app : activeGroupWhitelist_) {
48         WS_HILOGI("activeGroupWhitelist_: %{public}s.", app.c_str());
49     }
50 }
51 
IsInActiveGroupWhitelist(const std::string & bundleName)52 bool WorkSchedulerConfig::IsInActiveGroupWhitelist(const std::string &bundleName)
53 {
54     std::lock_guard<std::mutex> lock(configMutex_);
55     return activeGroupWhitelist_.count(bundleName) > 0;
56 }
57 } // WorkScheduler
58 } // OHOS