• 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 NET_ACTIVATE_H
17 #define NET_ACTIVATE_H
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "i_net_conn_callback.h"
25 #include "net_specifier.h"
26 #include "net_supplier.h"
27 class NetSupplier;
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 constexpr uint32_t DEFAULT_REQUEST_ID = 0;
32 constexpr uint32_t MIN_REQUEST_ID = DEFAULT_REQUEST_ID + 1;
33 constexpr uint32_t MAX_REQUEST_ID = 0x7FFFFFFF;
34 class INetActivateCallback {
35 public:
36     virtual ~INetActivateCallback() = default;
37 
38 public:
39     virtual void OnNetActivateTimeOut(uint32_t reqId) = 0;
40 };
41 
42 class NetActivate : public std::enable_shared_from_this<NetActivate> {
43 public:
44     using TimeOutHandler = std::function<int32_t(uint32_t &reqId)>;
45 
46 public:
47     NetActivate(const sptr<NetSpecifier> &specifier, const sptr<INetConnCallback> &callback,
48                 std::weak_ptr<INetActivateCallback> timeoutCallback, const uint32_t &timeoutMS,
49                 const std::shared_ptr<AppExecFwk::EventHandler> &netActEventHandler, uint32_t uid = 0,
50                 const int32_t registerType = REGISTER);
51     ~NetActivate();
52     bool MatchRequestAndNetwork(sptr<NetSupplier> supplier, bool skipCheckIdent = false);
53     void SetRequestId(uint32_t reqId);
54     uint32_t GetRequestId() const;
55     sptr<NetSupplier> GetServiceSupply() const;
56     void SetServiceSupply(sptr<NetSupplier> netServiceSupplied);
57     sptr<INetConnCallback> GetNetCallback();
58     sptr<NetSpecifier> GetNetSpecifier();
59     int32_t GetRegisterType() const;
60     std::set<NetBearType> GetBearType() const;
61     void StartTimeOutNetAvailable();
62     uint32_t GetUid() const;
63     bool IsAppFrozened() const;
64     void SetIsAppFrozened(bool isFrozened);
65     CallbackType GetLastCallbackType() const;
66     void SetLastCallbackType(CallbackType callbackType);
67     bool IsAllowCallback(CallbackType callbackType);
68     sptr<NetSupplier> GetLastServiceSupply();
69     void SetLastServiceSupply(sptr<NetSupplier> lastNetServiceSupplied);
70 private:
71     bool CompareByNetworkIdent(const std::string &ident, NetBearType bearerType, bool skipCheckIdent);
72     bool CompareByNetworkCapabilities(const NetCaps &netCaps);
73     bool CompareByNetworkNetType(NetBearType bearerType);
74     bool CompareByNetworkBand(uint32_t netLinkUpBand, uint32_t netLinkDownBand);
75     bool HaveCapability(NetCap netCap) const;
76     bool HaveTypes(const std::set<NetBearType> &bearerTypes) const;
77     void TimeOutNetAvailable();
78 
79 private:
80     uint32_t requestId_ = 1;
81     sptr<NetSpecifier> netSpecifier_ = nullptr;
82     sptr<INetConnCallback> netConnCallback_ = nullptr;
83     sptr<NetSupplier> netServiceSupplied_ = nullptr;
84     uint32_t timeoutMS_ = 0;
85     std::weak_ptr<INetActivateCallback> timeoutCallback_;
86     std::shared_ptr<AppExecFwk::EventHandler> netActEventHandler_;
87     std::string activateName_ = "";
88     uint32_t uid_ = 0;
89     int32_t registerType_ = REGISTER;
90     std::atomic<bool> isAppFrozened_ = false;
91     std::atomic<int32_t> lastCallbackType_ = 0;
92     sptr<NetSupplier> lastNetServiceSupplied_ = nullptr;
93     std::mutex lastNetServiceSuppliedMutex_;
94 };
95 } // namespace NetManagerStandard
96 } // namespace OHOS
97 #endif // NET_ACTIVATE_H