diff --git a/frameworks/native/ability/native/BUILD.gn b/frameworks/native/ability/native/BUILD.gn index 29d0c9bd676209e1b2f750dbad874f5ffb5d6774..8e509f69b767e50973e453567fa7a726a625d599 100644 --- a/frameworks/native/ability/native/BUILD.gn +++ b/frameworks/native/ability/native/BUILD.gn @@ -549,6 +549,7 @@ ohos_shared_library("extensionkit_native") { "${ability_runtime_native_path}/ability/native/extension_impl.cpp", "${ability_runtime_native_path}/ability/native/extension_module_loader.cpp", "${ability_runtime_native_path}/ability/native/js_extension_common.cpp", + "${ability_runtime_native_path}/ability/native/sts_extension_common.cpp", "${ability_runtime_native_path}/appkit/ability_runtime/extension_context.cpp", "${ability_runtime_native_path}/appkit/ability_runtime/ui_holder_extension_context.cpp", "${ability_runtime_services_path}/abilitymgr/src/last_exit_detail_info.cpp", @@ -579,6 +580,7 @@ ohos_shared_library("extensionkit_native") { "ipc:ipc_napi", "json:nlohmann_json_static", "napi:ace_napi", + "runtime_core:ani", ] public_deps = [ ":abilitykit_utils" ] @@ -1582,6 +1584,7 @@ config("ui_extension_public_config") { "${ability_runtime_path}/interfaces/kits/native/ability/native/insight_intent_executor", "${ability_runtime_path}/interfaces/kits/native/ability/native/ui_extension_ability", "${ability_runtime_path}/interfaces/kits/native/ability/native/ui_extension_base", + "${ability_runtime_path}/frameworks/ets/ani/ui_extension_ability/include", ] } @@ -1605,6 +1608,7 @@ ohos_shared_library("ui_extension") { "${ability_runtime_path}/frameworks/ets/ani/ui_extension_ability/src/sts_ui_extension.cpp", "${ability_runtime_path}/frameworks/ets/ani/ui_extension_ability/src/sts_ui_extension_content_session.cpp", "${ability_runtime_path}/frameworks/ets/ani/ui_extension_ability/src/sts_ui_extension_context.cpp", + "${ability_runtime_native_path}/ability/native/ui_extension_base/sts_ui_extension_base.cpp", ] public_configs = [ ":ui_extension_public_config" ] diff --git a/frameworks/native/ability/native/sts_extension_common.cpp b/frameworks/native/ability/native/sts_extension_common.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d483c6fac7c9a6c9f21540b97ca173ff0d502277 --- /dev/null +++ b/frameworks/native/ability/native/sts_extension_common.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022-2024 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 "sts_extension_common.h" + +#include "hilog_tag_wrapper.h" +#include "js_extension_context.h" +#include "sts_runtime.h" +#include "js_runtime_utils.h" + + +namespace OHOS { +namespace AbilityRuntime { + +using namespace OHOS::AppExecFwk; + +std::shared_ptr StsExtensionCommon::Create(STSRuntime &stsRuntime, STSNativeReference &stsObj, + const std::shared_ptr &shellContextRef) +{ + return std::make_shared(stsRuntime, stsObj, shellContextRef); +} + +StsExtensionCommon::StsExtensionCommon(STSRuntime &stsRuntime, STSNativeReference &stsObj, + const std::shared_ptr &shellContextRef) + : shellContextRef_(shellContextRef) {} + +StsExtensionCommon::~StsExtensionCommon() +{ +} + +void StsExtensionCommon::OnConfigurationUpdated(const std::shared_ptr &fullConfig) +{ + TAG_LOGI(AAFwkTag::EXT, "StsExtensionCommon::OnConfigurationUpdated Called"); +} + +void StsExtensionCommon::OnMemoryLevel(int level) +{ + TAG_LOGD(AAFwkTag::EXT, "StsExtensionCommon::OnMemoryLevel Called"); +} + +} +} diff --git a/frameworks/native/ability/native/ui_extension_base/sts_ui_extension_base.cpp b/frameworks/native/ability/native/ui_extension_base/sts_ui_extension_base.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df3d791eb6f8bde8ae1548286e037ff88b7fa933 --- /dev/null +++ b/frameworks/native/ability/native/ui_extension_base/sts_ui_extension_base.cpp @@ -0,0 +1,490 @@ +/* + * Copyright (c) 2023-2024 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 "sts_ui_extension_base.h" + +#include +#include + +#include "ability_info.h" +#include "ability_manager_client.h" +#include "array_wrapper.h" +#include "configuration_utils.h" +#include "connection_manager.h" +#include "context.h" +#include "hilog_tag_wrapper.h" +#include "hilog_wrapper.h" +#include "hitrace_meter.h" +#include "int_wrapper.h" +#include "js_data_struct_converter.h" +#include "sts_extension_common.h" +#include "js_extension_context.h" +#include "sts_runtime.h" +#include "js_runtime_utils.h" +#include "js_ui_extension_content_session.h" + +#include "string_wrapper.h" +#include "ui_extension_window_command.h" +#include "want_params_wrapper.h" +#include "application_configuration_manager.h" +#include "ohos_application.h" + +namespace OHOS { +namespace AbilityRuntime { + +STSUIExtensionBase::STSUIExtensionBase(const std::unique_ptr &runtime) + : stsRuntime_(static_cast(*runtime)) +{ + abilityResultListeners_ = std::make_shared(); +} + +STSUIExtensionBase::~STSUIExtensionBase() +{ + TAG_LOGD(AAFwkTag::UI_EXT, "destructor"); + contentSessions_.clear(); +} + +std::shared_ptr STSUIExtensionBase::Init(const std::shared_ptr &record, + const std::shared_ptr &application, std::shared_ptr &handler, + const sptr &token) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + TAG_LOGD(AAFwkTag::UI_EXT, "called"); + if (abilityInfo_ == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null abilityInfo"); + return nullptr; + } + if (abilityInfo_->srcEntrance.empty()) { + TAG_LOGE(AAFwkTag::UI_EXT, "empty abilityInfo srcEntrance"); + return nullptr; + } + + RegisterAbilityConfigUpdateCallback(); + + if (record != nullptr) { + token_ = record->GetToken(); + } + std::string srcPath(abilityInfo_->moduleName + "/"); + srcPath.append(abilityInfo_->srcEntrance); + srcPath.erase(srcPath.rfind('.')); + srcPath.append(".abc"); + + std::string moduleName(abilityInfo_->moduleName); + moduleName.append("::").append(abilityInfo_->name); + + stsObj_ = stsRuntime_.LoadModule( + moduleName, srcPath, abilityInfo_->hapPath, abilityInfo_->compileMode == CompileMode::ES_MODULE, false, + abilityInfo_->srcEntrance); + if (stsObj_ == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null stsObj_"); + return nullptr; + } + + BindContext(); + handler_ = handler; + RegisterDisplayInfoChangedListener(); + return StsExtensionCommon::Create(stsRuntime_, static_cast(*stsObj_), shellContextRef_); +} + +void STSUIExtensionBase::RegisterAbilityConfigUpdateCallback() +{ + auto uiExtensionAbility = std::static_pointer_cast(shared_from_this()); + std::weak_ptr abilityWptr = uiExtensionAbility; + context_->RegisterAbilityConfigUpdateCallback( + [abilityWptr, abilityContext = context_](AppExecFwk::Configuration &config) { + std::shared_ptr abilitySptr = abilityWptr.lock(); + if (abilitySptr == nullptr) { + TAG_LOGE(AAFwkTag::UIABILITY, "null abilitySptr"); + return; + } + if (abilityContext == nullptr || abilityContext->GetAbilityInfo() == nullptr) { + TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext or null GetAbilityInfo"); + return; + } + if (abilityContext->GetAbilityConfiguration() == nullptr) { + auto abilityModuleContext = abilityContext->CreateModuleContext( + abilityContext->GetAbilityInfo()->moduleName); + if (abilityModuleContext == nullptr) { + TAG_LOGE(AAFwkTag::UIABILITY, "null abilityModuleContext"); + return; + } + auto abilityResourceMgr = abilityModuleContext->GetResourceManager(); + abilityContext->SetAbilityResourceManager(abilityResourceMgr); + AbilityRuntime::ApplicationConfigurationManager::GetInstance(). + AddIgnoreContext(abilityContext, abilityResourceMgr); + TAG_LOGD(AAFwkTag::UIABILITY, "%{public}zu", + AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetIgnoreContext().size()); + } + abilityContext->SetAbilityConfiguration(config); + if (config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE). + compare(AppExecFwk::ConfigurationInner::COLOR_MODE_AUTO) == 0) { + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, + ApplicationConfigurationManager::GetInstance().GetColorMode()); + + if (AbilityRuntime::ApplicationConfigurationManager::GetInstance(). + GetColorModeSetLevel() > AbilityRuntime::SetLevel::System) { + config.AddItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_APP, + AppExecFwk::ConfigurationInner::IS_SET_BY_APP); + } + abilityContext->GetAbilityConfiguration()-> + RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE); + abilityContext->GetAbilityConfiguration()-> + RemoveItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_APP); + } + + abilitySptr->OnAbilityConfigurationUpdated(config); + }); +} + +void STSUIExtensionBase::BindContext() +{ +} + +void STSUIExtensionBase::OnStart( + const AAFwk::Want &want, AAFwk::LaunchParam &launchParam, sptr sessionInfo) +{} + +void STSUIExtensionBase::OnStop() +{ +} + +void STSUIExtensionBase::OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback) +{ +} + +void STSUIExtensionBase::OnStopCallBack() +{ +} + +void STSUIExtensionBase::OnCommandWindow( + const AAFwk::Want &want, const sptr &sessionInfo, AAFwk::WindowCommand winCmd) +{} + +void STSUIExtensionBase::ForegroundWindowInitInsightIntentExecutorInfo(const AAFwk::Want &want, + const sptr &sessionInfo, InsightIntentExecutorInfo &executorInfo) +{} + +bool STSUIExtensionBase::ForegroundWindowWithInsightIntent(const AAFwk::Want &want, + const sptr &sessionInfo, bool needForeground) +{ + return true; +} + +void STSUIExtensionBase::ExecuteInsightIntentDone(uint64_t intentId, const InsightIntentExecuteResult &result) +{ +} + +void STSUIExtensionBase::PostInsightIntentExecuted(const sptr &sessionInfo, + const AppExecFwk::InsightIntentExecuteResult &result, bool needForeground) +{ +} + +void STSUIExtensionBase::OnCommandWindowDone(const sptr &sessionInfo, AAFwk::WindowCommand winCmd) +{ +} + +void STSUIExtensionBase::OnInsightIntentExecuteDone(const sptr &sessionInfo, + const AppExecFwk::InsightIntentExecuteResult &result) +{ +} + +void STSUIExtensionBase::OnCommand(const AAFwk::Want &want, bool restart, int32_t startId) +{ + TAG_LOGD(AAFwkTag::UI_EXT, "called"); +} + +void STSUIExtensionBase::OnForeground(const Want &want, sptr sessionInfo) +{ +} + +void STSUIExtensionBase::OnBackground() +{ +} + +bool STSUIExtensionBase::CallStsOnSessionCreate(const AAFwk::Want &want, const sptr &sessionInfo, + const sptr &uiWindow, const uint64_t &uiExtensionComponentId) +{ + return true; +} + +sptr STSUIExtensionBase::CreateWindowOption(const sptr &sessionInfo) +{ + auto option = sptr::MakeSptr(); + if (option == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "make option failed"); + return nullptr; + } + + option->SetWindowName(context_->GetBundleName() + context_->GetAbilityInfo()->name); + option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_UI_EXTENSION); + option->SetWindowSessionType(Rosen::WindowSessionType::EXTENSION_SESSION); + option->SetParentId(sessionInfo->hostWindowId); + option->SetRealParentId(sessionInfo->realHostWindowId); + option->SetParentWindowType(static_cast(sessionInfo->parentWindowType)); + option->SetUIExtensionUsage(static_cast(sessionInfo->uiExtensionUsage)); + option->SetDensity(sessionInfo->density); + option->SetIsDensityFollowHost(sessionInfo->isDensityFollowHost); + if (context_->isNotAllow != -1) { + bool isNotAllow = context_->isNotAllow == 1 ? true : false; + TAG_LOGD(AAFwkTag::UI_EXT, "isNotAllow: %{public}d", isNotAllow); + option->SetConstrainedModal(isNotAllow); + } + return option; +} + +bool STSUIExtensionBase::HandleSessionCreate(const AAFwk::Want &want, const sptr &sessionInfo) +{ + if (sessionInfo == nullptr || sessionInfo->uiExtensionComponentId == 0) { + TAG_LOGE(AAFwkTag::UI_EXT, "Invalid sessionInfo"); + return false; + } + if (sessionInfo == nullptr || sessionInfo->uiExtensionComponentId == 0) { + TAG_LOGE(AAFwkTag::UI_EXT, "Invalid sessionInfo"); + return false; + } + std::shared_ptr sharedWant = std::make_shared(want); + auto componentId = sessionInfo->uiExtensionComponentId; + if (uiWindowMap_.find(componentId) == uiWindowMap_.end()) { + if (context_ == nullptr || context_->GetAbilityInfo() == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null context"); + return false; + } + auto option = CreateWindowOption(sessionInfo); + if (option == nullptr) { + return false; + } + sptr uiWindow; + { + HITRACE_METER_NAME(HITRACE_TAG_APP, "Rosen::Window::Create"); + option->SetDisplayId(sessionInfo->displayId); + uiWindow = Rosen::Window::Create(option, context_, sessionInfo->sessionToken); + } + if (uiWindow == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null uiWindow"); + return false; + } + uiWindow->UpdateExtensionConfig(sharedWant); + if (!CallStsOnSessionCreate(*sharedWant, sessionInfo, uiWindow, componentId)) { + return false; + } + uiWindowMap_[componentId] = uiWindow; +#ifdef SUPPORT_GRAPHICS + if (context_->GetWindow() == nullptr) { + context_->SetWindow(uiWindow); + } +#endif // SUPPORT_GRAPHICS + } else { + auto uiWindow = uiWindowMap_[componentId]; + if (uiWindow == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null uiWindow"); + return false; + } + uiWindow->UpdateExtensionConfig(sharedWant); + } + return true; +} + +void STSUIExtensionBase::ForegroundWindow(const AAFwk::Want &want, const sptr &sessionInfo) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + if (!HandleSessionCreate(want, sessionInfo)) { + TAG_LOGE(AAFwkTag::UI_EXT, "HandleSessionCreate failed"); + return; + } + TAG_LOGD(AAFwkTag::UI_EXT, "UIExtension component id: %{public}" PRId64, sessionInfo->uiExtensionComponentId); + auto componentId = sessionInfo->uiExtensionComponentId; + auto &uiWindow = uiWindowMap_[componentId]; + if (uiWindow) { + HITRACE_METER_NAME(HITRACE_TAG_APP, "Rosen::Window::show"); + uiWindow->Show(); + foregroundWindows_.emplace(componentId); + } +} + +void STSUIExtensionBase::BackgroundWindow(const sptr &sessionInfo) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + if (sessionInfo == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "Invalid sessionInfo"); + return; + } + + TAG_LOGD(AAFwkTag::UI_EXT, "UIExtension component id: %{public}" PRId64, sessionInfo->uiExtensionComponentId); + auto componentId = sessionInfo->uiExtensionComponentId; + if (uiWindowMap_.find(componentId) == uiWindowMap_.end()) { + TAG_LOGE(AAFwkTag::UI_EXT, "not find uiWindow"); + return; + } + auto &uiWindow = uiWindowMap_[componentId]; + if (uiWindow) { + uiWindow->Hide(); + foregroundWindows_.erase(componentId); + } +} + +void STSUIExtensionBase::DestroyWindow(const sptr &sessionInfo) +{ +} + +void STSUIExtensionBase::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + TAG_LOGD(AAFwkTag::UI_EXT, "called"); + if (context_ == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null context"); + return; + } + + auto abilityConfig = context_->GetAbilityConfiguration(); + auto configUtils = std::make_shared(); + + if (abilityConfig != nullptr) { + auto newConfig = configUtils->UpdateGlobalConfig(configuration, context_->GetConfiguration(), + abilityConfig, context_->GetResourceManager()); + if (newConfig.GetItemSize() == 0) { + return; + } + if (context_->GetWindow()) { + TAG_LOGI(AAFwkTag::UIABILITY, "newConfig: %{public}s", newConfig.GetName().c_str()); + auto diffConfiguration = std::make_shared(newConfig); + context_->GetWindow()->UpdateConfigurationForSpecified(diffConfiguration, context_->GetResourceManager()); + } + } else { + auto configUtils = std::make_shared(); + configUtils->UpdateGlobalConfig(configuration, context_->GetConfiguration(), context_->GetResourceManager()); + } + + ConfigurationUpdated(); +} + +void STSUIExtensionBase::OnAbilityConfigurationUpdated(const AppExecFwk::Configuration& configuration) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); + TAG_LOGD(AAFwkTag::UI_EXT, "called"); + + if (context_ == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null context"); + return; + } + + auto configUtils = std::make_shared(); + configUtils->UpdateAbilityConfig(configuration, context_->GetResourceManager()); + + if (context_->GetWindow()) { + TAG_LOGI(AAFwkTag::UIABILITY, "newConfig: %{public}s", configuration.GetName().c_str()); + auto diffConfiguration = std::make_shared(configuration); + context_->GetWindow()->UpdateConfigurationForSpecified(diffConfiguration, context_->GetResourceManager()); + } + + ConfigurationUpdated(); +} + +void STSUIExtensionBase::Dump(const std::vector ¶ms, std::vector &info) +{ +} + +void STSUIExtensionBase::OnAbilityResult(int32_t requestCode, int32_t resultCode, const Want &resultData) +{ + TAG_LOGD(AAFwkTag::UI_EXT, "called"); + if (context_ == nullptr) { + TAG_LOGW(AAFwkTag::UI_EXT, "null context"); + return; + } + context_->OnAbilityResult(requestCode, resultCode, resultData); + if (abilityResultListeners_ == nullptr) { + TAG_LOGW(AAFwkTag::UI_EXT, "null abilityResultListeners"); + return; + } + abilityResultListeners_->OnAbilityResult(requestCode, resultCode, resultData); +} + +void STSUIExtensionBase::SetAbilityInfo(const std::shared_ptr &abilityInfo) +{ + abilityInfo_ = abilityInfo; +} + +void STSUIExtensionBase::SetContext(const std::shared_ptr &context) +{ + context_ = context; +} + +void STSUIExtensionBase::ConfigurationUpdated() +{ +} + +#ifdef SUPPORT_GRAPHICS +void STSUIExtensionBase::OnDisplayInfoChange( + const sptr &token, Rosen::DisplayId displayId, float density, Rosen::DisplayOrientation orientation) +{ + TAG_LOGI(AAFwkTag::UI_EXT, "displayId: %{public}" PRIu64 "", displayId); + if (context_ == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null context"); + return; + } + + auto contextConfig = context_->GetConfiguration(); + if (contextConfig == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null configuration"); + return; + } + + TAG_LOGI(AAFwkTag::UI_EXT, "Config dump: %{public}s", contextConfig->GetName().c_str()); + auto configUtils = std::make_shared(); + auto result = configUtils->UpdateDisplayConfig( + contextConfig, context_->GetResourceManager(), displayId, density, orientation); + TAG_LOGI(AAFwkTag::UI_EXT, "Config dump after update: %{public}s", contextConfig->GetName().c_str()); + if (result) { + auto jsUiExtension = std::static_pointer_cast(shared_from_this()); + auto task = [jsUiExtension]() { + if (jsUiExtension) { + jsUiExtension->ConfigurationUpdated(); + } + }; + if (handler_ != nullptr) { + handler_->PostTask(task, "STSUIExtensionBase:OnChange"); + } + } +} + +void STSUIExtensionBase::RegisterDisplayInfoChangedListener() +{ + // register displayid change callback + auto jsUiExtensionBase = std::static_pointer_cast(shared_from_this()); + stsUiExtensionBaseDisplayListener_ = sptr::MakeSptr(jsUiExtensionBase); + if (stsUiExtensionBaseDisplayListener_ == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null STSUIExtensionBaseDisplayListener"); + return; + } + if (context_ == nullptr || context_->GetToken() == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null context"); + return; + } + TAG_LOGI(AAFwkTag::UI_EXT, "RegisterDisplayInfoChangedListener"); + Rosen::WindowManager::GetInstance().RegisterDisplayInfoChangedListener( + context_->GetToken(), stsUiExtensionBaseDisplayListener_); +} + +void STSUIExtensionBase::UnregisterDisplayInfoChangedListener() +{ + if (context_ == nullptr || context_->GetToken() == nullptr) { + TAG_LOGE(AAFwkTag::UI_EXT, "null context"); + return; + } + Rosen::WindowManager::GetInstance().UnregisterDisplayInfoChangedListener( + context_->GetToken(), stsUiExtensionBaseDisplayListener_); +} +#endif // SUPPORT_GRAPHICS +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/interfaces/kits/native/ability/native/sts_extension_common.h b/interfaces/kits/native/ability/native/sts_extension_common.h new file mode 100644 index 0000000000000000000000000000000000000000..5652f923f5cdcc44cd2ab8503981aad611aca2a7 --- /dev/null +++ b/interfaces/kits/native/ability/native/sts_extension_common.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 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_STS_EXTENSION_COMMON_H +#define OHOS_ABILITY_RUNTIME_STS_EXTENSION_COMMON_H + +#include "sts_runtime.h" +#include "configuration.h" +#include "service_extension.h" + +class NativeReference; + +namespace OHOS { +namespace AbilityRuntime { +class ServiceExtension; +class STSRuntime; +/** + * @brief Basic sts extension common components. + */ +class StsExtensionCommon : public ExtensionCommon, + public std::enable_shared_from_this { +public: + StsExtensionCommon(STSRuntime &stsRuntime, STSNativeReference &stsObj, + const std::shared_ptr &shellContextRef); + + virtual ~StsExtensionCommon() override; + + /** + * @brief Create StsServiceExtension. + * + * @param stsRuntime The runtime. + * @param stsObj The sts object instance. + * @return The StsServiceExtension instance. + */ + static std::shared_ptr Create(STSRuntime &jsRuntime, STSNativeReference &stsObj, + const std::shared_ptr &shellContextRef); + + /** + * @brief Called when the system configuration is updated. + * + * @param configuration Indicates the updated configuration information. + */ + void OnConfigurationUpdated(const std::shared_ptr &fullConfig) override; + + /** + * @brief Notify current memory level. + * + * @param level Current memory level. + */ + virtual void OnMemoryLevel(int level) override; + +private: + std::shared_ptr shellContextRef_ = nullptr; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_STS_EXTENSION_COMMON_H diff --git a/interfaces/kits/native/ability/native/ui_extension_base/sts_ui_extension_base.h b/interfaces/kits/native/ability/native/ui_extension_base/sts_ui_extension_base.h new file mode 100644 index 0000000000000000000000000000000000000000..883a398c810ca92ae1d09f959131302349358e8a --- /dev/null +++ b/interfaces/kits/native/ability/native/ui_extension_base/sts_ui_extension_base.h @@ -0,0 +1,243 @@ +/* + * Copyright (c) 2023-2024 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_STS_UI_EXTENSION_BASE_H +#define OHOS_ABILITY_RUNTIME_STS_UI_EXTENSION_BASE_H + +#include "ability_handler.h" +#include "ability_local_record.h" +#include "configuration.h" +#include "js_extension_common.h" +#include "sts_runtime.h" +#include "js_ui_extension_content_session.h" +#include "native_engine/native_engine.h" +#include "ohos_application.h" +#include "session_info.h" +#include "ui_extension_base.h" +#include "ui_extension_context.h" +#include "ui_extension_window_command.h" +#include "want.h" +#include "window.h" +#ifdef SUPPORT_GRAPHICS +#include "display_manager.h" +#include "window_manager.h" +#endif // SUPPORT_GRAPHICS + +class NativeReference; + +namespace OHOS { +namespace AbilityRuntime { +using Want = OHOS::AAFwk::Want; +class STSRuntime; +/** + * @brief Sts ui extension base. + */ +class STSUIExtensionBase : public UIExtensionBaseImpl, + public std::enable_shared_from_this { +public: + explicit STSUIExtensionBase(const std::unique_ptr &runtime); + virtual ~STSUIExtensionBase(); + + /** + * @brief Init the ui extension. + * + * @param record the ui extension record. + * @param application the application info. + * @param handler the ui extension handler. + * @param token the remote token. + * @return sts extension common object. + */ + std::shared_ptr Init(const std::shared_ptr &record, + const std::shared_ptr &application, + std::shared_ptr &handler, const sptr &token) override; + + /** + * @brief Called when this ui extension is started. You must override this function if you want to perform some + * initialization operations during ui extension startup. + * + * This function can be called only once in the entire lifecycle of an ui extension. + * + * @param Want Indicates the {@link Want} structure containing startup information about the ui extension. + * @param launchParam The launch param. + * @param sessionInfo The session info of the ability. + */ + void OnStart( + const AAFwk::Want &want, AAFwk::LaunchParam &launchParam, sptr sessionInfo) override; + + /** + * @brief Called back when ui extension is started. + * + * This method can be called only by ui extension. You can use the StartAbility(Want) method to start + * ui extension. Then the system calls back the current method to use the transferred want parameter to + * execute its own logic. + * + * @param want Indicates the want of ui extension to start. + * @param restart Indicates the startup mode. The value true indicates that ui extension is restarted after + * being destroyed, and the value false indicates a normal startup. + * @param startId Indicates the number of times the ui extension has been started. The startId is incremented + * by 1 every time the ui extension is started. For example, if the ui extension has been started for six + * times, the value of startId is 6. + */ + void OnCommand(const AAFwk::Want &want, bool restart, int32_t startId) override; + + void OnCommandWindow( + const AAFwk::Want &want, const sptr &sessionInfo, AAFwk::WindowCommand winCmd) override; + + /** + * @brief Called when this ui extension enters the STATE_STOP state. + * + * The ui extension in the STATE_STOP is being destroyed. + * You can override this function to implement your own processing logic. + */ + void OnStop() override; + void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback) override; + /** + * @brief The callback of OnStop. + */ + void OnStopCallBack() override; + + /** + * @brief Called when the system configuration is updated. + * + * @param configuration Indicates the updated configuration information. + */ + void OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) override; + + /** + * @brief Called when this extension enters the STATE_FOREGROUND state. + * + * + * The extension in the STATE_FOREGROUND state is visible. + * You can override this function to implement your own processing logic. + */ + void OnForeground(const Want &want, sptr sessionInfo) override; + + /** + * @brief Called when this extension enters the STATE_BACKGROUND state. + * + * + * The extension in the STATE_BACKGROUND state is invisible. + * You can override this function to implement your own processing logic. + */ + void OnBackground() override; + + /** + * @brief Called when ui extension need dump info. + * + * @param params The params from ui extension. + * @param info The dump info to show. + */ + void Dump(const std::vector ¶ms, std::vector &info) override; + + /** + * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int32_t) is called to start an extension ability + * and the result is returned. + * @param requestCode Indicates the request code returned after the ability is started. You can define the request + * code to identify the results returned by abilities. The value ranges from 0 to 65535. + * @param resultCode Indicates the result code returned after the ability is started. You can define the result + * code to identify an error. + * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The + * value can be null. + */ + void OnAbilityResult(int32_t requestCode, int32_t resultCode, const Want &resultData) override; + + /** + * @brief Set ability info. + */ + void SetAbilityInfo(const std::shared_ptr &abilityInfo) override; + + /** + * @brief Set ui extension context. + */ + void SetContext(const std::shared_ptr &context) override; + + void BindContext() override; + + /** + * @brief Called when configuration changed, including system configuration and window configuration. + */ + void ConfigurationUpdated(); + + void OnAbilityConfigurationUpdated(const AppExecFwk::Configuration& configuration); + + void RegisterAbilityConfigUpdateCallback(); + +protected: + void ForegroundWindow(const AAFwk::Want &want, const sptr &sessionInfo); + void BackgroundWindow(const sptr &sessionInfo); + void DestroyWindow(const sptr &sessionInfo); + bool CallStsOnSessionCreate(const AAFwk::Want &want, const sptr &sessionInfo, + const sptr &uiWindow, const uint64_t &uiExtensionComponentId); + void OnCommandWindowDone(const sptr &sessionInfo, AAFwk::WindowCommand winCmd); + void ForegroundWindowInitInsightIntentExecutorInfo(const AAFwk::Want &want, + const sptr &sessionInfo, InsightIntentExecutorInfo &executorInfo); + bool ForegroundWindowWithInsightIntent(const AAFwk::Want &want, const sptr &sessionInfo, + bool needForeground); + bool HandleSessionCreate(const AAFwk::Want &want, const sptr &sessionInfo); + void OnInsightIntentExecuteDone(const sptr &sessionInfo, + const AppExecFwk::InsightIntentExecuteResult &result); + void PostInsightIntentExecuted(const sptr &sessionInfo, + const AppExecFwk::InsightIntentExecuteResult &result, bool needForeground); + void ExecuteInsightIntentDone(uint64_t intentId, const InsightIntentExecuteResult &result); + +protected: + STSRuntime &stsRuntime_; + std::shared_ptr shellContextRef_; + std::unique_ptr stsObj_; + std::shared_ptr context_; + std::map> uiWindowMap_; + std::set foregroundWindows_; + std::map> contentSessions_; + std::shared_ptr abilityResultListeners_ = nullptr; + std::shared_ptr abilityInfo_; + sptr token_ = nullptr; + std::shared_ptr handler_ = nullptr; + +private: + sptr CreateWindowOption(const sptr &sessionInfo); + +#ifdef SUPPORT_GRAPHICS +private: + class STSUIExtensionBaseDisplayListener : public OHOS::Rosen::IDisplayInfoChangedListener { + public: + explicit STSUIExtensionBaseDisplayListener(const std::weak_ptr &stsUiExtensionBase) + { + stsUiExtensionBase_ = stsUiExtensionBase; + } + + void OnDisplayInfoChange(const sptr &token, Rosen::DisplayId displayId, float density, + Rosen::DisplayOrientation orientation) override + { + auto sptr = stsUiExtensionBase_.lock(); + if (sptr != nullptr) { + sptr->OnDisplayInfoChange(token, displayId, density, orientation); + } + } + + private: + std::weak_ptr stsUiExtensionBase_; + }; + + void RegisterDisplayInfoChangedListener(); + void UnregisterDisplayInfoChangedListener(); + void OnDisplayInfoChange(const sptr &token, Rosen::DisplayId displayId, float density, + Rosen::DisplayOrientation orientation); + + sptr stsUiExtensionBaseDisplayListener_ = nullptr; +#endif +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_STS_UI_EXTENSION_BASE_H