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