!7048 UIExtension支持递归查询是否聚焦

Merge pull request !7048 from yangzk/uiext
This commit is contained in:
openharmony_ci
2023-12-02 10:18:23 +00:00
committed by Gitee
16 changed files with 504 additions and 223 deletions
@@ -428,8 +428,11 @@ bool JsUIExtension::ForegroundWindowWithInsightIntent(const AAFwk::Want &want,
auto context = GetContext();
InsightIntentExecutorInfo executorInfo;
executorInfo.hapPath = context->GetAbilityInfo()->hapPath;
executorInfo.windowMode = context->GetAbilityInfo()->compileMode == AppExecFwk::CompileMode::ES_MODULE;
std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = context->GetAbilityInfo();
if (abilityInfo != nullptr) {
executorInfo.hapPath = abilityInfo->hapPath;
executorInfo.windowMode = abilityInfo->compileMode == AppExecFwk::CompileMode::ES_MODULE;
}
executorInfo.token = context->GetToken();
executorInfo.pageLoader = contentSessions_[sessionInfo->uiExtensionComponentId];
executorInfo.executeParam = std::make_shared<InsightIntentExecuteParam>();
@@ -259,8 +259,11 @@ bool JsUIExtensionBase::ForegroundWindowWithInsightIntent(const AAFwk::Want &wan
});
InsightIntentExecutorInfo executorInfo;
executorInfo.hapPath = context_->GetAbilityInfo()->hapPath;
executorInfo.windowMode = context_->GetAbilityInfo()->compileMode == AppExecFwk::CompileMode::ES_MODULE;
std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo = context_->GetAbilityInfo();
if (abilityInfo != nullptr) {
executorInfo.hapPath = abilityInfo->hapPath;
executorInfo.windowMode = abilityInfo->compileMode == AppExecFwk::CompileMode::ES_MODULE;
}
executorInfo.token = context_->GetToken();
executorInfo.pageLoader = contentSessions_[sessionInfo->uiExtensionComponentId];
executorInfo.executeParam = std::make_shared<InsightIntentExecuteParam>();
+3 -1
View File
@@ -63,7 +63,9 @@ abilityms_files = [
# new ability manager service here
"src/task_data_persistence_mgr.cpp",
"src/ui_extension_ability_connect_manager.cpp",
"src/extension_record_manager.cpp",
"src/extension_record.cpp",
"src/ui_extension_record.cpp",
"src/start_options.cpp",
"src/stop_user_callback_proxy.cpp",
"src/stop_user_callback_stub.cpp",
@@ -32,7 +32,7 @@
#include "connection_record.h"
#include "element_name.h"
#include "ui_extension_ability_connect_info.h"
#include "ui_extension_ability_connect_manager.h"
#include "extension_record_manager.h"
#include "want.h"
#include "iremote_object.h"
#include "nocopyable.h"
@@ -41,7 +41,7 @@ namespace OHOS {
namespace AAFwk {
using OHOS::AppExecFwk::AbilityType;
using UIExtensionAbilityConnectInfo = AbilityRuntime::UIExtensionAbilityConnectInfo;
using UIExtensionAbilityConnectManager = AbilityRuntime::UIExtensionAbilityConnectManager;
using UIExtensionAbilityConnectManager = AbilityRuntime::ExtensionRecordManager;
/**
* @class AbilityConnectManager
@@ -502,12 +502,12 @@ private:
bool IsLauncher(std::shared_ptr<AbilityRecord> serviceExtension) const;
bool IsSceneBoard(std::shared_ptr<AbilityRecord> serviceExtension) const;
void KillProcessesByUserId() const;
inline bool IsUIExtensionAbility(const std::shared_ptr<AbilityRecord> abilityRecord);
inline bool IsUIExtensionAbility(const std::shared_ptr<AbilityRecord> &abilityRecord);
inline bool CheckUIExtensionAbilityLoaded(const AbilityRequest &abilityRequest);
inline bool CheckUIExtensionAbilitySessionExistLocked(const std::shared_ptr<AbilityRecord> abilityRecord);
inline int32_t AddUIExtensionAbilityRecord(const std::shared_ptr<AAFwk::AbilityRecord> abilityRecord,
const std::string hostBundleName, const int32_t inputId) const;
inline void RemoveUIExtensionAbilityRecord(const std::shared_ptr<AbilityRecord> abilityRecord);
inline bool CheckUIExtensionAbilitySessionExistLocked(const std::shared_ptr<AbilityRecord> &abilityRecord);
inline int32_t AddUIExtensionAbilityRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, const int32_t inputId) const;
inline void RemoveUIExtensionAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);
private:
const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
@@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_RECORD_H
#define OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_RECORD_H
#ifndef OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_H
#define OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_H
#include <atomic>
#include <map>
@@ -25,19 +25,28 @@
namespace OHOS {
namespace AbilityRuntime {
class UIExtensionAbilityRecord : public std::enable_shared_from_this<UIExtensionAbilityRecord> {
class ExtensionRecord : public std::enable_shared_from_this<ExtensionRecord> {
public:
UIExtensionAbilityRecord(const std::shared_ptr<AAFwk::AbilityRecord> abilityRecord, std::string hostBundleName,
int32_t uiExtensionAbilityId)
: abilityRecord_(abilityRecord), hostBundleName_(hostBundleName), uiExtensionAbilityId_(uiExtensionAbilityId)
{}
ExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, int32_t extensionRecordId);
virtual ~UIExtensionAbilityRecord() = default;
virtual ~ExtensionRecord();
sptr<IRemoteObject> GetCallToken() const;
sptr<IRemoteObject> GetRootCallerToken() const;
void SetRootCallerToken(sptr<IRemoteObject> &rootCallerToken);
virtual bool ContinueToGetCallerToken();
std::shared_ptr<AAFwk::AbilityRecord> abilityRecord_ = nullptr;
std::string hostBundleName_ = "";
int32_t uiExtensionAbilityId_ = 0;
std::string hostBundleName_;
int32_t extensionRecordId_ = 0;
private:
sptr<IRemoteObject> rootCallerToken_ = nullptr;
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_RECORD_H
#endif // OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_H
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2023 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 OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H
#define OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H
#include <atomic>
#include <map>
#include <memory>
#include <set>
#include "extension_record.h"
namespace OHOS {
namespace AbilityRuntime {
constexpr int32_t INVALID_EXTENSION_RECORD_ID = 0;
class ExtensionRecordManager : public std::enable_shared_from_this<ExtensionRecordManager> {
public:
using ExtensionAbilityRecordMap = std::map<int32_t, std::shared_ptr<ExtensionRecord>>;
explicit ExtensionRecordManager(const int32_t userId);
virtual ~ExtensionRecordManager();
/**
* @brief Generate extension record id, if input id didn't exist, return it, else assign one.
*
* @param extensionRecordId Input extension record id.
* @return int32_t Generated extension record id.
*/
int32_t GenerateExtensionRecordId(const int32_t extensionRecordId);
/**
* @brief Add extension record by id, if record exist, replace it.
*
* @param extensionRecordId extension record id.
* @param record extension record.
*/
void AddExtensionRecord(const int32_t extensionRecordId, const std::shared_ptr<ExtensionRecord> &record);
/**
* @brief Remove extension record by id
*
* @param extensionRecordId extension record id.
*/
void RemoveExtensionRecord(const int32_t extensionRecordId);
/**
* @brief Check if host bundleName matched to stored record by specified id.
*
* @param extensionRecordId extension record id.
* @param hostBundleName bundleName of target extension.
* @return true Matched.
* @return false Not Match.
*/
bool CheckExtensionLoaded(const int32_t extensionRecordId, const std::string &hostBundleName);
static bool IsBelongToManager(const AppExecFwk::AbilityInfo &abilityInfo);
bool IsFocused(int32_t extensionRecordId, const sptr<IRemoteObject>& focusToken);
int32_t CreateExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, int32_t &extensionRecordId);
private:
int32_t userId_;
static std::atomic_int32_t extensionRecordId_;
std::mutex mutex_;
std::set<int32_t> extensionRecordIdSet_;
ExtensionAbilityRecordMap extensionRecords_;
sptr<IRemoteObject> GetRootCallerTokenLocked(int32_t extensionRecordId);
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_EXTENSION_RECORD_MANAGER_H
@@ -1,80 +0,0 @@
/*
* Copyright (c) 2023 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 OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_CONNECT_MANAGER_H
#define OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_CONNECT_MANAGER_H
#include <atomic>
#include <map>
#include <memory>
#include <set>
#include "ui_extension_ability_record.h"
namespace OHOS {
namespace AbilityRuntime {
constexpr int32_t INVALID_UI_EXTENSION_ABILITY_ID = 0;
class UIExtensionAbilityConnectManager : public std::enable_shared_from_this<UIExtensionAbilityConnectManager> {
public:
using UIExtensionAbilityRecordMap = std::map<int32_t, std::shared_ptr<UIExtensionAbilityRecord>>;
explicit UIExtensionAbilityConnectManager(const int32_t userId);
virtual ~UIExtensionAbilityConnectManager();
/**
* @brief Generate uiextensionability id, if input id didn't exist, return it, else assign one.
*
* @param uiExtensionAbilityId Input uiextensionability id.
* @return int32_t Generated uiextensionability id.
*/
int32_t GenerateUIExtensionAbilityId(const int32_t uiExtensionAbilityId);
/**
* @brief Add uiextensionability record by id, if record exist, replace it.
*
* @param uiExtensionAbilityId uiextensionability id.
* @param record uiextensionability record.
*/
void AddUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId,
const std::shared_ptr<UIExtensionAbilityRecord> record);
/**
* @brief Remove uiextensionability record by id
*
* @param uiExtensionAbilityId uiextensionability id.
*/
void RemoveUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId);
/**
* @brief Check if host bundleName matched to stored record by specified id.
*
* @param uiExtensionAbilityId uiextensionability id.
* @param hostBundleName bundleName of target uiextensionability.
* @return true Matched.
* @return false Not Match.
*/
bool CheckUIExtensionAbilityLoaded(const int32_t uiExtensionAbilityId, const std::string hostBundleName);
private:
int32_t userId_;
static std::atomic_int32_t uiExtensionAbilityId_;
std::mutex mutex_;
std::set<int32_t> uiExtensionAbilityIdSet_;
UIExtensionAbilityRecordMap uiExtensionAbilityRecords_;
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_ABILITY_CONNECT_MANAGER_H
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 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 OHOS_ABILITY_RUNTIME_UI_EXTENSION_RECORD_H
#define OHOS_ABILITY_RUNTIME_UI_EXTENSION_RECORD_H
#include <atomic>
#include <map>
#include <memory>
#include <string>
#include "extension_record.h"
namespace OHOS {
namespace AbilityRuntime {
class UIExtensionRecord : public ExtensionRecord {
public:
UIExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, int32_t extensionRecordId);
~UIExtensionRecord() override;
bool ContinueToGetCallerToken() override;
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_RECORD_H
@@ -30,7 +30,7 @@
#include "mock_session_manager_service.h"
#include "parameter.h"
#include "session/host/include/zidl/session_interface.h"
#include "ui_extension_ability_record.h"
#include "extension_record.h"
#include "ui_extension_utils.h"
namespace OHOS {
@@ -69,7 +69,7 @@ const std::unordered_set<std::string> FROZEN_WHITE_LIST {
AbilityConnectManager::AbilityConnectManager(int userId) : userId_(userId)
{
uiExtensionAbilityRecordMgr_ = std::make_unique<AbilityRuntime::UIExtensionAbilityConnectManager>(userId);
uiExtensionAbilityRecordMgr_ = std::make_unique<AbilityRuntime::ExtensionRecordManager>(userId);
}
AbilityConnectManager::~AbilityConnectManager()
@@ -136,7 +136,7 @@ int AbilityConnectManager::StartAbilityLocked(const AbilityRequest &abilityReque
if (IsUIExtensionAbility(targetService) && abilityRequest.sessionInfo != nullptr) {
std::string hostBundleName = abilityRequest.abilityInfo.bundleName;
int32_t inputId = abilityRequest.sessionInfo->want.GetIntParam(UIEXTENSION_ABILITY_ID,
INVALID_UI_EXTENSION_ABILITY_ID);
INVALID_EXTENSION_RECORD_ID);
auto uiExtensionAbilityId = AddUIExtensionAbilityRecord(targetService, hostBundleName, inputId);
HILOG_DEBUG("UIExtensionAbility id %{public}d.", uiExtensionAbilityId);
}
@@ -723,7 +723,7 @@ int AbilityConnectManager::ScheduleDisconnectAbilityDoneLocked(const sptr<IRemot
abilityRecord->RemoveConnectRecordFromList(connect);
if (abilityRecord->IsConnectListEmpty() && abilityRecord->GetStartId() == 0) {
if (IsUIExtensionAbility(abilityRecord) && CheckUIExtensionAbilitySessionExistLocked(abilityRecord)) {
HILOG_INFO("There exist ui extension component, don't terminate when disconnet.");
HILOG_INFO("There exist ui extension component, don't terminate when disconnect.");
} else {
HILOG_INFO("Service ability has no any connection, and not started, need terminate.");
TerminateRecord(abilityRecord);
@@ -2190,12 +2190,15 @@ void AbilityConnectManager::HandleUIExtWindowDiedTask(const sptr<IRemoteObject>
bool AbilityConnectManager::IsUIExtensionFocused(uint32_t uiExtensionTokenId, const sptr<IRemoteObject>& focusToken)
{
HILOG_DEBUG("called, id: %{public}u", uiExtensionTokenId);
CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, false);
std::lock_guard guard(Lock_);
for (auto& item: uiExtensionMap_) {
auto uiExtension = item.second.first.lock();
auto sessionInfo = item.second.second;
if (uiExtension && uiExtension->GetApplicationInfo().accessTokenId == uiExtensionTokenId
&& sessionInfo && sessionInfo->callerToken == focusToken) {
&& uiExtensionAbilityRecordMgr_->IsFocused(uiExtension->GetUIExtensionAbilityId(), focusToken)) {
HILOG_INFO("id: %{public}u, isFocused.", uiExtensionTokenId);
return true;
}
}
@@ -2302,7 +2305,7 @@ void AbilityConnectManager::HandleExtensionDisconnectTask(const std::shared_ptr<
}
}
bool AbilityConnectManager::IsUIExtensionAbility(const std::shared_ptr<AbilityRecord> abilityRecord)
bool AbilityConnectManager::IsUIExtensionAbility(const std::shared_ptr<AbilityRecord> &abilityRecord)
{
CHECK_POINTER_AND_RETURN(abilityRecord, false);
return UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType);
@@ -2314,20 +2317,20 @@ bool AbilityConnectManager::CheckUIExtensionAbilityLoaded(const AbilityRequest &
CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, false);
int32_t uiExtensionAbilityId = abilityRequest.sessionInfo->want.GetIntParam(UIEXTENSION_ABILITY_ID,
INVALID_UI_EXTENSION_ABILITY_ID);
if (uiExtensionAbilityId == INVALID_UI_EXTENSION_ABILITY_ID) {
INVALID_EXTENSION_RECORD_ID);
if (uiExtensionAbilityId == INVALID_EXTENSION_RECORD_ID) {
HILOG_DEBUG("Didn't carry uiextension ability id when start.");
return true;
}
auto ret = uiExtensionAbilityRecordMgr_->CheckUIExtensionAbilityLoaded(
auto ret = uiExtensionAbilityRecordMgr_->CheckExtensionLoaded(
uiExtensionAbilityId, abilityRequest.abilityInfo.bundleName);
HILOG_DEBUG("UIExtensionAbility loaded status: %{public}s.", ret ? "true" : "false");
return ret;
}
bool AbilityConnectManager::CheckUIExtensionAbilitySessionExistLocked(
const std::shared_ptr<AbilityRecord> abilityRecord)
const std::shared_ptr<AbilityRecord> &abilityRecord)
{
CHECK_POINTER_AND_RETURN(abilityRecord, false);
@@ -2342,26 +2345,21 @@ bool AbilityConnectManager::CheckUIExtensionAbilitySessionExistLocked(
return false;
}
int32_t AbilityConnectManager::AddUIExtensionAbilityRecord(const std::shared_ptr<AAFwk::AbilityRecord> abilityRecord,
const std::string hostBundleName, const int32_t inputId) const
int32_t AbilityConnectManager::AddUIExtensionAbilityRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, const int32_t inputId) const
{
CHECK_POINTER_AND_RETURN(abilityRecord, INVALID_UI_EXTENSION_ABILITY_ID);
CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, INVALID_UI_EXTENSION_ABILITY_ID);
auto uiExtensionAbilityId = uiExtensionAbilityRecordMgr_->GenerateUIExtensionAbilityId(inputId);
HILOG_DEBUG("Generated id is %{public}d.", uiExtensionAbilityId);
abilityRecord->SetUIExtensionAbilityId(uiExtensionAbilityId);
auto uiExtensionAbilityRecord = std::make_shared<UIExtensionAbilityRecord>(abilityRecord,
hostBundleName, uiExtensionAbilityId);
uiExtensionAbilityRecordMgr_->AddUIExtensionAbilityRecord(uiExtensionAbilityId, uiExtensionAbilityRecord);
CHECK_POINTER_AND_RETURN(abilityRecord, INVALID_EXTENSION_RECORD_ID);
CHECK_POINTER_AND_RETURN(uiExtensionAbilityRecordMgr_, INVALID_EXTENSION_RECORD_ID);
int32_t uiExtensionAbilityId = inputId;
uiExtensionAbilityRecordMgr_->CreateExtensionRecord(abilityRecord, hostBundleName, uiExtensionAbilityId);
return uiExtensionAbilityId;
}
void AbilityConnectManager::RemoveUIExtensionAbilityRecord(const std::shared_ptr<AbilityRecord> abilityRecord)
void AbilityConnectManager::RemoveUIExtensionAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord)
{
CHECK_POINTER(abilityRecord);
CHECK_POINTER(uiExtensionAbilityRecordMgr_);
uiExtensionAbilityRecordMgr_->RemoveUIExtensionAbilityRecord(abilityRecord->GetUIExtensionAbilityId());
uiExtensionAbilityRecordMgr_->RemoveExtensionRecord(abilityRecord->GetUIExtensionAbilityId());
}
} // namespace AAFwk
} // namespace OHOS
@@ -75,7 +75,7 @@
#include "string_wrapper.h"
#include "system_ability_definition.h"
#include "system_ability_token_callback.h"
#include "ui_extension_ability_connect_manager.h"
#include "extension_record_manager.h"
#include "ui_extension_utils.h"
#include "uri_permission_manager_client.h"
#include "view_data.h"
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023 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.
*/
#include "extension_record.h"
#include "ability_util.h"
#include "errors.h"
namespace OHOS {
namespace AbilityRuntime {
ExtensionRecord::ExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, int32_t extensionRecordId)
: abilityRecord_(abilityRecord), hostBundleName_(hostBundleName), extensionRecordId_(extensionRecordId)
{}
ExtensionRecord::~ExtensionRecord() = default;
sptr<IRemoteObject> ExtensionRecord::GetCallToken() const
{
CHECK_POINTER_AND_RETURN(abilityRecord_, nullptr);
auto sessionInfo = abilityRecord_->GetSessionInfo();
CHECK_POINTER_AND_RETURN(sessionInfo, nullptr);
return sessionInfo->callerToken;
}
sptr<IRemoteObject> ExtensionRecord::GetRootCallerToken() const
{
return rootCallerToken_;
}
void ExtensionRecord::SetRootCallerToken(sptr<IRemoteObject> &rootCallerToken)
{
rootCallerToken_ = rootCallerToken;
}
bool ExtensionRecord::ContinueToGetCallerToken()
{
return false;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -0,0 +1,164 @@
/*
* Copyright (c) 2023 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.
*/
#include "extension_record_manager.h"
#include "ability_util.h"
#include "hilog_wrapper.h"
#include "ui_extension_utils.h"
#include "ui_extension_record.h"
namespace OHOS {
namespace AbilityRuntime {
std::atomic_int32_t ExtensionRecordManager::extensionRecordId_ = INVALID_EXTENSION_RECORD_ID;
ExtensionRecordManager::ExtensionRecordManager(const int32_t userId) : userId_(userId)
{
HILOG_DEBUG("constructor.");
}
ExtensionRecordManager::~ExtensionRecordManager()
{
HILOG_INFO("deconstructor.");
}
int32_t ExtensionRecordManager::GenerateExtensionRecordId(const int32_t extensionRecordId)
{
HILOG_DEBUG("Input id is %{public}d.", extensionRecordId);
std::lock_guard<std::mutex> lock(mutex_);
if (extensionRecordId != INVALID_EXTENSION_RECORD_ID &&
!extensionRecordIdSet_.count(extensionRecordId)) {
extensionRecordIdSet_.insert(extensionRecordId);
extensionRecordId_ = extensionRecordId;
return extensionRecordId_;
}
if (extensionRecordId == INVALID_EXTENSION_RECORD_ID) {
++extensionRecordId_;
}
while (extensionRecordIdSet_.count(extensionRecordId_)) {
extensionRecordId_++;
}
return extensionRecordId_;
}
void ExtensionRecordManager::AddExtensionRecord(const int32_t extensionRecordId,
const std::shared_ptr<ExtensionRecord> &record)
{
HILOG_DEBUG("extensionRecordId %{public}d.", extensionRecordId);
std::lock_guard<std::mutex> lock(mutex_);
extensionRecords_.emplace(extensionRecordId, record);
}
void ExtensionRecordManager::RemoveExtensionRecord(const int32_t extensionRecordId)
{
HILOG_DEBUG("extensionRecordId %{public}d.", extensionRecordId);
std::lock_guard<std::mutex> lock(mutex_);
extensionRecords_.erase(extensionRecordId);
}
bool ExtensionRecordManager::CheckExtensionLoaded(const int32_t extensionRecordId,
const std::string &hostBundleName)
{
HILOG_DEBUG("extensionRecordId %{public}d.", extensionRecordId);
std::lock_guard<std::mutex> lock(mutex_);
// find target record firstly
auto it = extensionRecords_.find(extensionRecordId);
if (it != extensionRecords_.end() && it->second != nullptr) {
// check bundleName
HILOG_DEBUG("Stored host bundleName: %{public}s, input bundleName is %{public}s.",
it->second->hostBundleName_.c_str(), hostBundleName.c_str());
if (it->second->hostBundleName_ == hostBundleName) {
return true;
}
}
HILOG_DEBUG("Not found stored id %{public}d.", extensionRecordId);
return false;
}
bool ExtensionRecordManager::IsBelongToManager(const AppExecFwk::AbilityInfo &abilityInfo)
{
// only support UIExtension now
return AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo.extensionAbilityType);
}
bool ExtensionRecordManager::IsFocused(int32_t extensionRecordId, const sptr<IRemoteObject>& focusToken)
{
std::lock_guard<std::mutex> lock(mutex_);
sptr<IRemoteObject> rootCallerToken = GetRootCallerTokenLocked(extensionRecordId);
bool isFocused = rootCallerToken == focusToken;
HILOG_DEBUG("id: %{public}d isFocused: %{public}d.", extensionRecordId, isFocused);
return isFocused;
}
sptr<IRemoteObject> ExtensionRecordManager::GetRootCallerTokenLocked(int32_t extensionRecordId)
{
auto it = extensionRecords_.find(extensionRecordId);
if (it != extensionRecords_.end() && it->second != nullptr) {
sptr<IRemoteObject> rootCallerToken = it->second->GetRootCallerToken();
if (rootCallerToken != nullptr) {
return rootCallerToken;
}
if (!it->second->ContinueToGetCallerToken()) {
return it->second->GetCallToken();
}
auto callerToken = it->second->GetCallToken();
if (callerToken == nullptr) {
HILOG_ERROR("callerToken is null, id: %{public}d.", extensionRecordId);
return nullptr;
}
auto callerAbilityRecord = AAFwk::Token::GetAbilityRecordByToken(callerToken);
if (callerAbilityRecord == nullptr) {
HILOG_ERROR("callerAbilityRecord is null, id: %{public}d.", extensionRecordId);
return nullptr;
}
if (callerAbilityRecord->GetUIExtensionAbilityId() == INVALID_EXTENSION_RECORD_ID) {
HILOG_DEBUG("update rootCallerToken, id: %{public}d.", extensionRecordId);
it->second->SetRootCallerToken(callerToken);
return callerToken;
}
rootCallerToken = GetRootCallerTokenLocked(callerAbilityRecord->GetUIExtensionAbilityId());
HILOG_DEBUG("update rootCallerToken, id: %{public}d.", extensionRecordId);
it->second->SetRootCallerToken(rootCallerToken);
return rootCallerToken;
}
HILOG_ERROR("Not found id %{public}d.", extensionRecordId);
return nullptr;
}
int32_t ExtensionRecordManager::CreateExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, int32_t &extensionRecordId)
{
// factory pattern with ability request
if (abilityRecord == nullptr) {
HILOG_ERROR("abilityRecord is null");
return ERR_NULL_OBJECT;
}
extensionRecordId = GenerateExtensionRecordId(extensionRecordId);
if (AAFwk::UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) {
std::shared_ptr<ExtensionRecord> extensionRecord = std::make_shared<UIExtensionRecord>(abilityRecord,
hostBundleName, extensionRecordId);
std::lock_guard<std::mutex> lock(mutex_);
HILOG_DEBUG("add UIExtension, id %{public}d.", extensionRecordId);
extensionRecords_[extensionRecordId] = extensionRecord;
abilityRecord->SetUIExtensionAbilityId(extensionRecordId);
return ERR_OK;
}
return ERR_INVALID_VALUE;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -1,90 +0,0 @@
/*
* Copyright (c) 2023 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.
*/
#include "ui_extension_ability_connect_manager.h"
#include "hilog_wrapper.h"
namespace OHOS {
namespace AbilityRuntime {
std::atomic_int32_t UIExtensionAbilityConnectManager::uiExtensionAbilityId_ = INVALID_UI_EXTENSION_ABILITY_ID;
UIExtensionAbilityConnectManager::UIExtensionAbilityConnectManager(const int userId) : userId_(userId)
{
HILOG_DEBUG("constructor.");
}
UIExtensionAbilityConnectManager::~UIExtensionAbilityConnectManager()
{
HILOG_INFO("deconstructor.");
}
int32_t UIExtensionAbilityConnectManager::GenerateUIExtensionAbilityId(const int32_t uiExtensionAbilityId)
{
HILOG_DEBUG("Input id is %{public}d.", uiExtensionAbilityId);
std::lock_guard<std::mutex> lock(mutex_);
if (uiExtensionAbilityId != INVALID_UI_EXTENSION_ABILITY_ID &&
!uiExtensionAbilityIdSet_.count(uiExtensionAbilityId)) {
uiExtensionAbilityIdSet_.insert(uiExtensionAbilityId);
uiExtensionAbilityId_ = uiExtensionAbilityId;
return uiExtensionAbilityId_;
}
if (uiExtensionAbilityId == INVALID_UI_EXTENSION_ABILITY_ID) {
++uiExtensionAbilityId_;
}
while (uiExtensionAbilityIdSet_.count(uiExtensionAbilityId_)) {
uiExtensionAbilityId_++;
}
return uiExtensionAbilityId_;
}
void UIExtensionAbilityConnectManager::AddUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId,
const std::shared_ptr<UIExtensionAbilityRecord> record)
{
HILOG_DEBUG("UIExtensionAbilityId %{public}d.", uiExtensionAbilityId);
std::lock_guard<std::mutex> lock(mutex_);
uiExtensionAbilityRecords_.emplace(uiExtensionAbilityId, record);
}
void UIExtensionAbilityConnectManager::RemoveUIExtensionAbilityRecord(const int32_t uiExtensionAbilityId)
{
HILOG_DEBUG("UIExtensionAbilityId %{public}d.", uiExtensionAbilityId);
std::lock_guard<std::mutex> lock(mutex_);
uiExtensionAbilityRecords_.erase(uiExtensionAbilityId);
}
bool UIExtensionAbilityConnectManager::CheckUIExtensionAbilityLoaded(const int32_t uiExtensionAbilityId,
const std::string hostBundleName)
{
HILOG_DEBUG("UIExtensionAbilityId %{public}d.", uiExtensionAbilityId);
std::lock_guard<std::mutex> lock(mutex_);
// find target record firstly
auto it = uiExtensionAbilityRecords_.find(uiExtensionAbilityId);
if (it != uiExtensionAbilityRecords_.end() && it->second != nullptr) {
// check bundlename
HILOG_DEBUG("Stored host bundleName: %{public}s, input bundleName is %{public}s.",
it->second->hostBundleName_.c_str(), hostBundleName.c_str());
if (it->second->hostBundleName_ == hostBundleName) {
return true;
}
}
HILOG_DEBUG("Not found stored id %{public}d.", uiExtensionAbilityId);
return false;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 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.
*/
#include "ui_extension_record.h"
#include "ability_util.h"
namespace OHOS {
namespace AbilityRuntime {
UIExtensionRecord::UIExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord,
const std::string &hostBundleName, int32_t extensionRecordId)
: ExtensionRecord(abilityRecord, hostBundleName, extensionRecordId)
{}
UIExtensionRecord::~UIExtensionRecord() = default;
bool UIExtensionRecord::ContinueToGetCallerToken()
{
return true;
}
} // namespace AbilityRuntime
} // namespace OHOS
+3
View File
@@ -139,6 +139,8 @@ ohos_source_set("abilityms_test_source") {
"${ability_runtime_services_path}/abilitymgr/src/data_ability_record.cpp",
"${ability_runtime_services_path}/abilitymgr/src/dialog_session_record.cpp",
"${ability_runtime_services_path}/abilitymgr/src/dlp_state_item.cpp",
"${ability_runtime_services_path}/abilitymgr/src/extension_record.cpp",
"${ability_runtime_services_path}/abilitymgr/src/extension_record_manager.cpp",
"${ability_runtime_services_path}/abilitymgr/src/free_install_manager.cpp",
"${ability_runtime_services_path}/abilitymgr/src/insight_intent_execute_manager.cpp",
"${ability_runtime_services_path}/abilitymgr/src/insight_intent_profile.cpp",
@@ -154,6 +156,7 @@ ohos_source_set("abilityms_test_source") {
"${ability_runtime_services_path}/abilitymgr/src/start_ability_handler.cpp",
"${ability_runtime_services_path}/abilitymgr/src/start_ability_handler/start_ability_sandbox_savefile.cpp",
"${ability_runtime_services_path}/abilitymgr/src/task_data_persistence_mgr.cpp",
"${ability_runtime_services_path}/abilitymgr/src/ui_extension_record.cpp",
"${ability_runtime_services_path}/abilitymgr/src/user_controller.cpp",
"${ability_runtime_services_path}/abilitymgr/src/user_event_handler.cpp",
"${ability_runtime_services_path}/abilitymgr/src/want_receiver_proxy.cpp",
@@ -3082,14 +3082,71 @@ HWTEST_F(AbilityConnectManagerTest, IsUIExtensionFocused_001, TestSize.Level1)
bool isFocused = connectManager->IsUIExtensionFocused(
serviceRecord_->GetApplicationInfo().accessTokenId, serviceRecord1_->GetToken());
EXPECT_EQ(isFocused, false);
connectManager.reset();
}
sptr<SessionInfo> sessionInfo = new (std::nothrow) SessionInfo();
sessionInfo->callerToken = serviceRecord1_->GetToken();
/*
* Feature: AbilityConnectManager
* Function: IsUIExtensionFocused
* SubFunction: IsUIExtensionFocused
* FunctionPoints: NA
* EnvConditions: NA
* CaseDescription: Verify AbilityConnectManager IsUIExtensionFocused
*/
HWTEST_F(AbilityConnectManagerTest, IsUIExtensionFocused_002, TestSize.Level1)
{
std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(3);
ASSERT_NE(connectManager, nullptr);
connectManager->uiExtensionMap_.clear();
std::string device = "device";
std::string abilityName = "uiExtensionUserAbility";
std::string appName = "uiExtensionUser";
std::string bundleName = "com.ix.uiExtensionUser";
std::string moduleName = "entry";
auto request = GenerateAbilityRequest(device, abilityName, appName, bundleName, moduleName);
auto uiExtensionUser = AbilityRecord::CreateAbilityRecord(request);
EXPECT_NE(uiExtensionUser, nullptr);
std::string abilityName1 = "uiExtensionAbility1";
std::string appName1 = "uiExtensionProvider1";
std::string bundleName1 = "com.ix.uiExtensionProvider1";
std::string moduleName1 = "entry";
auto request1 = GenerateAbilityRequest(device, abilityName1, appName1, bundleName1, moduleName1);
auto uiExtension1 = AbilityRecord::CreateAbilityRecord(request1);
EXPECT_NE(uiExtension1, nullptr);
uiExtension1->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI;
sptr<SessionInfo> sessionInfo1 = new (std::nothrow) SessionInfo();
sessionInfo1->callerToken = uiExtensionUser->GetToken();
uiExtension1->sessionInfo_ = sessionInfo1;
connectManager->uiExtensionMap_.emplace(
callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(serviceRecord_, sessionInfo));
isFocused = connectManager->IsUIExtensionFocused(
serviceRecord_->GetApplicationInfo().accessTokenId, serviceRecord1_->GetToken());
EXPECT_EQ(isFocused, true);
callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(uiExtension1, sessionInfo1));
int32_t extensionId1 = 1;
int32_t ret = connectManager->uiExtensionAbilityRecordMgr_->CreateExtensionRecord(uiExtension1, "", extensionId1);
EXPECT_EQ(ret, ERR_OK);
bool isFocused1 = connectManager->IsUIExtensionFocused(
uiExtension1->GetApplicationInfo().accessTokenId, uiExtensionUser->GetToken());
EXPECT_EQ(isFocused1, true);
std::string abilityName2 = "uiExtensionAbility2";
std::string appName2 = "uiExtensionProvider2";
std::string bundleName2 = "com.ix.uiExtensionProvider2";
std::string moduleName2 = "entry";
auto request2 = GenerateAbilityRequest(device, abilityName2, appName2, bundleName2, moduleName2);
auto uiExtension2 = AbilityRecord::CreateAbilityRecord(request2);
EXPECT_NE(uiExtension2, nullptr);
uiExtension2->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI;
sptr<SessionInfo> sessionInfo2 = new (std::nothrow) SessionInfo();
sessionInfo2->callerToken = uiExtension1->GetToken();
uiExtension2->sessionInfo_ = sessionInfo2;
connectManager->uiExtensionMap_.emplace(
callbackA_->AsObject(), AbilityConnectManager::UIExtWindowMapValType(uiExtension2, sessionInfo2));
int32_t extensionId2 = 2;
ret = connectManager->uiExtensionAbilityRecordMgr_->CreateExtensionRecord(uiExtension2, "", extensionId2);
EXPECT_EQ(ret, ERR_OK);
bool isFocused2 = connectManager->IsUIExtensionFocused(
uiExtension2->GetApplicationInfo().accessTokenId, uiExtensionUser->GetToken());
EXPECT_EQ(isFocused2, true);
connectManager.reset();
}