diff --git a/frameworks/js/napi/app/application_context/application_context.js b/frameworks/js/napi/app/application_context/application_context.js index 2b55313c6ea37a8d9070a2d0b3527fad01451fc2..28cb7093343575869e760613ade474cce2b13398 100644 --- a/frameworks/js/napi/app/application_context/application_context.js +++ b/frameworks/js/napi/app/application_context/application_context.js @@ -115,6 +115,10 @@ class ApplicationContext { return this.__context_impl__.createModuleContext(bundleName, moduleName); } + getGroupDir(groupId, callback) { + return this.__context_impl__.getGroupDir(groupId, callback); + } + getApplicationContext() { return this.__context_impl__.getApplicationContext(); } diff --git a/frameworks/js/napi/app/context/context.js b/frameworks/js/napi/app/context/context.js index e2ae3227ca0b377828f63addd65d6d09c585606e..a9209b57a6cd1cded42f7a2050ffcb403da251cd 100644 --- a/frameworks/js/napi/app/context/context.js +++ b/frameworks/js/napi/app/context/context.js @@ -80,6 +80,10 @@ class Context { return this.__context_impl__.getApplicationContext(); } + getGroupDir(groupId, callback) { + return this.__context_impl__.getGroupDir(groupId, callback); + } + set area(mode) { return this.__context_impl__.switchArea(mode); } diff --git a/frameworks/native/ability/ability_runtime/ability_context_impl.cpp b/frameworks/native/ability/ability_runtime/ability_context_impl.cpp index 5f2f33e51c0e0154e992621d7a42fb32c67c383d..7dcb79d806eb90ee4a33ca18767b1058a281e493 100644 --- a/frameworks/native/ability/ability_runtime/ability_context_impl.cpp +++ b/frameworks/native/ability/ability_runtime/ability_context_impl.cpp @@ -70,6 +70,11 @@ std::string AbilityContextImpl::GetPreferencesDir() return stageContext_ ? stageContext_->GetPreferencesDir() : ""; } +std::string AbilityContextImpl::GetGroupDir(std::string groupId) +{ + return stageContext_ ? stageContext_->GetGroupDir(groupId) : ""; +} + std::string AbilityContextImpl::GetTempDir() { return stageContext_ ? stageContext_->GetTempDir() : ""; diff --git a/frameworks/native/appkit/ability_runtime/context/application_context.cpp b/frameworks/native/appkit/ability_runtime/context/application_context.cpp index 902e39a0e3b1b149e43f0adb7773e3261d2ad06a..a33ee28e13d0fed2da61283083ba1d79ad71ddfe 100644 --- a/frameworks/native/appkit/ability_runtime/context/application_context.cpp +++ b/frameworks/native/appkit/ability_runtime/context/application_context.cpp @@ -368,6 +368,11 @@ std::string ApplicationContext::GetPreferencesDir() return (contextImpl_ != nullptr) ? contextImpl_->GetPreferencesDir() : ""; } +std::string ApplicationContext::GetGroupDir(std::string groupId) +{ + return (contextImpl_ != nullptr) ? contextImpl_->GetGroupDir(groupId) : ""; +} + std::string ApplicationContext::GetDistributedFilesDir() { return (contextImpl_ != nullptr) ? contextImpl_->GetDistributedFilesDir() : ""; diff --git a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp index 70a8f66adc8ce54a1f5d42243d1a7c802ba35447..e21d2680a0546ebd8b23e4e8d4a134e6ef6d3802 100644 --- a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp +++ b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp @@ -59,6 +59,7 @@ const std::string ContextImpl::CONTEXT_DATA_STORAGE("/data/storage/"); const std::string ContextImpl::CONTEXT_BASE("base"); const std::string ContextImpl::CONTEXT_CACHE("cache"); const std::string ContextImpl::CONTEXT_PREFERENCES("preferences"); +const std::string ContextImpl::CONTEXT_GROUP("group"); const std::string ContextImpl::CONTEXT_DATABASE("database"); const std::string ContextImpl::CONTEXT_TEMP("/temp"); const std::string ContextImpl::CONTEXT_FILES("/files"); @@ -136,6 +137,28 @@ std::string ContextImpl::GetPreferencesDir() return dir; } +std::string ContextImpl::GetGroupDir(std::string groupId) +{ + std::string dir = ""; + sptr bundleMgr = GetBundleManager(); + if (bundleMgr == nullptr) { + HILOG_ERROR("GetBundleManager is nullptr"); + return dir; + } + + std::string groupDir; + bool ret = bundleMgr->GetGroupDir(groupId, groupDir); + if (!ret || groupDir.empty()) { + HILOG_ERROR("GetGroupDir failed or groupDir is empty"); + return dir; + } + std::string uuid = groupDir.substr(groupDir.rfind("/")); + dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_GROUP + uuid; + CreateDirIfNotExist(dir, MODE); + HILOG_DEBUG("GroupDir:%{public}s", dir.c_str()); + return dir; +} + std::string ContextImpl::GetTempDir() { std::string dir = GetBaseDir() + CONTEXT_TEMP; diff --git a/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp b/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp index 64a147b5b82232ed18a5175f788c3ea985afb163..bf5ca425fa75ab1a600bb7e36f3689c0ed0c2cde 100644 --- a/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp +++ b/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp @@ -353,6 +353,14 @@ NativeValue *JsApplicationContextUtils::GetPreferencesDir(NativeEngine *engine, return me != nullptr ? me->OnGetPreferencesDir(*engine, *info) : nullptr; } +NativeValue *JsApplicationContextUtils::GetGroupDir(NativeEngine *engine, NativeCallbackInfo *info) +{ + HILOG_INFO("called"); + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetGroupDir(*engine, *info) : nullptr; +} + NativeValue *JsApplicationContextUtils::OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info) { auto applicationContext = applicationContext_.lock(); @@ -364,6 +372,41 @@ NativeValue *JsApplicationContextUtils::OnGetPreferencesDir(NativeEngine &engine return engine.CreateString(path.c_str(), path.length()); } +NativeValue *JsApplicationContextUtils::OnGetGroupDir(NativeEngine &engine, NativeCallbackInfo &info) +{ + if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) { + HILOG_ERROR("Not enough params"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); + return engine.CreateUndefined(); + } + + std::string groupId; + if (!ConvertFromJsValue(engine, info.argv[0], groupId)) { + HILOG_ERROR("Parse groupId failed"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); + return engine.CreateUndefined(); + } + + HILOG_DEBUG("Get Group Dir"); + auto complete = [applicationContext = applicationContext_, groupId] + (NativeEngine& engine, AsyncTask& task, int32_t status) { + auto context = applicationContext.lock(); + if (!context) { + task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_CONTEXT_NOT_EXIST, + "applicationContext if already released.")); + return; + } + std::string path = context->GetGroupDir(groupId); + task.ResolveWithNoError(engine, CreateJsValue(engine, path)); + }; + + NativeValue* lastParam = (info.argc == ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr; + NativeValue* result = nullptr; + AsyncTask::Schedule("JsApplicationContextUtils::OnGetGroupDir", + engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + return result; +} + NativeValue *JsApplicationContextUtils::GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info) { HILOG_INFO("JsApplicationContextUtils::GetBundleCodeDir is called"); @@ -1061,6 +1104,8 @@ void JsApplicationContextUtils::BindNativeApplicationContext(NativeEngine &engin JsApplicationContextUtils::GetRunningProcessInformation); BindNativeFunction(engine, *object, "getRunningProcessInformation", MD_NAME, JsApplicationContextUtils::GetRunningProcessInformation); + BindNativeFunction(engine, *object, "getGroupDir", MD_NAME, + JsApplicationContextUtils::GetGroupDir); } JsAppProcessState JsApplicationContextUtils::ConvertToJsAppProcessState( diff --git a/frameworks/native/appkit/ability_runtime/context/js_context_utils.cpp b/frameworks/native/appkit/ability_runtime/context/js_context_utils.cpp index 2cb269f82055236e03779cbff7b74c2117f9da92..83160b4898755aab186c8df3b1c1574e92673b6a 100644 --- a/frameworks/native/appkit/ability_runtime/context/js_context_utils.cpp +++ b/frameworks/native/appkit/ability_runtime/context/js_context_utils.cpp @@ -31,6 +31,10 @@ namespace AbilityRuntime { namespace { constexpr char BASE_CONTEXT_NAME[] = "__base_context_ptr__"; +constexpr size_t ARGC_ONE = 1; +constexpr size_t ARGC_TWO = 2; +constexpr size_t INDEX_ONE = 1; + class JsBaseContext { public: explicit JsBaseContext(std::weak_ptr&& context) : context_(std::move(context)) {} @@ -49,6 +53,7 @@ public: NativeValue* OnGetDistributedFilesDir(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnGetDatabaseDir(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnGetPreferencesDir(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnGetGroupDir(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnGetBundleCodeDir(NativeEngine& engine, NativeCallbackInfo& info); static NativeValue* GetCacheDir(NativeEngine* engine, NativeCallbackInfo* info); @@ -57,6 +62,7 @@ public: static NativeValue* GetDistributedFilesDir(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* GetDatabaseDir(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* GetPreferencesDir(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* GetGroupDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetBundleCodeDir(NativeEngine* engine, NativeCallbackInfo* info); protected: @@ -334,6 +340,48 @@ NativeValue* JsBaseContext::OnGetPreferencesDir(NativeEngine& engine, NativeCall return engine.CreateString(path.c_str(), path.length()); } +NativeValue* JsBaseContext::GetGroupDir(NativeEngine* engine, NativeCallbackInfo* info) +{ + HILOG_DEBUG("called"); + JsBaseContext* me = CheckParamsAndGetThis(engine, info, BASE_CONTEXT_NAME); + return me != nullptr ? me->OnGetGroupDir(*engine, *info) : nullptr; +} + +NativeValue* JsBaseContext::OnGetGroupDir(NativeEngine& engine, NativeCallbackInfo& info) +{ + if (info.argc != ARGC_ONE && info.argc != ARGC_TWO) { + HILOG_ERROR("Not enough params"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); + return engine.CreateUndefined(); + } + + std::string groupId; + if (!ConvertFromJsValue(engine, info.argv[0], groupId)) { + HILOG_ERROR("Parse groupId failed"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); + return engine.CreateUndefined(); + } + + HILOG_DEBUG("Get Group Dir"); + auto complete = [context = context_, groupId] + (NativeEngine& engine, AsyncTask& task, int32_t status) { + auto completeContext = context.lock(); + if (!completeContext) { + task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_CONTEXT_NOT_EXIST, + "completeContext if already released.")); + return; + } + std::string path = completeContext->GetGroupDir(groupId); + task.ResolveWithNoError(engine, CreateJsValue(engine, path)); + }; + + NativeValue* lastParam = (info.argc == ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr; + NativeValue* result = nullptr; + AsyncTask::Schedule("JsBaseContext::OnGetGroupDir", + engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + return result; +} + NativeValue* JsBaseContext::GetBundleCodeDir(NativeEngine* engine, NativeCallbackInfo* info) { HILOG_DEBUG("JsBaseContext::GetBundleCodeDir is called"); @@ -573,7 +621,7 @@ NativeValue* CreateJsBaseContext(NativeEngine& engine, std::shared_ptr BindNativeFunction(engine, *object, "switchArea", moduleName, JsBaseContext::SwitchArea); BindNativeFunction(engine, *object, "getArea", moduleName, JsBaseContext::GetArea); BindNativeFunction(engine, *object, "createModuleContext", moduleName, JsBaseContext::CreateModuleContext); - + BindNativeFunction(engine, *object, "getGroupDir", moduleName, JsBaseContext::GetGroupDir); return objValue; } } // namespace AbilityRuntime diff --git a/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h b/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h index 53b621a4a078c0881e8799286cbe349b8c2a9a3a..d3dc15815474bc51372ff19c8ff55d41b78216f2 100644 --- a/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h +++ b/interfaces/kits/native/ability/ability_runtime/ability_context_impl.h @@ -40,6 +40,7 @@ public: bool IsUpdatingConfigurations() override; bool PrintDrawnCompleted() override; std::string GetDatabaseDir() override; + std::string GetGroupDir(std::string groupId) override; std::string GetPreferencesDir() override; std::string GetDistributedFilesDir() override; void SwitchArea(int mode) override; diff --git a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h index 4ce2af37dc77be4c36c665a0bebc8c075d14fa6c..cacb52bd1bdefe5a3fb12a9d2b7142d912e90c19 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h @@ -72,6 +72,7 @@ public: bool PrintDrawnCompleted() override; std::string GetDatabaseDir() override; std::string GetPreferencesDir() override; + std::string GetGroupDir(std::string groupId) override; std::string GetDistributedFilesDir() override; sptr GetToken() override; void SetToken(const sptr &token) override; diff --git a/interfaces/kits/native/appkit/ability_runtime/context/context.h b/interfaces/kits/native/appkit/ability_runtime/context/context.h index a0b72168a0ed858a2560ad917147627412e37665..bb870c74587c13c67e2a32671505047135477060 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/context.h @@ -150,6 +150,13 @@ public: */ virtual std::string GetPreferencesDir() = 0; + /** + * @brief Obtains the path storing the storage file of the application by the groupId. + * + * @return Returns the local storage file. + */ + virtual std::string GetGroupDir(std::string groupId) = 0; + /** * @brief Obtains the path distributed file of the application * diff --git a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h index b302223d2347666aa8d3ce4dcd14305afdd1f451..1720abb426d605942949f57d86546c58d5c77415 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h @@ -98,6 +98,13 @@ public: */ std::string GetPreferencesDir() override; + /** + * @brief Obtains the path storing the storage file of the application by the groupId. + * + * @return Returns the local storage file. + */ + std::string GetGroupDir(std::string groupId) override; + /** * @brief Obtains the path distributed file of the application * @@ -296,6 +303,7 @@ private: static const std::string CONTEXT_PRIVATE; static const std::string CONTEXT_CACHE; static const std::string CONTEXT_PREFERENCES; + static const std::string CONTEXT_GROUP; static const std::string CONTEXT_DATABASE; static const std::string CONTEXT_TEMP; static const std::string CONTEXT_FILES; diff --git a/interfaces/kits/native/appkit/ability_runtime/context/js_application_context_utils.h b/interfaces/kits/native/appkit/ability_runtime/context/js_application_context_utils.h index e485abe730133fe6f1015fcfa87abca89baa8742..1d41a85c8931113e587750a23fe92b333fe10510 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/js_application_context_utils.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/js_application_context_utils.h @@ -76,6 +76,7 @@ public: NativeValue* OnGetDistributedFilesDir(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnGetDatabaseDir(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetGroupDir(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnGetBundleCodeDir(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnKillProcessBySelf(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnGetRunningProcessInformation(NativeEngine &engine, NativeCallbackInfo &info); @@ -86,6 +87,7 @@ public: static NativeValue* GetDistributedFilesDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetDatabaseDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetPreferencesDir(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* GetGroupDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetApplicationContext(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* KillProcessBySelf(NativeEngine *engine, NativeCallbackInfo *info); diff --git a/services/appmgr/include/app_spawn_client.h b/services/appmgr/include/app_spawn_client.h index abc7fbcdfd9658010d3c9e9b7e4cd37a3bad71bd..c8471bc083ef307ef115266357d5b6a90608441a 100644 --- a/services/appmgr/include/app_spawn_client.h +++ b/services/appmgr/include/app_spawn_client.h @@ -113,6 +113,13 @@ private: */ ErrCode StartProcessForWriteMsg(const AppSpawnMsgWrapper &msgWrapper, const AppSpawnStartMsg &startMsg); + /** + * write hsp data group info list to appspawn + * + * @param msgWrapper, request message wrapper. + */ + ErrCode WriteDataGroupInfoList(AppSpawnMsgWrapper &msgWrapper); + private: std::shared_ptr socket_; SpawnConnectionState state_ = SpawnConnectionState::STATE_NOT_CONNECT; diff --git a/services/appmgr/include/app_spawn_msg_wrapper.h b/services/appmgr/include/app_spawn_msg_wrapper.h index 06d8c04a72f9306ab49b1127ccae89faf9c4984d..8d2d3e025fee7017744b261014711991214143c9 100644 --- a/services/appmgr/include/app_spawn_msg_wrapper.h +++ b/services/appmgr/include/app_spawn_msg_wrapper.h @@ -22,12 +22,14 @@ #include "nocopyable.h" #include "client_socket.h" +#include "data_group_info.h" #include "shared/base_shared_bundle_info.h" namespace OHOS { namespace AppExecFwk { using AppSpawnMsg = AppSpawn::ClientSocket::AppProperty; using HspList = std::vector; +using DataGroupInfoList = std::vector; struct AppSpawnStartMsg { int32_t uid; @@ -51,6 +53,7 @@ struct AppSpawnStartMsg { uint32_t hapFlags = 0; // whether is pre installed hap HspList hspList; // list of harmony shared package std::string overlayInfo; + DataGroupInfoList dataGroupInfoList; // list of harmony shared package uint32_t mountPermissionFlags; }; @@ -125,6 +128,14 @@ public: return hspListStr; } + /** + * Get function, return data group info list string + */ + const std::string& GetDataGroupInfoListStr() const + { + return dataGroupInfoListStr; + } + private: /** * Verify message. @@ -150,6 +161,7 @@ private: // because AppSpawnMsg's size is uncertain, so should use raw pointer. AppSpawnMsg *msg_ = nullptr; std::string hspListStr; + std::string dataGroupInfoListStr; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index b2b016c6121f07b34700a9f69146a23a85f493e4..0e4afc1c214a36be6cc8cffe4b8b987a4d46d4bc 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -1784,6 +1784,12 @@ void AppMgrServiceInner::StartProcess(const std::string &appName, const std::str return; } + DataGroupInfoList dataGroupInfoList; + bool result = bundleMgr_->QueryDataGroupInfos(bundleName, userId, dataGroupInfoList); + if (!result || dataGroupInfoList.empty()) { + HILOG_DEBUG("the bundle has no groupInfos"); + } + bool hasAccessBundleDirReq = std::any_of(bundleInfo.reqPermissions.begin(), bundleInfo.reqPermissions.end(), [] (const auto &reqPermission) { if (PERMISSION_ACCESS_BUNDLE_DIR == reqPermission) { @@ -1832,6 +1838,7 @@ void AppMgrServiceInner::StartProcess(const std::string &appName, const std::str startMsg.setAllowInternet = setAllowInternet; startMsg.allowInternet = allowInternet; startMsg.hspList = hspList; + startMsg.dataGroupInfoList = dataGroupInfoList; startMsg.hapFlags = bundleInfo.isPreInstallApp ? 1 : 0; std::set mountPermissionList = AppSpawn::AppspawnMountPermission::GetMountPermissionList(); std::set permissions; diff --git a/services/appmgr/src/app_spawn_client.cpp b/services/appmgr/src/app_spawn_client.cpp index 89bb342bc85e4f237d8457ec2f70e6727f796cd5..c7de8608c2ccc61c1677e596777287015e1644ea 100644 --- a/services/appmgr/src/app_spawn_client.cpp +++ b/services/appmgr/src/app_spawn_client.cpp @@ -146,6 +146,11 @@ ErrCode AppSpawnClient::StartProcessImpl(const AppSpawnStartMsg &startMsg, pid_t HILOG_ERROR("StartProcessForWriteMsg failed!"); return result; } + result = WriteDataGroupInfoList(msgWrapper); + if (FAILED(result)) { + HILOG_ERROR("WriteDataGroupInfoList failed!"); + return result; + } result = socket_->ReadMessage(reinterpret_cast(pidMsg.pidBuf), LEN_PID); if (FAILED(result)) { HILOG_ERROR("ReadMessage failed!"); @@ -204,6 +209,22 @@ ErrCode AppSpawnClient::WriteStrInfoMessage(const std::string &strInfo) return result; } +ErrCode AppSpawnClient::WriteDataGroupInfoList(AppSpawnMsgWrapper &msgWrapper) +{ + ErrCode result = ERR_OK; + const std::string& dataGroupInfoListStr = msgWrapper.GetDataGroupInfoListStr(); + if (dataGroupInfoListStr.empty()) { + return result; + } + + // split msg + const char *buff = dataGroupInfoListStr.c_str(); + size_t leftLen = dataGroupInfoListStr.size() + 1; + HILOG_DEBUG("dataGroupInfoListStr length is %zu", leftLen); + result = socket_->WriteMessage(buff, leftLen); + return result; +} + ErrCode AppSpawnClient::GetRenderProcessTerminationStatus(const AppSpawnStartMsg &startMsg, int &status) { if (!socket_) { diff --git a/services/appmgr/src/app_spawn_msg_wrapper.cpp b/services/appmgr/src/app_spawn_msg_wrapper.cpp index e08e9e2ca512b9560de86ff8794245f7997cfae2..585a6a446e7cf0d9e9875637cc79781ebb67f4da 100644 --- a/services/appmgr/src/app_spawn_msg_wrapper.cpp +++ b/services/appmgr/src/app_spawn_msg_wrapper.cpp @@ -26,6 +26,11 @@ namespace { const std::string HSPLIST_BUNDLES = "bundles"; const std::string HSPLIST_MODULES = "modules"; const std::string HSPLIST_VERSIONS = "versions"; + const std::string DATAGROUPINFOLIST_DATAGROUPID = "dataGroupId"; + const std::string DATAGROUPINFOLIST_GID = "gid"; + const std::string DATAGROUPINFOLIST_DIR = "dir"; + const std::string JSON_DATA_APP = "/data/app/el2/"; + const std::string JSON_GROUP = "/group/"; const std::string VERSION_PREFIX = "v"; } @@ -45,6 +50,19 @@ static std::string DumpToJson(const HspList &hspList) return hspListJson.dump(); } +static std::string DumpToJson(const DataGroupInfoList &dataGroupInfoList) +{ + nlohmann::json dataGroupInfoListJson; + for (auto& dataGroupInfo : dataGroupInfoList) { + dataGroupInfoListJson[DATAGROUPINFOLIST_DATAGROUPID].emplace_back(dataGroupInfo.dataGroupId); + dataGroupInfoListJson[DATAGROUPINFOLIST_GID].emplace_back(std::to_string(dataGroupInfo.gid)); + std::string dir = JSON_DATA_APP + std::to_string(dataGroupInfo.userId) + + JSON_GROUP + dataGroupInfo.uuid; + dataGroupInfoListJson[DATAGROUPINFOLIST_DIR].emplace_back(dir); + } + return dataGroupInfoListJson.dump(); +} + bool AppSpawnMsgWrapper::AssembleMsg(const AppSpawnStartMsg &startMsg) { if (!VerifyMsg(startMsg)) { @@ -66,14 +84,17 @@ bool AppSpawnMsgWrapper::AssembleMsg(const AppSpawnStartMsg &startMsg) // || msg_->code == AppSpawn::ClientSocket::AppOperateCode::SPAWN_NATIVE_PROCESS) { msg_->uid = startMsg.uid; msg_->gid = startMsg.gid; - msg_->gidCount = startMsg.gids.size(); + msg_->gidCount = startMsg.gids.size() + startMsg.dataGroupInfoList.size(); msg_->bundleIndex = startMsg.bundleIndex; msg_->setAllowInternet = startMsg.setAllowInternet; msg_->allowInternet = startMsg.allowInternet; msg_->mountPermissionFlags = startMsg.mountPermissionFlags; - for (uint32_t i = 0; i < msg_->gidCount; ++i) { + for (uint32_t i = 0; i < startMsg.gids.size(); ++i) { msg_->gidTable[i] = startMsg.gids[i]; } + for (uint32_t i = startMsg.gids.size(); i < msg_->gidCount; ++i) { + msg_->gidTable[i] = startMsg.dataGroupInfoList[i - startMsg.gids.size()].gid; + } if (strcpy_s(msg_->processName, sizeof(msg_->processName), startMsg.procName.c_str()) != EOK) { HILOG_ERROR("failed to transform procName!"); return false; @@ -105,6 +126,11 @@ bool AppSpawnMsgWrapper::AssembleMsg(const AppSpawnStartMsg &startMsg) msg_->hspList.totalLength = this->hspListStr.size() + 1; // including termination char '\0' } + if (!startMsg.dataGroupInfoList.empty()) { + this->dataGroupInfoListStr = DumpToJson(startMsg.dataGroupInfoList); + msg_->dataGroupInfoList.totalLength = this->dataGroupInfoListStr.size() + 1; + } + if (!startMsg.overlayInfo.empty()) { msg_->overlayInfo.totalLength = startMsg.overlayInfo.size() + 1; // including termination char '\0' } diff --git a/test/unittest/application_context_test/application_context_test.cpp b/test/unittest/application_context_test/application_context_test.cpp index 99e409f5f8a162a4654aa13156331971258e571e..c3c518639452a616e6ccace4ae60bbbf3010f860 100644 --- a/test/unittest/application_context_test/application_context_test.cpp +++ b/test/unittest/application_context_test/application_context_test.cpp @@ -362,6 +362,35 @@ HWTEST_F(ApplicationContextTest, GetTempDir_0200, TestSize.Level1) GTEST_LOG_(INFO) << "GetTempDir_0200 end"; } +/** + * @tc.number: GetGroupDir_0100 + * @tc.name: GetGroupDir + * @tc.desc: Get Group Dir failed + */ +HWTEST_F(ApplicationContextTest, GetGroupDir_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetGroupDir_0100 start"; + std::shared_ptr contextImpl = nullptr; + context_->AttachContextImpl(contextImpl); + auto ret = context_->GetGroupDir("1"); + EXPECT_EQ(ret, ""); + GTEST_LOG_(INFO) << "GetGroupDir_0100 end"; +} + +/** + * @tc.number: GetGroupDir_0200 + * @tc.name: GetGroupDir + * @tc.desc:Get Group Dir sucess + */ +HWTEST_F(ApplicationContextTest, GetGroupDir_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetGroupDir_0200 start"; + context_->AttachContextImpl(mock_); + auto ret = context_->GetGroupDir("1"); + EXPECT_EQ(ret, "/group"); + GTEST_LOG_(INFO) << "GetGroupDir_0200 end"; +} + /** * @tc.number: GetFilesDir_0100 * @tc.name: GetFilesDir diff --git a/test/unittest/application_context_test/mock_context_impl.cpp b/test/unittest/application_context_test/mock_context_impl.cpp index a072205b2e4ae339b8a7a3d45ec826df42fc2c17..3b41749b0c18022914d4f7f75dcf0de1a752bf48 100644 --- a/test/unittest/application_context_test/mock_context_impl.cpp +++ b/test/unittest/application_context_test/mock_context_impl.cpp @@ -69,6 +69,11 @@ std::string MockContextImpl::GetDistributedFilesDir() return "/mnt/hmdfs/device_view/local/data/bundleName"; } +std::string MockContextImpl::GetGroupDir(std::string groupId) +{ + return "/group"; +} + std::shared_ptr MockContextImpl::CreateModuleContext(const std::string &moduleName) { std::shared_ptr appContext = std::make_shared(); diff --git a/test/unittest/application_context_test/mock_context_impl.h b/test/unittest/application_context_test/mock_context_impl.h index 2c1439ce2be76468809300d4f07fd18b62f32631..54695d9c3773abc799805fc5962359b7d31ac872 100644 --- a/test/unittest/application_context_test/mock_context_impl.h +++ b/test/unittest/application_context_test/mock_context_impl.h @@ -74,6 +74,8 @@ public: std::shared_ptr GetResourceManager() const override; std::shared_ptr GetConfiguration() const override; + + std::string GetGroupDir(std::string groupId) override; }; } // namespace AbilityRuntime } // namespace OHOS diff --git a/test/unittest/frameworks_kits_ability_ability_runtime_test/ability_context_impl_test.cpp b/test/unittest/frameworks_kits_ability_ability_runtime_test/ability_context_impl_test.cpp index 592c1349790d776f921f1c5b953ad6166025d0c0..dd4701ae4832d181eec1430e9d08e10ef9b968c5 100644 --- a/test/unittest/frameworks_kits_ability_ability_runtime_test/ability_context_impl_test.cpp +++ b/test/unittest/frameworks_kits_ability_ability_runtime_test/ability_context_impl_test.cpp @@ -684,6 +684,30 @@ HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_GetTempDir_0200, Function EXPECT_EQ(ret, ""); } +/** + * @tc.number: Ability_Context_Impl_GetGroupDir_0100 + * @tc.name: GetGroupDir + * @tc.desc: Get Group Dir sucess + */ +HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_GetGroupDir_0100, Function | MediumTest | Level1) +{ + context_->SetStageContext(mock_); + auto ret = context_->GetGroupDir("1"); + EXPECT_EQ(ret, "/group"); +} + +/** + * @tc.number: Ability_Context_Impl_GetGroupDir_0200 + * @tc.name: GetGroupDir + * @tc.desc: Get Group Dir failed + */ +HWTEST_F(AbilityContextImplTest, Ability_Context_Impl_GetGroupDir_0200, Function | MediumTest | Level1) +{ + context_->SetStageContext(nullptr); + auto ret = context_->GetGroupDir("1"); + EXPECT_EQ(ret, ""); +} + /** * @tc.number: Ability_Context_Impl_GetFilesDir_0100 * @tc.name: GetFilesDir diff --git a/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.cpp b/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.cpp index 5ec44018ab97def69783b9464d26d5ac3b54d0e8..5a81e0ca483a1cd21c66f5c6f3535e816efeb10d 100644 --- a/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.cpp +++ b/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.cpp @@ -159,5 +159,10 @@ void MockContext::SwitchArea(int mode) { mode_ = mode; } + +std::string MockContext::GetGroupDir(std::string groupId) +{ + return "/group"; +} } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.h b/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.h index a4f8e7a77950128d988759c967c8c2a50d569c5b..2ccf632ad2bb113615f04c465ff4af3c3d69b04e 100644 --- a/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.h +++ b/test/unittest/frameworks_kits_ability_ability_runtime_test/mock_context.h @@ -91,6 +91,8 @@ public: Global::Resource::DeviceType GetDeviceType() const override; + std::string GetGroupDir(std::string groupId) override; + int32_t mode_ = 0; }; } // namespace AbilityRuntime diff --git a/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.cpp b/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.cpp index dec3ef1d6b4a3e97fdd13d2c233a756a5da60c65..0aa23a5ddba4fe441f55be57c66ffc97b0e691df 100644 --- a/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.cpp +++ b/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.cpp @@ -143,5 +143,10 @@ Global::Resource::DeviceType MockAbilityRuntimeContext::GetDeviceType() const { return {}; }; + +std::string MockAbilityRuntimeContext::GetGroupDir(std::string groupId) +{ + return {}; +} } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.h b/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.h index b8081c9554895c14fc6ab9e845d1390f192d8e0b..e89090d3918a6e52df759a523cb4d6678f9a7fcb 100644 --- a/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.h +++ b/test/unittest/frameworks_kits_ability_native_test/mock_ability_runtime_context.h @@ -56,6 +56,7 @@ public: std::shared_ptr GetConfiguration() const override; std::string GetBaseDir() const override; Global::Resource::DeviceType GetDeviceType() const override; + std::string GetGroupDir(std::string groupId) override; }; } // namespace AbilityRuntime } // namespace OHOS