• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 REQUEST_NETWORK_ADAPTER_H
17 #define REQUEST_NETWORK_ADAPTER_H
18 
19 #include <stdint.h>
20 #include <mutex>
21 #include <functional>
22 #include "net_handle.h"
23 #include "net_link_info.h"
24 #include "net_all_capabilities.h"
25 #include "net_conn_callback_stub.h"
26 #include "refbase.h"
27 #include "constant.h"
28 namespace OHOS::Request::Download {
29 struct NetworkInfo {
30     NetworkType networkType_ = NETWORK_INVALID;
31     bool isMetered_ = false;
32     bool isRoaming_ = false;
33 };
34 class NetworkAdapter {
35 public:
36     using RegCallBack = std::function<void()>;
37 
38     bool RegOnNetworkChange(RegCallBack&& callback);
39     bool IsOnline();
40     static NetworkAdapter& GetInstance();
41     friend class NetConnCallbackObserver;
42     NetworkInfo GetNetworkInfo();
43 private:
44     class NetConnCallbackObserver :  public NetManagerStandard::NetConnCallbackStub {
45     public:
NetConnCallbackObserver(NetworkAdapter & netAdapter)46         explicit NetConnCallbackObserver(NetworkAdapter &netAdapter) : netAdapter_(netAdapter) {}
47         ~NetConnCallbackObserver() override = default;
48         int32_t NetAvailable(sptr<NetManagerStandard::NetHandle> &netHandle) override;
49 
50         int32_t NetCapabilitiesChange(sptr<NetManagerStandard::NetHandle> &netHandle,
51             const sptr<NetManagerStandard::NetAllCapabilities> &netAllCap) override;
52 
53         int32_t NetConnectionPropertiesChange(sptr<NetManagerStandard::NetHandle> &netHandle,
54             const sptr<NetManagerStandard::NetLinkInfo> &info) override;
55 
56         int32_t NetLost(sptr<NetManagerStandard::NetHandle> &netHandle) override;
57 
58         int32_t NetUnavailable() override;
59 
60         int32_t NetBlockStatusChange(sptr<NetManagerStandard::NetHandle> &netHandle, bool blocked) override;
61     private:
62         void UpdateRoaming();
63     private:
64         NetworkAdapter& netAdapter_;
65     };
66 
67     RegCallBack callback_ = nullptr;
68     bool isOnline_ = false;
69     std::mutex mutex_;
70     NetworkInfo networkInfo_;
71 };
72 }   // namespace OHOS::Request::Download
73 #endif /* REQUEST_NETWORK_ADAPTER_H */
74