6#include <QtHarmonyExtras/private/qohosoperationstatus_p.h>
8#include <QtCore/qscopeguard.h>
9#include <QtCore/private/qcore_ohos_p.h>
10#include <QtCore/private/qohoscommon_p.h>
11#include <QtCore/private/qohoslogger_p.h>
12#include <QtCore/private/qohospathutils_p.h>
22#include <filemanagement/fileshare/oh_file_share.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
56std::shared_ptr<
char> makeSharedNullTerminatedString(
std::string str)
58 auto sharedStrData = QtOhos::moveToSharedPtr(std::move(str) +
'\0');
59 return std::shared_ptr<
char>(sharedStrData, &sharedStrData->front());
62std::shared_ptr<
char> makeSharedNullTerminatedString(
const char *str)
64 return makeSharedNullTerminatedString(std::string(str !=
nullptr ? str :
""));
67std::shared_ptr<::FileShare_PolicyInfo> makeFileSharePolicyInfo(
68 std::string uri,
unsigned operationMode)
70 auto sharedUri = makeSharedNullTerminatedString(std::move(uri));
72 auto policyInfo = QtOhos::moveToSharedPtr(
73 ::FileShare_PolicyInfo{
74 .uri = sharedUri.get(),
75 .length =
static_cast<
unsigned>(std::strlen(sharedUri.get())),
76 .operationMode = operationMode,
79 return QtOhos::makeSharedPtrWithAttachedExtraData(
80 policyInfo, sharedUri);
83std::vector<std::shared_ptr<::FileShare_PolicyInfo>> convertToFileSharePolicyInfos(
84 const QList<PathPolicy> &policies)
86 std::vector<std::shared_ptr<::FileShare_PolicyInfo>> fileSharePolicies;
88 for (
const auto &policy : policies) {
89 fileSharePolicies.push_back(
90 makeFileSharePolicyInfo(
91 tryMapPathToOhosFileUri(policy.path.toStdString()).value_or(
""),
92 static_cast<
unsigned>(policy.operationModes.toInt())));
95 return fileSharePolicies;
98std::shared_ptr<::FileShare_PolicyErrorResult> makeFileSharePolicyErrorResultFromRawStruct(
99 const ::FileShare_PolicyErrorResult &inputStruct)
101 auto sharedUri = makeSharedNullTerminatedString(inputStruct.uri);
102 auto sharedMessage = makeSharedNullTerminatedString(inputStruct.message);
104 auto policyErrorResult = QtOhos::moveToSharedPtr(
105 ::FileShare_PolicyErrorResult{
106 .uri = sharedUri.get(),
107 .code = inputStruct.code,
108 .message = sharedMessage.get(),
111 return QtOhos::makeSharedPtrWithAttachedExtraData(
113 QtOhos::moveToSharedPtr(std::make_tuple(sharedUri, sharedMessage)));
116std::vector<::FileShare_PolicyInfo> makePoliciesRawVectorView(
117 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies)
119 std::vector<::FileShare_PolicyInfo> rawVectorView;
120 for (
const auto &policyPtr : policies)
121 rawVectorView.push_back(*policyPtr);
123 return rawVectorView;
126template<
typename PermissionActionFunc>
127::FileManagement_ErrCode callFileSharePermissionActionFunc(
128 PermissionActionFunc permissionActionFunc,
129 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
130 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
132 auto policiesRawVectorView = makePoliciesRawVectorView(policies);
133 ::FileShare_PolicyErrorResult *resultParam =
nullptr;
134 unsigned resultNumParam = 0;
135 auto resultParamReleaseGuard = qScopeGuard(
137 if (resultParam !=
nullptr && resultNumParam != 0)
138 ::OH_FileShare_ReleasePolicyErrorResult(resultParam, resultNumParam);
141 auto errCode = permissionActionFunc(
142 policiesRawVectorView.data(), policiesRawVectorView.size(),
143 &resultParam, &resultNumParam);
146 if (resultParam !=
nullptr) {
147 for (
unsigned i = 0; i < resultNumParam; ++i) {
149 makeFileSharePolicyErrorResultFromRawStruct(resultParam[i]));
156::FileManagement_ErrCode fileShareCheckPersistentPermission(
157 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
158 std::vector<
bool> &outResult)
160 auto policiesRawVectorView = makePoliciesRawVectorView(policies);
161 bool *resultParam =
nullptr;
162 auto resultParamReleaseGuard = qScopeGuard(
166 unsigned resultNumParam = 0;
168 auto errCode = ::OH_FileShare_CheckPersistentPermission(
169 policiesRawVectorView.data(), policiesRawVectorView.size(),
170 &resultParam, &resultNumParam);
173 if (resultParam !=
nullptr) {
174 for (
unsigned i = 0; i < resultNumParam; ++i)
175 outResult.push_back(resultParam[i]);
181::FileManagement_ErrCode fileSharePersistPermission(
182 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
183 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
185 return callFileSharePermissionActionFunc(
186 Q_OHOS_NAMED_FUNC(::OH_FileShare_PersistPermission),
187 policies, outResult);
190::FileManagement_ErrCode fileShareRevokePermission(
191 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
192 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
194 return callFileSharePermissionActionFunc(
195 Q_OHOS_NAMED_FUNC(::OH_FileShare_RevokePermission),
196 policies, outResult);
199::FileManagement_ErrCode fileShareActivatePermission(
200 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
201 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
203 return callFileSharePermissionActionFunc(
204 Q_OHOS_NAMED_FUNC(::OH_FileShare_ActivatePermission),
205 policies, outResult);
208::FileManagement_ErrCode fileShareDeactivatePermission(
209 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
210 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
212 return callFileSharePermissionActionFunc(
213 Q_OHOS_NAMED_FUNC(::OH_FileShare_DeactivatePermission),
214 policies, outResult);
217std::optional<PathPolicyError> tryMapFileSharePolicyErrorCode(::FileShare_PolicyErrorCode errorCode)
220 case ::FileShare_PolicyErrorCode::PERSISTENCE_FORBIDDEN:
221 return std::make_optional(PathPolicyError::PersistenceForbidden);
222 case ::FileShare_PolicyErrorCode::INVALID_MODE:
223 return std::make_optional(PathPolicyError::InvalidMode);
224 case ::FileShare_PolicyErrorCode::INVALID_PATH:
225 return std::make_optional(PathPolicyError::InvalidPath);
226 case ::FileShare_PolicyErrorCode::PERMISSION_NOT_PERSISTED:
227 return std::make_optional(PathPolicyError::PermissionNotPersisted);
232QList<PathPolicyErrorInfo> convertToPathPolicyErrorInfos(
233 const std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &policyErrorResults)
235 QList<PathPolicyErrorInfo> result;
236 for (
const auto &policyErrorResult : policyErrorResults) {
238 .path = policyErrorResult->uri !=
nullptr
239 ? QString::fromStdString(tryMapOhosFileUriToPath(policyErrorResult->uri).value_or(
""))
242 tryMapFileSharePolicyErrorCode(policyErrorResult->code)
243 .value_or(PathPolicyError::Unknown),
244 .errorMessage = QLatin1String(
245 policyErrorResult->message !=
nullptr ? policyErrorResult->message :
""),
252QList<PathPolicyCheckResult> convertToPathPolicyCheckResults(
253 const std::vector<
bool> &checkResults,
254 const QList<PathPolicy> &policies)
256 QList<PathPolicyCheckResult> result;
257 for (
std::size_t i = 0; i < checkResults.size() && policies.size(); ++i)
258 result.push_back({policies[i], checkResults[i]});
263bool validateCheckResultsAgainstPolicies(
264 const std::vector<
bool> &checkResults,
265 const QList<PathPolicy> &policies)
267 return checkResults.size() ==
static_cast<
std::size_t>(policies.size());
270bool isSuccessErrorCode(::FileManagement_ErrCode errorCode)
272 return errorCode == ::FileManagement_ErrCode::ERR_OK;
277
278
279
280
281
282
283
284
285class ActionResultImpl :
public ActionResult
288 ActionResultImpl(
bool successFlag, QList<PathPolicyErrorInfo> errorInfoList);
289 std::shared_ptr<OperationStatus> operationStatus()
const override;
290 QList<PathPolicyErrorInfo> errorInfoList()
const override;
293 std::shared_ptr<OperationStatus> m_operationStatus;
294 QList<PathPolicyErrorInfo> m_errorInfoList;
297ActionResultImpl::ActionResultImpl(
bool successFlag, QList<PathPolicyErrorInfo> errorInfoList)
298 : m_operationStatus(createOperationStatus(successFlag))
299 , m_errorInfoList(std::move(errorInfoList))
304
305
306
307
308
309
310std::shared_ptr<OperationStatus> ActionResultImpl::operationStatus()
const
312 return m_operationStatus;
316
317
318
319
320
321
322
323
324QList<PathPolicyErrorInfo> ActionResultImpl::errorInfoList()
const
326 return m_errorInfoList;
331
332
333
334
335
336
337class CheckResultImpl :
public CheckResult
341 bool successFlag,
const std::vector<
bool> &checkResults,
342 const QList<PathPolicy> &policies);
343 std::shared_ptr<OperationStatus> operationStatus()
const override;
344 QList<PathPolicyCheckResult> checkResultList()
const override;
347 std::shared_ptr<OperationStatus> m_operationStatus;
348 QList<PathPolicyCheckResult> m_checkResultList;
351CheckResultImpl::CheckResultImpl(
352 bool successFlag,
const std::vector<
bool> &checkResults,
353 const QList<PathPolicy> &policies)
355 if (validateCheckResultsAgainstPolicies(checkResults, policies)) {
356 m_operationStatus = createOperationStatus(successFlag);
357 m_checkResultList = convertToPathPolicyCheckResults(checkResults, policies);
360 "%s: didn't get check results for all requested paths. "
361 "Got %lld check results, but requested checks for %lld paths.",
362 Q_FUNC_INFO,
static_cast<
long long>(checkResults.size()),
363 static_cast<
long long>(policies.size()));
364 m_operationStatus = createOperationStatus(
false);
369
370
371
372
373
374
375std::shared_ptr<OperationStatus> CheckResultImpl::operationStatus()
const
377 return m_operationStatus;
381
382
383
384
385
386
387
388
389QList<PathPolicyCheckResult> CheckResultImpl::checkResultList()
const
391 return m_checkResultList;
397
398
399
400
401
402
403
404
405
406
407
410
411
412
413
414
415
416
417
418
419
420
421
422
423
426
427
428
429
430
433
434
435
438
439
440
441
442
445
446
447
448
449
450
451
454
455
456
459
460
461
462
463
466
467
468
471
472
473
474
475
478
479
480
481
482
485
486
487
498
499
500
501
502
503
504
505
506
507
508
509
510
513 auto actionResult = QOhosJsThreadGateway::eval(
514 [&](QOhosJsState &) {
515 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
516 auto retCode = fileSharePersistPermission(
517 convertToFileSharePolicyInfos(policies), outResults);
519 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
522 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
526
527
528
529
530
531
532
533
534
535
536
539 auto actionResult = QOhosJsThreadGateway::eval(
540 [&](QOhosJsState &) {
541 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
542 auto retCode = fileShareRevokePermission(
543 convertToFileSharePolicyInfos(policies), outResults);
545 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
548 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
552
553
554
555
556
557
558
559
560
561
562
563
564
567 auto actionResult = QOhosJsThreadGateway::eval(
568 [&](QOhosJsState &) {
569 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
570 auto retCode = fileShareActivatePermission(
571 convertToFileSharePolicyInfos(policies), outResults);
573 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
576 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
580
581
582
583
584
585
586
587
588
589
590
593 auto actionResult = QOhosJsThreadGateway::eval(
594 [&](QOhosJsState &) {
595 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
596 auto retCode = fileShareDeactivatePermission(
597 convertToFileSharePolicyInfos(policies), outResults);
599 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
602 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
606
607
608
609
610
611
612
613
614
615
616
617
620 auto checkResult = QOhosJsThreadGateway::eval(
621 [&](QOhosJsState &) {
622 std::vector<
bool> outResults;
623 auto retCode = fileShareCheckPersistentPermission(
624 convertToFileSharePolicyInfos(policies), outResults);
626 return std::make_pair(isSuccessErrorCode(retCode), outResults);
629 return std::make_shared<CheckResultImpl>(checkResult.first, checkResult.second, policies);