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 #include "ans_inner_errors.h"
25 #include "nlohmann/json.hpp"
26
27 using namespace OHOS::AppExecFwk;
28 namespace OHOS {
29 namespace Notification {
PushCallBackStub()30 PushCallBackStub::PushCallBackStub() {}
31
~PushCallBackStub()32 PushCallBackStub::~PushCallBackStub() {}
33
34 enum PushCheckErrCode : int32_t {
35 SUCCESS = 0,
36 FIXED_PARAMETER_INVALID = 1,
37 NETWORK_UNREACHABLE = 2,
38 SPECIFIED_NOTIFICATIONS_FAILED = 3,
39 SYSTEM_ERROR = 4,
40 OPTIONAL_PARAMETER_INVALID = 5
41 };
42
ConvertPushCheckCodeToErrCode(int32_t pushCheckCode)43 ErrCode PushCallBackStub::ConvertPushCheckCodeToErrCode(int32_t pushCheckCode)
44 {
45 ErrCode errCode;
46 PushCheckErrCode checkCode = static_cast<PushCheckErrCode>(pushCheckCode);
47
48 switch (checkCode) {
49 case PushCheckErrCode::SUCCESS:
50 errCode = ERR_OK;
51 break;
52 case PushCheckErrCode::FIXED_PARAMETER_INVALID:
53 errCode = ERR_ANS_TASK_ERR;
54 break;
55 case PushCheckErrCode::NETWORK_UNREACHABLE:
56 errCode = ERR_ANS_PUSH_CHECK_NETWORK_UNREACHABLE;
57 break;
58 case PushCheckErrCode::SPECIFIED_NOTIFICATIONS_FAILED:
59 errCode = ERR_ANS_PUSH_CHECK_FAILED;
60 break;
61 case PushCheckErrCode::SYSTEM_ERROR:
62 errCode = ERR_ANS_TASK_ERR;
63 break;
64 case PushCheckErrCode::OPTIONAL_PARAMETER_INVALID:
65 errCode = ERR_ANS_PUSH_CHECK_EXTRAINFO_INVALID;
66 break;
67 default:
68 errCode = ERR_ANS_PUSH_CHECK_FAILED;
69 break;
70 }
71 return errCode;
72 }
73
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)74 int PushCallBackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
75 {
76 ANS_LOGD("called.");
77 if (data.ReadInterfaceToken() != GetDescriptor()) {
78 ANS_LOGE("local descriptor is not equal to remote");
79 return ERR_INVALID_STATE;
80 }
81 switch (code) {
82 case static_cast<uint32_t>(NotificationInterfaceCode::ON_CHECK_NOTIFICATION): {
83 auto notificationData = data.ReadString();
84 int32_t checkResult = ERR_ANS_TASK_ERR;
85
86 std::shared_ptr<EventHandler> handler = std::make_shared<EventHandler>(EventRunner::GetMainEventRunner());
87 wptr<PushCallBackStub> weak = this;
88 std::shared_ptr<PushCallBackParam> pushCallBackParam = std::make_shared<PushCallBackParam>();
89 std::unique_lock<std::mutex> uniqueLock(pushCallBackParam->callBackMutex);
90 if (handler) {
91 handler->PostTask([weak, notificationData, pushCallBackParam]() {
92 auto pushCallBackStub = weak.promote();
93 if (pushCallBackStub == nullptr) {
94 ANS_LOGE("pushCallBackStub is nullptr!");
95 return;
96 }
97 pushCallBackStub->OnCheckNotification(notificationData, pushCallBackParam);
98 });
99 }
100
101 pushCallBackParam->callBackCondition.wait(uniqueLock, [=]() {return pushCallBackParam->ready; });
102 checkResult = ConvertPushCheckCodeToErrCode(pushCallBackParam->result);
103 ANS_LOGD("Push check result:%{public}d", checkResult);
104 if (!reply.WriteInt32(checkResult)) {
105 ANS_LOGE("Failed to write reply ");
106 return ERR_INVALID_REPLY;
107 }
108 return NO_ERROR;
109 }
110
111 default: {
112 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
113 }
114 }
115 }
116
OnCheckNotification(const std::string & notificationData,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)117 int32_t PushCallBackProxy::OnCheckNotification(
118 const std::string ¬ificationData, const std::shared_ptr<PushCallBackParam> &pushCallBackParam)
119 {
120 MessageParcel data;
121 MessageParcel reply;
122 MessageOption option;
123
124 if (!data.WriteInterfaceToken(PushCallBackProxy::GetDescriptor())) {
125 ANS_LOGE("Write interface token failed.");
126 return false;
127 }
128
129 if (!data.WriteString(notificationData)) {
130 ANS_LOGE("Connect done element error.");
131 return false;
132 }
133
134 auto remote = Remote();
135 if (remote == nullptr) {
136 ANS_LOGE("Get Remote fail.");
137 return false;
138 }
139
140 int error = remote->SendRequest(static_cast<uint32_t>(NotificationInterfaceCode::ON_CHECK_NOTIFICATION),
141 data, reply, option);
142 if (error != NO_ERROR) {
143 ANS_LOGE("Connect done fail, error: %{public}d", error);
144 return false;
145 }
146
147 int result = reply.ReadInt32();
148 std::string eventControl;
149 if (reply.ReadString(eventControl)) {
150 HandleEventControl(eventControl, pushCallBackParam);
151 }
152 return result;
153 }
154
HandleEventControl(std::string eventControl,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)155 void PushCallBackProxy::HandleEventControl(
156 std::string eventControl, const std::shared_ptr<PushCallBackParam> &pushCallBackParam)
157 {
158 if (pushCallBackParam == nullptr) {
159 ANS_LOGI("pushCallBackParam is null");
160 return;
161 }
162 std::string event = pushCallBackParam->event;
163 if (event.empty()) {
164 ANS_LOGI("event is null");
165 return;
166 }
167 ANS_LOGI("eventControl:%{public}s,event:%{public}s", eventControl.c_str(), event.c_str());
168 if (eventControl.empty() || !nlohmann::json::accept(eventControl)) {
169 return;
170 }
171 auto jsonObject = nlohmann::json::parse(eventControl);
172 if (jsonObject.is_null() || !jsonObject.is_object()) {
173 ANS_LOGE("jsonObject is not right");
174 return;
175 }
176 if (jsonObject.find(event) == jsonObject.cend()) {
177 ANS_LOGI("This event has not eventControl");
178 return;
179 }
180 pushCallBackParam->eventControl = jsonObject.at(event).dump();
181 }
182 } // namespace Notification
183 } // namespace OHOS
184