1 /*
2 * Copyright (c) 2022-2023 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 "bridge/common/utils/engine_helper.h"
17
18 #include "base/log/ace_checker.h"
19 #include "base/subwindow/subwindow_manager.h"
20
21 namespace OHOS::Ace {
22 std::shared_mutex EngineHelper::mutex_;
23 std::unordered_map<int32_t, WeakPtr<Framework::JsEngine>> EngineHelper::engineWeakMap_;
24
25 std::function<void(int32_t)> EngineHelper::removeUIContextFunc_;
26
ScopedDelegate(const RefPtr<Framework::FrontendDelegate> & delegate,int32_t id)27 ScopedDelegate::ScopedDelegate(const RefPtr<Framework::FrontendDelegate>& delegate, int32_t id)
28 : delegate_(delegate), scope_(new ContainerScope(id))
29 {}
30
~ScopedDelegate()31 ScopedDelegate::~ScopedDelegate()
32 {
33 delete scope_;
34 scope_ = nullptr;
35 }
36
AddEngine(int32_t id,WeakPtr<Framework::JsEngine> engine)37 void EngineHelper::AddEngine(int32_t id, WeakPtr<Framework::JsEngine> engine)
38 {
39 std::unique_lock<std::shared_mutex> lock(mutex_);
40 engineWeakMap_.emplace(id, engine);
41 }
42
GetEngine(int32_t id)43 RefPtr<Framework::JsEngine> EngineHelper::GetEngine(int32_t id)
44 {
45 std::shared_lock<std::shared_mutex> lock(mutex_);
46 if (id >= MIN_SUBCONTAINER_ID && id < MIN_PLUGIN_SUBCONTAINER_ID) {
47 id = SubwindowManager::GetInstance()->GetParentContainerId(id);
48 }
49 auto iter = engineWeakMap_.find(id);
50 if (iter != engineWeakMap_.end()) {
51 return iter->second.Upgrade();
52 }
53 return nullptr;
54 }
55
RemoveEngine(int32_t id)56 void EngineHelper::RemoveEngine(int32_t id)
57 {
58 std::unique_lock<std::shared_mutex> lock(mutex_);
59 engineWeakMap_.erase(id);
60
61 if (removeUIContextFunc_) {
62 removeUIContextFunc_(id);
63 }
64 }
65
GetCurrentEngine()66 RefPtr<Framework::JsEngine> EngineHelper::GetCurrentEngine()
67 {
68 return GetEngine(Container::CurrentId());
69 }
70
GetCurrentEngineSafely()71 RefPtr<Framework::JsEngine> EngineHelper::GetCurrentEngineSafely()
72 {
73 return GetEngine(Container::CurrentIdSafely());
74 }
75
GetCurrentDelegate()76 ScopedDelegate EngineHelper::GetCurrentDelegate()
77 {
78 auto engine = GetCurrentEngine();
79 if (engine) {
80 return { engine->GetFrontend(), Container::CurrentId() };
81 }
82 auto container = Container::GetActive();
83 return GetDelegateByContainer(container);
84 }
85
GetCurrentDelegateSafely()86 ScopedDelegate EngineHelper::GetCurrentDelegateSafely()
87 {
88 auto engine = GetCurrentEngineSafely();
89 if (engine) { // AddEngine will be called in AceContainer::InitializeFrontend
90 return { engine->GetFrontend(), Container::CurrentIdSafely() };
91 }
92 auto container = Container::CurrentSafely();
93 return GetDelegateByContainer(container);
94 }
95
GetDefaultDelegate()96 ScopedDelegate EngineHelper::GetDefaultDelegate()
97 {
98 auto engine = GetCurrentEngine();
99 if (engine) {
100 return { engine->GetFrontend(), Container::CurrentId() };
101 }
102 auto container = Container::GetDefault();
103 return GetDelegateByContainer(container);
104 }
105
GetDelegateByContainer(RefPtr<Container> container)106 ScopedDelegate EngineHelper::GetDelegateByContainer(RefPtr<Container> container)
107 {
108 if (!container) {
109 return { nullptr, -1 };
110 }
111 auto engine = GetEngine(container->GetInstanceId());
112 return { engine ? engine->GetFrontend() : nullptr, container->GetInstanceId() };
113 }
114
GetPositionOnJsCode()115 std::tuple<std::string, int32_t, int32_t> EngineHelper::GetPositionOnJsCode()
116 {
117 if (!AceChecker::IsPerformanceCheckEnabled()) {
118 return {"", 0, 0 };
119 }
120 auto jsEngine = GetCurrentEngine();
121 std::string stack;
122 CHECK_NULL_RETURN(jsEngine, std::make_tuple("", 0, 0));
123 jsEngine->GetStackTrace(stack);
124 std::regex reg("([^|]+)\\|([^|]+)\\|[^|]+\\|([^:]+):(\\d+):(\\d+)");
125 std::smatch match;
126 if (std::regex_search(stack, match, reg)) {
127 std::string filePath = match[2].str() + "/" + match[3].str();
128 return {
129 filePath,
130 StringUtils::StringToInt(match[4].str()),
131 StringUtils::StringToInt(match[5].str())
132 };
133 }
134 return {"", 0, 0 };
135 }
136
RegisterRemoveUIContextFunc(const std::function<void (int32_t)> & removeUIContextFunc)137 void EngineHelper::RegisterRemoveUIContextFunc(const std::function<void(int32_t)>& removeUIContextFunc)
138 {
139 if (!removeUIContextFunc_) {
140 removeUIContextFunc_ = removeUIContextFunc;
141 }
142 }
143 } // namespace OHOS::Ace
144