1 /*
2 * Copyright (c) 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 "push_callback_stub.h"
17
18 #include "ans_log_wrapper.h"
19 #include "event_handler.h"
20 #include "ipc_types.h"
21 #include "message_parcel.h"
22 #include "push_callback_proxy.h"
23 #include "singleton.h"
24
25 using namespace OHOS::AppExecFwk;
26 namespace OHOS {
27 namespace Notification {
PushCallBackStub()28 PushCallBackStub::PushCallBackStub() {}
29
~PushCallBackStub()30 PushCallBackStub::~PushCallBackStub() {}
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int PushCallBackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
33 {
34 ANS_LOGD("called.");
35 if (data.ReadInterfaceToken() != GetDescriptor()) {
36 ANS_LOGE("local descriptor is not equal to remote");
37 return ERR_INVALID_STATE;
38 }
39 switch (code) {
40 case static_cast<uint32_t>(NotificationInterfaceCode::ON_CHECK_NOTIFICATION): {
41 auto notificationData = data.ReadString();
42 bool ret = false;
43 std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>(EventRunner::GetMainEventRunner());
44 wptr<PushCallBackStub> weak = this;
45 if (handler) {
46 handler->PostSyncTask([weak, notificationData, &ret]() {
47 auto pushCallBackStub = weak.promote();
48 if (pushCallBackStub == nullptr) {
49 ANS_LOGE("pushCallBackStub is nullptr!");
50 ret = false;
51 return;
52 }
53 ret = pushCallBackStub->OnCheckNotification(notificationData);
54 });
55 }
56 ANS_LOGI("ret:%{public}d", ret);
57 if (!reply.WriteBool(ret)) {
58 ANS_LOGE("Failed to write reply ");
59 return ERR_INVALID_REPLY;
60 }
61 return NO_ERROR;
62 }
63 default: {
64 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65 }
66 }
67 }
68
OnCheckNotification(const std::string & notificationData)69 bool PushCallBackProxy::OnCheckNotification(const std::string ¬ificationData)
70 {
71 MessageParcel data;
72 MessageParcel reply;
73 MessageOption option;
74
75 if (!data.WriteInterfaceToken(PushCallBackProxy::GetDescriptor())) {
76 ANS_LOGE("Write interface token failed.");
77 return false;
78 }
79
80 if (!data.WriteString(notificationData)) {
81 ANS_LOGE("Connect done element error.");
82 return false;
83 }
84
85 auto remote = Remote();
86 if (remote == nullptr) {
87 ANS_LOGE("Get Remote fail.");
88 return false;
89 }
90
91 int error = remote->SendRequest(static_cast<uint32_t>(NotificationInterfaceCode::ON_CHECK_NOTIFICATION),
92 data, reply, option);
93 if (error != NO_ERROR) {
94 ANS_LOGE("Connect done fail, error: %{public}d", error);
95 return false;
96 }
97
98 return reply.ReadBool();
99 }
100 } // namespace Notification
101 } // namespace OHOS
102