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 "drag_server.h"
17
18 #include "drag_params.h"
19 #include "devicestatus_define.h"
20
21 #undef LOG_TAG
22 #define LOG_TAG "DragServer"
23
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27
DragServer(IContext * env)28 DragServer::DragServer(IContext *env)
29 : env_(env)
30 {}
31
Enable(CallingContext & context,MessageParcel & data,MessageParcel & reply)32 int32_t DragServer::Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
33 {
34 CALL_DEBUG_ENTER;
35 return RET_ERR;
36 }
37
Disable(CallingContext & context,MessageParcel & data,MessageParcel & reply)38 int32_t DragServer::Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
39 {
40 CALL_DEBUG_ENTER;
41 return RET_ERR;
42 }
43
Start(CallingContext & context,MessageParcel & data,MessageParcel & reply)44 int32_t DragServer::Start(CallingContext &context, MessageParcel &data, MessageParcel &reply)
45 {
46 CALL_DEBUG_ENTER;
47 DragData dragData {};
48 StartDragParam param { dragData };
49
50 if (!param.Unmarshalling(data)) {
51 FI_HILOGE("Failed to unmarshalling param");
52 return RET_ERR;
53 }
54 CHKPR(env_, RET_ERR);
55 auto session = env_->GetSocketSessionManager().FindSessionByPid(context.pid);
56 CHKPR(session, RET_ERR);
57 session->SetProgramName(GetPackageName(context.tokenId));
58 return env_->GetDragManager().StartDrag(dragData, context.pid);
59 }
60
Stop(CallingContext & context,MessageParcel & data,MessageParcel & reply)61 int32_t DragServer::Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply)
62 {
63 CALL_DEBUG_ENTER;
64 StopDragParam param {};
65
66 if (!param.Unmarshalling(data)) {
67 FI_HILOGE("Failed to unmarshalling param");
68 return RET_ERR;
69 }
70 CHKPR(env_, RET_ERR);
71 return env_->GetDragManager().StopDrag(param.dropResult_, GetPackageName(context.tokenId));
72 }
73
AddWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)74 int32_t DragServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
75 {
76 CALL_DEBUG_ENTER;
77 switch (id) {
78 case DragRequestID::ADD_DRAG_LISTENER: {
79 FI_HILOGI("Add drag listener, from:%{public}d", context.pid);
80 return env_->GetDragManager().AddListener(context.pid);
81 }
82 case DragRequestID::ADD_SUBSCRIPT_LISTENER: {
83 FI_HILOGD("Add subscript listener, from:%{public}d", context.pid);
84 return env_->GetDragManager().AddSubscriptListener(context.pid);
85 }
86 default: {
87 FI_HILOGE("Unexpected request ID (%{public}u)", id);
88 return RET_ERR;
89 }
90 }
91 }
92
RemoveWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)93 int32_t DragServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
94 {
95 CALL_DEBUG_ENTER;
96 switch (id) {
97 case DragRequestID::REMOVE_DRAG_LISTENER: {
98 FI_HILOGD("Remove drag listener, from:%{public}d", context.pid);
99 return env_->GetDragManager().RemoveListener(context.pid);
100 }
101 case DragRequestID::REMOVE_SUBSCRIPT_LISTENER: {
102 FI_HILOGD("Remove subscript listener, from:%{public}d", context.pid);
103 return env_->GetDragManager().RemoveSubscriptListener(context.pid);
104 }
105 default: {
106 FI_HILOGE("Unexpected request ID (%{public}u)", id);
107 return RET_ERR;
108 }
109 }
110 }
111
SetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)112 int32_t DragServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
113 {
114 CALL_DEBUG_ENTER;
115 switch (id) {
116 case DragRequestID::SET_DRAG_WINDOW_VISIBLE: {
117 return SetDragWindowVisible(context, data, reply);
118 }
119 case DragRequestID::UPDATE_DRAG_STYLE: {
120 return UpdateDragStyle(context, data, reply);
121 }
122 case DragRequestID::UPDATE_SHADOW_PIC: {
123 return UpdateShadowPic(context, data, reply);
124 }
125 case DragRequestID::UPDATE_PREVIEW_STYLE: {
126 return UpdatePreviewStyle(context, data, reply);
127 }
128 case DragRequestID::UPDATE_PREVIEW_STYLE_WITH_ANIMATION: {
129 return UpdatePreviewAnimation(context, data, reply);
130 }
131 default: {
132 FI_HILOGE("Unexpected request ID (%{public}u)", id);
133 return RET_ERR;
134 }
135 }
136 }
137
GetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)138 int32_t DragServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
139 {
140 CALL_DEBUG_ENTER;
141 switch (id) {
142 case DragRequestID::GET_DRAG_TARGET_PID: {
143 FI_HILOGI("Get drag target pid, from:%{public}d", context.pid);
144 return GetDragTargetPid(context, data, reply);
145 }
146 case DragRequestID::GET_UDKEY: {
147 FI_HILOGI("Get udkey, from:%{public}d", context.pid);
148 return GetUdKey(context, data, reply);
149 }
150 case DragRequestID::GET_SHADOW_OFFSET: {
151 FI_HILOGI("Get shadow offset, from:%{public}d", context.pid);
152 return GetShadowOffset(context, data, reply);
153 }
154 case DragRequestID::GET_DRAG_DATA: {
155 FI_HILOGI("Get drag data, from:%{public}d", context.pid);
156 return GetDragData(context, data, reply);
157 }
158 case DragRequestID::GET_DRAG_STATE: {
159 FI_HILOGI("Get drag state, from:%{public}d", context.pid);
160 return GetDragState(context, data, reply);
161 }
162 case DragRequestID::GET_DRAG_SUMMARY: {
163 FI_HILOGI("Get drag summary, from:%{public}d", context.pid);
164 return GetDragSummary(context, data, reply);
165 }
166 case DragRequestID::GET_DRAG_ACTION: {
167 FI_HILOGI("Get drag action, from:%{public}d", context.pid);
168 return GetDragAction(context, data, reply);
169 }
170 case DragRequestID::GET_EXTRA_INFO: {
171 FI_HILOGI("Get extra info, from:%{public}d", context.pid);
172 return GetExtraInfo(context, data, reply);
173 }
174 default: {
175 FI_HILOGE("Unexpected request ID (%{public}u)", id);
176 return RET_ERR;
177 }
178 }
179 }
180
Control(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)181 int32_t DragServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
182 {
183 CALL_DEBUG_ENTER;
184 switch (id) {
185 case DragRequestID::ADD_PRIVILEGE: {
186 FI_HILOGI("Add privilege, from:%{public}d", context.pid);
187 return env_->GetDragManager().AddPrivilege(context.tokenId);
188 }
189 case DragRequestID::ENTER_TEXT_EDITOR_AREA: {
190 FI_HILOGI("Enter text editor area, from:%{public}d", context.pid);
191 return EnterTextEditorArea(context, data, reply);
192 }
193 default: {
194 FI_HILOGE("Unexpected request ID (%{public}u)", id);
195 return RET_ERR;
196 }
197 }
198 }
199
SetDragWindowVisible(CallingContext & context,MessageParcel & data,MessageParcel & reply)200 int32_t DragServer::SetDragWindowVisible(CallingContext &context, MessageParcel &data, MessageParcel &reply)
201 {
202 SetDragWindowVisibleParam param {};
203
204 if (!param.Unmarshalling(data)) {
205 FI_HILOGE("SetDragWindowVisibleParam::Unmarshalling fail");
206 return RET_ERR;
207 }
208 FI_HILOGI("SetDragWindowVisible(%{public}d, %{public}d)", param.visible_, param.isForce_);
209 return env_->GetDragManager().OnSetDragWindowVisible(param.visible_, param.isForce_);
210 }
211
UpdateDragStyle(CallingContext & context,MessageParcel & data,MessageParcel & reply)212 int32_t DragServer::UpdateDragStyle(CallingContext &context, MessageParcel &data, MessageParcel &reply)
213 {
214 UpdateDragStyleParam param {};
215
216 if (!param.Unmarshalling(data)) {
217 FI_HILOGE("UpdateDragStyleParam::Unmarshalling fail");
218 return RET_ERR;
219 }
220 FI_HILOGI("UpdateDragStyle(%{public}d)", static_cast<int32_t>(param.cursorStyle_));
221 return env_->GetDragManager().UpdateDragStyle(param.cursorStyle_, context.pid, context.tokenId);
222 }
223
UpdateShadowPic(CallingContext & context,MessageParcel & data,MessageParcel & reply)224 int32_t DragServer::UpdateShadowPic(CallingContext &context, MessageParcel &data, MessageParcel &reply)
225 {
226 UpdateShadowPicParam param {};
227
228 if (!param.Unmarshalling(data)) {
229 FI_HILOGE("UpdateShadowPicParam::Unmarshalling fail");
230 return RET_ERR;
231 }
232 FI_HILOGD("Updata shadow pic");
233 return env_->GetDragManager().UpdateShadowPic(param.shadowInfo_);
234 }
235
UpdatePreviewStyle(CallingContext & context,MessageParcel & data,MessageParcel & reply)236 int32_t DragServer::UpdatePreviewStyle(CallingContext &context, MessageParcel &data, MessageParcel &reply)
237 {
238 UpdatePreviewStyleParam param {};
239
240 if (!param.Unmarshalling(data)) {
241 FI_HILOGE("UpdatePreviewStyleParam::Unmarshalling fail");
242 return RET_ERR;
243 }
244 return env_->GetDragManager().UpdatePreviewStyle(param.previewStyle_);
245 }
246
UpdatePreviewAnimation(CallingContext & context,MessageParcel & data,MessageParcel & reply)247 int32_t DragServer::UpdatePreviewAnimation(CallingContext &context, MessageParcel &data, MessageParcel &reply)
248 {
249 UpdatePreviewAnimationParam param {};
250
251 if (!param.Unmarshalling(data)) {
252 FI_HILOGE("UpdatePreviewAnimationParam::Unmarshalling fail");
253 return RET_ERR;
254 }
255 return env_->GetDragManager().UpdatePreviewStyleWithAnimation(param.previewStyle_, param.previewAnimation_);
256 }
257
GetDragTargetPid(CallingContext & context,MessageParcel & data,MessageParcel & reply)258 int32_t DragServer::GetDragTargetPid(CallingContext &context, MessageParcel &data, MessageParcel &reply)
259 {
260 int32_t targetPid = env_->GetDragManager().GetDragTargetPid();
261 GetDragTargetPidReply targetPidReply { targetPid };
262
263 if (!targetPidReply.Marshalling(reply)) {
264 FI_HILOGE("GetDragTargetPidReply::Marshalling fail");
265 return RET_ERR;
266 }
267 return RET_OK;
268 }
269
GetUdKey(CallingContext & context,MessageParcel & data,MessageParcel & reply)270 int32_t DragServer::GetUdKey(CallingContext &context, MessageParcel &data, MessageParcel &reply)
271 {
272 std::string udKey;
273
274 int32_t ret = env_->GetDragManager().GetUdKey(udKey);
275 if (ret != RET_OK) {
276 FI_HILOGE("IDragManager::GetUdKey fail, error:%{public}d", ret);
277 return ret;
278 }
279 GetUdKeyReply udKeyReply { std::move(udKey) };
280
281 if (!udKeyReply.Marshalling(reply)) {
282 FI_HILOGE("GetUdKeyReply::Marshalling fail");
283 return RET_ERR;
284 }
285 return RET_OK;
286 }
287
GetShadowOffset(CallingContext & context,MessageParcel & data,MessageParcel & reply)288 int32_t DragServer::GetShadowOffset(CallingContext &context, MessageParcel &data, MessageParcel &reply)
289 {
290 ShadowOffset shadowOffset {};
291
292 int32_t ret = env_->GetDragManager().OnGetShadowOffset(shadowOffset);
293 if (ret != RET_OK) {
294 FI_HILOGE("IDragManager::GetShadowOffset fail, error:%{public}d", ret);
295 return ret;
296 }
297 GetShadowOffsetReply shadowOffsetReply { shadowOffset };
298
299 if (!shadowOffsetReply.Marshalling(reply)) {
300 FI_HILOGE("GetShadowOffsetReply::Marshalling fail");
301 return RET_ERR;
302 }
303 return RET_OK;
304 }
305
GetDragData(CallingContext & context,MessageParcel & data,MessageParcel & reply)306 int32_t DragServer::GetDragData(CallingContext &context, MessageParcel &data, MessageParcel &reply)
307 {
308 DragData dragData {};
309
310 int32_t ret = env_->GetDragManager().GetDragData(dragData);
311 if (ret != RET_OK) {
312 FI_HILOGE("IDragManager::GetDragData fail, error:%{public}d", ret);
313 return ret;
314 }
315 GetDragDataReply dragDataReply { static_cast<const DragData&>(dragData) };
316
317 if (!dragDataReply.Marshalling(reply)) {
318 FI_HILOGE("GetDragDataReply::Marshalling fail");
319 return RET_ERR;
320 }
321 return RET_OK;
322 }
323
GetDragState(CallingContext & context,MessageParcel & data,MessageParcel & reply)324 int32_t DragServer::GetDragState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
325 {
326 DragState dragState {};
327
328 int32_t ret = env_->GetDragManager().GetDragState(dragState);
329 if (ret != RET_OK) {
330 FI_HILOGE("IDragManager::GetDragState fail, error:%{public}d", ret);
331 return ret;
332 }
333 GetDragStateReply dragStateReply { dragState };
334
335 if (!dragStateReply.Marshalling(reply)) {
336 FI_HILOGE("GetDragStateReply::Marshalling fail");
337 return RET_ERR;
338 }
339 return RET_OK;
340 }
341
GetDragSummary(CallingContext & context,MessageParcel & data,MessageParcel & reply)342 int32_t DragServer::GetDragSummary(CallingContext &context, MessageParcel &data, MessageParcel &reply)
343 {
344 std::map<std::string, int64_t> summaries;
345
346 int32_t ret = env_->GetDragManager().GetDragSummary(summaries);
347 if (ret != RET_OK) {
348 FI_HILOGE("IDragManager::GetDragSummary fail, error:%{public}d", ret);
349 return ret;
350 }
351 GetDragSummaryReply summaryReply { std::move(summaries) };
352
353 if (!summaryReply.Marshalling(reply)) {
354 FI_HILOGE("GetDragSummaryReply::Marshalling fail");
355 return RET_ERR;
356 }
357 return RET_OK;
358 }
359
GetDragAction(CallingContext & context,MessageParcel & data,MessageParcel & reply)360 int32_t DragServer::GetDragAction(CallingContext &context, MessageParcel &data, MessageParcel &reply)
361 {
362 DragAction dragAction {};
363
364 int32_t ret = env_->GetDragManager().GetDragAction(dragAction);
365 if (ret != RET_OK) {
366 FI_HILOGE("IDragManager::GetDragSummary fail, error:%{public}d", ret);
367 return ret;
368 }
369 GetDragActionReply dragActionReply { dragAction };
370
371 if (!dragActionReply.Marshalling(reply)) {
372 FI_HILOGE("GetDragActionReply::Marshalling fail");
373 return RET_ERR;
374 }
375 return RET_OK;
376 }
377
GetExtraInfo(CallingContext & context,MessageParcel & data,MessageParcel & reply)378 int32_t DragServer::GetExtraInfo(CallingContext &context, MessageParcel &data, MessageParcel &reply)
379 {
380 std::string extraInfo;
381
382 int32_t ret = env_->GetDragManager().GetExtraInfo(extraInfo);
383 if (ret != RET_OK) {
384 FI_HILOGE("IDragManager::GetExtraInfo fail, error:%{public}d", ret);
385 return ret;
386 }
387 GetExtraInfoReply extraInfoReply { std::move(extraInfo) };
388
389 if (!extraInfoReply.Marshalling(reply)) {
390 FI_HILOGE("GetExtraInfoReply::Marshalling fail");
391 return RET_ERR;
392 }
393 return RET_OK;
394 }
395
EnterTextEditorArea(CallingContext & context,MessageParcel & data,MessageParcel & reply)396 int32_t DragServer::EnterTextEditorArea(CallingContext &context, MessageParcel &data, MessageParcel &reply)
397 {
398 EnterTextEditorAreaParam param {};
399
400 if (!param.Unmarshalling(data)) {
401 FI_HILOGE("EnterTextEditorAreaParam::Unmarshalling fail");
402 return RET_ERR;
403 }
404 return env_->GetDragManager().EnterTextEditorArea(param.state);
405 }
406
GetPackageName(Security::AccessToken::AccessTokenID tokenId)407 std::string DragServer::GetPackageName(Security::AccessToken::AccessTokenID tokenId)
408 {
409 std::string packageName = std::string();
410 int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
411 switch (tokenType) {
412 case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
413 Security::AccessToken::HapTokenInfo hapInfo;
414 if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) {
415 FI_HILOGE("Get hap token info failed");
416 } else {
417 packageName = hapInfo.bundleName;
418 }
419 break;
420 }
421 case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE:
422 case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: {
423 Security::AccessToken::NativeTokenInfo tokenInfo;
424 if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
425 FI_HILOGE("Get native token info failed");
426 } else {
427 packageName = tokenInfo.processName;
428 }
429 break;
430 }
431 default: {
432 FI_HILOGW("token type not match");
433 break;
434 }
435 }
436 return packageName;
437 }
438 } // namespace DeviceStatus
439 } // namespace Msdp
440 } // namespace OHOS