• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/common/container.h"
17 
18 #include <dirent.h>
19 
20 #include "frameworks/base/utils/utils.h"
21 #include "core/common/ace_engine.h"
22 #ifdef PLUGIN_COMPONENT_SUPPORTED
23 #include "core/common/plugin_manager.h"
24 #endif
25 
26 #include "core/components_ng/pattern/window_scene/helper/window_scene_helper.h"
27 #include "core/pipeline/pipeline_base.h"
28 
29 namespace OHOS::Ace {
30 
CurrentId()31 int32_t Container::CurrentId()
32 {
33     return ContainerScope::CurrentId();
34 }
35 
GetKeyboardSafeArea()36 NG::SafeAreaInsets Container::GetKeyboardSafeArea()
37 {
38     return {};
39 }
40 
SafelyId()41 int32_t Container::SafelyId()
42 {
43     uint32_t containerCount = ContainerScope::ContainerCount();
44     if (containerCount == 0) {
45         return INSTANCE_ID_UNDEFINED;
46     }
47     if (containerCount == 1) {
48         return ContainerScope::SingletonId();
49     }
50     int32_t currentId = ContainerScope::RecentActiveId();
51     if (currentId >= 0) {
52         return currentId;
53     }
54     currentId = ContainerScope::RecentForegroundId();
55     if (currentId >= 0) {
56         return currentId;
57     }
58     return ContainerScope::DefaultId();
59 }
60 
CurrentIdSafely()61 int32_t Container::CurrentIdSafely()
62 {
63     int32_t currentId = ContainerScope::CurrentId();
64     if (currentId >= 0) {
65         return currentId;
66     }
67     return SafelyId();
68 }
69 
Current()70 RefPtr<Container> Container::Current()
71 {
72     return AceEngine::Get().GetContainer(ContainerScope::CurrentId());
73 }
74 
CurrentSafely()75 RefPtr<Container> Container::CurrentSafely()
76 {
77     return AceEngine::Get().GetContainer(Container::CurrentIdSafely());
78 }
79 
CurrentSafelyWithCheck()80 RefPtr<Container> Container::CurrentSafelyWithCheck()
81 {
82     int32_t currentId = CurrentId();
83     if (currentId >= 0) {
84         auto container = GetContainer(currentId);
85         if (container) {
86             return container;
87         }
88     }
89     currentId = SafelyId();
90     return GetContainer(currentId);
91 }
92 
CurrentIdSafelyWithCheck()93 int32_t Container::CurrentIdSafelyWithCheck()
94 {
95     int32_t currentId = CurrentId();
96     if (currentId >= 0) {
97         if (AceEngine::Get().HasContainer(currentId)) {
98             return currentId;
99         }
100     }
101     return SafelyId();
102 }
103 
GetContainer(int32_t containerId)104 RefPtr<Container> Container::GetContainer(int32_t containerId)
105 {
106     return AceEngine::Get().GetContainer(containerId);
107 }
108 
GetActive()109 RefPtr<Container> Container::GetActive()
110 {
111     RefPtr<Container> activeContainer;
112     AceEngine::Get().NotifyContainers([&activeContainer](const RefPtr<Container>& container) {
113         auto front = container->GetFrontend();
114         if (front && front->IsForeground()) {
115             activeContainer = container;
116         }
117     });
118     return activeContainer;
119 }
120 
GetDefault()121 RefPtr<Container> Container::GetDefault()
122 {
123     RefPtr<Container> defaultContainer;
124     AceEngine::Get().NotifyContainers([&defaultContainer](const RefPtr<Container>& container) {
125         auto front = container->GetFrontend();
126         if (front) {
127             defaultContainer = container;
128         }
129     });
130     return defaultContainer;
131 }
132 
GetFoucsed()133 RefPtr<Container> Container::GetFoucsed()
134 {
135     RefPtr<Container> foucsContainer;
136     AceEngine::Get().NotifyContainers([&foucsContainer](const RefPtr<Container>& container) {
137         auto pipeline = container->GetPipelineContext();
138         if (pipeline && pipeline->IsWindowFocused()) {
139             foucsContainer = container;
140         }
141     });
142     return foucsContainer;
143 }
144 
GetByWindowId(uint32_t windowId)145 RefPtr<Container> Container::GetByWindowId(uint32_t windowId)
146 {
147     RefPtr<Container> windowContainer;
148     AceEngine::Get().NotifyContainers([&windowContainer, windowId](const RefPtr<Container>& container) {
149         if (windowId == container->GetWindowId()) {
150             windowContainer = container;
151         }
152     });
153     return windowContainer;
154 }
155 
CurrentTaskExecutor()156 RefPtr<TaskExecutor> Container::CurrentTaskExecutor()
157 {
158     auto curContainer = Current();
159     CHECK_NULL_RETURN(curContainer, nullptr);
160     return curContainer->GetTaskExecutor();
161 }
162 
CurrentTaskExecutorSafely()163 RefPtr<TaskExecutor> Container::CurrentTaskExecutorSafely()
164 {
165     auto curContainer = CurrentSafely();
166     CHECK_NULL_RETURN(curContainer, nullptr);
167     return curContainer->GetTaskExecutor();
168 }
169 
CurrentTaskExecutorSafelyWithCheck()170 RefPtr<TaskExecutor> Container::CurrentTaskExecutorSafelyWithCheck()
171 {
172     auto curContainer = CurrentSafelyWithCheck();
173     CHECK_NULL_RETURN(curContainer, nullptr);
174     return curContainer->GetTaskExecutor();
175 }
176 
UpdateCurrent(int32_t id)177 void Container::UpdateCurrent(int32_t id)
178 {
179     ContainerScope::UpdateCurrent(id);
180 }
181 
CurrentColorMode()182 ColorMode Container::CurrentColorMode()
183 {
184     auto curContainer = CurrentSafely();
185     CHECK_NULL_RETURN(curContainer, ColorMode::LIGHT);
186     return curContainer->GetColorMode();
187 }
188 
UpdateState(const Frontend::State & state)189 bool Container::UpdateState(const Frontend::State& state)
190 {
191     std::lock_guard<std::mutex> lock(stateMutex_);
192     if (state_ == state) {
193         return false;
194     }
195     state_ = state;
196     return true;
197 }
198 
Dump(const std::vector<std::string> & params,std::vector<std::string> & info)199 bool Container::Dump(const std::vector<std::string>& params, std::vector<std::string>& info)
200 {
201     std::string tip("container not support, type:");
202     tip.append(AceType::TypeName(this));
203     info.emplace_back(tip);
204     return true;
205 }
206 
IsIdAvailable(int32_t id)207 bool Container::IsIdAvailable(int32_t id)
208 {
209     return !AceEngine::Get().GetContainer(id);
210 }
211 
SetFontScale(int32_t instanceId,float fontScale)212 void Container::SetFontScale(int32_t instanceId, float fontScale)
213 {
214     auto container = AceEngine::Get().GetContainer(instanceId);
215     CHECK_NULL_VOID(container);
216     ContainerScope scope(instanceId);
217     auto pipelineContext = container->GetPipelineContext();
218     CHECK_NULL_VOID(pipelineContext);
219     pipelineContext->SetFontScale(fontScale);
220 }
221 
SetFontWeightScale(int32_t instanceId,float fontWeightScale)222 void Container::SetFontWeightScale(int32_t instanceId, float fontWeightScale)
223 {
224     auto container = AceEngine::Get().GetContainer(instanceId);
225     CHECK_NULL_VOID(container);
226     ContainerScope scope(instanceId);
227     auto pipelineContext = container->GetPipelineContext();
228     CHECK_NULL_VOID(pipelineContext);
229     pipelineContext->SetFontWeightScale(fontWeightScale);
230 }
231 
GetDisplayInfo()232 RefPtr<DisplayInfo> Container::GetDisplayInfo()
233 {
234     return displayManager_->GetDisplayInfo(currentDisplayId_);
235 }
236 
InitIsFoldable()237 void Container::InitIsFoldable()
238 {
239     displayManager_->InitIsFoldable();
240 }
241 
IsFoldable()242 bool Container::IsFoldable()
243 {
244     return displayManager_->GetIsFoldable();
245 }
246 
GetCurrentFoldStatus()247 FoldStatus Container::GetCurrentFoldStatus()
248 {
249     return displayManager_->GetCurrentFoldStatus();
250 }
251 
GetCurrentFoldCreaseRegion()252 std::vector<Rect> Container::GetCurrentFoldCreaseRegion()
253 {
254     return displayManager_->GetCurrentFoldCreaseRegion();
255 }
256 
DestroyToastSubwindow(int32_t instanceId)257 void Container::DestroyToastSubwindow(int32_t instanceId)
258 {
259     auto subwindow = SubwindowManager::GetInstance()->GetToastSubwindow(instanceId);
260     if (subwindow && subwindow->IsToastSubWindow()) {
261         subwindow->DestroyWindow();
262     }
263     auto systemToastWindow = SubwindowManager::GetInstance()->GetSystemToastWindow(instanceId);
264     if (systemToastWindow && systemToastWindow->IsToastSubWindow()) {
265         systemToastWindow->DestroyWindow();
266     }
267 }
268 
DestroySelectOverlaySubwindow(int32_t instanceId)269 void Container::DestroySelectOverlaySubwindow(int32_t instanceId)
270 {
271     auto subwindow = SubwindowManager::GetInstance()->GetSelectOverlaySubwindow(instanceId);
272     if (subwindow && subwindow->GetIsSelectOverlaySubWindow()) {
273         subwindow->DestroyWindow();
274         TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "Destroy selectOverlay subwindow, instanceId is %{public}d", instanceId);
275     }
276 }
277 
IsFontFileExistInPath(const std::string & path)278 bool Container::IsFontFileExistInPath(const std::string& path)
279 {
280     DIR* dir;
281     struct dirent* ent;
282     bool isFlagFileExist = false;
283     bool isFontDirExist = false;
284     if ((dir = opendir(path.c_str())) == nullptr) {
285         if (errno == ENOENT) {
286             LOGE("ERROR ENOENT");
287         } else if (errno == EACCES) {
288             LOGE("ERROR EACCES");
289         } else {
290             LOGE("ERROR Other");
291         }
292         return false;
293     }
294     while ((ent = readdir(dir)) != nullptr) {
295         if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
296             continue;
297         }
298         if (strcmp(ent->d_name, "flag") == 0) {
299             isFlagFileExist = true;
300         } else if (strcmp(ent->d_name, "fonts") == 0) {
301             isFontDirExist = true;
302         }
303     }
304     closedir(dir);
305     if (isFlagFileExist && isFontDirExist) {
306         LOGI("font path exist");
307         return true;
308     }
309     return false;
310 }
311 
GetFontFamilyName(const std::string & path)312 std::vector<std::string> Container::GetFontFamilyName(const std::string& path)
313 {
314     std::vector<std::string> fontFamilyName;
315     std::string manifest = "manifest.json";
316     std::string manifestContent = ReadFileToString(path, manifest);
317     auto json = JsonUtil::ParseJsonString(manifestContent);
318     if (!json || !json->IsValid()) {
319         return fontFamilyName;
320     }
321     std::string ttfFileSrc = json->GetString("ttfFileSrc");
322     if (!ttfFileSrc.empty()) {
323         size_t lastSlashPos = ttfFileSrc.find_last_of("/\\");
324         std::string ttfFileName =
325             (lastSlashPos != std::string::npos) ? ttfFileSrc.substr(lastSlashPos + 1) : ttfFileSrc;
326         fontFamilyName.push_back(ttfFileName);
327     }
328     auto ttfFileSrcExtArray = json->GetValue("ttfFileSrcExt");
329     if (ttfFileSrcExtArray && ttfFileSrcExtArray->IsArray()) {
330         for (int32_t index = 0; index < ttfFileSrcExtArray->GetArraySize(); ++index) {
331             auto ttfFileSrcExtArrayItem = ttfFileSrcExtArray->GetArrayItem(index);
332             if (ttfFileSrcExtArrayItem && ttfFileSrcExtArrayItem->IsString()) {
333                 std::string ttfFileSrcExt = ttfFileSrcExtArrayItem->GetString();
334                 size_t lastSlashPos = ttfFileSrcExt.find_last_of("/\\");
335                 std::string ttfFileExtName =
336                     (lastSlashPos != std::string::npos) ? ttfFileSrcExt.substr(lastSlashPos + 1) : ttfFileSrcExt;
337                 fontFamilyName.emplace_back(ttfFileExtName);
338             }
339         }
340     }
341     return fontFamilyName;
342 }
343 
endsWith(std::string str,std::string suffix)344 bool Container::endsWith(std::string str, std::string suffix)
345 {
346     if (str.length() < suffix.length()) {
347         return false;
348     }
349     return str.substr(str.length() - suffix.length()) == suffix;
350 }
351 
352 template<>
GenerateId()353 int32_t Container::GenerateId<PLUGIN_SUBCONTAINER>()
354 {
355 #ifdef PLUGIN_COMPONENT_SUPPORTED
356     return PluginManager::GetInstance().GetPluginSubContainerId();
357 #else
358     return INSTANCE_ID_UNDEFINED;
359 #endif
360 }
361 
IsNodeInKeyGuardWindow(const RefPtr<NG::FrameNode> & node)362 bool Container::IsNodeInKeyGuardWindow(const RefPtr<NG::FrameNode>& node)
363 {
364 #ifdef WINDOW_SCENE_SUPPORTED
365     return NG::WindowSceneHelper::IsNodeInKeyGuardWindow(node);
366 #else
367     return false;
368 #endif
369 }
CheckRunOnThreadByThreadId(int32_t currentId,bool defaultRes)370 bool Container::CheckRunOnThreadByThreadId(int32_t currentId, bool defaultRes)
371 {
372     auto container = GetContainer(currentId);
373     CHECK_NULL_RETURN(container, defaultRes);
374     auto executor = container->GetTaskExecutor();
375     CHECK_NULL_RETURN(executor, defaultRes);
376     return executor->WillRunOnCurrentThread(TaskExecutor::TaskType::UI);
377 }
378 
GetWindow() const379 Window* Container::GetWindow() const
380 {
381     auto context = GetPipelineContext();
382     return context ? context->GetWindow() : nullptr;
383 }
384 
LessThanAPIVersion(PlatformVersion version)385 bool Container::LessThanAPIVersion(PlatformVersion version)
386 {
387     return static_cast<int32_t>(version) < 15
388                ? PipelineBase::GetCurrentContext() &&
389                      PipelineBase::GetCurrentContext()->GetMinPlatformVersion() < static_cast<int32_t>(version)
390                : LessThanAPITargetVersion(version);
391 }
392 
GreatOrEqualAPIVersion(PlatformVersion version)393 bool Container::GreatOrEqualAPIVersion(PlatformVersion version)
394 {
395     return static_cast<int32_t>(version) < 15
396                ? PipelineBase::GetCurrentContext() &&
397                      PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= static_cast<int32_t>(version)
398                : GreatOrEqualAPITargetVersion(version);
399 }
400 
LessThanAPIVersionWithCheck(PlatformVersion version)401 bool Container::LessThanAPIVersionWithCheck(PlatformVersion version)
402 {
403     return PipelineBase::GetCurrentContextSafelyWithCheck() &&
404            PipelineBase::GetCurrentContextSafelyWithCheck()->GetMinPlatformVersion() < static_cast<int32_t>(version);
405 }
406 
GreatOrEqualAPIVersionWithCheck(PlatformVersion version)407 bool Container::GreatOrEqualAPIVersionWithCheck(PlatformVersion version)
408 {
409     return PipelineBase::GetCurrentContextSafelyWithCheck() &&
410            PipelineBase::GetCurrentContextSafelyWithCheck()->GetMinPlatformVersion() >= static_cast<int32_t>(version);
411 }
412 } // namespace OHOS::Ace
413