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