• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "observer_handler.h"
17 
18 #include "telephony_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Telephony {
ObserverHandler()22 ObserverHandler::ObserverHandler() {}
23 
~ObserverHandler()24 ObserverHandler::~ObserverHandler() {}
25 
RegObserver(int32_t what,const std::shared_ptr<AppExecFwk::EventHandler> handler)26 void ObserverHandler::RegObserver(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler)
27 {
28     std::lock_guard<std::mutex> lock(mutex_);
29     auto iter = observerHandlerMap_.find(what);
30     if (iter != observerHandlerMap_.end()) {
31         std::list<std::shared_ptr<OHOS::AppExecFwk::EventHandler>> &handlers = iter->second;
32         auto it = find(handlers.begin(), handlers.end(), handler);
33         if (it == handlers.end()) {
34             handlers.push_back(handler);
35         }
36         TELEPHONY_LOGD("ObserverHandler RegObserver update callback what: %{public}d, list size: %{public}zu", what,
37             handlers.size());
38     } else {
39         TELEPHONY_LOGD("ObserverHandler RegObserver callback what: %{public}d", what);
40         std::list<std::shared_ptr<AppExecFwk::EventHandler>> handlers;
41         handlers.push_back(handler);
42         observerHandlerMap_.emplace(what, handlers);
43     }
44 }
45 
RemoveAll()46 void ObserverHandler::RemoveAll()
47 {
48     std::lock_guard<std::mutex> lock(mutex_);
49     observerHandlerMap_.clear();
50 }
51 
Remove(int32_t what,const std::shared_ptr<AppExecFwk::EventHandler> handler)52 void ObserverHandler::Remove(int32_t what, const std::shared_ptr<AppExecFwk::EventHandler> handler)
53 {
54     if (handler == nullptr) {
55         TELEPHONY_LOGE("ObserverHandler handler==nullptr");
56         return;
57     }
58 
59     std::lock_guard<std::mutex> lock(mutex_);
60     auto iter = observerHandlerMap_.find(what);
61     if (iter != observerHandlerMap_.end()) {
62         std::list<std::shared_ptr<OHOS::AppExecFwk::EventHandler>> &handlers = iter->second;
63         auto it = find(handlers.begin(), handlers.end(), handler);
64         if (it != handlers.end()) {
65             handlers.erase(it);
66         }
67         TELEPHONY_LOGD("ObserverHandler Remove handlers list: %{public}zu", handlers.size());
68     }
69 }
70 
NotifyObserver(int32_t what)71 void ObserverHandler::NotifyObserver(int32_t what)
72 {
73     std::lock_guard<std::mutex> lock(mutex_);
74     auto iter = observerHandlerMap_.find(what);
75     if (iter == observerHandlerMap_.end()) {
76         TELEPHONY_LOGE("ObserverHandler NotifyObserver %{public}d not register", what);
77         return;
78     }
79 
80     for (auto handler : iter->second) {
81         TELEPHONY_LOGD("handler->SendEvent:%{public}d", what);
82         handler->SendEvent(what);
83     }
84 }
85 } // namespace Telephony
86 } // namespace OHOS