1/* 2 * Copyright (c) 2021-2024 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 21namespace OHOS { 22namespace AbilityRuntime { 23template<class C> 24void ExtensionBase<C>::Init(const std::shared_ptr<AbilityLocalRecord> &record, 25 const std::shared_ptr<OHOSApplication> &application, 26 std::shared_ptr<AbilityHandler> &handler, 27 const sptr<IRemoteObject> &token) 28{ 29 Extension::Init(record, application, handler, token); 30 TAG_LOGD(AAFwkTag::EXT, "begin"); 31 context_ = CreateAndInitContext(record, application, handler, token); 32} 33 34template<class C> 35std::shared_ptr<C> ExtensionBase<C>::CreateAndInitContext(const std::shared_ptr<AbilityLocalRecord> &record, 36 const std::shared_ptr<OHOSApplication> &application, 37 std::shared_ptr<AbilityHandler> &handler, 38 const sptr<IRemoteObject> &token) 39{ 40 TAG_LOGD(AAFwkTag::EXT, "begin"); 41 std::shared_ptr<C> context = std::make_shared<C>(); 42 auto appContext = Context::GetApplicationContext(); 43 if (appContext == nullptr) { 44 TAG_LOGE(AAFwkTag::EXT, "null appContext"); 45 return context; 46 } 47 context->SetApplicationInfo(appContext->GetApplicationInfo()); 48 context->SetResourceManager(appContext->GetResourceManager()); 49 context->SetParentContext(appContext); 50 context->SetToken(token); 51 if (record == nullptr) { 52 TAG_LOGE(AAFwkTag::EXT, "null record"); 53 return context; 54 } 55 TAG_LOGD(AAFwkTag::EXT, "begin init abilityInfo"); 56 auto abilityInfo = record->GetAbilityInfo(); 57 context->SetAbilityInfo(abilityInfo); 58 context->InitHapModuleInfo(abilityInfo); 59 context->SetConfiguration(appContext->GetConfiguration()); 60 if (abilityInfo->applicationInfo.multiProjects) { 61 std::shared_ptr<Context> moduleContext = context->CreateModuleContext(abilityInfo->moduleName); 62 if (moduleContext != nullptr) { 63 auto rm = moduleContext->GetResourceManager(); 64 context->SetResourceManager(rm); 65 } 66 } 67 return context; 68} 69 70template<class C> 71std::shared_ptr<C> ExtensionBase<C>::GetContext() 72{ 73 return context_; 74} 75 76template<class C> 77void ExtensionBase<C>::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) 78{ 79 Extension::OnConfigurationUpdated(configuration); 80 TAG_LOGD(AAFwkTag::EXT, "called"); 81 82 if (!context_) { 83 TAG_LOGE(AAFwkTag::EXT, "null context_"); 84 return; 85 } 86 87 auto fullConfig = context_->GetConfiguration(); 88 if (!fullConfig) { 89 TAG_LOGE(AAFwkTag::EXT, "null config"); 90 return; 91 } 92 93 if (extensionCommon_) { 94 extensionCommon_->OnConfigurationUpdated(fullConfig); 95 } 96} 97 98template<class C> 99void ExtensionBase<C>::OnMemoryLevel(int level) 100{ 101 Extension::OnMemoryLevel(level); 102 TAG_LOGD(AAFwkTag::EXT, "called"); 103 104 if (extensionCommon_) { 105 extensionCommon_->OnMemoryLevel(level); 106 } 107} 108 109template<class C> 110void ExtensionBase<C>::SetExtensionCommon(const std::shared_ptr<ExtensionCommon> &common) 111{ 112 extensionCommon_ = common; 113} 114} 115} 116