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 "core/common/container.h" 17 18 #include "base/utils/utils.h" 19 #include "core/common/ace_engine.h" 20 #include "core/common/container_scope.h" 21 #ifdef PLUGIN_COMPONENT_SUPPORTED 22 #include "core/common/plugin_manager.h" 23 #endif 24 25 namespace OHOS::Ace { 26 27 bool Container::isLauncherApp_ = false; 28 CurrentId()29int32_t Container::CurrentId() 30 { 31 return ContainerScope::CurrentId(); 32 } 33 CurrentIdSafely()34int32_t Container::CurrentIdSafely() 35 { 36 int32_t currentId = ContainerScope::CurrentId(); 37 if (currentId >= 0) { 38 return currentId; 39 } 40 uint32_t containerCount = ContainerScope::ContainerCount(); 41 if (containerCount == 0) { 42 return INSTANCE_ID_UNDEFINED; 43 } 44 if (containerCount == 1) { 45 return ContainerScope::SingletonId(); 46 } 47 currentId = ContainerScope::RecentActiveId(); 48 if (currentId >= 0) { 49 return currentId; 50 } 51 currentId = ContainerScope::RecentForegroundId(); 52 if (currentId >= 0) { 53 return currentId; 54 } 55 return ContainerScope::DefaultId(); 56 } 57 Current()58RefPtr<Container> Container::Current() 59 { 60 return AceEngine::Get().GetContainer(ContainerScope::CurrentId()); 61 } 62 CurrentSafely()63RefPtr<Container> Container::CurrentSafely() 64 { 65 return AceEngine::Get().GetContainer(Container::CurrentIdSafely()); 66 } 67 GetContainer(int32_t containerId)68RefPtr<Container> Container::GetContainer(int32_t containerId) 69 { 70 return AceEngine::Get().GetContainer(containerId); 71 } 72 GetActive()73RefPtr<Container> Container::GetActive() 74 { 75 RefPtr<Container> activeContainer; 76 AceEngine::Get().NotifyContainers([&activeContainer](const RefPtr<Container>& container) { 77 auto front = container->GetFrontend(); 78 if (front && front->IsForeground()) { 79 activeContainer = container; 80 } 81 }); 82 return activeContainer; 83 } 84 GetDefault()85RefPtr<Container> Container::GetDefault() 86 { 87 RefPtr<Container> defaultContainer; 88 AceEngine::Get().NotifyContainers([&defaultContainer](const RefPtr<Container>& container) { 89 auto front = container->GetFrontend(); 90 if (front) { 91 defaultContainer = container; 92 } 93 }); 94 return defaultContainer; 95 } 96 GetFoucsed()97RefPtr<Container> Container::GetFoucsed() 98 { 99 RefPtr<Container> foucsContainer; 100 AceEngine::Get().NotifyContainers([&foucsContainer](const RefPtr<Container>& container) { 101 auto pipeline = container->GetPipelineContext(); 102 if (pipeline && pipeline->GetOnFoucs()) { 103 foucsContainer = container; 104 } 105 }); 106 return foucsContainer; 107 } 108 CurrentTaskExecutor()109RefPtr<TaskExecutor> Container::CurrentTaskExecutor() 110 { 111 auto curContainer = Current(); 112 CHECK_NULL_RETURN(curContainer, nullptr); 113 return curContainer->GetTaskExecutor(); 114 } 115 UpdateCurrent(int32_t id)116void Container::UpdateCurrent(int32_t id) 117 { 118 ContainerScope::UpdateCurrent(id); 119 } 120 UpdateState(const Frontend::State & state)121bool Container::UpdateState(const Frontend::State& state) 122 { 123 std::lock_guard<std::mutex> lock(stateMutex_); 124 if (state_ == state) { 125 return false; 126 } 127 state_ = state; 128 return true; 129 } 130 Dump(const std::vector<std::string> & params,std::vector<std::string> & info)131bool Container::Dump(const std::vector<std::string>& params, std::vector<std::string>& info) 132 { 133 std::string tip("container not support, type:"); 134 tip.append(AceType::TypeName(this)); 135 info.emplace_back(tip); 136 return true; 137 } 138 IsIdAvailable(int32_t id)139bool Container::IsIdAvailable(int32_t id) 140 { 141 return !AceEngine::Get().GetContainer(id); 142 } 143 144 template<> GenerateId()145int32_t Container::GenerateId<PLUGIN_SUBCONTAINER>() 146 { 147 #ifdef PLUGIN_COMPONENT_SUPPORTED 148 return PluginManager::GetInstance().GetPluginSubContainerId(); 149 #else 150 return INSTANCE_ID_UNDEFINED; 151 #endif 152 } 153 154 } // namespace OHOS::Ace 155