• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<PushCallBackParam> pushCallBackParam = std::make_shared<PushCallBackParam>();
87             checkResult = this->OnCheckNotification(notificationData, pushCallBackParam);
88             checkResult = ConvertPushCheckCodeToErrCode(checkResult);
89             ANS_LOGI("Push check result:%{public}d,eventControl:%{public}s",
90                 checkResult, pushCallBackParam->eventControl.c_str());
91 
92             if (!reply.WriteInt32(checkResult)) {
93                 ANS_LOGE("Failed to write reply ");
94                 return ERR_INVALID_REPLY;
95             }
96             if (!reply.WriteString(pushCallBackParam->eventControl)) {
97                 ANS_LOGE("Failed to write reply ");
98                 return ERR_INVALID_REPLY;
99             }
100             return NO_ERROR;
101         }
102 
103         default: {
104             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
105         }
106     }
107 }
108 
OnCheckNotification(const std::string & notificationData,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)109 int32_t PushCallBackProxy::OnCheckNotification(
110     const std::string &notificationData, const std::shared_ptr<PushCallBackParam> &pushCallBackParam)
111 {
112     MessageParcel data;
113     MessageParcel reply;
114     MessageOption option;
115 
116     if (!data.WriteInterfaceToken(PushCallBackProxy::GetDescriptor())) {
117         ANS_LOGE("Write interface token failed.");
118         return false;
119     }
120 
121     if (!data.WriteString(notificationData)) {
122         ANS_LOGE("Connect done element error.");
123         return false;
124     }
125 
126     auto remote = Remote();
127     if (remote == nullptr) {
128         ANS_LOGE("null remote");
129         return false;
130     }
131 
132     int error = remote->SendRequest(static_cast<uint32_t>(NotificationInterfaceCode::ON_CHECK_NOTIFICATION),
133         data, reply, option);
134     if (error != NO_ERROR) {
135         ANS_LOGE("error: %{public}d", error);
136         return false;
137     }
138 
139     int result = reply.ReadInt32();
140     std::string eventControl;
141     if (reply.ReadString(eventControl)) {
142         HandleEventControl(eventControl, pushCallBackParam);
143     }
144     return result;
145 }
146 
HandleEventControl(std::string eventControl,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)147 void PushCallBackProxy::HandleEventControl(
148     std::string eventControl, const std::shared_ptr<PushCallBackParam> &pushCallBackParam)
149 {
150     if (pushCallBackParam == nullptr) {
151         ANS_LOGE("null pushCallBackParam");
152         return;
153     }
154     std::string event = pushCallBackParam->event;
155     if (event.empty()) {
156         ANS_LOGE("null event");
157         return;
158     }
159     ANS_LOGI("eventControl:%{public}s,event:%{public}s", eventControl.c_str(), event.c_str());
160     if (eventControl.empty() || !nlohmann::json::accept(eventControl)) {
161         return;
162     }
163     auto jsonObject = nlohmann::json::parse(eventControl);
164     if (jsonObject.is_null() || !jsonObject.is_object()) {
165         ANS_LOGE("jsonObject is not right");
166         return;
167     }
168     if (jsonObject.find(event)  == jsonObject.cend()) {
169         ANS_LOGE("This event has not eventControl");
170         return;
171     }
172     pushCallBackParam->eventControl = jsonObject.at(event).dump();
173 }
174 } // namespace Notification
175 } // namespace OHOS
176