1 /*
2  * Copyright (C) 2021-2022 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 #ifndef OHOS_WIFI_APP_NETWORK_SPEED_LIMIT_SERVICE_H
17 #define OHOS_WIFI_APP_NETWORK_SPEED_LIMIT_SERVICE_H
18 
19 #include "wifi_errcode.h"
20 #include "define.h"
21 #include "app_mgr_interface.h"
22 #include "sta_service_callback.h"
23 #include <unordered_set>
24 
25 namespace OHOS {
26 namespace Wifi {
27 constexpr const int UNKNOWN_UID = -1;
28 
29 class AppNetworkSpeedLimitService {
30 public:
31     explicit AppNetworkSpeedLimitService();
32     ~AppNetworkSpeedLimitService();
33     static AppNetworkSpeedLimitService &GetInstance();
34     StaServiceCallback GetStaCallback() const;
35     void HandleForegroundAppChangedAction(const AppExecFwk::AppStateData &appStateData);
36     void LimitSpeed(const int controlId, const int level);
37 
38 private:
39     void Init();
40     void InitWifiLimitRecord();
41     void InitCellarLimitRecord();
42     void DealStaConnChanged(OperateResState state, const WifiLinkedInfo &info, int instId = 0);
43     void HandleWifiConnectStateChanged(const bool isWifiConnected);
44     int GetBgLimitMaxMode();
45     ErrCode GetAppList(std::vector<AppExecFwk::RunningProcessInfo> &appList, bool getFgAppFlag);
46     bool CheckNetWorkCanBeLimited(const int controlId);
47     void UpdateSpeedLimitConfigs(const int controlId, const int limitMode);
48     void LogSpeedLimitConfigs();
49     bool IsLimitSpeedBgApp(const int controlId, const std::string bundleName);
50 
51 private:
52     StaServiceCallback m_staCallback;
53     std::atomic<bool> m_isWifiConnected = false;
54     std::map<int, int> m_bgLimitRecordMap;
55     int m_currentLimitMode{0};
56     std::unordered_set<int> m_bgUidSet;
57     std::unordered_set<int> m_bgPidSet;
58     std::unordered_set<int> m_fgUidSet;
59     std::mutex m_mutex;
60     std::mutex m_readWritemutex;
61 };
62 
63 } // namespace Wifi
64 } // namespace OHOS
65 
66 #endif