• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         REPORT_OHOS_ACCOUNT_FAIL(userId, EVENT_PUBLISH, false,
64             "PublishCommonEvent failed, event=" + event + ", userId=" + std::to_string(userId));
65         FinishTraceAdapter();
66         return false;
67     } else {
68         ACCOUNT_LOGI("PublishCommonEvent succeed! event %{public}s.", event.c_str());
69     }
70     FinishTraceAdapter();
71 #else // HAS_CES_PART
72     ACCOUNT_LOGI("No common event part, do not publish anything! event %{public}s.", event.c_str());
73 #endif // HAS_CES_PART
74     return true;
75 }
76 
EventPublishAsUser(const std::string & event,int32_t userId)77 bool AccountEventProvider::EventPublishAsUser(const std::string& event, int32_t userId)
78 {
79     if (userId == UNDEFINED_USER) {
80         ACCOUNT_LOGE("EventPublishAsUser failed, userId is UNDEFINED_USER");
81         return EventPublish(event, userId, nullptr);
82     }
83 #ifdef HAS_CES_PART
84     Want want;
85     want.SetAction(event);
86     CommonEventData data;
87     if (event == EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED) {
88         data.SetCode(userId);
89     } else {
90         want.SetParam("userId", userId);
91     }
92 
93     data.SetWant(want);
94     StartTraceAdapter("Ohos account event publish.");
95     /* publish */
96     if (!CommonEventManager::PublishCommonEventAsUser(data, userId)) {
97         ACCOUNT_LOGE("PublishCommonEventAsUser failed! event %{public}s. userId is %{public}d", event.c_str(), userId);
98         REPORT_OHOS_ACCOUNT_FAIL(userId, EVENT_PUBLISH, false,
99             "PublishCommonEventAsUser failed, event=" + event + ", userId=" + std::to_string(userId));
100         FinishTraceAdapter();
101         return false;
102     } else {
103         ACCOUNT_LOGI("PublishCommonEventAsUser succeed! event %{public}s.", event.c_str());
104     }
105     FinishTraceAdapter();
106 #else // HAS_CES_PART
107     ACCOUNT_LOGI("No common event part, do not publish anything! event %{public}s.", event.c_str());
108 #endif // HAS_CES_PART
109     return true;
110 }
111 } // namespace AccountSA
112 } // namespace OHOS