1 /*
2  * Copyright (c) 2022-2024 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 #include "connection_observer_client.h"
17 
18 #include "connection_observer_client_impl.h"
19 #include "connection_observer_errors.h"
20 #include "hilog_tag_wrapper.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
ConnectionObserverClient()24 ConnectionObserverClient::ConnectionObserverClient()
25 {
26     clientImpl_ = std::make_shared<ConnectionObserverClientImpl>();
27 }
28 
GetInstance()29 ConnectionObserverClient& ConnectionObserverClient::GetInstance()
30 {
31     static ConnectionObserverClient instance;
32     return instance;
33 }
34 
RegisterObserver(const std::shared_ptr<ConnectionObserver> & observer)35 int32_t ConnectionObserverClient::RegisterObserver(const std::shared_ptr<ConnectionObserver> &observer)
36 {
37     if (!clientImpl_) {
38         TAG_LOGE(AAFwkTag::CONNECTION, "null clientImpl_");
39         return ERR_NO_CLIENT_IMPL;
40     }
41 
42     return clientImpl_->RegisterObserver(observer);
43 }
44 
UnregisterObserver(const std::shared_ptr<ConnectionObserver> & observer)45 int32_t ConnectionObserverClient::UnregisterObserver(const std::shared_ptr<ConnectionObserver> &observer)
46 {
47     if (!clientImpl_) {
48         TAG_LOGE(AAFwkTag::CONNECTION, "null clientImpl_");
49         return ERR_NO_CLIENT_IMPL;
50     }
51 
52     return clientImpl_->UnregisterObserver(observer);
53 }
54 
GetDlpConnectionInfos(std::vector<DlpConnectionInfo> & infos)55 int32_t ConnectionObserverClient::GetDlpConnectionInfos(std::vector<DlpConnectionInfo> &infos)
56 {
57 #ifdef WITH_DLP
58     if (!clientImpl_) {
59         TAG_LOGE(AAFwkTag::CONNECTION, "null clientImpl_");
60         return ERR_NO_CLIENT_IMPL;
61     }
62 
63     return clientImpl_->GetDlpConnectionInfos(infos);
64 #else
65     return ERR_READ_INFO_FAILED;
66 #endif // WITH_DLP
67 }
68 
GetConnectionData(std::vector<ConnectionData> & connectionData)69 int32_t ConnectionObserverClient::GetConnectionData(std::vector<ConnectionData> &connectionData)
70 {
71     if (!clientImpl_) {
72         TAG_LOGE(AAFwkTag::CONNECTION, "null clientImpl_");
73         return ERR_NO_CLIENT_IMPL;
74     }
75 
76     return clientImpl_->GetConnectionData(connectionData);
77 }
78 }
79 }
80