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 d90f15f006e6ca1f7e1a1694dea4ddc7af05c963..00b87d680e31a4a54582ba72a65f9eceb4b5c90d 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 @@ -236,7 +236,9 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("access", NAPI_Access), DECLARE_NAPI_FUNCTION("getFileInfoFromUri", NAPI_GetFileInfoFromUri), DECLARE_NAPI_FUNCTION("getFileInfoFromRelativePath", NAPI_GetFileInfoFromRelativePath), - DECLARE_NAPI_FUNCTION("getThumbnail", NAPI_GetThumbnail) + DECLARE_NAPI_FUNCTION("getThumbnail", NAPI_GetThumbnail), + DECLARE_NAPI_FUNCTION("setDefaultSavePath", NAPI_SetDefaultSavePath), + DECLARE_NAPI_FUNCTION("getDefaultSavePath", NAPI_GetDefaultSavePath) }; napi_value cons = nullptr; NAPI_CALL(env, @@ -1198,5 +1200,109 @@ napi_value NAPI_GetThumbnail(napi_env env, napi_callback_info info) } return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).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 209bbc0fa348e3e8e7a2995cfd223260208fd7c5..65be3032b863fcbb245f1101dc2780f2885b43c6 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 @@ -37,6 +37,8 @@ namespace FileAccessFwk { napi_value NAPI_GetFileInfoFromUri(napi_env env, napi_callback_info info); napi_value NAPI_GetFileInfoFromRelativePath(napi_env env, napi_callback_info info); napi_value NAPI_GetThumbnail(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..029253b68dca9a57a6f01d21a3b1efa9c043cc48 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 @@ -57,8 +57,10 @@ public: virtual int GetFileInfoFromRelativePath(const std::string &selectFile, FileInfo &fileInfo) override; virtual int GetRoots(std::vector &rootInfoVec) override; virtual int Access(const Uri &uri, bool &isExist) override; - virtual int StartWatcher(const Uri &uri) 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 4205e38a6c05ec66d40b61677880746c37a7cdf7..0d698ed17bda1aaab74d8113a0a35a53b62789de 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_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 @@ -84,6 +84,8 @@ public: int GetFileInfoFromUri(Uri &selectFile, FileInfo &fileInfo); int GetFileInfoFromRelativePath(std::string &selectFile, FileInfo &fileInfo); int GetRoots(std::vector &rootInfoVec); + int SetDefaultSavePath(int uid, const std::string &path); + int GetDefaultSavePath(int uid, std::string &path); private: int StartWatcher(Uri &uri); int StopWatcher(Uri &uri); 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..d4808a1063eb81a1628feb485364aa313bc72440 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 @@ -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/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..966d6579c49d8d22fe66bb1d914ffba05d0cada3 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 @@ -1028,6 +1028,91 @@ int FileAccessExtProxy::StopWatcher(const Uri &uri) return ret; } +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(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; } 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 3ec372fe85e3549590dbd637202a57c76de13496..135525a426f4bdbf1d53c1698e8491bb08645446 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 @@ -1005,6 +1005,67 @@ 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 = GetProxyByUri(uri); + 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 = GetProxyByBundleName(MEDIA_BNUDLE_NAME); + 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 333bb11d119f8d1df097c7d4eab8832741b4b9d1..c9c70d338b3c78e13987d328f6dbb925574d41e6 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 @@ -1131,6 +1131,91 @@ int JsFileAccessExtAbility::Query(const Uri &uri, std::vector &colu return ERR_OK; } +int JsFileAccessExtAbility::SetDefaultPath(const std::string &defaultPath) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "SetDefaultPath"); + auto ret = std::make_shared(); + if (ret == nullptr) { + HILOG_ERROR("SetDefaultPath value is nullptr."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_GETRESULT; + } + + auto argParser = [path = defaultPath](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + 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] = nativePath; + argc = ARGC_ONE; + 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("SetDefaultPath", 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::GetDefaultPath(std::string &defaultPath) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetDefaultPath"); + auto ret = std::make_shared(); + auto retPath = std::make_shared(); + if (ret == nullptr) { + HILOG_ERROR("GetDefaultPath value is nullptr."); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_GETRESULT; + } + + auto argParser = [](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + argc = ARGC_ZERO; + 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("GetDefaultPath", 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/external_file_access_test.cpp b/test/unittest/external_file_access_test.cpp index 43b1ef943d053548ff6f7cfefde96168fc71dbb5..60305c8b177e20af135439c80cd583d606f243a7 100644 --- a/test/unittest/external_file_access_test.cpp +++ b/test/unittest/external_file_access_test.cpp @@ -3901,4 +3901,109 @@ HWTEST_F(FileExtensionHelperTest, external_file_access_Query_0007, testing::ext: } GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_Query_0007"; } -} // namespace \ No newline at end of file + +/** + * @tc.number: user_file_service_external_file_access_SetDefaultPath_0001 + * @tc.name: external_file_access_SetDefaultPath_0001 + * @tc.desc: Test function of SetDefaultPath 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_SetDefaultPath_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_SetDefaultPath_0001"; + try { + int result = g_fah->SetDefaultPath(""); + EXPECT_EQ(result, E_URIS); + GTEST_LOG_(INFO) << "SetDefaultPath_0001 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_SetDefaultPath_0001 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_SetDefaultPath_0001"; +} + +/** + * @tc.number: user_file_service_external_file_access_SetDefaultPath_0002 + * @tc.name: external_file_access_SetDefaultPath_0002 + * @tc.desc: Test function of SetDefaultPath 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_SetDefaultPath_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_SetDefaultPath_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(); + result = g_fah->SetDefaultPath(destUri.ToString()); + EXPECT_NE(result, OHOS::FileAccessFwk::ERR_OK); //IPC + GTEST_LOG_(INFO) << "SetDefaultPath_0002 result:" << result;//1 + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_SetDefaultPath_0002 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_SetDefaultPath_0002"; +} + +/** + * @tc.number: user_file_service_external_file_access_GetDefaultPath_0001 + * @tc.name: external_file_access_GetDefaultPath_0001 + * @tc.desc: Test function of GetDefaultPath 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_GetDefaultPath_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_GetDefaultPath_0001"; + try { + std::string path; + int result = g_fah->GetDefaultPath(path); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); // + GTEST_LOG_(INFO) << "GetDefaultPath_0001 result:" << result;//14300001 + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_GetDefaultPath_0001 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_GetDefaultPath_0001"; +} + +/** + * @tc.number: user_file_service_external_file_access_GetDefaultPath_0002 + * @tc.name: external_file_access_GetDefaultPath_0002 + * @tc.desc: Test function of GetDefaultPath 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_GetDefaultPath_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionHelperTest-begin external_file_access_GetDefaultPath_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();//14300001 + result = g_fah->SetDefaultPath(destUri.ToString()); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + std::string path; + result = g_fah->GetDefaultPath(path); + GTEST_LOG_(INFO) << "GetDefaultPath:" << path; + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + EXPECT_EQ(destUri.ToString(), path); + GTEST_LOG_(INFO) << "GetDefaultPath_0002 result:" << result; + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_GetDefaultPath_0002 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionHelperTest-end external_file_access_GetDefaultPath_0002"; +} +} // namespace