1 /*
2  * Copyright (c) 2021 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 "net_settings.h"
16 
17 namespace OHOS {
18 namespace NetManagerStandard {
19 static constexpr uint32_t DEFAULT_SYSTEM_UID = 456;
NetSettings()20 NetSettings::NetSettings()
21 {
22     AddSystemUid(DEFAULT_SYSTEM_UID);
23 }
24 
~NetSettings()25 NetSettings::~NetSettings() {}
26 
GetInstance()27 NetSettings &NetSettings::GetInstance()
28 {
29     static NetSettings gNetSettings;
30     return gNetSettings;
31 }
32 
IsUidForeground(uint32_t uid)33 bool NetSettings::IsUidForeground(uint32_t uid)
34 {
35     return foregroundUid_ == uid;
36 }
37 
SetForegroundUid(uint32_t uid)38 void NetSettings::SetForegroundUid(uint32_t uid)
39 {
40     foregroundUid_ = uid;
41 }
42 
IsSystem(uint32_t uid)43 bool NetSettings::IsSystem(uint32_t uid)
44 {
45     std::lock_guard<std::mutex> lock(mutex_);
46     return std::find(systemUids_.begin(), systemUids_.end(), uid) != systemUids_.end();
47 }
48 
AddSystemUid(uint32_t uid)49 void NetSettings::AddSystemUid(uint32_t uid)
50 {
51     std::lock_guard<std::mutex> lock(mutex_);
52     systemUids_.push_back(uid);
53 }
54 
RemoveSystemUid(uint32_t uid)55 void NetSettings::RemoveSystemUid(uint32_t uid)
56 {
57     std::lock_guard<std::mutex> lock(mutex_);
58     if (uid == 0) {
59         systemUids_.clear();
60         return;
61     }
62 
63     for (auto it = systemUids_.begin(); it != systemUids_.end(); it++) {
64         if (*it == uid) {
65             systemUids_.erase(it);
66             return;
67         }
68     }
69 }
70 } // namespace NetManagerStandard
71 } // namespace OHOS
72