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