1/* 2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "application_context.h" 17#include "context.h" 18#include "hilog_tag_wrapper.h" 19#include "hilog_wrapper.h" 20#include "ui_extension_utils.h" 21 22namespace OHOS { 23namespace AbilityRuntime { 24template<class C> 25void ExtensionBase<C>::Init(const std::shared_ptr<AbilityLocalRecord> &record, 26 const std::shared_ptr<OHOSApplication> &application, 27 std::shared_ptr<AbilityHandler> &handler, 28 const sptr<IRemoteObject> &token) 29{ 30 Extension::Init(record, application, handler, token); 31 TAG_LOGD(AAFwkTag::EXT, "begin"); 32 context_ = CreateAndInitContext(record, application, handler, token); 33} 34 35template<class C> 36std::shared_ptr<C> ExtensionBase<C>::CreateAndInitContext(const std::shared_ptr<AbilityLocalRecord> &record, 37 const std::shared_ptr<OHOSApplication> &application, 38 std::shared_ptr<AbilityHandler> &handler, 39 const sptr<IRemoteObject> &token) 40{ 41 TAG_LOGD(AAFwkTag::EXT, "begin"); 42 std::shared_ptr<C> context = std::make_shared<C>(); 43 auto appContext = Context::GetApplicationContext(); 44 if (appContext == nullptr) { 45 TAG_LOGE(AAFwkTag::EXT, "null appContext"); 46 return context; 47 } 48 context->SetApplicationInfo(appContext->GetApplicationInfo()); 49 context->SetResourceManager(appContext->GetResourceManager()); 50 context->SetParentContext(appContext); 51 context->SetToken(token); 52 context->SetProcessName(appContext->GetProcessName()); 53 if (record == nullptr) { 54 TAG_LOGE(AAFwkTag::EXT, "null record"); 55 return context; 56 } 57 TAG_LOGD(AAFwkTag::EXT, "begin init abilityInfo"); 58 auto abilityInfo = record->GetAbilityInfo(); 59 context->SetAbilityInfo(abilityInfo); 60 context->InitHapModuleInfo(abilityInfo); 61 if (AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo->extensionAbilityType) && 62 appContext->GetConfiguration() != nullptr) { 63 auto appConfig = appContext->GetConfiguration(); 64 auto contextConfig = std::make_shared<AppExecFwk::Configuration>(*appConfig); 65 context->SetConfiguration(contextConfig); 66 } else { 67 context->SetConfiguration(appContext->GetConfiguration()); 68 } 69 if (abilityInfo->applicationInfo.multiProjects) { 70 std::shared_ptr<Context> moduleContext = context->CreateModuleContext(abilityInfo->moduleName); 71 if (moduleContext != nullptr) { 72 auto rm = moduleContext->GetResourceManager(); 73 context->SetResourceManager(rm); 74 } 75 } 76 return context; 77} 78 79template<class C> 80std::shared_ptr<C> ExtensionBase<C>::GetContext() 81{ 82 return context_; 83} 84 85template<class C> 86void ExtensionBase<C>::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) 87{ 88 Extension::OnConfigurationUpdated(configuration); 89 TAG_LOGD(AAFwkTag::EXT, "called"); 90 91 if (!context_) { 92 TAG_LOGE(AAFwkTag::EXT, "null context_"); 93 return; 94 } 95 96 auto fullConfig = context_->GetConfiguration(); 97 if (!fullConfig) { 98 TAG_LOGE(AAFwkTag::EXT, "null config"); 99 return; 100 } 101 102 if (extensionCommon_) { 103 extensionCommon_->OnConfigurationUpdated(fullConfig); 104 } 105} 106 107template<class C> 108void ExtensionBase<C>::OnMemoryLevel(int level) 109{ 110 Extension::OnMemoryLevel(level); 111 TAG_LOGD(AAFwkTag::EXT, "called"); 112 113 if (extensionCommon_) { 114 extensionCommon_->OnMemoryLevel(level); 115 } 116} 117 118template<class C> 119void ExtensionBase<C>::SetExtensionCommon(const std::shared_ptr<ExtensionCommon> &common) 120{ 121 extensionCommon_ = common; 122} 123 124template<class C> 125void ExtensionBase<C>::OnExtensionAbilityRequestFailure(const std::string &requestId, 126 const AppExecFwk::ElementName &element, const std::string &message, int32_t resultCode) 127{ 128 TAG_LOGD(AAFwkTag::EXT, "OnAbilityRequestFailure called"); 129 if (context_ == nullptr) { 130 TAG_LOGE(AAFwkTag::EXT, "null context_"); 131 return; 132 } 133 context_->OnRequestFailure(requestId, element, message, resultCode); 134} 135 136template<class C> 137void ExtensionBase<C>::OnExtensionAbilityRequestSuccess( 138 const std::string &requestId, const AppExecFwk::ElementName &element, const std::string &message) 139{ 140 TAG_LOGD(AAFwkTag::EXT, "OnAbilityRequestSuccess called"); 141 if (context_ == nullptr) { 142 TAG_LOGE(AAFwkTag::EXT, "null context_"); 143 return; 144 } 145 context_->OnRequestSuccess(requestId, element, message); 146} 147} 148} 149