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 "core/common/ace_engine.h"
19 #ifdef PLUGIN_COMPONENT_SUPPORTED
20 #include "core/common/plugin_manager.h"
21 #endif
22
23 namespace OHOS::Ace {
24
CurrentId()25 int32_t Container::CurrentId()
26 {
27 return ContainerScope::CurrentId();
28 }
29
SafelyId()30 int32_t Container::SafelyId()
31 {
32 uint32_t containerCount = ContainerScope::ContainerCount();
33 if (containerCount == 0) {
34 return INSTANCE_ID_UNDEFINED;
35 }
36 if (containerCount == 1) {
37 return ContainerScope::SingletonId();
38 }
39 int32_t currentId = ContainerScope::RecentActiveId();
40 if (currentId >= 0) {
41 return currentId;
42 }
43 currentId = ContainerScope::RecentForegroundId();
44 if (currentId >= 0) {
45 return currentId;
46 }
47 return ContainerScope::DefaultId();
48 }
49
CurrentIdSafely()50 int32_t Container::CurrentIdSafely()
51 {
52 int32_t currentId = ContainerScope::CurrentId();
53 if (currentId >= 0) {
54 return currentId;
55 }
56 return SafelyId();
57 }
58
Current()59 RefPtr<Container> Container::Current()
60 {
61 return AceEngine::Get().GetContainer(ContainerScope::CurrentId());
62 }
63
CurrentSafely()64 RefPtr<Container> Container::CurrentSafely()
65 {
66 return AceEngine::Get().GetContainer(Container::CurrentIdSafely());
67 }
68
CurrentSafelyWithCheck()69 RefPtr<Container> Container::CurrentSafelyWithCheck()
70 {
71 int32_t currentId = CurrentId();
72 if (currentId >= 0) {
73 auto container = GetContainer(currentId);
74 if (container) {
75 return container;
76 }
77 }
78 currentId = SafelyId();
79 return GetContainer(currentId);
80 }
81
CurrentIdSafelyWithCheck()82 int32_t Container::CurrentIdSafelyWithCheck()
83 {
84 int32_t currentId = CurrentId();
85 if (currentId >= 0) {
86 if (AceEngine::Get().HasContainer(currentId)) {
87 return currentId;
88 }
89 }
90 return SafelyId();
91 }
92
GetContainer(int32_t containerId)93 RefPtr<Container> Container::GetContainer(int32_t containerId)
94 {
95 return AceEngine::Get().GetContainer(containerId);
96 }
97
GetActive()98 RefPtr<Container> Container::GetActive()
99 {
100 RefPtr<Container> activeContainer;
101 AceEngine::Get().NotifyContainers([&activeContainer](const RefPtr<Container>& container) {
102 auto front = container->GetFrontend();
103 if (front && front->IsForeground()) {
104 activeContainer = container;
105 }
106 });
107 return activeContainer;
108 }
109
GetDefault()110 RefPtr<Container> Container::GetDefault()
111 {
112 RefPtr<Container> defaultContainer;
113 AceEngine::Get().NotifyContainers([&defaultContainer](const RefPtr<Container>& container) {
114 auto front = container->GetFrontend();
115 if (front) {
116 defaultContainer = container;
117 }
118 });
119 return defaultContainer;
120 }
121
GetFoucsed()122 RefPtr<Container> Container::GetFoucsed()
123 {
124 RefPtr<Container> foucsContainer;
125 AceEngine::Get().NotifyContainers([&foucsContainer](const RefPtr<Container>& container) {
126 auto pipeline = container->GetPipelineContext();
127 if (pipeline && pipeline->IsWindowFocused()) {
128 foucsContainer = container;
129 }
130 });
131 return foucsContainer;
132 }
133
CurrentTaskExecutor()134 RefPtr<TaskExecutor> Container::CurrentTaskExecutor()
135 {
136 auto curContainer = Current();
137 CHECK_NULL_RETURN(curContainer, nullptr);
138 return curContainer->GetTaskExecutor();
139 }
140
CurrentTaskExecutorSafely()141 RefPtr<TaskExecutor> Container::CurrentTaskExecutorSafely()
142 {
143 auto curContainer = CurrentSafely();
144 CHECK_NULL_RETURN(curContainer, nullptr);
145 return curContainer->GetTaskExecutor();
146 }
147
CurrentTaskExecutorSafelyWithCheck()148 RefPtr<TaskExecutor> Container::CurrentTaskExecutorSafelyWithCheck()
149 {
150 auto curContainer = CurrentSafelyWithCheck();
151 CHECK_NULL_RETURN(curContainer, nullptr);
152 return curContainer->GetTaskExecutor();
153 }
154
UpdateCurrent(int32_t id)155 void Container::UpdateCurrent(int32_t id)
156 {
157 ContainerScope::UpdateCurrent(id);
158 }
159
UpdateState(const Frontend::State & state)160 bool Container::UpdateState(const Frontend::State& state)
161 {
162 std::lock_guard<std::mutex> lock(stateMutex_);
163 if (state_ == state) {
164 return false;
165 }
166 state_ = state;
167 return true;
168 }
169
Dump(const std::vector<std::string> & params,std::vector<std::string> & info)170 bool Container::Dump(const std::vector<std::string>& params, std::vector<std::string>& info)
171 {
172 std::string tip("container not support, type:");
173 tip.append(AceType::TypeName(this));
174 info.emplace_back(tip);
175 return true;
176 }
177
IsIdAvailable(int32_t id)178 bool Container::IsIdAvailable(int32_t id)
179 {
180 return !AceEngine::Get().GetContainer(id);
181 }
182
SetFontScale(int32_t instanceId,float fontScale)183 void Container::SetFontScale(int32_t instanceId, float fontScale)
184 {
185 auto container = AceEngine::Get().GetContainer(instanceId);
186 CHECK_NULL_VOID(container);
187 ContainerScope scope(instanceId);
188 auto pipelineContext = container->GetPipelineContext();
189 CHECK_NULL_VOID(pipelineContext);
190 pipelineContext->SetFontScale(fontScale);
191 }
192
SetFontWeightScale(int32_t instanceId,float fontWeightScale)193 void Container::SetFontWeightScale(int32_t instanceId, float fontWeightScale)
194 {
195 auto container = AceEngine::Get().GetContainer(instanceId);
196 CHECK_NULL_VOID(container);
197 ContainerScope scope(instanceId);
198 auto pipelineContext = container->GetPipelineContext();
199 CHECK_NULL_VOID(pipelineContext);
200 pipelineContext->SetFontWeightScale(fontWeightScale);
201 }
202
GetDisplayInfo()203 RefPtr<DisplayInfo> Container::GetDisplayInfo()
204 {
205 return DisplayInfoUtils::GetInstance().GetDisplayInfo();
206 }
207
InitIsFoldable()208 void Container::InitIsFoldable()
209 {
210 DisplayInfoUtils::GetInstance().InitIsFoldable();
211 }
212
IsFoldable()213 bool Container::IsFoldable()
214 {
215 return DisplayInfoUtils::GetInstance().IsFoldable();
216 }
217
GetCurrentFoldStatus()218 FoldStatus Container::GetCurrentFoldStatus()
219 {
220 return DisplayInfoUtils::GetInstance().GetCurrentFoldStatus();
221 }
222
223 template<>
GenerateId()224 int32_t Container::GenerateId<PLUGIN_SUBCONTAINER>()
225 {
226 #ifdef PLUGIN_COMPONENT_SUPPORTED
227 return PluginManager::GetInstance().GetPluginSubContainerId();
228 #else
229 return INSTANCE_ID_UNDEFINED;
230 #endif
231 }
232
233 } // namespace OHOS::Ace
234