1 /*
2 * Copyright (C) 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 #include <unistd.h>
16 #include "download_service_stub.h"
17 #include "ipc_skeleton.h"
18 #include "message_parcel.h"
19 #include "download_common.h"
20 #include "download_service_interface.h"
21 #include "download_config.h"
22 #include "download_info.h"
23 #include "download_notify_interface.h"
24 #include "hilog/log_cpp.h"
25 #include "ipc_object_stub.h"
26 #include "ipc_types.h"
27 #include "iremote_broker.h"
28 #include "iremote_object.h"
29 #include "log.h"
30
31 namespace OHOS::Request::Download {
32 using namespace OHOS::HiviewDFX;
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t DownloadServiceStub::OnRemoteRequest(
35 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
36 {
37 DOWNLOAD_HILOGE("request code = %{public}d", code);
38 auto descriptorToken = data.ReadInterfaceToken();
39 if (descriptorToken != GetDescriptor()) {
40 DOWNLOAD_HILOGE("remote descriptor not the same as local descriptor");
41 return E_DOWNLOAD_TRANSACT_ERROR;
42 }
43 switch (code) {
44 case CMD_REQUEST:
45 return OnRequest(data, reply);
46 case CMD_PAUSE:
47 return OnPause(data, reply);
48 case CMD_QUERY:
49 return OnQuery(data, reply);
50 case CMD_QUERYMIMETYPE:
51 return OnQueryMimeType(data, reply);
52 case CMD_REMOVE:
53 return OnRemove(data, reply);
54 case CMD_RESUME:
55 return OnResume(data, reply);
56 case CMD_ON:
57 return OnEventOn(data, reply);
58 case CMD_OFF:
59 return OnEventOff(data, reply);
60 case CMD_CHECKPERMISSION:
61 return OnCheckPermission(data, reply);
62 default:
63 DOWNLOAD_HILOGE("Default value received, check needed.");
64 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65 }
66 return E_DOWNLOAD_OK;
67 }
68
OnRequest(MessageParcel & data,MessageParcel & reply)69 bool DownloadServiceStub::OnRequest(MessageParcel &data, MessageParcel &reply)
70 {
71 if (!CheckPermission()) {
72 DOWNLOAD_HILOGE("download no permission, pid:%{public}d", IPCSkeleton::GetCallingPid());
73 return false;
74 }
75 DOWNLOAD_HILOGD("Receive request");
76 DownloadConfig config;
77 int32_t fd = data.ReadFileDescriptor();
78 DOWNLOAD_HILOGI("Get FD from client, fd [%{public}d]", fd);
79 config.SetFD(fd);
80 config.SetFDError(data.ReadInt32());
81 config.SetUrl(data.ReadString());
82 config.SetMetered(data.ReadBool());
83 config.SetRoaming(data.ReadBool());
84 config.SetDescription(data.ReadString());
85 config.SetNetworkType(data.ReadUint32());
86 config.SetFilePath(data.ReadString());
87 config.SetTitle(data.ReadString());
88 config.SetDescription(data.ReadString());
89 config.SetBackground(data.ReadBool());
90 config.SetBundleName(data.ReadString());
91 config.SetApplicationInfoUid(data.ReadInt32());
92 uint32_t headerSize = data.ReadUint32();
93 size_t readAbleSize = data.GetReadableBytes() / MIN_HEADER_LENGTH;
94 if (static_cast<size_t>(headerSize) > readAbleSize) {
95 if (fd > 0) {
96 close(fd);
97 }
98 return false;
99 }
100 for (uint32_t i = 0; i < headerSize; i++) {
101 config.SetHeader(data.ReadString(), data.ReadString());
102 }
103 config.Dump();
104 ExceptionError err;
105 int32_t result = Request(config, err);
106 if (!reply.WriteInt32(result)) {
107 DOWNLOAD_HILOGE("WriteBool failed");
108 return false;
109 }
110 return true;
111 }
112
OnPause(MessageParcel & data,MessageParcel & reply)113 bool DownloadServiceStub::OnPause(MessageParcel &data, MessageParcel &reply)
114 {
115 if (!CheckPermission()) {
116 DOWNLOAD_HILOGE("pause no permission, pid:%{public}d", IPCSkeleton::GetCallingPid());
117 return false;
118 }
119 bool result = Pause(data.ReadUint32());
120 if (!reply.WriteBool(result)) {
121 DOWNLOAD_HILOGE("WriteBool failed");
122 return false;
123 }
124 return true;
125 }
126
OnQuery(MessageParcel & data,MessageParcel & reply)127 bool DownloadServiceStub::OnQuery(MessageParcel &data, MessageParcel &reply)
128 {
129 if (!CheckPermission()) {
130 DOWNLOAD_HILOGE("query no permission, pid:%{public}d", IPCSkeleton::GetCallingPid());
131 return false;
132 }
133 DownloadInfo info;
134 bool result = Query(data.ReadUint32(), info);
135 if (result) {
136 reply.WriteString(info.GetDescription());
137 reply.WriteInt64(info.GetDownloadedBytes());
138 reply.WriteUint32(info.GetDownloadId());
139 reply.WriteUint32(info.GetFailedReason());
140 reply.WriteString(info.GetFileName());
141 reply.WriteString(info.GetFilePath());
142 reply.WriteUint32(info.GetPausedReason());
143 reply.WriteUint32(info.GetStatus());
144 reply.WriteString(info.GetTargetURI());
145 reply.WriteString(info.GetDownloadTitle());
146 reply.WriteInt64(info.GetDownloadTotalBytes());
147 info.Dump();
148 }
149 if (!reply.WriteBool(result)) {
150 DOWNLOAD_HILOGE("WriteBool failed");
151 return false;
152 }
153 return true;
154 }
155
OnQueryMimeType(MessageParcel & data,MessageParcel & reply)156 bool DownloadServiceStub::OnQueryMimeType(MessageParcel &data, MessageParcel &reply)
157 {
158 if (!CheckPermission()) {
159 DOWNLOAD_HILOGE("query mime type no permission, pid:%{public}d", IPCSkeleton::GetCallingPid());
160 return false;
161 }
162 std::string mime;
163 bool result = QueryMimeType(data.ReadInt32(), mime);
164 if (result) {
165 reply.WriteString(mime);
166 }
167 if (!reply.WriteBool(result)) {
168 DOWNLOAD_HILOGE("WriteBool failed");
169 return false;
170 }
171 return true;
172 }
173
OnRemove(MessageParcel & data,MessageParcel & reply)174 bool DownloadServiceStub::OnRemove(MessageParcel &data, MessageParcel &reply)
175 {
176 if (!CheckPermission()) {
177 DOWNLOAD_HILOGE("query mime type no permission, pid:%{public}d", IPCSkeleton::GetCallingPid());
178 return false;
179 }
180 bool result = Remove(data.ReadInt32());
181 if (!reply.WriteBool(result)) {
182 DOWNLOAD_HILOGE("WriteBool failed");
183 return false;
184 }
185 return true;
186 }
187
OnResume(MessageParcel & data,MessageParcel & reply)188 bool DownloadServiceStub::OnResume(MessageParcel &data, MessageParcel &reply)
189 {
190 if (!CheckPermission()) {
191 DOWNLOAD_HILOGE("resume no permission, pid:%{public}d", IPCSkeleton::GetCallingPid());
192 return false;
193 }
194 bool result = Resume(data.ReadInt32());
195 if (!reply.WriteBool(result)) {
196 DOWNLOAD_HILOGE("WriteBool failed");
197 return false;
198 }
199 return true;
200 }
201
OnEventOn(MessageParcel & data,MessageParcel & reply)202 bool DownloadServiceStub::OnEventOn(MessageParcel &data, MessageParcel &reply)
203 {
204 if (!CheckPermission()) {
205 DOWNLOAD_HILOGE("register listener no permission, pid:%{public}d", IPCSkeleton::GetCallingPid());
206 return false;
207 }
208 uint32_t taskId = data.ReadUint32();
209 std::string type = data.ReadString();
210 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOn taskId = %{public}d type=%{public}s ", taskId, type.c_str());
211 if (type.empty()) {
212 DOWNLOAD_HILOGE("DownloadServiceStub::OnEventOn type is null.");
213 return false;
214 }
215 sptr<IRemoteObject> remote = data.ReadRemoteObject();
216 if (remote == nullptr) {
217 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOn remote is nullptr");
218 if (!reply.WriteInt32(ERR_NONE)) {
219 return false;
220 }
221 return true;
222 }
223 sptr<DownloadNotifyInterface> listener = iface_cast<DownloadNotifyInterface>(remote);
224 if (listener.GetRefPtr() == nullptr) {
225 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOn listener is null");
226 return false;
227 }
228 bool result = On(taskId, type, listener);
229 if (!reply.WriteBool(result)) {
230 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOn 4444");
231 return false;
232 }
233 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOn out");
234 return true;
235 }
236
OnEventOff(MessageParcel & data,MessageParcel & reply)237 bool DownloadServiceStub::OnEventOff(MessageParcel &data, MessageParcel &reply)
238 {
239 if (!CheckPermission()) {
240 DOWNLOAD_HILOGE("de-register listener, pid:%{public}d", IPCSkeleton::GetCallingPid());
241 return false;
242 }
243 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOff in");
244 uint32_t taskId = data.ReadUint32();
245 std::string type = data.ReadString();
246 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOff taskId = %{public}d type=%{public}s ", taskId, type.c_str());
247 bool result = Off(taskId, type);
248 if (!reply.WriteBool(result)) {
249 return false;
250 }
251 DOWNLOAD_HILOGD("DownloadServiceStub::OnEventOff out");
252 return true;
253 }
254
OnCheckPermission(MessageParcel & data,MessageParcel & reply)255 bool DownloadServiceStub::OnCheckPermission(MessageParcel &data, MessageParcel &reply)
256 {
257 DOWNLOAD_HILOGD("DownloadServiceStub::OnCheckPermission in");
258
259 bool result = CheckPermission();
260 if (!reply.WriteBool(result)) {
261 return false;
262 }
263 DOWNLOAD_HILOGD("DownloadServiceStub::OnCheckPermission out");
264 return true;
265 }
266 } // namespace OHOS::Request::Download
267