From ede1dc1e25cbde9595fbc6cd6d696193835b1aca Mon Sep 17 00:00:00 2001 From: wentao Date: Sat, 12 Oct 2024 10:49:43 +0800 Subject: [PATCH] Developers can use custom app package names in Cangjie applications Signed-off-by: wentao --- .../cj_environment/src/cj_environment.cpp | 10 ++++++++++ .../interfaces/inner_api/cj_environment.h | 2 ++ cj_environment/interfaces/inner_api/cj_envsetup.h | 1 + frameworks/native/appkit/app/main_thread.cpp | 4 +++- frameworks/native/runtime/cj_runtime.cpp | 14 +++++++++++++- interfaces/inner_api/runtime/include/cj_runtime.h | 2 ++ 6 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cj_environment/frameworks/cj_environment/src/cj_environment.cpp b/cj_environment/frameworks/cj_environment/src/cj_environment.cpp index d2c79a56a7..95e31851f2 100644 --- a/cj_environment/frameworks/cj_environment/src/cj_environment.cpp +++ b/cj_environment/frameworks/cj_environment/src/cj_environment.cpp @@ -411,6 +411,7 @@ void* CJEnvironment::LoadCJLibrary(const char* dlName) return nullptr; } + isLoadCJLibrary_ = true; return handle; } @@ -437,9 +438,15 @@ void* CJEnvironment::LoadCJLibrary(OHOS::CJEnvironment::LibraryKind kind, const LOGE("load cj library failed: %{public}s", DynamicGetError()); return nullptr; } + isLoadCJLibrary_ = true; return handle; } +bool CJEnvironment::CheckLoadCJLibrary() +{ + return isLoadCJLibrary_; +} + void CJEnvironment::UnLoadCJLibrary(void* handle) { DynamicFreeLibrary(handle); @@ -516,6 +523,9 @@ CJ_EXPORT extern "C" CJEnvMethods* OHOS_GetCJEnvInstance() }, .setSanitizerKindRuntimeVersion = [](SanitizerKind kind) { return CJEnvironment::GetInstance()->SetSanitizerKindRuntimeVersion(kind); + }, + .checkLoadCJLibrary = []() { + return CJEnvironment::GetInstance()->CheckLoadCJLibrary(); } }; return &gCJEnvMethods; diff --git a/cj_environment/interfaces/inner_api/cj_environment.h b/cj_environment/interfaces/inner_api/cj_environment.h index 9ba9163634..d1d5c7e447 100644 --- a/cj_environment/interfaces/inner_api/cj_environment.h +++ b/cj_environment/interfaces/inner_api/cj_environment.h @@ -57,6 +57,7 @@ public: return isUISchedulerStarted_; } bool StartUIScheduler(); + bool CheckLoadCJLibrary(); void StopUIScheduler(); enum LibraryKind { SYSTEM, @@ -86,6 +87,7 @@ private: bool LoadRuntimeApis(); static CJRuntimeAPI lazyApis_; bool isRuntimeStarted_{false}; + bool isLoadCJLibrary_{false}; bool isUISchedulerStarted_{false}; void* uiScheduler_ {nullptr}; SanitizerKind sanitizerKind_ {SanitizerKind::NONE}; diff --git a/cj_environment/interfaces/inner_api/cj_envsetup.h b/cj_environment/interfaces/inner_api/cj_envsetup.h index ae425359c7..367881c7cc 100644 --- a/cj_environment/interfaces/inner_api/cj_envsetup.h +++ b/cj_environment/interfaces/inner_api/cj_envsetup.h @@ -51,6 +51,7 @@ struct CJEnvMethods { bool (*startDebugger)() = nullptr; void (*registerCJUncaughtExceptionHandler)(const CJUncaughtExceptionInfo& uncaughtExceptionInfo) = nullptr; void (*setSanitizerKindRuntimeVersion)(SanitizerKind kind) = nullptr; + bool (*checkLoadCJLibrary)() = nullptr; }; class CJEnv { diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index 6da357339f..258a6f1ede 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -1345,7 +1345,9 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con } #ifdef CJ_FRONTEND if (!entryHapModuleInfo.abilityInfos.empty()) { - isCJApp = AbilityRuntime::CJRuntime::IsCJAbility(entryHapModuleInfo.abilityInfos.front().srcEntrance); + auto srcEntrancenName = entryHapModuleInfo.abilityInfos.front().srcEntrance; + isCJApp = AbilityRuntime::CJRuntime::IsCJAbility(srcEntrancenName); + AbilityRuntime::CJRuntime::SetPackageName(srcEntrancenName); } #endif moduelJson = entryHapModuleInfo.isModuleJson; diff --git a/frameworks/native/runtime/cj_runtime.cpp b/frameworks/native/runtime/cj_runtime.cpp index 3b73ce7442..fb08a75ffb 100644 --- a/frameworks/native/runtime/cj_runtime.cpp +++ b/frameworks/native/runtime/cj_runtime.cpp @@ -69,6 +69,8 @@ CJEnvMethods* CJEnv::LoadInstance() } AppLibPathVec CJRuntime::appLibPaths_; +std::string CJRuntime::packageName_; + std::unique_ptr CJRuntime::Create(const Options& options) { auto instance = std::make_unique(); @@ -152,10 +154,13 @@ bool CJRuntime::LoadCJAppLibrary(const AppLibPathVec& appLibPaths) return false; } void* handle = nullptr; + // According to the OHOS rule, the format of the SO name is as follows + auto targetSoName = "lib" + packageName_ + ".so"; + for (const auto& libPath : appLibPaths) { for (auto& itor : std::filesystem::directory_iterator(libPath)) { // According to the convention, the names of cj generated products must contain the following keywords - if (itor.path().string().find("ohos_app_cangjie") == std::string::npos) { + if (itor.path().string().find(targetSoName) == std::string::npos) { continue; } handle = cjEnv->loadCJLibrary(itor.path().c_str()); @@ -171,6 +176,13 @@ bool CJRuntime::LoadCJAppLibrary(const AppLibPathVec& appLibPaths) return true; } +void CJRuntime::SetPackageName(std::string srcEntryName) +{ + // According to the srcEntry rule in the Cangjie application, + // the last '.' The previous strings were all package names + packageName_ = srcEntryName.substr(0, srcEntryName.find_last_of(".")); +} + void CJRuntime::SetSanitizerVersion(SanitizerKind kind) { auto cjEnv = OHOS::CJEnv::LoadInstance(); diff --git a/interfaces/inner_api/runtime/include/cj_runtime.h b/interfaces/inner_api/runtime/include/cj_runtime.h index de2b588fd9..8faf5d95c5 100644 --- a/interfaces/inner_api/runtime/include/cj_runtime.h +++ b/interfaces/inner_api/runtime/include/cj_runtime.h @@ -36,6 +36,7 @@ public: static void SetAppLibPath(const AppLibPathMap& appLibPaths); static bool IsCJAbility(const std::string& info); static void SetSanitizerVersion(SanitizerKind kind); + static void SetPackageName(std::string srcEntryName); ~CJRuntime() override = default; Language GetLanguage() const override @@ -78,6 +79,7 @@ private: std::string bundleName_; uint32_t instanceId_ = 0; static AppLibPathVec appLibPaths_; + static std::string packageName_; }; } // namespace AbilityRuntime } // namespace OHOS