1/*
2 * Copyright (c) 2023 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 DUPDATE_NET_MANAGER_H
17#define DUPDATE_NET_MANAGER_H
18
19#include "singleton.h"
20
21#include <map>
22#include <set>
23
24#include "dupdate_inet_observer.h"
25#ifdef NETMANAGER_BASE_ENABLE
26#include "dupdate_net_observer.h"
27#endif
28
29namespace OHOS {
30namespace UpdateEngine {
31struct NetChangeCallback {
32    std::set<NetType> netTypes;
33    NetObserverCallback function;
34};
35
36class NetManager final : public INetObserverCallback,
37                         public std::enable_shared_from_this<NetManager>,
38                         public DelayedSingleton<NetManager> {
39    DECLARE_DELAYED_SINGLETON(NetManager);
40
41public:
42    void Init();
43    NetType GetNetType();
44    bool IsNetAvailable(NetType netType = NetType::CELLULAR_AND_WIFI);
45
46    bool RegisterCallback(
47        NetChangeCallbackType callbackType, const std::set<NetType> &netTypes, NetObserverCallback cb);
48    void UnregisterCallback(NetChangeCallbackType callbackType);
49
50    bool OnNetChange(NetType netType) final;
51
52private:
53    bool IsBaseNetType(NetType netType);
54
55#ifdef NETMANAGER_BASE_ENABLE
56    sptr<NetObserver> observer_ = nullptr;
57#endif
58    std::mutex netChangeMutex_;
59    NetType netType_ = NetType::NO_NET;
60    std::map<NetChangeCallbackType, NetChangeCallback> netChangeCallbackMap_;
61};
62} // namespace UpdateEngine
63} // namespace OHOS
64#endif // DUPDATE_NET_MANAGER_H