1 /*
2 * Copyright (c) 2025-2025 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 "feature/param_update/param_common_event.h"
17
18 #include <common_event_data.h>
19 #include <common_event_manager.h>
20 #include <common_event_support.h>
21 #include "common_event_subscriber.h"
22 #include <memory>
23 #include <unistd.h>
24
25 #include "feature/param_update/param_manager.h"
26 #include "fms_log_wrapper.h"
27 #include "form_mgr_errors.h"
28
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 constexpr int32_t RETRY_SUBSCRIBER = 3;
33 const std::string EVENT_INFO_TYPE = "type";
34 const std::string EVENT_INFO_SUBTYPE = "subtype";
35 const std::string CFG_UPDATED_ACTION = "usual.event.DUE_SA_CFG_UPDATED";
36 const std::string PARAM_CONFIG_TYPE = "FormMgrConfig";
37 const std::string RECEIVE_UPDATE_PERMISSION = "ohos.permission.RECEIVE_UPDATE_MESSAGE";
38 }
39
ParamCommonEvent()40 ParamCommonEvent::ParamCommonEvent()
41 {
42 HILOG_INFO("call");
43 handleEventFunc_[CFG_UPDATED_ACTION] = &ParamCommonEvent::HandleParamUpdate;
44 for (auto it = handleEventFunc_.begin(); it != handleEventFunc_.end(); ++it) {
45 HILOG_INFO("Add event: %{public}s", it->first.c_str());
46 eventHandles_.emplace(it->first, std::bind(it->second, this, std::placeholders::_1));
47 }
48 }
49
~ParamCommonEvent()50 ParamCommonEvent::~ParamCommonEvent()
51 {
52 UnSubscriberEvent();
53 }
54
SubscriberEvent()55 void ParamCommonEvent::SubscriberEvent()
56 {
57 HILOG_INFO("call");
58 if (subscriber_) {
59 HILOG_WARN("Common Event is already subscribered!");
60 return;
61 }
62 EventFwk::MatchingSkills matchingSkills;
63 for (auto &event : handleEventFunc_) {
64 HILOG_INFO("Add event: %{public}s", event.first.c_str());
65 matchingSkills.AddEvent(event.first);
66 }
67 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
68 subscribeInfo.SetPermission(RECEIVE_UPDATE_PERMISSION);
69 subscriber_ = std::make_shared<ParamCommonEventSubscriber>(subscribeInfo, *this);
70
71 int32_t retry = RETRY_SUBSCRIBER;
72 do {
73 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
74 if (subscribeResult) {
75 HILOG_INFO("SubscriberEvent success.");
76 return;
77 } else {
78 HILOG_ERROR("SubscriberEvent failed, retry %{public}d", retry);
79 retry--;
80 sleep(1);
81 }
82 } while (retry);
83 HILOG_ERROR("SubscriberEvent failed.");
84 }
85
UnSubscriberEvent()86 void ParamCommonEvent::UnSubscriberEvent()
87 {
88 HILOG_INFO("call");
89 eventHandles_.clear();
90 handleEventFunc_.clear();
91 if (subscriber_) {
92 EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
93 subscriber_ = nullptr;
94 }
95 HILOG_INFO("UnSubscriberEvent end.");
96 }
97
OnReceiveEvent(const AAFwk::Want & want)98 void ParamCommonEvent::OnReceiveEvent(const AAFwk::Want &want)
99 {
100 std::string action = want.GetAction();
101 auto it = eventHandles_.find(action);
102 if (it == eventHandles_.end()) {
103 HILOG_WARN("Ignore event: %{public}s", action.c_str());
104 return;
105 }
106 it->second(want);
107 }
108
109
HandleParamUpdate(const AAFwk::Want & want) const110 void ParamCommonEvent::HandleParamUpdate(const AAFwk::Want &want) const
111 {
112 std::string action = want.GetAction();
113 std::string type = want.GetStringParam(EVENT_INFO_TYPE);
114 std::string subtype = want.GetStringParam(EVENT_INFO_SUBTYPE);
115 HILOG_INFO("param update event: %{public}s, %{public}s, %{public}s", action.c_str(),
116 type.c_str(), subtype.c_str());
117 if (action != CFG_UPDATED_ACTION || type != PARAM_CONFIG_TYPE) {
118 HILOG_WARN("Ignore type: %{public}s", type.c_str());
119 return;
120 }
121 ParamManager::GetInstance().InitParam();
122 }
123 } // namespace AppExecFwk
124 } // namespace OHOS