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 #include <dirent.h>
24
25 namespace OHOS::Ace {
26
CurrentId()27 int32_t Container::CurrentId()
28 {
29 return ContainerScope::CurrentId();
30 }
31
SafelyId()32 int32_t Container::SafelyId()
33 {
34 uint32_t containerCount = ContainerScope::ContainerCount();
35 if (containerCount == 0) {
36 return INSTANCE_ID_UNDEFINED;
37 }
38 if (containerCount == 1) {
39 return ContainerScope::SingletonId();
40 }
41 int32_t currentId = ContainerScope::RecentActiveId();
42 if (currentId >= 0) {
43 return currentId;
44 }
45 currentId = ContainerScope::RecentForegroundId();
46 if (currentId >= 0) {
47 return currentId;
48 }
49 return ContainerScope::DefaultId();
50 }
51
CurrentIdSafely()52 int32_t Container::CurrentIdSafely()
53 {
54 int32_t currentId = ContainerScope::CurrentId();
55 if (currentId >= 0) {
56 return currentId;
57 }
58 return SafelyId();
59 }
60
Current()61 RefPtr<Container> Container::Current()
62 {
63 return AceEngine::Get().GetContainer(ContainerScope::CurrentId());
64 }
65
CurrentSafely()66 RefPtr<Container> Container::CurrentSafely()
67 {
68 return AceEngine::Get().GetContainer(Container::CurrentIdSafely());
69 }
70
CurrentSafelyWithCheck()71 RefPtr<Container> Container::CurrentSafelyWithCheck()
72 {
73 int32_t currentId = CurrentId();
74 if (currentId >= 0) {
75 auto container = GetContainer(currentId);
76 if (container) {
77 return container;
78 }
79 }
80 currentId = SafelyId();
81 return GetContainer(currentId);
82 }
83
CurrentIdSafelyWithCheck()84 int32_t Container::CurrentIdSafelyWithCheck()
85 {
86 int32_t currentId = CurrentId();
87 if (currentId >= 0) {
88 if (AceEngine::Get().HasContainer(currentId)) {
89 return currentId;
90 }
91 }
92 return SafelyId();
93 }
94
GetContainer(int32_t containerId)95 RefPtr<Container> Container::GetContainer(int32_t containerId)
96 {
97 return AceEngine::Get().GetContainer(containerId);
98 }
99
GetActive()100 RefPtr<Container> Container::GetActive()
101 {
102 RefPtr<Container> activeContainer;
103 AceEngine::Get().NotifyContainers([&activeContainer](const RefPtr<Container>& container) {
104 auto front = container->GetFrontend();
105 if (front && front->IsForeground()) {
106 activeContainer = container;
107 }
108 });
109 return activeContainer;
110 }
111
GetDefault()112 RefPtr<Container> Container::GetDefault()
113 {
114 RefPtr<Container> defaultContainer;
115 AceEngine::Get().NotifyContainers([&defaultContainer](const RefPtr<Container>& container) {
116 auto front = container->GetFrontend();
117 if (front) {
118 defaultContainer = container;
119 }
120 });
121 return defaultContainer;
122 }
123
GetFoucsed()124 RefPtr<Container> Container::GetFoucsed()
125 {
126 RefPtr<Container> foucsContainer;
127 AceEngine::Get().NotifyContainers([&foucsContainer](const RefPtr<Container>& container) {
128 auto pipeline = container->GetPipelineContext();
129 if (pipeline && pipeline->IsWindowFocused()) {
130 foucsContainer = container;
131 }
132 });
133 return foucsContainer;
134 }
135
CurrentTaskExecutor()136 RefPtr<TaskExecutor> Container::CurrentTaskExecutor()
137 {
138 auto curContainer = Current();
139 CHECK_NULL_RETURN(curContainer, nullptr);
140 return curContainer->GetTaskExecutor();
141 }
142
CurrentTaskExecutorSafely()143 RefPtr<TaskExecutor> Container::CurrentTaskExecutorSafely()
144 {
145 auto curContainer = CurrentSafely();
146 CHECK_NULL_RETURN(curContainer, nullptr);
147 return curContainer->GetTaskExecutor();
148 }
149
CurrentTaskExecutorSafelyWithCheck()150 RefPtr<TaskExecutor> Container::CurrentTaskExecutorSafelyWithCheck()
151 {
152 auto curContainer = CurrentSafelyWithCheck();
153 CHECK_NULL_RETURN(curContainer, nullptr);
154 return curContainer->GetTaskExecutor();
155 }
156
UpdateCurrent(int32_t id)157 void Container::UpdateCurrent(int32_t id)
158 {
159 ContainerScope::UpdateCurrent(id);
160 }
161
UpdateState(const Frontend::State& state)162 bool Container::UpdateState(const Frontend::State& state)
163 {
164 std::lock_guard<std::mutex> lock(stateMutex_);
165 if (state_ == state) {
166 return false;
167 }
168 state_ = state;
169 return true;
170 }
171
Dump(const std::vector<std::string>& params, std::vector<std::string>& info)172 bool Container::Dump(const std::vector<std::string>& params, std::vector<std::string>& info)
173 {
174 std::string tip("container not support, type:");
175 tip.append(AceType::TypeName(this));
176 info.emplace_back(tip);
177 return true;
178 }
179
IsIdAvailable(int32_t id)180 bool Container::IsIdAvailable(int32_t id)
181 {
182 return !AceEngine::Get().GetContainer(id);
183 }
184
SetFontScale(int32_t instanceId, float fontScale)185 void Container::SetFontScale(int32_t instanceId, float fontScale)
186 {
187 auto container = AceEngine::Get().GetContainer(instanceId);
188 CHECK_NULL_VOID(container);
189 ContainerScope scope(instanceId);
190 auto pipelineContext = container->GetPipelineContext();
191 CHECK_NULL_VOID(pipelineContext);
192 pipelineContext->SetFontScale(fontScale);
193 }
194
SetFontWeightScale(int32_t instanceId, float fontWeightScale)195 void Container::SetFontWeightScale(int32_t instanceId, float fontWeightScale)
196 {
197 auto container = AceEngine::Get().GetContainer(instanceId);
198 CHECK_NULL_VOID(container);
199 ContainerScope scope(instanceId);
200 auto pipelineContext = container->GetPipelineContext();
201 CHECK_NULL_VOID(pipelineContext);
202 pipelineContext->SetFontWeightScale(fontWeightScale);
203 }
204
GetDisplayInfo()205 RefPtr<DisplayInfo> Container::GetDisplayInfo()
206 {
207 return DisplayInfoUtils::GetInstance().GetDisplayInfo();
208 }
209
InitIsFoldable()210 void Container::InitIsFoldable()
211 {
212 DisplayInfoUtils::GetInstance().InitIsFoldable();
213 }
214
IsFoldable()215 bool Container::IsFoldable()
216 {
217 return DisplayInfoUtils::GetInstance().IsFoldable();
218 }
219
GetCurrentFoldStatus()220 FoldStatus Container::GetCurrentFoldStatus()
221 {
222 return DisplayInfoUtils::GetInstance().GetCurrentFoldStatus();
223 }
224
DestroyToastSubwindow(int32_t instanceId)225 void Container::DestroyToastSubwindow(int32_t instanceId)
226 {
227 auto subwindow = SubwindowManager::GetInstance()->GetToastSubwindow(instanceId);
228 if (subwindow && subwindow->IsToastSubWindow()) {
229 subwindow->DestroyWindow();
230 }
231 auto systemToastWindow = SubwindowManager::GetInstance()->GetSystemToastWindow(instanceId);
232 if (systemToastWindow && systemToastWindow->IsToastSubWindow()) {
233 systemToastWindow->DestroyWindow();
234 }
235 }
236
IsFontFileExistInPath(const std::string& path)237 bool Container::IsFontFileExistInPath(const std::string& path)
238 {
239 DIR* dir;
240 struct dirent* ent;
241 bool isFlagFileExist = false;
242 bool isFontDirExist = false;
243 if ((dir = opendir(path.c_str())) == nullptr) {
244 if (errno == ENOENT) {
245 LOGE("ERROR ENOENT");
246 } else if (errno == EACCES) {
247 LOGE("ERROR EACCES");
248 } else {
249 LOGE("ERROR Other");
250 }
251 return false;
252 }
253 while ((ent = readdir(dir)) != nullptr) {
254 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
255 continue;
256 }
257 if (strcmp(ent->d_name, "flag") == 0) {
258 isFlagFileExist = true;
259 } else if (strcmp(ent->d_name, "fonts") == 0) {
260 isFontDirExist = true;
261 }
262 }
263 closedir(dir);
264 if (isFlagFileExist && isFontDirExist) {
265 LOGI("font path exist");
266 return true;
267 }
268 return false;
269 }
270
GetFontFamilyName(std::string path)271 std::string Container::GetFontFamilyName(std::string path)
272 {
273 std::string fontFamilyName = "";
274 DIR* dir;
275 struct dirent* ent;
276 if ((dir = opendir(path.c_str())) == nullptr) {
277 return fontFamilyName;
278 }
279 while ((ent = readdir(dir)) != nullptr) {
280 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
281 continue;
282 }
283 if (endsWith(ent->d_name, ".ttf")) {
284 fontFamilyName = ent->d_name;
285 break;
286 }
287 }
288 closedir(dir);
289 return fontFamilyName;
290 }
291
endsWith(std::string str, std::string suffix)292 bool Container::endsWith(std::string str, std::string suffix)
293 {
294 if (str.length() < suffix.length()) {
295 return false;
296 }
297 return str.substr(str.length() - suffix.length()) == suffix;
298 }
299
300 template<>
GenerateId()301 int32_t Container::GenerateId<PLUGIN_SUBCONTAINER>()
302 {
303 #ifdef PLUGIN_COMPONENT_SUPPORTED
304 return PluginManager::GetInstance().GetPluginSubContainerId();
305 #else
306 return INSTANCE_ID_UNDEFINED;
307 #endif
308 }
309
310 } // namespace OHOS::Ace
311