1 /*
2  * Copyright (c) 2021-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 "account_event_provider.h"
17 #include "account_info.h"
18 #ifdef HAS_CES_PART
19 #include <common_event_data.h>
20 #include <common_event_support.h>
21 #endif // HAS_CES_PART
22 #include "account_log_wrapper.h"
23 #ifdef HAS_CES_PART
24 #include "common_event_manager.h"
25 #include "want.h"
26 #endif // HAS_CES_PART
27 #include "account_hisysevent_adapter.h"
28 #include "hitrace_adapter.h"
29 #include "common_event_constant.h"
30 
31 #ifdef HAS_CES_PART
32 using namespace OHOS::EventFwk;
33 #endif // HAS_CES_PART
34 
35 namespace OHOS {
36 namespace AccountSA {
EventPublish(const std::string & event,int32_t userId,const DomainAccountEventData * report)37 bool AccountEventProvider::EventPublish(const std::string& event, int32_t userId, const DomainAccountEventData *report)
38 {
39 #ifdef HAS_CES_PART
40     Want want;
41     want.SetAction(event);
42     CommonEventData data;
43     if (event == EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED) {
44         data.SetCode(userId);
45     } else if (event == EventFwk::CommonEventSupport::COMMON_EVENT_DOMAIN_ACCOUNT_STATUS_CHANGED) {
46         if (report == nullptr) {
47             ACCOUNT_LOGE("Report is nullptr");
48             return false;
49         }
50         want.SetParam("domain", report->domainAccountInfo.domain_);
51         want.SetParam("accountName", report->domainAccountInfo.accountName_);
52         want.SetParam("event", report->event);
53         want.SetParam("status", report->status);
54     } else {
55         want.SetParam("userId", userId);
56     }
57 
58     data.SetWant(want);
59     StartTraceAdapter("Ohos account event publish.");
60     /* publish */
61     if (!CommonEventManager::PublishCommonEvent(data)) {
62         ACCOUNT_LOGE("PublishCommonEvent failed! event %{public}s. userId is %{public}d", event.c_str(), userId);
63         ReportOhosAccountOperationFail(userId, EVENT_PUBLISH, false, "PublishCommonEvent failed");
64         FinishTraceAdapter();
65         return false;
66     } else {
67         ACCOUNT_LOGI("PublishCommonEvent succeed! event %{public}s.", event.c_str());
68     }
69     FinishTraceAdapter();
70 #else // HAS_CES_PART
71     ACCOUNT_LOGI("No common event part, do not publish anything! event %{public}s.", event.c_str());
72 #endif // HAS_CES_PART
73     return true;
74 }
75 
EventPublishAsUser(const std::string & event,int32_t userId)76 bool AccountEventProvider::EventPublishAsUser(const std::string& event, int32_t userId)
77 {
78     if (userId == UNDEFINED_USER) {
79         ACCOUNT_LOGE("EventPublishAsUser failed, userId is UNDEFINED_USER");
80         return EventPublish(event, userId, nullptr);
81     }
82 #ifdef HAS_CES_PART
83     Want want;
84     want.SetAction(event);
85     CommonEventData data;
86     if (event == EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED) {
87         data.SetCode(userId);
88     } else {
89         want.SetParam("userId", userId);
90     }
91 
92     data.SetWant(want);
93     StartTraceAdapter("Ohos account event publish.");
94     /* publish */
95     if (!CommonEventManager::PublishCommonEventAsUser(data, userId)) {
96         ACCOUNT_LOGE("PublishCommonEventAsUser failed! event %{public}s. userId is %{public}d", event.c_str(), userId);
97         ReportOhosAccountOperationFail(userId, EVENT_PUBLISH, false, "PublishCommonEventAsUser failed");
98         FinishTraceAdapter();
99         return false;
100     } else {
101         ACCOUNT_LOGI("PublishCommonEventAsUser succeed! event %{public}s.", event.c_str());
102     }
103     FinishTraceAdapter();
104 #else // HAS_CES_PART
105     ACCOUNT_LOGI("No common event part, do not publish anything! event %{public}s.", event.c_str());
106 #endif // HAS_CES_PART
107     return true;
108 }
109 } // namespace AccountSA
110 } // namespace OHOS