!16971 merge add_getUIExtensionWindowProxy into master

【AMS】add ets getUIExtensionWindowProxy

Created-by: zhongshield1
Commit-by: zhangzezhong
Merged-by: openharmony_ci
Description: **IssueNo**:
https://gitcode.com/openharmony/ability_ability_runtime/issues/13315
**Description**:
验证截图:
![image.png](https://raw.gitcode.com/user-images/assets/4421702/77762f8c-b21c-4579-a6a8-696faf0df7a4/image.png 'image.png')
**稳定性自检:**
| 自检项                                                       | 自检结果  |
| ------------------------------------------------------------ | -------- |
| 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发              |          |
| 成员变量进行赋值或创建需要排查并发                               |          |
| 谨慎在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!16971
This commit is contained in:
openharmony_ci
2025-10-21 20:00:42 +08:00
8 changed files with 74 additions and 50 deletions
@@ -89,6 +89,7 @@ bool GetStaticFieldString(ani_env *env, ani_class classObj, const char *fieldNam
ani_env *AttachAniEnv(ani_vm * etsVm, bool &isAttachThread);
void DetachAniEnv(ani_vm * etsVm, bool &isAttachThread);
bool IsValidProperty(ani_env *env, ani_ref param);
bool CheckCallerIsSystemApp();
} // namespace AppExecFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_ANI_COMMON_UTIL_H
@@ -19,7 +19,9 @@
#include "ani_enum_convert.h"
#include "running_process_info.h"
#include "hilog_tag_wrapper.h"
#include "ipc_skeleton.h"
#include "securec.h"
#include "tokenid_kit.h"
namespace OHOS {
namespace AppExecFwk {
@@ -1455,5 +1457,15 @@ bool IsValidProperty(ani_env *env, ani_ref param)
}
return true;
}
bool CheckCallerIsSystemApp()
{
auto selfToken = IPCSkeleton::GetSelfTokenID();
if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
TAG_LOGE(AAFwkTag::ANI, "Non-system app forbidden to call");
return false;
}
return true;
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -424,12 +424,6 @@ std::shared_ptr<Context> GetBaseContext(ani_env *env, ani_object aniObj)
return weakContext != nullptr ? weakContext->lock() : nullptr;
}
bool CheckCallerIsSystemApp()
{
auto selfToken = IPCSkeleton::GetSelfTokenID();
return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken);
}
ani_object CreateModuleResourceManagerSync(ani_env *env, ani_object aniObj,
ani_string bundleName, ani_string moduleName)
{
@@ -447,7 +441,7 @@ ani_object CreateModuleResourceManagerSync(ani_env *env, ani_object aniObj,
EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
return nullptr;
}
if (!CheckCallerIsSystemApp()) {
if (!AppExecFwk::CheckCallerIsSystemApp()) {
TAG_LOGE(AAFwkTag::APPKIT, "not system-app");
EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP);
return nullptr;
@@ -107,9 +107,6 @@ public:
private:
static sptr<AppExecFwk::IAppMgr> GetAppManagerInstance();
static sptr<AAFwk::IAbilityManager> GetAbilityManagerInstance();
#ifdef SUPPORT_SCREEN
static bool CheckCallerIsSystemApp();
#endif
static bool CheckOnOnApplicationStateInnerParam(ani_env *env, ani_string type, ani_object observer,
ani_object etsBundleNameList, std::vector<std::string> &bundleNameList);
static ani_int OnOnApplicationStateInner(
@@ -146,14 +143,6 @@ sptr<AAFwk::IAbilityManager> EtsAppManager::GetAbilityManagerInstance()
return iface_cast<AAFwk::IAbilityManager>(abilityObject);
}
#ifdef SUPPORT_SCREEN
bool EtsAppManager::CheckCallerIsSystemApp()
{
auto selfToken = IPCSkeleton::GetSelfTokenID();
return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken);
}
#endif
void EtsAppManager::PreloadApplication(ani_env *env, ani_object callback, ani_string aniBundleName,
ani_int aniUserId, ani_enum_item aniMode, ani_object aniAppIndex)
{
@@ -296,7 +285,7 @@ void EtsAppManager::GetRunningMultiAppInfo(ani_env *env, ani_string aniBundleNam
}
ani_object emptyMultiAppInfo = CreateEmptyMultiAppInfo(env);
#ifdef SUPPORT_SCREEN
if (!CheckCallerIsSystemApp()) {
if (!AppExecFwk::CheckCallerIsSystemApp()) {
TAG_LOGE(AAFwkTag::APPMGR, "Non-system app");
AppExecFwk::AsyncCallback(env, callback,
AbilityRuntime::EtsErrorUtil::CreateError(
@@ -1131,7 +1120,7 @@ void EtsAppManager::OnOnAbilityFirstFrameState(
TAG_LOGE(AAFwkTag::APPMGR, "env null ptr");
return;
}
if (!CheckCallerIsSystemApp()) {
if (!AppExecFwk::CheckCallerIsSystemApp()) {
TAG_LOGE(AAFwkTag::APPMGR, "Non-system app");
AbilityRuntime::EtsErrorUtil::ThrowError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP);
return;
@@ -1219,7 +1208,7 @@ void EtsAppManager::OnOffAbilityFirstFrameState(ani_env *env, ani_string type, a
TAG_LOGE(AAFwkTag::APPMGR, "env null ptr");
return;
}
if (!CheckCallerIsSystemApp()) {
if (!AppExecFwk::CheckCallerIsSystemApp()) {
TAG_LOGE(AAFwkTag::APPMGR, "Non-system app");
AbilityRuntime::EtsErrorUtil::ThrowError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP);
return;
@@ -79,6 +79,7 @@ public:
static void NativeSetReceiveDataCallback(ani_env *env, ani_object clsObj, ani_object funcObj);
static void NativeSetReceiveDataForResultCallback(ani_env *env, ani_object clsObj, ani_object funcObj);
static ani_object NativeGetUIExtensionHostWindowProxy(ani_env *env, ani_object obj);
static ani_object NativeGetUIExtensionWindowProxy(ani_env *env, ani_object obj);
static ani_object NativeStartAbilityByTypeSync(
ani_env *env, ani_object obj, ani_string type, ani_ref wantParam, ani_object startCallback);
@@ -87,7 +88,6 @@ public:
void TerminateSelf();
int32_t TerminateSelfWithResult();
void SetWindowBackgroundColor(ani_env *env, ani_string color);
ani_object GetUIExtensionHostWindowProxy(ani_env *env, ani_object object);
void SetReceiveDataCallback(ani_env *env, ani_object functionObj);
static void CallReceiveDataCallback(ani_vm *vm, ani_ref callbackRef, const AAFwk::WantParams &wantParams);
void SetReceiveDataForResultCallback(ani_env *env, ani_object object);
@@ -109,6 +109,8 @@ private:
ani_object StartAbilityByTypeSync(ani_env *env, ani_string aniType, ani_ref aniWantParam, ani_object startCallback);
bool CheckStartAbilityByTypeParam(
ani_env *env, ani_string aniType, ani_ref aniWantParam, std::string &type, AAFwk::WantParams &wantParam);
ani_object GetUIExtensionHostWindowProxy(ani_env *env, ani_object object);
ani_object GetUIExtensionWindowProxy(ani_env *env, ani_object object);
sptr<AAFwk::SessionInfo> sessionInfo_;
sptr<Rosen::Window> uiWindow_;
@@ -47,6 +47,10 @@ const std::string FLAG_AUTH_READ_URI_PERMISSION = "ability.want.params.uriPermis
constexpr const char *SIGNATURE_START_ABILITY_BY_TYPE =
"Lstd/core/String;Lescompat/Record;Lapplication/AbilityStartCallback/AbilityStartCallback;:L@ohos/base/"
"BusinessError;";
constexpr const char *SIGNATURE_GET_UI_EXTENSION_HOST_WINDOW_PROXY =
":L@ohos/uiExtensionHost/uiExtensionHost/UIExtensionHostWindowProxy;";
constexpr const char *SIGNATURE_GET_UI_EXTENSION_WINDOW_PROXY =
":L@ohos/arkui/uiExtension/uiExtension/WindowProxy;";
} // namespace
EtsUIExtensionContentSession* EtsUIExtensionContentSession::GetEtsContentSession(ani_env *env, ani_object obj)
@@ -170,22 +174,32 @@ void EtsUIExtensionContentSession::NativeSetWindowBackgroundColor(ani_env *env,
ani_object EtsUIExtensionContentSession::NativeGetUIExtensionHostWindowProxy(ani_env *env, ani_object obj)
{
auto etsContentSession = EtsUIExtensionContentSession::GetEtsContentSession(env, obj);
ani_object object = nullptr;
if (etsContentSession != nullptr) {
object = etsContentSession->GetUIExtensionHostWindowProxy(env, obj);
if (etsContentSession == nullptr) {
TAG_LOGE(AAFwkTag::UI_EXT, "null etsContentSession");
return nullptr;
}
return object;
return etsContentSession->GetUIExtensionHostWindowProxy(env, obj);
}
ani_object EtsUIExtensionContentSession::NativeGetUIExtensionWindowProxy(ani_env *env, ani_object obj)
{
auto etsContentSession = EtsUIExtensionContentSession::GetEtsContentSession(env, obj);
if (etsContentSession == nullptr) {
TAG_LOGE(AAFwkTag::UI_EXT, "null etsContentSession");
return nullptr;
}
return etsContentSession->GetUIExtensionWindowProxy(env, obj);
}
ani_object EtsUIExtensionContentSession::NativeStartAbilityByTypeSync(
ani_env *env, ani_object obj, ani_string type, ani_ref wantParam, ani_object startCallback)
{
auto etsContentSession = EtsUIExtensionContentSession::GetEtsContentSession(env, obj);
ani_object object = nullptr;
if (etsContentSession != nullptr) {
object = etsContentSession->StartAbilityByTypeSync(env, type, wantParam, startCallback);
if (etsContentSession == nullptr) {
TAG_LOGE(AAFwkTag::UI_EXT, "null etsContentSession");
return nullptr;
}
return object;
return etsContentSession->StartAbilityByTypeSync(env, type, wantParam, startCallback);
}
EtsUIExtensionContentSession::EtsUIExtensionContentSession(
@@ -275,8 +289,10 @@ ani_object EtsUIExtensionContentSession::CreateEtsUIExtensionContentSession(ani_
reinterpret_cast<void *>(EtsUIExtensionContentSession::NativeTerminateSelfWithResult)},
ani_native_function {"setWindowBackgroundColor", nullptr,
reinterpret_cast<void *>(EtsUIExtensionContentSession::NativeSetWindowBackgroundColor)},
ani_native_function {"getUIExtensionHostWindowProxy", nullptr,
ani_native_function {"getUIExtensionHostWindowProxy", SIGNATURE_GET_UI_EXTENSION_HOST_WINDOW_PROXY,
reinterpret_cast<void *>(EtsUIExtensionContentSession::NativeGetUIExtensionHostWindowProxy)},
ani_native_function {"getUIExtensionWindowProxy", SIGNATURE_GET_UI_EXTENSION_WINDOW_PROXY,
reinterpret_cast<void *>(EtsUIExtensionContentSession::NativeGetUIExtensionWindowProxy)},
ani_native_function {"nativeSetReceiveDataCallback", nullptr,
reinterpret_cast<void *>(EtsUIExtensionContentSession::NativeSetReceiveDataCallback)},
ani_native_function {"nativeSetReceiveDataForResultCallback", nullptr,
@@ -385,25 +401,43 @@ void EtsUIExtensionContentSession::SetWindowBackgroundColor(ani_env *env, ani_st
ani_object EtsUIExtensionContentSession::GetUIExtensionHostWindowProxy(ani_env *env, ani_object object)
{
TAG_LOGD(AAFwkTag::UI_EXT, "called");
TAG_LOGD(AAFwkTag::UI_EXT, "GetUIExtensionHostWindowProxy called");
if (!AppExecFwk::CheckCallerIsSystemApp()) {
TAG_LOGE(AAFwkTag::UI_EXT, "This application is not system-app, can not use system-api");
EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP);
return nullptr;
}
if (sessionInfo_ == nullptr) {
TAG_LOGE(AAFwkTag::UI_EXT, "null sessionInfo_");
EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER);
return nullptr;
}
ani_object etsExtensionWindow = nullptr;
etsExtensionWindow =
ani_object etsExtensionWindow =
Rosen::AniExtensionWindow::CreateAniExtensionWindow(env, uiWindow_, sessionInfo_->hostWindowId);
if (etsExtensionWindow == nullptr) {
TAG_LOGE(AAFwkTag::UI_EXT, "null etsExtensionWindow");
EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER);
return nullptr;
}
ani_ref resultRef = nullptr;
ani_status status = ANI_ERROR;
if ((status = env->GlobalReference_Create(etsExtensionWindow, &resultRef)) != ANI_OK) {
return etsExtensionWindow;
}
ani_object EtsUIExtensionContentSession::GetUIExtensionWindowProxy(ani_env *env, ani_object object)
{
TAG_LOGD(AAFwkTag::UI_EXT, "GetUIExtensionWindowProxy called");
if (sessionInfo_ == nullptr) {
TAG_LOGE(AAFwkTag::UI_EXT, "null sessionInfo_");
EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER);
return nullptr;
}
return reinterpret_cast<ani_object>(resultRef);
ani_object etsExtensionWindow =
Rosen::AniExtensionWindow::CreateAniExtensionWindow(env, uiWindow_, sessionInfo_->hostWindowId, false);
if (etsExtensionWindow == nullptr) {
TAG_LOGE(AAFwkTag::UI_EXT, "null etsExtensionWindow");
EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER);
return nullptr;
}
return etsExtensionWindow;
}
void EtsUIExtensionContentSession::SetReceiveDataCallback(ani_env *env, ani_object functionObj)
@@ -35,16 +35,6 @@ constexpr int32_t PARAMETER_ERROR = -1;
constexpr const char* COMPLETE_DATA_IMPL_CLASS_NAME = "L@ohos/app/ability/wantAgent/wantAgent/CompleteDataImpl;";
constexpr const char* WANT_AGENT_NAMESPACE = "L@ohos/app/ability/wantAgent/wantAgent;";
constexpr const char* CLEANER_CLASS = "L@ohos/app/ability/wantAgent/wantAgent/Cleaner;";
bool CheckCallerIsSystemApp()
{
auto selfToken = IPCSkeleton::GetSelfTokenID();
if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
TAG_LOGE(AAFwkTag::WANTAGENT, "Non-system app forbidden to call");
return false;
}
return true;
}
} // namespace
TriggerCompleteCallBack::TriggerCompleteCallBack()
@@ -347,7 +337,7 @@ void EtsWantAgent::OnGetWant(ani_env *env, ani_object agent, ani_object call)
return;
}
WantAgent* pWantAgent = nullptr;
if (!CheckCallerIsSystemApp()) {
if (!AppExecFwk::CheckCallerIsSystemApp()) {
TAG_LOGE(AAFwkTag::WANTAGENT, "Non-system app");
EtsErrorUtil::ThrowError(env, ERR_ABILITY_RUNTIME_NOT_SYSTEM_APP,
AbilityRuntimeErrorUtil::GetErrMessage(ERR_ABILITY_RUNTIME_NOT_SYSTEM_APP));
@@ -609,7 +599,7 @@ int32_t EtsWantAgent::GetTriggerInfo(ani_env *env, ani_object triggerInfoObj, Tr
}
std::shared_ptr<AAFwk::StartOptions> startOptions = nullptr;
if (!isUndefined) {
if (!CheckCallerIsSystemApp()) {
if (!AppExecFwk::CheckCallerIsSystemApp()) {
TAG_LOGE(AAFwkTag::WANTAGENT, "Non-system app");
EtsErrorUtil::ThrowError(env, ERR_ABILITY_RUNTIME_NOT_SYSTEM_APP,
AbilityRuntimeErrorUtil::GetErrMessage(ERR_ABILITY_RUNTIME_NOT_SYSTEM_APP));
@@ -20,6 +20,7 @@ import uiExtensionHost from '@ohos.uiExtensionHost';
import { LocalStorage } from '@ohos.arkui.stateManagement';
import AsyncCallbackWrapper from './utils/AbilityUtils';
import AbilityStartCallback from 'application.AbilityStartCallback';
import uiExtension from '@ohos.arkui.uiExtension';
class Cleaner {
static callback(cleaner: Cleaner): void {
@@ -67,6 +68,7 @@ export default class UIExtensionContentSession {
native loadContent(path: string, storage?: LocalStorage): void;
native setWindowBackgroundColor(color: string): void;
native getUIExtensionHostWindowProxy(): uiExtensionHost.UIExtensionHostWindowProxy;
native getUIExtensionWindowProxy(): uiExtension.WindowProxy;
native nativeSetReceiveDataCallback(callback: (data: Record<string, Object>) => void): void;
native nativeSetReceiveDataForResultCallback(callback: (data: Record<string, Object>) => Record<string, Object>): void;
native terminateSelfSync(callback:AsyncCallbackWrapper<void>): void;