1 /*
2 * Copyright (c) 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 "app_event_publisher_factory.h"
17
18 #include "hiview_logger.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 namespace {
23 DEFINE_LOG_TAG("HiView-AppEventPublisherFactory");
24 }
25
GetPublisherRegistryMap()26 std::shared_ptr<std::unordered_map<std::string, bool>> AppEventPublisherFactory::GetPublisherRegistryMap()
27 {
28 static std::shared_ptr<std::unordered_map<std::string, bool>> publisherMap;
29 if (publisherMap == nullptr) {
30 publisherMap = std::make_shared<std::unordered_map<std::string, bool>>();
31 }
32 return publisherMap;
33 }
34
IsPublisher(const std::string & name)35 bool AppEventPublisherFactory::IsPublisher(const std::string& name)
36 {
37 auto publisherMap = GetPublisherRegistryMap();
38 auto it = publisherMap->find(name);
39 if (it != publisherMap->end()) {
40 return it->second;
41 }
42 return false;
43 }
44
RegisterPublisher(const std::string & name)45 void AppEventPublisherFactory::RegisterPublisher(const std::string& name)
46 {
47 if (name.empty()) {
48 HIVIEW_LOGW("Register publisher empty name.");
49 return;
50 }
51 // force update publisher constructor
52 auto publisherMap = GetPublisherRegistryMap();
53 if (publisherMap->find(name) == publisherMap->end()) {
54 publisherMap->insert(std::pair<std::string, bool>(name, true));
55 } else {
56 HIVIEW_LOGW("publisher %{public}s already exists! register publisher failed", name.c_str());
57 }
58 HIVIEW_LOGI("Register publisher constructor from %{public}s.", name.c_str());
59 }
60
UnregisterPublisher(const std::string & name)61 void AppEventPublisherFactory::UnregisterPublisher(const std::string& name)
62 {
63 HIVIEW_LOGD("UnregisterPublisher from %{public}s.", name.c_str());
64 auto publisherMap = GetPublisherRegistryMap();
65 publisherMap->erase(name);
66 }
67 } // namespace HiviewDFX
68 } // namespace OHOS