!17969 merge dev_0109 into master

add createModuleOrPluginContext

Created-by: ProfaneAria
Commit-by: 任国军
Merged-by: openharmony_ci
Description: **IssueNo**:

**Description**:

**稳定性自检:**
| 自检项                                                       | 自检结果  |
| ------------------------------------------------------------ | -------- |
| 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发              |          |
| 成员变量进行赋值或创建需要排查并发                               |          |
| 谨慎在lambda表达式中使用引用捕获                                |          |
| 谨慎在未经拷贝的情况下使用外部传入的string、C字符串               |          |
| map\vector\list\set等stl模板类使用时需要排查并发                |          |
| 谨慎考虑加锁范围                                               |          |
| 在IPC通信中谨慎使用同步通信方式                                 |          |
| 禁止传递this指针至其他模块或线程(特别是eventhandler任务)        |          |
| 禁止将外部传入的裸指针在内部直接构造智能指针                      |          |
| 禁止多个独立创建的智能指针管理同一地址                           |          |
| 禁止在析构函数中抛异步任务                                      |          |
| 禁止js对象在非js线程(例如在IPC线程)创建、使用或销毁             |          |
| 禁止在对外接口中未经判空直接使用外部传入的指针                    |          |
| 禁止接口返回局部变量引用                                        |          |
| 禁止在信号函数中加锁                                            |          |
| 禁止在关键流程(SA启动、应用启动等主流程)执行耗时的操作           |          |
| 禁止将同一个cpp编译在不同的so中                                 |          |

**安全编码自检:**
| 自检项                                                          | 自检结果 |
| -------------------------------------------------------------- | -------- |
| 裸指针避免通过隐式转换构造为sptr                                 |          |
| json对象在取值之前必须先判断类型,避免类型不匹配                   |          |
| 序列化时必须对传入的数组大小进行校验,避免出现超大数组              |          |
| 避免使用未明确位宽的整型,选择使用int8_t、uint8_t等类型            |          |
| 外部传入的路径要做规范化校验,对路径中的.、..、../等特殊字符严格校验 |          |
| 指针变量、表示资源描述符的变量、bool变量必须赋初值                  |          |
| readParcelable获取的对象使用前需要判空                            |          |
| 分配和释放内存的函数需要成对出现                                   |          |
| 申请内存后异常退出前需要及时进行内存释放                            |          |
| 内存申请前必须对内存大小进行合法性校验                              |          |
| 内存分配后必须判断是否成功                                         |          |
| 禁止使用realloc、alloca函数                                       |          |
| 禁止打印文件路径、口令等敏感信息,如有需要,使用private修饰          |          |
| 禁止打印内存地址                                                  |          |
| 整数之间运算时必须严格检查,确保不会出现溢出、反转、除0               |          |
| 禁止对有符号整数进行位操作符运算                                    |          |
| 禁止对指针进行逻辑或位运算                                         |          |
| 循环次数如果收外部数据控制,需要检验其合法性                         |          |
| 禁止使用内存操作类危险函数,需要使用安全函数                         |          |
| 谨慎使用不可重入函数                                               |          |
| 必须检查安全函数的返回值,并进行正确处理                             |          |
| 禁止仅通过TokenType类型判断绕过权限校验                             |          |

**TDD Result**:

**XTS Result**:

### 是否已执行L0用例
- [ ] 已验证
- [ ] 不涉及。如不涉及,请写明理由


