1 /*
2 * Copyright (c) 2023-2025 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 "tokenid_kit.h"
19
20 #include "accesstoken_kit.h"
21 #include "devicestatus_define.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "DragServer"
25
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29
30 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
DragServer(IContext * env)31 DragServer::DragServer(IContext *env)
32 : env_(env), universalDragWrapper_(env)
33 #else
34 DragServer::DragServer(IContext *env)
35 : env_(env)
36 #endif // OHOS_BUILD_UNIVERSAL_DRAG
37 {
38 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
39 bool ret = universalDragWrapper_.InitUniversalDrag();
40 if (!ret) {
41 FI_HILOGW("Init universal drag failed");
42 }
43 #endif // OHOS_BUILD_UNIVERSAL_DRAG
44 }
45
StartDrag(CallingContext & context,const DragData & dragData)46 int32_t DragServer::StartDrag(CallingContext &context, const DragData &dragData)
47 {
48 CHKPR(env_, RET_ERR);
49 auto session = env_->GetSocketSessionManager().FindSessionByPid(context.pid);
50 CHKPR(session, RET_ERR);
51 session->SetProgramName(GetPackageName(context.tokenId));
52 return env_->GetDragManager().StartDrag(dragData, context.pid);
53 }
54
StopDrag(CallingContext & context,const DragDropResult & dropResult)55 int32_t DragServer::StopDrag(CallingContext &context, const DragDropResult &dropResult)
56 {
57 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
58 universalDragWrapper_.StopLongPressDrag();
59 #endif // OHOS_BUILD_UNIVERSAL_DRAG
60 CHKPR(env_, RET_ERR);
61 return env_->GetDragManager().StopDrag(dropResult, GetPackageName(context.tokenId), context.pid);
62 }
63
EnableInternalDropAnimation(CallingContext & context,const std::string & animationInfo)64 int32_t DragServer::EnableInternalDropAnimation(CallingContext &context, const std::string &animationInfo)
65 {
66 #ifdef OHOS_BUILD_INTERNAL_DROP_ANIMATION
67 if (!IsSystemHAPCalling(context)) {
68 FI_HILOGE("The caller is not system hap");
69 return COMMON_NOT_SYSTEM_APP;
70 }
71 CHKPR(env_, RET_ERR);
72 return env_->GetDragManager().EnableInternalDropAnimation(animationInfo);
73 #endif // OHOS_BUILD_INTERNAL_DROP_ANIMATION
74 return COMMON_CAPABILITY_NOT_SUPPORT;
75 }
76
AddDraglistener(CallingContext & context,bool isJsCaller)77 int32_t DragServer::AddDraglistener(CallingContext &context, bool isJsCaller)
78 {
79 if (!IsSystemHAPCalling(context)) {
80 FI_HILOGE("The caller is not system hap");
81 return COMMON_NOT_SYSTEM_APP;
82 }
83 FI_HILOGI("Add drag listener, from:%{public}d", context.pid);
84 CHKPR(env_, RET_ERR);
85 if (int32_t ret = env_->GetDragManager().AddListener(context.pid); ret != RET_OK) {
86 FI_HILOGE("IDragManager::AddListener fail, error:%{public}d", ret);
87 return ret;
88 }
89 return RET_OK;
90 }
91
RemoveDraglistener(CallingContext & context,bool isJsCaller)92 int32_t DragServer::RemoveDraglistener(CallingContext &context, bool isJsCaller)
93 {
94 if (!IsSystemHAPCalling(context)) {
95 FI_HILOGE("The caller is not system hap");
96 return COMMON_NOT_SYSTEM_APP;
97 }
98 FI_HILOGD("Remove drag listener, from:%{public}d", context.pid);
99 CHKPR(env_, RET_ERR);
100 if (int32_t ret = env_->GetDragManager().RemoveListener(context.pid); ret != RET_OK) {
101 FI_HILOGE("IDragManager::RemoveListener fail, error:%{public}d", ret);
102 return ret;
103 }
104 return RET_OK;
105 }
106
AddSubscriptListener(CallingContext & context)107 int32_t DragServer::AddSubscriptListener(CallingContext &context)
108 {
109 if (!IsSystemHAPCalling(context)) {
110 FI_HILOGE("The caller is not system hap");
111 return COMMON_NOT_SYSTEM_APP;
112 }
113 FI_HILOGD("Add subscript listener, from:%{public}d", context.pid);
114 CHKPR(env_, RET_ERR);
115 if (int32_t ret = env_->GetDragManager().AddSubscriptListener(context.pid); ret != RET_OK) {
116 FI_HILOGE("IDragManager::AddSubscriptListener fail, error:%{public}d", ret);
117 return ret;
118 }
119 return RET_OK;
120 }
121
RemoveSubscriptListener(CallingContext & context)122 int32_t DragServer::RemoveSubscriptListener(CallingContext &context)
123 {
124 if (!IsSystemHAPCalling(context)) {
125 FI_HILOGE("The caller is not system hap");
126 return COMMON_NOT_SYSTEM_APP;
127 }
128 FI_HILOGD("Remove subscript listener, from:%{public}d", context.pid);
129 CHKPR(env_, RET_ERR);
130 if (int32_t ret = env_->GetDragManager().RemoveSubscriptListener(context.pid); ret != RET_OK) {
131 FI_HILOGE("IDragManager::RemoveSubscriptListener fail, error:%{public}d", ret);
132 return ret;
133 }
134 return RET_OK;
135 }
136
SetDragWindowVisible(bool visible,bool isForce,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)137 int32_t DragServer::SetDragWindowVisible(bool visible, bool isForce,
138 const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
139 {
140 FI_HILOGI("SetDragWindowVisible(%{public}d, %{public}d)", visible, isForce);
141 CHKPR(env_, RET_ERR);
142 return env_->GetDragManager().OnSetDragWindowVisible(visible, isForce, false, rsTransaction);
143 }
144
GetDragTargetPid(CallingContext & context,int32_t & targetPid)145 int32_t DragServer::GetDragTargetPid(CallingContext &context, int32_t &targetPid)
146 {
147 if (!IsSystemHAPCalling(context)) {
148 FI_HILOGE("The caller is not system hap");
149 return COMMON_NOT_SYSTEM_APP;
150 }
151 CHKPR(env_, RET_ERR);
152 targetPid = env_->GetDragManager().GetDragTargetPid();
153 return RET_OK;
154 }
155
GetUdKey(std::string & udKey)156 int32_t DragServer::GetUdKey(std::string &udKey)
157 {
158 CHKPR(env_, RET_ERR);
159 if (int32_t ret = env_->GetDragManager().GetUdKey(udKey); ret != RET_OK) {
160 FI_HILOGE("IDragManager::GetUdKey fail, error:%{public}d", ret);
161 return ret;
162 }
163 return RET_OK;
164 }
165
GetShadowOffset(ShadowOffset & shadowOffset)166 int32_t DragServer::GetShadowOffset(ShadowOffset &shadowOffset)
167 {
168 CHKPR(env_, RET_ERR);
169 if (int32_t ret = env_->GetDragManager().OnGetShadowOffset(shadowOffset); ret != RET_OK) {
170 FI_HILOGE("IDragManager::OnGetShadowOffset fail, error:%{public}d", ret);
171 return ret;
172 }
173 return RET_OK;
174 }
175
UpdateDragStyle(CallingContext & context,DragCursorStyle style,int32_t eventId)176 int32_t DragServer::UpdateDragStyle(CallingContext &context, DragCursorStyle style, int32_t eventId)
177 {
178 CHKPR(env_, RET_ERR);
179 int32_t ret = env_->GetDragManager().UpdateDragStyle(style, context.pid, context.tokenId, eventId);
180 if (ret != RET_OK) {
181 FI_HILOGE("IDragManager::UpdateDragStyle fail, error:%{public}d", ret);
182 return ret;
183 }
184 return RET_OK;
185 }
186
UpdateShadowPic(const ShadowInfo & shadowInfo)187 int32_t DragServer::UpdateShadowPic(const ShadowInfo &shadowInfo)
188 {
189 CHKPR(env_, RET_ERR);
190 int32_t ret = env_->GetDragManager().UpdateShadowPic(shadowInfo);
191 if (ret != RET_OK) {
192 FI_HILOGE("IDragManager::UpdateShadowPic fail, error:%{public}d", ret);
193 return ret;
194 }
195 return RET_OK;
196 }
197
GetDragData(CallingContext & context,DragData & dragData)198 int32_t DragServer::GetDragData(CallingContext &context, DragData &dragData)
199 {
200 if (!IsSystemHAPCalling(context)) {
201 FI_HILOGE("The caller is not system hap");
202 return COMMON_NOT_SYSTEM_APP;
203 }
204 CHKPR(env_, RET_ERR);
205 if (int32_t ret = env_->GetDragManager().GetDragData(dragData); ret != RET_OK) {
206 FI_HILOGE("IDragManager::GetDragData fail, error:%{public}d", ret);
207 return ret;
208 }
209 return RET_OK;
210 }
211
UpdatePreviewStyle(const PreviewStyle & previewStyle)212 int32_t DragServer::UpdatePreviewStyle(const PreviewStyle &previewStyle)
213 {
214 CHKPR(env_, RET_ERR);
215 if (int32_t ret = env_->GetDragManager().UpdatePreviewStyle(previewStyle); ret != RET_OK) {
216 FI_HILOGE("IDragManager::UpdatePreviewStyle fail, error:%{public}d", ret);
217 return ret;
218 }
219 return RET_OK;
220 }
221
UpdatePreviewStyleWithAnimation(const PreviewStyle & previewStyle,const PreviewAnimation & animation)222 int32_t DragServer::UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle, const PreviewAnimation &animation)
223 {
224 CHKPR(env_, RET_ERR);
225 if (int32_t ret = env_->GetDragManager().UpdatePreviewStyleWithAnimation(previewStyle, animation); ret != RET_OK) {
226 FI_HILOGE("IDragManager::UpdatePreviewStyleWithAnimation fail, error:%{public}d", ret);
227 return ret;
228 }
229 return RET_OK;
230 }
231
RotateDragWindowSync(CallingContext & context,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)232 int32_t DragServer::RotateDragWindowSync(CallingContext &context,
233 const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
234 {
235 if (!IsSystemHAPCalling(context)) {
236 FI_HILOGE("The caller is not system hap");
237 return COMMON_NOT_SYSTEM_APP;
238 }
239 CHKPR(env_, RET_ERR);
240 if (int32_t ret = env_->GetDragManager().RotateDragWindowSync(rsTransaction); ret != RET_OK) {
241 FI_HILOGE("IDragManager::RotateDragWindowSync fail, error:%{public}d", ret);
242 return ret;
243 }
244 return RET_OK;
245 }
246
SetDragWindowScreenId(CallingContext & context,uint64_t displayId,uint64_t screenId)247 int32_t DragServer::SetDragWindowScreenId(CallingContext &context, uint64_t displayId, uint64_t screenId)
248 {
249 if (!IsSystemHAPCalling(context)) {
250 FI_HILOGE("The caller is not system hap");
251 return COMMON_NOT_SYSTEM_APP;
252 }
253 CHKPR(env_, RET_ERR);
254 env_->GetDragManager().SetDragWindowScreenId(displayId, screenId);
255 return RET_OK;
256 }
257
GetDragSummary(CallingContext & context,std::map<std::string,int64_t> & summarys,bool isJsCaller)258 int32_t DragServer::GetDragSummary(CallingContext &context, std::map<std::string, int64_t> &summarys, bool isJsCaller)
259 {
260 if (isJsCaller && !IsSystemHAPCalling(context)) {
261 FI_HILOGE("The caller is not system hap");
262 return COMMON_NOT_SYSTEM_APP;
263 }
264 CHKPR(env_, RET_ERR);
265 if (int32_t ret = env_->GetDragManager().GetDragSummary(summarys); ret != RET_OK) {
266 FI_HILOGE("IDragManager::GetDragSummary fail, error:%{public}d", ret);
267 return ret;
268 }
269 return RET_OK;
270 }
271
SetDragSwitchState(CallingContext & context,bool enable,bool isJsCaller)272 int32_t DragServer::SetDragSwitchState(CallingContext &context, bool enable, bool isJsCaller)
273 {
274 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
275 if (!IsSystemHAPCalling(context)) {
276 FI_HILOGE("The caller is not system hap");
277 return COMMON_NOT_SYSTEM_APP;
278 }
279 universalDragWrapper_.SetDragSwitchState(enable);
280 #endif // OHOS_BUILD_UNIVERSAL_DRAG
281 return RET_OK;
282 }
283
SetAppDragSwitchState(CallingContext & context,bool enable,const std::string & pkgName,bool isJsCaller)284 int32_t DragServer::SetAppDragSwitchState(
285 CallingContext &context, bool enable, const std::string &pkgName, bool isJsCaller)
286 {
287 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
288 if (!IsSystemHAPCalling(context)) {
289 FI_HILOGE("The caller is not system hap");
290 return COMMON_NOT_SYSTEM_APP;
291 }
292 universalDragWrapper_.SetAppDragSwitchState(enable, pkgName);
293 #endif // OHOS_BUILD_UNIVERSAL_DRAG
294 return RET_OK;
295 }
296
GetDragState(CallingContext & context,DragState & dragState)297 int32_t DragServer::GetDragState(CallingContext &context, DragState &dragState)
298 {
299 CHKPR(env_, RET_ERR);
300 if (!IsSystemHAPCalling(context)) {
301 FI_HILOGE("The caller is not system hap");
302 return COMMON_NOT_SYSTEM_APP;
303 }
304
305 if (int32_t ret = env_->GetDragManager().GetDragState(dragState); ret != RET_OK) {
306 FI_HILOGE("IDragManager::GetDragState fail, error:%{public}d", ret);
307 return ret;
308 }
309 return RET_OK;
310 }
311
EnableUpperCenterMode(bool enable)312 int32_t DragServer::EnableUpperCenterMode(bool enable)
313 {
314 CHKPR(env_, RET_ERR);
315 if (int32_t ret = env_->GetDragManager().EnterTextEditorArea(enable); ret != RET_OK) {
316 FI_HILOGE("IDragManager::EnableUpperCenterMode fail, error:%{public}d", ret);
317 return ret;
318 }
319 return RET_OK;
320 }
321
GetDragAction(DragAction & dragAction)322 int32_t DragServer::GetDragAction(DragAction &dragAction)
323 {
324 CHKPR(env_, RET_ERR);
325 if (int32_t ret = env_->GetDragManager().GetDragAction(dragAction); ret != RET_OK) {
326 FI_HILOGE("IDragManager::GetDragAction fail, error:%{public}d", ret);
327 return ret;
328 }
329 return RET_OK;
330 }
331
GetExtraInfo(std::string & extraInfo)332 int32_t DragServer::GetExtraInfo(std::string &extraInfo)
333 {
334 CHKPR(env_, RET_ERR);
335 if (int32_t ret = env_->GetDragManager().GetExtraInfo(extraInfo); ret != RET_OK) {
336 FI_HILOGE("IDragManager::GetExtraInfo fail, error:%{public}d", ret);
337 return ret;
338 }
339 return RET_OK;
340 }
341
AddPrivilege(CallingContext & context)342 int32_t DragServer::AddPrivilege(CallingContext &context)
343 {
344 CHKPR(env_, RET_ERR);
345 if (int32_t ret = env_->GetDragManager().AddPrivilege(context.tokenId); ret != RET_OK) {
346 FI_HILOGE("IDragManager::AddPrivilege fail, error:%{public}d", ret);
347 return ret;
348 }
349 return RET_OK;
350 }
351
EraseMouseIcon(CallingContext & context)352 int32_t DragServer::EraseMouseIcon(CallingContext &context)
353 {
354 if (!IsSystemHAPCalling(context)) {
355 FI_HILOGE("The caller is not system hap");
356 return COMMON_NOT_SYSTEM_APP;
357 }
358 CHKPR(env_, RET_ERR);
359 if (int32_t ret = env_->GetDragManager().EraseMouseIcon(); ret != RET_OK) {
360 FI_HILOGE("IDragManager::EraseMouseIcon fail, error:%{public}d", ret);
361 return ret;
362 }
363 return RET_OK;
364 }
365
SetMouseDragMonitorState(bool state)366 int32_t DragServer::SetMouseDragMonitorState(bool state)
367 {
368 CHKPR(env_, RET_ERR);
369 if (int32_t ret = env_->GetDragManager().SetMouseDragMonitorState(state); ret != RET_OK) {
370 FI_HILOGE("IDragManager::SetMouseDragMonitorState fail, error:%{public}d", ret);
371 return ret;
372 }
373 return RET_OK;
374 }
375
SetDraggableState(bool state)376 int32_t DragServer::SetDraggableState(bool state)
377 {
378 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
379 universalDragWrapper_.SetDragableState(state);
380 #endif // OHOS_BUILD_UNIVERSAL_DRAG
381 return RET_OK;
382 }
383
GetAppDragSwitchState(bool & state)384 int32_t DragServer::GetAppDragSwitchState(bool &state)
385 {
386 state = false;
387 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
388 if (int32_t ret = universalDragWrapper_.GetAppDragSwitchState(state); ret != RET_OK) {
389 FI_HILOGE("IDragManager::GetAppDragSwitchState fail, error:%{public}d", ret);
390 return ret;
391 }
392 #endif // OHOS_BUILD_UNIVERSAL_DRAG
393 return RET_OK;
394 }
395
SetDraggableStateAsync(bool state,int64_t downTime)396 int32_t DragServer::SetDraggableStateAsync(bool state, int64_t downTime)
397 {
398 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
399 CHKPR(env_, RET_ERR);
400 env_->GetDelegateTasks().PostAsyncTask([this, state, downTime] {
401 this->universalDragWrapper_.SetDraggableStateAsync(state, downTime);
402 return RET_OK;
403 });
404 #endif // OHOS_BUILD_UNIVERSAL_DRAG
405 return RET_OK;
406 }
407
GetDragBundleInfo(DragBundleInfo & dragBundleInfo)408 int32_t DragServer::GetDragBundleInfo(DragBundleInfo &dragBundleInfo)
409 {
410 CHKPR(env_, RET_ERR);
411 if (int32_t ret = env_->GetDragManager().GetDragBundleInfo(dragBundleInfo); ret != RET_OK) {
412 FI_HILOGE("IDragManager::GetDragBundleInfo fail, error:%{public}d", ret);
413 return ret;
414 }
415 return RET_OK;
416 }
417
GetPackageName(Security::AccessToken::AccessTokenID tokenId)418 std::string DragServer::GetPackageName(Security::AccessToken::AccessTokenID tokenId)
419 {
420 std::string packageName = std::string();
421 int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
422 switch (tokenType) {
423 case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
424 Security::AccessToken::HapTokenInfo hapInfo;
425 if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) {
426 FI_HILOGE("Get hap token info failed");
427 } else {
428 packageName = hapInfo.bundleName;
429 }
430 break;
431 }
432 case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE:
433 case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: {
434 Security::AccessToken::NativeTokenInfo tokenInfo;
435 if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
436 FI_HILOGE("Get native token info failed");
437 } else {
438 packageName = tokenInfo.processName;
439 }
440 break;
441 }
442 default: {
443 FI_HILOGW("token type not match");
444 break;
445 }
446 }
447 return packageName;
448 }
449
IsSystemServiceCalling(CallingContext & context)450 bool DragServer::IsSystemServiceCalling(CallingContext &context)
451 {
452 auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(context.tokenId);
453 if ((flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) ||
454 (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL)) {
455 FI_HILOGI("system service calling, flag:%{public}u", flag);
456 return true;
457 }
458 return false;
459 }
460
IsSystemHAPCalling(CallingContext & context)461 bool DragServer::IsSystemHAPCalling(CallingContext &context)
462 {
463 if (IsSystemServiceCalling(context)) {
464 return true;
465 }
466 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(context.fullTokenId);
467 }
468
IsDragStart(bool & isStart)469 int32_t DragServer::IsDragStart(bool &isStart)
470 {
471 CHKPR(env_, RET_ERR);
472 isStart = env_->GetDragManager().IsDragStart();
473 return RET_OK;
474 }
475
GetDragSummaryInfo(DragSummaryInfo & dragSummaryInfo)476 int32_t DragServer::GetDragSummaryInfo(DragSummaryInfo &dragSummaryInfo)
477 {
478 CHKPR(env_, RET_ERR);
479 int32_t ret = env_->GetDragManager().GetDragSummaryInfo(dragSummaryInfo);
480 if (ret != RET_OK) {
481 FI_HILOGE("IDragManager::GetDragSummaryInfo fail, error:%{public}d", ret);
482 return ret;
483 }
484 return RET_OK;
485 }
486 } // namespace DeviceStatus
487 } // namespace Msdp
488 } // namespace OHOS