• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unordered_set>
20 
21 #include "wifi_errcode.h"
22 #include "wifi_event_handler.h"
23 #include "define.h"
24 #include "app_mgr_interface.h"
25 #include "sta_service_callback.h"
26 
27 namespace OHOS {
28 namespace Wifi {
29 constexpr const int UNKNOWN_UID = -1;
30 constexpr const int UNKNOWN_MODE = -1;
31 
32 struct AsyncParamInfo {
33     int controlId;
34     int limitMode;
35     WifiNetworkControlInfo networkControlInfo;
36     std::string funcName;
37     std::string bundleName;
38 
AsyncParamInfoAsyncParamInfo39     AsyncParamInfo()
40     {
41         controlId = -1;
42         limitMode = -1;
43         funcName = "";
44         bundleName = "";
45     }
46 };
47 
48 class AppNetworkSpeedLimitService {
49 public:
50     explicit AppNetworkSpeedLimitService();
51     ~AppNetworkSpeedLimitService();
52     static AppNetworkSpeedLimitService &GetInstance();
53     StaServiceCallback GetStaCallback() const;
54     void HandleForegroundAppChangedAction(const AppExecFwk::AppStateData &appStateData);
55     void ReceiveNetworkControlInfo(const WifiNetworkControlInfo &networkControlInfo);
56     void LimitSpeed(const int controlId, const int limitMode);
57 
58 private:
59     void Init();
60     void InitWifiLimitRecord();
61     void InitCellarLimitRecord();
62     void DealStaConnChanged(OperateResState state, const WifiLinkedInfo &info, int instId = 0);
63     void HandleWifiConnectStateChanged(const bool isWifiConnected);
64     void SendLimitInfo();
65     void FilterLimitSpeedConfigs();
66     int GetBgLimitMaxMode();
67     ErrCode GetAppList(std::vector<AppExecFwk::RunningProcessInfo> &appList, bool getFgAppFlag);
68     bool CheckNetWorkCanBeLimited(const int controlId);
69     void UpdateSpeedLimitConfigs(const int enable);
70     void UpdateNoSpeedLimitConfigs(const WifiNetworkControlInfo &networkControlInfo);
71     bool IsLimitSpeedBgApp(const int controlId, const std::string &bundleName, const int enable);
72     bool IsTopNLimitSpeedSceneInNow();
73     void AsyncLimitSpeed(const AsyncParamInfo &asyncParamInfo);
74     void WifiConnectStateChanged();
75     void ForegroundAppChangedAction(const std::string &bundleName);
76     void HandleRequest(const AsyncParamInfo &asyncParamInfo);
77     void SendLimitCmd2Drv(const int controlId, const int limitMode, const int enable,
78         const int uid = -1);
79     void HighPriorityTransmit(int uid, int protocol, int enable);
80     void GameNetworkSpeedLimitConfigs(const WifiNetworkControlInfo &networkControlInfo);
81     void VideoCallNetworkSpeedLimitConfigs(const WifiNetworkControlInfo &networkControlInfo);
82     void LogSpeedLimitConfigs();
83 
84 private:
85     StaServiceCallback m_staCallback;
86     std::atomic<bool> m_isWifiConnected = false;
87     int m_isHighPriorityTransmit = 0;
88     std::map<int, int> m_bgLimitRecordMap;
89     int m_limitSpeedMode{0};
90     std::unordered_set<int> m_bgUidSet;
91     std::unordered_set<int> m_bgPidSet;
92     std::unordered_set<int> m_fgUidSet;
93     int m_lastLimitSpeedMode{UNKNOWN_MODE};
94     std::unordered_set<int> m_lastBgUidSet;
95     std::unordered_set<int> m_lastBgPidSet;
96     std::unordered_set<int> m_lastFgUidSet;
97     std::unordered_set<int> m_bgAudioPlaybackUidSet;
98     std::unordered_set<int> m_bgAudioPlaybackPidSet;
99     std::unordered_set<int> m_additionalWindowUidSet;
100     std::unordered_set<int> m_additionalWindowPidSet;
101     std::unique_ptr<WifiEventHandler> m_asyncSendLimit = nullptr;
102     int64_t m_delayTime;
103 };
104 } // namespace Wifi
105 } // namespace OHOS
106 
107 #endif