See merge request: openharmony/ability_ability_runtime!17969
This commit is contained in:
openharmony_ci
2026-01-13 20:53:53 +08:00
17 changed files with 172 additions and 0 deletions
@@ -1366,6 +1366,12 @@ std::shared_ptr<Context> AbilityContextImpl::CreateAreaModeContext(int areaMode)
return stageContext_ ? stageContext_->CreateAreaModeContext(areaMode) : nullptr;
}
std::shared_ptr<Context> AbilityContextImpl::CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
return stageContext_ ? stageContext_->CreateModuleOrPluginContext(bundleName, moduleName) : nullptr;
}
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> AbilityContextImpl::CreateDisplayContext(uint64_t displayId)
{
@@ -464,6 +464,17 @@ std::shared_ptr<Context> AbilityStageContext::CreateAreaModeContext(int areaMode
return contextImpl_->CreateAreaModeContext(areaMode);
}
std::shared_ptr<Context> AbilityStageContext::CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
if (contextImpl_ == nullptr) {
TAG_LOGE(AAFwkTag::APPKIT, "invalid contextImpl");
return nullptr;
}
return contextImpl_->CreateModuleOrPluginContext(bundleName, moduleName);
}
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> AbilityStageContext::CreateDisplayContext(uint64_t displayId)
{
@@ -1247,6 +1247,12 @@ std::shared_ptr<Context> ApplicationContext::CreateAreaModeContext(int areaMode)
return contextImpl_ ? contextImpl_->CreateAreaModeContext(areaMode) : nullptr;
}
std::shared_ptr<Context> ApplicationContext::CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
return contextImpl_ ? contextImpl_->CreateModuleOrPluginContext(bundleName, moduleName) : nullptr;
}
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> ApplicationContext::CreateDisplayContext(uint64_t displayId)
{
@@ -1987,5 +1987,25 @@ std::shared_ptr<Context> ContextImpl::CreateTargetPluginContext(const std::strin
return WrapContext(pluginBundleName, moduleName, inputContext, pluginBundleInfo, hostBundName);
}
std::shared_ptr<Context> ContextImpl::CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
if (bundleName.empty() || moduleName.empty()) {
TAG_LOGE(AAFwkTag::APPKIT, "input params is null");
return nullptr;
}
std::shared_ptr<Context> currentContext = shared_from_this();
std::shared_ptr<Context> appContext = CreateModuleContext(bundleName, moduleName, currentContext);
if (appContext == nullptr) {
auto appInfo = GetApplicationInfo();
if (appInfo && appInfo->hasPlugin) {
appContext = CreatePluginContext(bundleName, moduleName, currentContext);
}
}
return appContext;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -74,6 +74,8 @@ public:
int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName,
const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override;
std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) override;
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
#endif
@@ -53,6 +53,8 @@ public:
int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName,
const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override;
std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) override;
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
#endif
@@ -101,6 +101,8 @@ public:
int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName,
const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override;
std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) override;
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
#endif
@@ -304,6 +304,18 @@ public:
*/
virtual std::shared_ptr<Context> CreateAreaModeContext(int areaMode) = 0;
/**
* @brief Creates a module or plugin context with the given bundleName and moduleName.
*
* @param bundleName Indicates the app name of the application.
* @param moduleName Indicates the module name of the hap.
*
* @return Returns a module or plugin context created for the specified hap and app.
*/
virtual std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) = 0;
virtual ConfigUpdateReason GetConfigUpdateReason()
{
return ConfigUpdateReason::CONFIG_UPDATE_REASON_DEFAULT;
@@ -228,6 +228,9 @@ public:
std::string GetBundleNameWithContext(std::shared_ptr<Context> inputContext = nullptr) const;
std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) override;
/**
* @brief Get file area
*
@@ -198,6 +198,12 @@ std::shared_ptr<Context> MockContextImpl::CreateAreaModeContext(int areaMode)
return nullptr;
}
std::shared_ptr<Context> MockContextImpl::CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
return nullptr;
}
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> MockContextImpl::CreateDisplayContext(uint64_t displayId)
{
@@ -96,6 +96,9 @@ public:
std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) override;
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
#endif
@@ -289,6 +289,12 @@ public:
{
return nullptr;
}
virtual std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
return nullptr;
}
virtual std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId)
{
return nullptr;
@@ -212,6 +212,12 @@ std::shared_ptr<Context> MockContext::CreateAreaModeContext(int areaMode)
return nullptr;
}
std::shared_ptr<Context> MockContext::CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
return nullptr;
}
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> MockContext::CreateDisplayContext(uint64_t displayId)
{
@@ -113,6 +113,9 @@ public:
std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) override;
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
#endif
@@ -198,6 +198,12 @@ std::shared_ptr<Context> MockAbilityRuntimeContext::CreateAreaModeContext(int ar
return {};
}
std::shared_ptr<Context> MockAbilityRuntimeContext::CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName)
{
return {};
}
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> MockAbilityRuntimeContext::CreateDisplayContext(uint64_t displayId)
{
@@ -68,6 +68,8 @@ public:
int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
std::shared_ptr<Context> CreateModuleOrPluginContext(const std::string &bundleName,
const std::string &moduleName) override;
#ifdef SUPPORT_GRAPHICS
std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
#endif
@@ -576,5 +576,81 @@ HWTEST_F(ContextImplSecondTest, AppExecFwk_AppContext_CreatePluginContext_002, F
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreatePluginContext_002 end";
}
/**
* @tc.number: AppExecFwk_AppContext_CreateModuleOrPluginContext_001
* @tc.name: CreateModuleOrPluginContext
* @tc.desc: Test CreateModuleOrPluginContext.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ContextImplSecondTest, AppExecFwk_AppContext_CreateModuleOrPluginContext_001, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_001 start";
auto contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
std::string bundleName = "com.example.bundleName";
std::string moduleName = "moduleName";
auto context = contextImpl->CreateModuleOrPluginContext(bundleName, moduleName);
EXPECT_EQ(context, nullptr);
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_001 end";
}
/**
* @tc.number: AppExecFwk_AppContext_CreateModuleOrPluginContext_002
* @tc.name: CreateModuleOrPluginContext
* @tc.desc: Test CreateModuleOrPluginContext.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ContextImplSecondTest, AppExecFwk_AppContext_CreateModuleOrPluginContext_002, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_002 start";
auto contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
std::string bundleName = "";
std::string moduleName = "moduleName";
auto context = contextImpl->CreateModuleOrPluginContext(bundleName, moduleName);
EXPECT_EQ(context, nullptr);
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_002 end";
}
/**
* @tc.number: AppExecFwk_AppContext_CreateModuleOrPluginContext_003
* @tc.name: CreateModuleOrPluginContext
* @tc.desc: Test CreateModuleOrPluginContext.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ContextImplSecondTest, AppExecFwk_AppContext_CreateModuleOrPluginContext_003, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_003 start";
auto contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
std::string bundleName = "com.example.hostBundleName";
std::string moduleName = "";
auto context = contextImpl->CreateModuleOrPluginContext(bundleName, moduleName);
EXPECT_EQ(context, nullptr);
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_003 end";
}
/**
* @tc.number: AppExecFwk_AppContext_CreateModuleOrPluginContext_004
* @tc.name: CreateModuleOrPluginContext
* @tc.desc: Test CreateModuleOrPluginContext.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ContextImplSecondTest, AppExecFwk_AppContext_CreateModuleOrPluginContext_004, Function | MediumTest | Level1)
{
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_004 start";
auto contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
std::string bundleName = "";
std::string moduleName = "";
auto context = contextImpl->CreateModuleOrPluginContext(bundleName, moduleName);
EXPECT_EQ(context, nullptr);
GTEST_LOG_(INFO) << "AppExecFwk_AppContext_CreateModuleOrPluginContext_004 end";
}
} // namespace AppExecFwk
}