diff --git a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp index 3097ee4a4706ebd4fcf417733cb1eaa3eca8695c..ff45e98cd157f9d97f83af50f6d761531824cd75 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -238,6 +238,8 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getFileInfoFromUri", NAPI_GetFileInfoFromUri), DECLARE_NAPI_FUNCTION("getFileInfoFromRelativePath", NAPI_GetFileInfoFromRelativePath), DECLARE_NAPI_FUNCTION("getThumbnail", NAPI_GetThumbnail), + DECLARE_NAPI_FUNCTION("setDefaultSavePath", NAPI_SetDefaultSavePath), + DECLARE_NAPI_FUNCTION("getDefaultSavePath", NAPI_GetDefaultSavePath), DECLARE_NAPI_FUNCTION("on", NAPI_RegisterObserver), DECLARE_NAPI_FUNCTION("off", NAPI_UnregisterObserver) }; @@ -1328,5 +1330,109 @@ napi_value NAPI_UnregisterObserver(napi_env env, napi_callback_info info) } return NVal::CreateUndefined(env).val_; } + +napi_value NAPI_SetDefaultSavePath(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + bool succ = false; + int uid = 0; + std::unique_ptr pathString; + std::tie(succ, uid) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::tie(succ, pathString, std::ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); + if (fileAccessHelper == nullptr) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto result = std::make_shared(); + string defaultPath(pathString.get()); + auto cbExec = [uid, defaultPath, result, fileAccessHelper]() -> NError { + std::string path(defaultPath); + int ret = fileAccessHelper->SetDefaultSavePath(uid, path); + *result = ERR_OK == ret; + return NError(ret); + }; + auto cbComplete = [result](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + const std::string procedureName = "setDefaultSavePath"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::TWO) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::THIRD]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value NAPI_GetDefaultSavePath(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); + if (fileAccessHelper == nullptr) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + bool succ = false; + int uid = 0; + std::tie(succ, uid) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto defaultPath = std::make_shared(); + auto cbExec = [uid, defaultPath, fileAccessHelper]() -> NError { + int ret = fileAccessHelper->GetDefaultSavePath(uid, *defaultPath); + return NError(ret); + }; + + auto cbComplete = [defaultPath](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUTF8String(env, *defaultPath) }; + }; + + const std::string procedureName = "getDefaultSavePath"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.h b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.h index 851f03016febaa7f7a3d59a010275ef3454a8db4..e7714555e56176e5147bdfa26a661d523279757c 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.h +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -39,6 +39,8 @@ namespace FileAccessFwk { napi_value NAPI_GetThumbnail(napi_env env, napi_callback_info info); napi_value NAPI_RegisterObserver(napi_env env, napi_callback_info info); napi_value NAPI_UnregisterObserver(napi_env env, napi_callback_info info); + napi_value NAPI_SetDefaultSavePath(napi_env env, napi_callback_info info); + napi_value NAPI_GetDefaultSavePath(napi_env env, napi_callback_info info); } // namespace FileAccessFwk } // namespace OHOS #endif // NAPI_FILEACCESS_HELPER_H diff --git a/interfaces/inner_api/file_access/include/file_access_ext_ability.h b/interfaces/inner_api/file_access/include/file_access_ext_ability.h index f807b363d240b665dfc2b8664e927e274cc6740f..fa8d0423c2515c383305905a07753ae936bf4bb2 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_ability.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #ifndef FILE_ACCESS_EXT_ABILITY_H #define FILE_ACCESS_EXT_ABILITY_H + #include "extension_base.h" #include "file_access_extension_info.h" #include "file_access_observer_common.h" @@ -61,6 +62,8 @@ public: virtual int GetFileInfoFromRelativePath(const std::string &selectFile, FileInfo &fileInfo); virtual int GetRoots(std::vector &rootInfoVec); virtual int Access(const Uri &uri, bool &isExist); + virtual int SetDefaultSavePath(int uid, const std::string &defaultPath); + virtual int GetDefaultSavePath(int uid, std::string &defaultPath); static void SetCreator(const CreatorFunc& creator); virtual int StartWatcher(const Uri &uri); virtual int StopWatcher(const Uri &uri); diff --git a/interfaces/inner_api/file_access/include/file_access_ext_proxy.h b/interfaces/inner_api/file_access/include/file_access_ext_proxy.h index c681ae6b47906adfe5fc2374e886fa5e698b9d50..8775e2f3eab73c1c9c2a6036d201995e67515d64 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_proxy.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -59,6 +59,8 @@ public: virtual int Access(const Uri &uri, bool &isExist) override; virtual int StartWatcher(const Uri &uri) override; virtual int StopWatcher(const Uri &uri) override; + virtual int SetDefaultSavePath(int uid, const std::string &defaultPath) override; + virtual int GetDefaultSavePath(int uid, std::string &defaultPath) override; private: static inline BrokerDelegator delegator_; }; diff --git a/interfaces/inner_api/file_access/include/file_access_ext_stub.h b/interfaces/inner_api/file_access/include/file_access_ext_stub.h index f804c1fddbb767bac08f5e80ed9af5ab0f860e97..0858781c7bd998bfd3186141e45d54a41d35d169 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_stub.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -52,6 +52,8 @@ private: ErrCode CmdAccess(MessageParcel &data, MessageParcel &reply); ErrCode CmdStartWatcher(MessageParcel &data, MessageParcel &reply); ErrCode CmdStopWatcher(MessageParcel &data, MessageParcel &reply); + ErrCode CmdSetDefaultSavePath(MessageParcel &data, MessageParcel &reply); + ErrCode CmdGetDefaultSavePath(MessageParcel &data, MessageParcel &reply); bool CheckCallingPermission(const std::string &permission); using RequestFuncType = int (FileAccessExtStub::*)(MessageParcel &data, MessageParcel &reply); std::map stubFuncMap_; diff --git a/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h b/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h index 9acb2c65b1467240a209f410c1e03e04f2a98228..040059309df4877ddc61bbb686e64863cfd98c1d 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_stub_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -56,7 +56,8 @@ public: int Access(const Uri &uri, bool &isExist) override; int StartWatcher(const Uri &uri) override; int StopWatcher(const Uri &uri) override; - + int SetDefaultSavePath(int uid, const std::string &defaultPath) override; + int GetDefaultSavePath(int uid, std::string &defaultPath) override; private: std::shared_ptr GetOwner(); std::shared_ptr extension_; diff --git a/interfaces/inner_api/file_access/include/file_access_helper.h b/interfaces/inner_api/file_access/include/file_access_helper.h index 80fe58d6e9313ae73faeed0fa64205aa6109288c..c669542c33cd360569930ff004081bce4623ca0a 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -87,12 +87,15 @@ public: int GetRoots(std::vector &rootInfoVec); int RegisterNotify(Uri uri, sptr &observer, bool notifyForDescendants); int UnregisterNotify(Uri uri, sptr &observer); + int SetDefaultSavePath(int uid, const std::string &path); + int GetDefaultSavePath(int uid, std::string &path); private: int StartWatcher(Uri &uri); int StopWatcher(Uri &uri); sptr GetProxyByUri(Uri &uri); sptr GetProxyByBundleName(const std::string &bundleName); bool GetProxy(); + sptr GetDefaultProxy(); static sptr GetBundleMgrProxy(); FileAccessHelper(const std::shared_ptr &context, const std::unordered_map> &cMap); diff --git a/interfaces/inner_api/file_access/include/ifile_access_ext_base.h b/interfaces/inner_api/file_access/include/ifile_access_ext_base.h index b01a81db7124af76d13e714c3e12a95f42257d8e..d702328a78c5440649367e45586c15fab2a742cf 100644 --- a/interfaces/inner_api/file_access/include/ifile_access_ext_base.h +++ b/interfaces/inner_api/file_access/include/ifile_access_ext_base.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -53,7 +53,9 @@ public: CMD_ACCESS, CMD_GET_THUMBNAIL, CMD_GET_FILEINFO_FROM_URI, - CMD_GET_FILEINFO_FROM_RELATIVE_PATH + CMD_GET_FILEINFO_FROM_RELATIVE_PATH, + CMD_SET_DEFAULT_PATH, + CMD_GET_DEFAULT_PATH, }; virtual int OpenFile(const Uri &uri, const int flags, int &fd) = 0; @@ -77,6 +79,8 @@ public: virtual int Access(const Uri &uri, bool &isExist) = 0; virtual int StartWatcher(const Uri &uri) = 0; virtual int StopWatcher(const Uri &uri) = 0; + virtual int SetDefaultSavePath(int uid, const std::string &defaultPath) = 0; + virtual int GetDefaultSavePath(int uid, std::string &defaultPath) = 0; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h index 2b690e94cb000966e48052c20aef39991927f433..484fa7fc1bd960b70ebc6fe36411e40177c43ae7 100644 --- a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h @@ -74,7 +74,8 @@ public: int Query(const Uri &uri, std::vector &columns, std::vector &results) override; int StartWatcher(const Uri &uri) override; int StopWatcher(const Uri &uri) override; - + int SetDefaultSavePath(int uid, const std::string &defaultPath) override; + int GetDefaultSavePath(int uid, std::string &defaultPath) override; private: NativeValue* CallObjectMethod(const char *name, NativeValue * const *argv = nullptr, size_t argc = 0); int CallJsMethod(const std::string &funcName, JsRuntime &jsRuntime, NativeReference *jsObj, diff --git a/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp index c56c4fd6bb491de8c5068defb03befa20b83b89c..4760ca1ab928d393ff97f3e9b58c8ad55ccc4768 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -170,5 +170,17 @@ int FileAccessExtAbility::Notify(Uri &uri, NotifyType notifyType) HILOG_ERROR("FileAccessExtAbility::Notify Undefined operation"); return EPERM; } + +int FileAccessExtAbility::SetDefaultSavePath(int uid, const std::string &defaultPath) +{ + HILOG_ERROR("FileAccessExtAbility::SetDefaultSavePath Undefined operation"); + return EPERM; +} + +int FileAccessExtAbility::GetDefaultSavePath(int uid, std::string &defaultPath) +{ + HILOG_ERROR("FileAccessExtAbility::GetDefaultSavePath Undefined operation"); + return EPERM; +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp b/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp index 130b4ab3469d0f722dc31f5d0f9240876e996561..1f20093be5d7a89a757a8db531b08ee0959b6f06 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -1031,5 +1031,94 @@ int FileAccessExtProxy::StopWatcher(const Uri &uri) FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ERR_OK; } + +int FileAccessExtProxy::SetDefaultSavePath(int uid, const std::string &defaultPath) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "SetDefaultSavePath"); + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + if (!data.WriteInt32(uid)) { + HILOG_ERROR("fail to WriteParcelable defaultPath"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + if (!data.WriteString(defaultPath)) { + HILOG_ERROR("fail to WriteParcelable defaultPath"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_SET_DEFAULT_PATH, data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return err; + } + + int ret = E_IPCS; + if (!reply.ReadInt32(ret)) { + HILOG_ERROR("fail to ReadInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + if (ret != ERR_OK) { + HILOG_ERROR("SetDefaultSavePath operation failed ret : %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + +int FileAccessExtProxy::GetDefaultSavePath(int uid, std::string &defaultPath) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetDefaultSavePath"); + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + if (!data.WriteInt32(uid)) { + HILOG_ERROR("fail to WriteParcelable defaultPath"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_GET_DEFAULT_PATH, data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return err; + } + int ret = E_IPCS; + if (!reply.ReadInt32(ret)) { + HILOG_ERROR("fail to ReadInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + if (ret != ERR_OK) { + HILOG_ERROR("GetDefaultSavePath operation failed ret : %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + if (!reply.ReadString(defaultPath)) { + HILOG_ERROR("fail to ReadString ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp b/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp index 4c2e17b82aec7bd4793270c7f2a1dd599137e0a2..79400b5817631fe02e92524ff7a07586389db289 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -60,6 +60,8 @@ FileAccessExtStub::FileAccessExtStub() stubFuncMap_[CMD_GET_THUMBNAIL] = &FileAccessExtStub::CmdGetThumbnail; stubFuncMap_[CMD_GET_FILEINFO_FROM_URI] = &FileAccessExtStub::CmdGetFileInfoFromUri; stubFuncMap_[CMD_GET_FILEINFO_FROM_RELATIVE_PATH] = &FileAccessExtStub::CmdGetFileInfoFromRelativePath; + stubFuncMap_[CMD_SET_DEFAULT_PATH] = &FileAccessExtStub::CmdSetDefaultSavePath; + stubFuncMap_[CMD_GET_DEFAULT_PATH] = &FileAccessExtStub::CmdGetDefaultSavePath; } FileAccessExtStub::~FileAccessExtStub() @@ -778,6 +780,59 @@ ErrCode FileAccessExtStub::CmdStopWatcher(MessageParcel &data, MessageParcel &re FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ERR_OK; } + +ErrCode FileAccessExtStub::CmdSetDefaultSavePath(MessageParcel &data, MessageParcel &reply) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CmdSetDefaultSavePath"); + std::string defaultPath; + int uid = 0; + if (!data.ReadInt32(uid)) { + HILOG_ERROR("Parameter CmdSetDefaultSavePath fail to ReadParcelable uid"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + if (!data.ReadString(defaultPath)) { + HILOG_ERROR("Parameter CmdSetDefaultSavePath fail to ReadParcelable defaultPath"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + int ret = SetDefaultSavePath(uid, defaultPath); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("Parameter CmdSetDefaultSavePath fail to WriteInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + +ErrCode FileAccessExtStub::CmdGetDefaultSavePath(MessageParcel &data, MessageParcel &reply) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CmdGetDefaultSavePath"); + std::string defaultPath; + int uid = 0; + if (!data.ReadInt32(uid)) { + HILOG_ERROR("Parameter CmdGetDefaultSavePath fail to ReadParcelable uid"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + int ret = GetDefaultSavePath(uid, defaultPath); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("Parameter CmdGetDefaultSavePath fail to WriteInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + if (!reply.WriteString(defaultPath)) { + HILOG_ERROR("Parameter CmdGetDefaultSavePath fail to WriteInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + bool FileAccessExtStub::CheckCallingPermission(const std::string &permission) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CheckCallingPermission"); diff --git a/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp b/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp index dc53c8f893a6384ead4e626dfe9e9248e69452d2..fd129fb1089fe1b4b41f3f2e87f2d80c6dc3979f 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -275,5 +275,33 @@ int FileAccessExtStubImpl::StopWatcher(const Uri &uri) FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ret; } + +int FileAccessExtStubImpl::SetDefaultSavePath(int uid, const std::string &defaultPath) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "SetDefaultSavePath"); + if (extension_ == nullptr) { + HILOG_ERROR("SetDefaultSavePath get extension failed."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + int ret = extension_->SetDefaultSavePath(uid, defaultPath); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; +} + +int FileAccessExtStubImpl::GetDefaultSavePath(int uid, std::string &defaultPath) +{ + HILOG_INFO("FileAccessExtStubImpl::GetDefaultSavePath."); + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetDefaultSavePath"); + if (extension_ == nullptr) { + HILOG_ERROR("GetDefaultSavePath get extension failed."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + int ret = extension_->GetDefaultSavePath(uid, defaultPath); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/src/file_access_helper.cpp b/interfaces/inner_api/file_access/src/file_access_helper.cpp index df45fb58f25f88bcbd84a871c604c26afcf02477..13ec010604d36588feebd5f0f8e6873d45d7598f 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -440,6 +440,15 @@ sptr FileAccessHelper::GetProxyByBundleName(const std::strin return fileAccessExtProxy; } +sptr FileAccessHelper::GetDefaultProxy() +{ + for (auto iter = cMap_.begin(); iter != cMap_.end(); ++iter) { + auto connectInfo = iter->second; + return connectInfo->fileAccessExtConnection->GetFileExtProxy(); + } + return nullptr; +} + bool FileAccessHelper::GetProxy() { for (auto iter = cMap_.begin(); iter != cMap_.end(); ++iter) { @@ -1005,6 +1014,64 @@ int FileAccessHelper::GetRoots(std::vector &rootInfoVec) return ERR_OK; } +int FileAccessHelper::SetDefaultSavePath(int uid, const std::string &path) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "SetDefaultSavePath"); + if (!IsSystemApp()) { + HILOG_ERROR("FileAccessHelper::SetDefaultSavePath check IsSystemAppByFullTokenID failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION_SYS; + } + Uri uri(path); + if (!CheckUri(uri)) { + HILOG_ERROR("Uri format check error."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_URIS; + } + sptr fileExtProxy = GetDefaultProxy(); + if (fileExtProxy == nullptr) { + HILOG_ERROR("failed with invalid fileAccessExtProxy"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + int ret = fileExtProxy->SetDefaultSavePath(uid, path); + if (ret != ERR_OK) { + HILOG_ERROR("SetDefaultSavePath get result error, code:%{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + +int FileAccessHelper::GetDefaultSavePath(int uid, std::string &path) +{ + HILOG_INFO("FileAccessHelper::GetDefaultSavePath"); + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetDefaultSavePath"); + if (!IsSystemApp()) { + HILOG_ERROR("FileAccessHelper::GetDefaultSavePath check IsSystemAppByFullTokenID failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION_SYS; + } + sptr fileExtProxy = GetDefaultProxy(); + HILOG_INFO("FileAccessHelper::fileExtProxy"); + if (fileExtProxy == nullptr) { + HILOG_ERROR("failed with invalid fileAccessExtProxy"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + + int ret = fileExtProxy->GetDefaultSavePath(uid, path); + if (ret != ERR_OK) { + HILOG_ERROR("GetDefaultSavePath get result error, code:%{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + int FileAccessHelper::GetRegisteredFileAccessExtAbilityInfo(std::vector &wantVec) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetRegisteredFileAccessExtAbilityInfo"); diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index 88980de219a59edf339b91e38c039dea74b28e9a..357478230550824340178b5a2f0948ed7d26124c 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -1132,6 +1132,103 @@ int JsFileAccessExtAbility::Query(const Uri &uri, std::vector &colu return ERR_OK; } +int JsFileAccessExtAbility::SetDefaultSavePath(int uid, const std::string &defaultPath) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "SetDefaultSavePath"); + auto ret = std::make_shared(); + if (ret == nullptr) { + HILOG_ERROR("SetDefaultSavePath value is nullptr."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_GETRESULT; + } + + auto argParser = [uid = uid, path = defaultPath](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + NativeValue *nativeUid = engine.CreateNumber(uid); + if (nativeUid == nullptr) { + HILOG_ERROR("create defaultPath uri native js value fail."); + return false; + } + NativeValue *nativePath = engine.CreateString(path.c_str(), path.length()); + if (nativePath == nullptr) { + HILOG_ERROR("create defaultPath uri native js value fail."); + return false; + } + argv[ARGC_ZERO] = nativeUid; + argv[ARGC_ONE] = nativePath; + argc = ARGC_TWO; + return true; + }; + auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool { + bool res = ConvertFromJsValue(engine, result, *ret); + if (!res) { + HILOG_ERROR("Convert js value fail."); + } + return res; + }; + + auto errCode = CallJsMethod("SetDefaultSavePath", jsRuntime_, jsObj_.get(), argParser, retParser); + if (errCode != ERR_OK) { + HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return errCode; + } + + if (*ret != ERR_OK) { + HILOG_ERROR("fileio fail."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return *ret; + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + +int JsFileAccessExtAbility::GetDefaultSavePath(int uid, std::string &defaultPath) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetDefaultSavePath"); + auto ret = std::make_shared(); + auto retPath = std::make_shared(); + if (ret == nullptr) { + HILOG_ERROR("GetDefaultSavePath value is nullptr."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_GETRESULT; + } + + auto argParser = [uid = uid](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + NativeValue *nativeUid = engine.CreateNumber(uid); + if (nativeUid == nullptr) { + HILOG_ERROR("create defaultPath uri native js value fail."); + return false; + } + argv[ARGC_ZERO] = nativeUid; + argc = ARGC_ONE; + return true; + }; + auto retParser = [ret, retPath](NativeEngine &engine, NativeValue *result) -> bool { + bool res = ConvertFromJsValue(engine, result, *retPath); + if (!res) { + HILOG_ERROR("Convert js value fail."); + } + return res; + }; + + auto errCode = CallJsMethod("GetDefaultSavePath", jsRuntime_, jsObj_.get(), argParser, retParser); + if (errCode != ERR_OK) { + HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return errCode; + } + defaultPath = *retPath; + if (*ret != ERR_OK) { + HILOG_ERROR("fileio fail."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return *ret; + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} + int JsFileAccessExtAbility::GetFileInfoFromUri(const Uri &selectFile, FileInfo &fileInfo) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetFileInfoFromUri"); diff --git a/interfaces/kits/picker/picker.js b/interfaces/kits/picker/picker.js index abc2c5144a3791743807c7c468a0ae9a0462fca9..08a23bb28ee4a613eac0e81980ccd4d9710ad2fe 100644 --- a/interfaces/kits/picker/picker.js +++ b/interfaces/kits/picker/picker.js @@ -321,4 +321,4 @@ export default { PhotoViewPicker : PhotoViewPicker, DocumentViewPicker: DocumentViewPicker, AudioViewPicker : AudioViewPicker, -} \ No newline at end of file +} diff --git a/test/unittest/abnormal_file_access_test.cpp b/test/unittest/abnormal_file_access_test.cpp index 1739f75943ebbb16dc4856aefe9ce186c4c7579d..f3aff907fbb0a1615230f3df8eeb4a9c3aaae6c5 100755 --- a/test/unittest/abnormal_file_access_test.cpp +++ b/test/unittest/abnormal_file_access_test.cpp @@ -494,4 +494,55 @@ HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_GetFileI GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end" "abnormal_external_file_access_GetFileInfoFromRelativePath_0000"; } + +/** + * @tc.number: user_file_service_external_file_access_GetDefaultSavePath_0000 + * @tc.name: abnormal_external_file_access_GetDefaultSavePath_0000 + * @tc.desc: Test function of GetDefaultSavePath interface for ERROR because of set not system app flag. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I76YA0 + */ +HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_GetDefaultSavePath_0000, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin" + "abnormal_external_file_access_GetDefaultSavePath_0000"; + try { + std::string uri; + int result = g_fah->GetDefaultSavePath(100, uri); + EXPECT_EQ(result, FileAccessFwk::E_PERMISSION_SYS); + EXPECT_EQ(uri, ""); + } catch (...) { + GTEST_LOG_(ERROR) << "abnormal_external_file_access_GetDefaultSavePath_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end" + "abnormal_external_file_access_GetDefaultSavePath_0000"; +} + +/** + * @tc.number: user_file_service_external_file_access_SetDefaultSavePath_0000 + * @tc.name: abnormal_external_file_access_SetDefaultSavePath_0000 + * @tc.desc: Test function of SetDefaultSavePath interface for ERROR because of set not system app flag. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I76YA0 + */ +HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_SetDefaultSavePath_0000, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin" + "abnormal_external_file_access_SetDefaultSavePath_0000"; + try { + std::string uri; + int result = g_fah->SetDefaultSavePath(100, uri); + EXPECT_EQ(result, FileAccessFwk::E_PERMISSION_SYS); + } catch (...) { + GTEST_LOG_(ERROR) << "abnormal_external_file_access_SetDefaultSavePath_0000 occurs an exception."; + } + GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end" + "abnormal_external_file_access_SetDefaultSavePath_0000"; +} } // namespace \ No newline at end of file diff --git a/test/unittest/external_file_access_test.cpp b/test/unittest/external_file_access_test.cpp index 43b1ef943d053548ff6f7cfefde96168fc71dbb5..4beec5b716a7ded3ca10405fbed2c51c03414142 100644 --- a/test/unittest/external_file_access_test.cpp +++ b/test/unittest/external_file_access_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -80,6 +80,7 @@ public: g_context = make_shared(); g_context->SetToken(remoteObj); AAFwk::Want want; + AAFwk::Want want2; vector wantVec; setuid(UID_TRANSFORM_TMP); int ret = FileAccessHelper::GetRegisteredFileAccessExtAbilityInfo(wantVec); @@ -87,6 +88,7 @@ public: bool sus = false; for (size_t i = 0; i < wantVec.size(); i++) { auto element = wantVec[i].GetElement(); + GTEST_LOG_(INFO) << "++++BundleName:" << element.GetBundleName(); if (element.GetBundleName() == "com.ohos.UserFile.ExternalFileManager" && element.GetAbilityName() == "FileExtensionAbility") { want = wantVec[i]; @@ -94,6 +96,7 @@ public: break; } } + EXPECT_TRUE(sus); vector wants{want}; g_fah = FileAccessHelper::Creator(remoteObj, wants); @@ -911,6 +914,7 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Mkdir_0004, testing::ext: Uri newDirUriTest(""); string displayName = ""; result = g_fah->Mkdir(parentUri, displayName, newDirUriTest); + std::cout << "Mkdir:" << parentUri.ToString(); EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); GTEST_LOG_(INFO) << "Mkdir_0004 result:" << result; } @@ -3901,4 +3905,113 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Query_0007, testing::ext: } GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_Query_0007"; } + +/** + * @tc.number: user_file_service_external_file_access_SetDefaultSavePath_0001 + * @tc.name: external_file_access_SetDefaultSavePath_0001 + * @tc.desc: Test function of SetDefaultSavePath interface for ERROR which path is empty. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0387 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_SetDefaultSavePath_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_SetDefaultSavePath_0001"; + try { + const int uid = 100; + int result = g_fah->SetDefaultSavePath(uid, ""); + EXPECT_EQ(result, E_URIS); + GTEST_LOG_(INFO) << "SetDefaultSavePath_0001 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_SetDefaultSavePath_0001 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_SetDefaultSavePath_0001"; +} + +/** + * @tc.number: user_file_service_external_file_access_SetDefaultSavePath_0002 + * @tc.name: external_file_access_SetDefaultSavePath_0002 + * @tc.desc: Test function of SetDefaultSavePath interface for ERROR which path not is empty. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0387 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_SetDefaultSavePath_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_SetDefaultSavePath_0002"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri destUri(info[0].uri); + GTEST_LOG_(INFO) << destUri.ToString(); + std::cout << "SetDefaultSavePath_0002:" << destUri.ToString(); + const int uid = 100; + result = g_fah->SetDefaultSavePath(uid, destUri.ToString()); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + GTEST_LOG_(INFO) << "SetDefaultSavePath_0002 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_SetDefaultSavePath_0002 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_SetDefaultSavePath_0002"; +} + +/** + * @tc.number: user_file_service_external_file_access_GetDefaultSavePath_0001 + * @tc.name: external_file_access_GetDefaultSavePath_0001 + * @tc.desc: Test function of GetDefaultSavePath interface for ERROR which path not is empty. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0387 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_GetDefaultSavePath_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_GetDefaultSavePath_0001"; + try { + std::string path; + const int uid = 100; + int result = g_fah->GetDefaultSavePath(uid, path); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + GTEST_LOG_(INFO) << "GetDefaultSavePath_0001 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_GetDefaultSavePath_0001 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_GetDefaultSavePath_0001"; +} + +/** + * @tc.number: user_file_service_external_file_access_GetDefaultSavePath_0002 + * @tc.name: external_file_access_GetDefaultSavePath_0002 + * @tc.desc: Test function of GetDefaultSavePath interface for ERROR which path not is empty. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0387 + */ +HWTEST_F(FileExtensionHelperTest, external_file_access_GetDefaultSavePath_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_GetDefaultSavePath_0002"; + try { + + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri destUri(info[0].uri); + const int uid = 100; + GTEST_LOG_(INFO) << destUri.ToString(); + std::cout<< "GetDefaultSavePath_0002:" << destUri.ToString(); + result = g_fah->SetDefaultSavePath(uid, destUri.ToString()); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + std::string path; + result = g_fah->GetDefaultSavePath(uid, path); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + GTEST_LOG_(INFO) << "GetDefaultSavePath_0002 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_GetDefaultSavePath_0002 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_GetDefaultSavePath_0002"; +} } // namespace \ No newline at end of file diff --git a/test/unittest/medialibrary_file_access_test.cpp b/test/unittest/medialibrary_file_access_test.cpp index a6b2a61bb07d923466197abed14b40320ff34154..c6f7e35b6212375ab5edd5299d2956eb3221a27b 100644 --- a/test/unittest/medialibrary_file_access_test.cpp +++ b/test/unittest/medialibrary_file_access_test.cpp @@ -3564,4 +3564,117 @@ HWTEST_F(FileAccessHelperTest, medialibrary_file_access_Query_0006, testing::ext } GTEST_LOG_(INFO) << "FileAccessHelperTest-end medialibrary_file_access_Query_0006"; } + +/** + * @tc.number: user_file_service_medialibrary_file_access_SetDefaultSavePath_0001 + * @tc.name: medialibrary_file_access_SetDefaultSavePath_0001 + * @tc.desc: Test function of SetDefaultSavePath interface for SUCCESS which SetDefaultSavePath folder. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileAccessHelperTest, medialibrary_file_access_SetDefaultSavePath_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileAccessHelperTest-begin medialibrary_file_access_SetDefaultSavePath_0001"; + try { + const int uid = 90010038; + Uri parentUri = GetParentUri(); + int result = g_fah->SetDefaultSavePath(uid, parentUri.ToString()); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + GTEST_LOG_(INFO) << "SetDefaultSavePath_0001 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "medialibrary_file_access_SetDefaultSavePath_0001 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileAccessHelperTest-end medialibrary_file_access_SetDefaultSavePath_0001"; +} + +/** + * @tc.number: user_file_service_medialibrary_file_access_SetDefaultSavePath_0002 + * @tc.name: medialibrary_file_access_SetDefaultSavePath_0002 + * @tc.desc: Test function of SetDefaultSavePath interface for SUCCESS which SetDefaultSavePath folder. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileAccessHelperTest, medialibrary_file_access_SetDefaultSavePath_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileAccessHelperTest-begin medialibrary_file_access_SetDefaultSavePath_0002"; + try { + const int uid = 90010038; + + int result = g_fah->SetDefaultSavePath(uid, "~!@#$%^&*()_"); + + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); + GTEST_LOG_(INFO) << "SetDefaultSavePath_0002 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "medialibrary_file_access_SetDefaultSavePath_0002 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileAccessHelperTest-end medialibrary_file_access_SetDefaultSavePath_0002"; +} + +/** + * @tc.number: user_file_service_medialibrary_file_access_SetDefaultSavePath_0003 + * @tc.name: medialibrary_file_access_SetDefaultSavePath_0003 + * @tc.desc: Test function of SetDefaultSavePath interface for SUCCESS which SetDefaultSavePath folder. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileAccessHelperTest, medialibrary_file_access_SetDefaultSavePath_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileAccessHelperTest-begin medialibrary_file_access_SetDefaultSavePath_0003"; + try { + const int uid = 90010038; + Uri parentUri = GetParentUri(); + int result = g_fah->SetDefaultSavePath(uid, parentUri.ToString()); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + std::string retUri; + result = g_fah->GetDefaultSavePath(uid, retUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + EXPECT_EQ(retUri, parentUri.ToString()); + GTEST_LOG_(INFO) << "SetDefaultSavePath_0003 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "medialibrary_file_access_SetDefaultSavePath_0003 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileAccessHelperTest-end medialibrary_file_access_SetDefaultSavePath_0003"; +} + +/** + * @tc.number: user_file_service_medialibrary_file_access_GetDefaultSavePath_0001 + * @tc.name: medialibrary_file_access_GetDefaultSavePath_0001 + * @tc.desc: Test function of GetDefaultSavePath interface for SUCCESS which GetDefaultSavePath folder. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileAccessHelperTest, medialibrary_file_access_GetDefaultSavePath_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileAccessHelperTest-begin medialibrary_file_access_GetDefaultSavePath_0001"; + try { + const int uid = 90010038; + std::string retUri; + int result = g_fah->GetDefaultSavePath(uid, retUri); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + std::string defaultPath = "file://com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon"; + result = g_fah->SetDefaultSavePath(uid, defaultPath); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + std::string retUri2; + result = g_fah->GetDefaultSavePath(uid, retUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + EXPECT_NE(retUri2, retUri); + EXPECT_EQ(defaultPath, retUri2); + + GTEST_LOG_(INFO) << "GetDefaultSavePath_0001 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "medialibrary_file_access_GetDefaultSavePath_0001 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileAccessHelperTest-end medialibrary_file_access_GetDefaultSavePath_0001"; +} } // namespace