1 /*
2 * Copyright (c) 2021-2022 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 "query_sys_event_callback_proxy.h"
17
18 #include "ash_mem_utils.h"
19 #include "errors.h"
20 #include "hiview_logger.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 DEFINE_LOG_TAG("HiView-QuerySysEventCallbackProxy");
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)25 void QuerySysEventCallbackProxy::OnQuery(const std::vector<std::u16string>& sysEvent, const std::vector<int64_t>& seq)
26 {
27 auto remote = Remote();
28 if (remote == nullptr) {
29 HIVIEW_LOGE("SysEventService Remote is NULL.");
30 return;
31 }
32 MessageParcel data;
33 if (!data.WriteInterfaceToken(QuerySysEventCallbackProxy::GetDescriptor())) {
34 HIVIEW_LOGE("write descriptor failed.");
35 return;
36 }
37 auto ashMemory = AshMemUtils::WriteBulkData(data, sysEvent);
38 if (ashMemory == nullptr) {
39 HIVIEW_LOGE("write sys event failed.");
40 return;
41 }
42 allAshMemories.emplace_back(ashMemory);
43 auto ret = data.WriteInt64Vector(seq);
44 if (!ret) {
45 HIVIEW_LOGE("write sys seq failed.");
46 return;
47 }
48 MessageParcel reply;
49 MessageOption option = {MessageOption::TF_ASYNC};
50 int32_t res = remote->SendRequest(static_cast<uint32_t>(ON_QUERY), data, reply, option);
51 if (res != ERR_OK) {
52 HIVIEW_LOGE("send request failed, error is %{public}d.", res);
53 }
54 }
55
OnComplete(int32_t reason,int32_t total,int64_t seq)56 void QuerySysEventCallbackProxy::OnComplete(int32_t reason, int32_t total, int64_t seq)
57 {
58 auto remote = Remote();
59 if (remote == nullptr) {
60 HIVIEW_LOGE("SysEventService Remote is NULL.");
61 return;
62 }
63 MessageParcel data;
64 if (!data.WriteInterfaceToken(QuerySysEventCallbackProxy::GetDescriptor())) {
65 HIVIEW_LOGE("write descriptor failed.");
66 return;
67 }
68 bool ret = data.WriteInt32(reason) && data.WriteInt32(total);
69 if (!ret) {
70 HIVIEW_LOGE("write params failed.");
71 return;
72 }
73 ret = data.WriteInt64(seq);
74 if (!ret) {
75 HIVIEW_LOGE("write seq failed.");
76 return;
77 }
78 MessageParcel reply;
79 MessageOption option = {MessageOption::TF_ASYNC};
80 int32_t res = remote->SendRequest(static_cast<uint32_t>(ON_COMPLETE), data, reply, option);
81 if (res != ERR_OK) {
82 HIVIEW_LOGE("send request failed, error is %{public}d.", res);
83 }
84 }
85
~QuerySysEventCallbackProxy()86 QuerySysEventCallbackProxy::~QuerySysEventCallbackProxy()
87 {
88 ClearAllAshMemories();
89 }
90
ClearAllAshMemories()91 void QuerySysEventCallbackProxy::ClearAllAshMemories()
92 {
93 for_each(allAshMemories.begin(), allAshMemories.end(), [] (auto& ashMem) {
94 AshMemUtils::CloseAshmem(ashMem);
95 });
96 allAshMemories.clear();
97 }
98 } // namespace HiviewDFX
99 } // namespace OHOS