• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "net_factoryreset_callback.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
23 constexpr const char *NET_FACTORYRESET_WORK_THREAD = "NET_FACTORYRESET_CALLBACK_WORK_THREAD";
24 constexpr int16_t LIMIT_CALLBACK_NUM = 200;
25 
NetFactoryResetCallback()26 NetFactoryResetCallback::NetFactoryResetCallback()
27 {
28     factoryResetCallRunner_ = AppExecFwk::EventRunner::Create(NET_FACTORYRESET_WORK_THREAD);
29     factoryResetCallHandler_ = std::make_shared<AppExecFwk::EventHandler>(factoryResetCallRunner_);
30 }
31 
~NetFactoryResetCallback()32 NetFactoryResetCallback::~NetFactoryResetCallback()
33 {
34     if (factoryResetCallRunner_) {
35         factoryResetCallRunner_->Stop();
36     }
37 }
38 
RegisterNetFactoryResetCallbackAsync(const sptr<INetFactoryResetCallback> & callback)39 int32_t NetFactoryResetCallback::RegisterNetFactoryResetCallbackAsync(const sptr<INetFactoryResetCallback> &callback)
40 {
41     if (callback == nullptr || callback->AsObject() == nullptr || callback->AsObject().GetRefPtr() == nullptr) {
42         NETMGR_LOG_E("The parameter callback is null");
43         return NETMANAGER_ERR_PARAMETER_ERROR;
44     }
45     int32_t ret = NETMANAGER_SUCCESS;
46     if (factoryResetCallHandler_) {
47         factoryResetCallHandler_->PostSyncTask([this, &callback, &ret]() {
48             ret = RegisterNetFactoryResetCallback(callback);
49         });
50     }
51 
52     return ret;
53 }
54 
RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> & callback)55 int32_t NetFactoryResetCallback::RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
56 {
57     uint32_t callbackCounts = callbacks_.size();
58     NETMGR_LOG_I("callback counts [%{public}u]", callbackCounts);
59     if (callbackCounts >= LIMIT_CALLBACK_NUM) {
60         NETMGR_LOG_E("callback counts cannot more than [%{public}u]", LIMIT_CALLBACK_NUM);
61         return NETMANAGER_ERR_PARAMETER_ERROR;
62     }
63 
64     for (uint32_t i = 0; i < callbackCounts; i++) {
65         if (callback->AsObject().GetRefPtr() == callbacks_[i]->AsObject().GetRefPtr()) {
66             NETMGR_LOG_W("NetFactoryResetCallback_ had this callback");
67             return NETMANAGER_ERR_PARAMETER_ERROR;
68         }
69     }
70 
71     callbacks_.emplace_back(callback);
72     NETMGR_LOG_I("End RegisterNetFactoryResetCallback,callback counts [%{public}zu]", callbacks_.size());
73     return NETMANAGER_SUCCESS;
74 }
75 
UnregisterNetFactoryResetCallbackAsync(const sptr<INetFactoryResetCallback> & callback)76 int32_t NetFactoryResetCallback::UnregisterNetFactoryResetCallbackAsync(const sptr<INetFactoryResetCallback> &callback)
77 {
78     if (callback == nullptr || callback->AsObject() == nullptr || callback->AsObject().GetRefPtr() == nullptr) {
79         NETMGR_LOG_E("The parameter of callback is null");
80         return NETMANAGER_ERR_PARAMETER_ERROR;
81     }
82 
83     int32_t ret = NETMANAGER_SUCCESS;
84     if (factoryResetCallHandler_) {
85         factoryResetCallHandler_->PostSyncTask([this, &callback, &ret]() {
86             ret = UnregisterNetFactoryResetCallback(callback);
87         });
88     }
89 
90     return ret;
91 }
92 
UnregisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> & callback)93 int32_t NetFactoryResetCallback::UnregisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
94 {
95     NETMGR_LOG_I("Enter UnregisterNetFactoryResetCallback");
96     auto it = std::remove_if(callbacks_.begin(), callbacks_.end(),
97         [callback](const sptr<INetFactoryResetCallback> &tempCallback) -> bool {
98             if (tempCallback == nullptr || tempCallback->AsObject() == nullptr ||
99                 tempCallback->AsObject().GetRefPtr() == nullptr) {
100                 return true;
101             }
102             return callback->AsObject().GetRefPtr() == tempCallback->AsObject().GetRefPtr();
103         });
104     callbacks_.erase(it, callbacks_.end());
105     NETMGR_LOG_I("End UnregisterNetFactoryResetCallback");
106     return NETMANAGER_SUCCESS;
107 }
108 
109 
NotifyNetFactoryResetAsync()110 int32_t NetFactoryResetCallback::NotifyNetFactoryResetAsync()
111 {
112     NETMGR_LOG_I("NotifyNetFactoryResetAsync enter");
113     int32_t ret = NETMANAGER_SUCCESS;
114     if (factoryResetCallHandler_) {
115         factoryResetCallHandler_->PostSyncTask([this, &ret]() {
116             ret = NotifyNetFactoryReset();
117         });
118     }
119 
120     return ret;
121 }
122 
NotifyNetFactoryReset()123 int32_t NetFactoryResetCallback::NotifyNetFactoryReset()
124 {
125     NETMGR_LOG_I("NotifyNetFactoryReset enter, callback count = [%{public}zu]", callbacks_.size());
126     for (const auto &callback : callbacks_) {
127         if (callback != nullptr && callback->AsObject() != nullptr && callback->AsObject().GetRefPtr() != nullptr) {
128             callback->OnNetFactoryReset();
129         }
130     }
131 
132     return NETMANAGER_SUCCESS;
133 }
134 
135 } // namespace NetManagerStandard
136 } // namespace OHOS
137