diff --git a/frameworks/kits/appkit/native/ability_runtime/context/ability_constants.h b/frameworks/kits/appkit/native/ability_runtime/context/ability_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..68d858b9b763d4381ea8a736d76e5b6ad9baa22a --- /dev/null +++ b/frameworks/kits/appkit/native/ability_runtime/context/ability_constants.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_AAFWK_ABILITY_CONSTANTS_H +#define FOUNDATION_AAFWK_ABILITY_CONSTANTS_H + +namespace OHOS { +namespace AbilityRuntime { +namespace Constants { +const std::string ABS_CODE_PATH = "/data/app/el1/bundle/public"; +const std::string LOCAL_CODE_PATH = "/data/storage/el1/bundle"; +const std::string LOCAL_BUNDLES = "/data/bundles"; +const std::string FILE_SEPARATOR = "/"; +} // namespace Constants +} // namespace AbilityRuntime +} // namespace OHOS +#endif // FOUNDATION_AAFWK_ABILITY_CONSTANTS_H \ No newline at end of file diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp index aa056ea239e6e549acbc434bcffb9452a2232902..dae56bfeaa94013763f4c42d0f5e135d07e8841b 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp @@ -15,6 +15,9 @@ #include "context_impl.h" +#include + +#include "ability_constants.h" #include "file_util.h" #include "hilog_wrapper.h" #include "ipc_singleton.h" @@ -26,6 +29,8 @@ namespace OHOS { namespace AbilityRuntime { +using namespace OHOS::AbilityRuntime::Constants; + const size_t Context::CONTEXT_TYPE_ID(std::hash {} ("Context")); const int64_t ContextImpl::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001); const std::string ContextImpl::CONTEXT_DATA_APP("/data/app/"); @@ -56,9 +61,14 @@ std::string ContextImpl::GetBundleName() const std::string ContextImpl::GetBundleCodeDir() { + auto appInfo = GetApplicationInfo(); + if (appInfo == nullptr) { + return ""; + } + std::string dir; if (IsCreateBySystemApp()) { - dir = CONTEXT_DATA_APP + CONTEXT_ELS[0] + CONTEXT_BUNDLE + GetBundleName(); + dir = std::regex_replace(appInfo->codePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES); } else { dir = CONTEXT_DATA_STORAGE + CONTEXT_ELS[0] + CONTEXT_BUNDLE; } @@ -240,7 +250,15 @@ void ContextImpl::InitResourceManager( HILOG_DEBUG( "ContextImpl::InitResourceManager moduleResPaths count: %{public}zu", bundleInfo.moduleResPaths.size()); - for (auto moduleResPath : bundleInfo.moduleResPaths) { + std::vector moduleResPaths; + std::regex pattern(ABS_CODE_PATH); + for (auto item : bundleInfo.moduleResPaths) { + if (item.empty()) { + continue; + } + moduleResPaths.emplace_back(std::regex_replace(item, pattern, LOCAL_BUNDLES)); + } + for (auto moduleResPath : moduleResPaths) { if (!moduleResPath.empty()) { HILOG_ERROR("ContextImpl::InitResourceManager length: %{public}zu, moduleResPath: %{public}s", moduleResPath.length(), diff --git a/frameworks/kits/appkit/native/app/include/context_deal.h b/frameworks/kits/appkit/native/app/include/context_deal.h index 4259664701092bcb6eff375ebbb6c77784e0adac..9d3e0bc2ddc41b5f173e12262fa23003e36770c4 100755 --- a/frameworks/kits/appkit/native/app/include/context_deal.h +++ b/frameworks/kits/appkit/native/app/include/context_deal.h @@ -25,6 +25,7 @@ namespace AppExecFwk { class ContextDeal : public Context, public std::enable_shared_from_this { public: ContextDeal() = default; + explicit ContextDeal(bool isCreateBySystemApp); virtual ~ContextDeal() = default; /** @@ -730,7 +731,6 @@ public: * @return Returns the lifecycle state info. */ AAFwk::LifeCycleStateInfo GetLifeCycleStateInfo() const; - public: static const std::string CONTEXT_DEAL_FILE_SEPARATOR; static const std::string CONTEXT_DEAL_CODE_CACHE; @@ -759,6 +759,7 @@ private: std::shared_ptr mainTaskDispatcher_; std::shared_ptr mainEventRunner_; std::shared_ptr hapModuleInfoLocal_ = nullptr; + bool isCreateBySystemApp_ = false; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/appkit/native/app/include/main_thread.h b/frameworks/kits/appkit/native/app/include/main_thread.h index 4520c6a637fdcbaf43f7577afd95ac0aea34c1e5..965b9c20f8bfcf17945263b8d8b6a7fd6a7cb67f 100644 --- a/frameworks/kits/appkit/native/app/include/main_thread.h +++ b/frameworks/kits/appkit/native/app/include/main_thread.h @@ -473,6 +473,9 @@ private: void LoadAppLibrary(); + void ChangeToLocalPath(const std::string &bundleName, + const std::vector &sourceDirs, std::vector &localPath); + /** * * @brief Close the ability library loaded. diff --git a/frameworks/kits/appkit/native/app/src/context_container.cpp b/frameworks/kits/appkit/native/app/src/context_container.cpp index 700eb866e6a87aea50343a33f961b1db0cd0984d..bbf1c48e51f8ce3c1972c5e34704649b1980dc3a 100644 --- a/frameworks/kits/appkit/native/app/src/context_container.cpp +++ b/frameworks/kits/appkit/native/app/src/context_container.cpp @@ -14,6 +14,9 @@ */ #include "context_container.h" +#include + +#include "ability_constants.h" #include "ability_manager_client.h" #include "ability_manager_errors.h" #include "app_log_wrapper.h" @@ -22,7 +25,7 @@ namespace OHOS { namespace AppExecFwk { -constexpr int DEFAULT_USERID = 100; +constexpr int CURRENT_USERID = -2; /** * Attaches a Context object to the current ability. * Generally, this method is called after Ability is loaded to provide the application context for the current ability. @@ -637,7 +640,7 @@ std::shared_ptr ContextContainer::CreateBundleContext(std::string bundl APP_LOGI("ContextContainer::CreateBundleContext length: %{public}zu, bundleName: %{public}s", bundleName.length(), bundleName.c_str()); - bundleMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, DEFAULT_USERID); + bundleMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, CURRENT_USERID); if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) { APP_LOGE("ContextContainer::CreateBundleContext GetBundleInfo is error"); @@ -649,7 +652,7 @@ std::shared_ptr ContextContainer::CreateBundleContext(std::string bundl APP_LOGE("ContextContainer::CreateBundleContext appContext is nullptr"); return nullptr; } - std::shared_ptr deal = std::make_shared(); + std::shared_ptr deal = std::make_shared(true); if (deal == nullptr) { APP_LOGE("ContextContainer::CreateBundleContext bundleName is empty"); return nullptr; @@ -673,9 +676,18 @@ void ContextContainer::InitResourceManager(BundleInfo &bundleInfo, std::shared_p APP_LOGI( "ContextContainer::InitResourceManager moduleResPaths count: %{public}zu", bundleInfo.moduleResPaths.size()); - for (auto moduleResPath : bundleInfo.moduleResPaths) { + std::vector moduleResPaths; + std::regex pattern(AbilityRuntime::Constants::ABS_CODE_PATH); + for (auto item : bundleInfo.moduleResPaths) { + if (item.empty()) { + continue; + } + moduleResPaths.emplace_back(std::regex_replace(item, pattern, AbilityRuntime::Constants::LOCAL_BUNDLES)); + } + + for (auto moduleResPath : moduleResPaths) { if (!moduleResPath.empty()) { - APP_LOGI("ContextContainer::InitResourceManager length: %{public}zu, moduleResPath: %{public}s", + APP_LOGI("ContextContainer::InitResourceManager length: %{public}zu, moduleResPath : %{public}s", moduleResPath.length(), moduleResPath.c_str()); if (!resourceManager->AddResource(moduleResPath.c_str())) { diff --git a/frameworks/kits/appkit/native/app/src/context_deal.cpp b/frameworks/kits/appkit/native/app/src/context_deal.cpp old mode 100755 new mode 100644 index d03ffb4d5542ea851dbaaecba80f5d9b16b41e20..d495386d0bd6549016902890edd3fe55f8e8f9fe --- a/frameworks/kits/appkit/native/app/src/context_deal.cpp +++ b/frameworks/kits/appkit/native/app/src/context_deal.cpp @@ -14,6 +14,10 @@ */ #include "context_deal.h" + +#include + +#include "ability_constants.h" #include "ability_manager_client.h" #include "ability_manager_interface.h" #include "app_log_wrapper.h" @@ -29,12 +33,17 @@ #define MODE 0771 namespace OHOS { namespace AppExecFwk { +using namespace OHOS::AbilityRuntime::Constants; + const std::string ContextDeal::CONTEXT_DEAL_FILE_SEPARATOR("/"); const std::string ContextDeal::CONTEXT_DEAL_CODE_CACHE("code_cache"); const std::string ContextDeal::CONTEXT_DEAL_Files("files"); const std::string ContextDeal::CONTEXT_DEAL_NO_BACKUP_Files("no_backup"); const std::string ContextDeal::CONTEXT_DEAL_DIRNAME("preferences"); +ContextDeal::ContextDeal(bool isCreateBySystemApp) : isCreateBySystemApp_(isCreateBySystemApp) +{} + /** * Called when getting the ProcessInfo * @@ -122,7 +131,18 @@ void ContextDeal::SetApplicationContext(const std::shared_ptr &context) */ std::string ContextDeal::GetBundleCodePath() { - return (applicationInfo_ != nullptr) ? applicationInfo_->codePath : ""; + if (applicationInfo_ == nullptr) { + return ""; + } + + std::string dir; + if (isCreateBySystemApp_) { + dir = std::regex_replace(applicationInfo_->codePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES); + } else { + dir = LOCAL_CODE_PATH; + } + + return dir; } /** @@ -456,7 +476,18 @@ std::string ContextDeal::GetBundleName() */ std::string ContextDeal::GetBundleResourcePath() { - return (abilityInfo_ != nullptr) ? abilityInfo_->resourcePath : ""; + if (abilityInfo_ == nullptr) { + return ""; + } + + std::string dir; + if (isCreateBySystemApp_) { + dir = std::regex_replace(abilityInfo_->resourcePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES); + } else { + std::regex pattern(ABS_CODE_PATH + FILE_SEPARATOR + abilityInfo_->bundleName); + dir = std::regex_replace(abilityInfo_->resourcePath, pattern, LOCAL_CODE_PATH); + } + return dir; } /** diff --git a/frameworks/kits/appkit/native/app/src/main_thread.cpp b/frameworks/kits/appkit/native/app/src/main_thread.cpp index 5abafb50084280bb348a33aa082b5ab9d50aa6d4..a808ff165cd1040ca88fb296449b970b453c752d 100644 --- a/frameworks/kits/appkit/native/app/src/main_thread.cpp +++ b/frameworks/kits/appkit/native/app/src/main_thread.cpp @@ -16,7 +16,9 @@ #include "main_thread.h" #include +#include +#include "ability_constants.h" #include "ability_delegator.h" #include "ability_delegator_registry.h" #include "ability_loader.h" @@ -51,6 +53,7 @@ namespace OHOS { namespace AppExecFwk { +using namespace OHOS::AbilityRuntime::Constants; std::shared_ptr MainThread::applicationForAnr_ = nullptr; namespace { constexpr int32_t DELIVERY_TIME = 200; @@ -737,7 +740,9 @@ bool MainThread::InitResourceManager(std::shared_ptr resPaths; + ChangeToLocalPath(bundleInfo.name, bundleInfo.moduleResPaths, resPaths); + for (auto moduleResPath : resPaths) { if (!moduleResPath.empty()) { APP_LOGI("MainThread::handleLaunchApplication length: %{public}zu, moduleResPath: %{public}s", moduleResPath.length(), @@ -786,7 +791,10 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con APP_LOGE("MainThread::handleLaunchApplication CheckForHandleLaunchApplication failed"); return; } - LoadAbilityLibrary(appLaunchData.GetApplicationInfo().moduleSourceDirs); + std::vector localPaths; + ChangeToLocalPath(appLaunchData.GetApplicationInfo().bundleName, + appLaunchData.GetApplicationInfo().moduleSourceDirs, localPaths); + LoadAbilityLibrary(localPaths); LoadAppLibrary(); ApplicationInfo appInfo = appLaunchData.GetApplicationInfo(); @@ -845,7 +853,7 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con if (isStageBased) { // Create runtime AbilityRuntime::Runtime::Options options; - options.codePath = appInfo.codePath; + options.codePath = LOCAL_CODE_PATH; options.eventRunner = mainHandler_->GetEventRunner(); auto runtime = AbilityRuntime::Runtime::Create(options); if (!runtime) { @@ -909,6 +917,19 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con APP_LOGI("MainThread::handleLaunchApplication called end."); } +void MainThread::ChangeToLocalPath(const std::string &bundleName, + const std::vector &sourceDirs, std::vector &localPath) +{ + for (auto item : sourceDirs) { + if (item.empty()) { + continue; + } + std::regex pattern(ABS_CODE_PATH + FILE_SEPARATOR + bundleName + FILE_SEPARATOR); + localPath.emplace_back( + std::regex_replace(item, pattern, LOCAL_CODE_PATH + FILE_SEPARATOR)); + } +} + void MainThread::HandleAbilityStage(const HapModuleInfo &abilityStage) { APP_LOGI("MainThread::HandleAbilityStageInfo");