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
GetCurrentDelegate()72 ScopedDelegate EngineHelper::GetCurrentDelegate()
73 {
74 auto engine = GetCurrentEngine();
75 if (engine) {
76 return { engine->GetFrontend(), Container::CurrentId() };
77 }
78 LOGW("Can't find current engine, using active one");
79 auto container = Container::GetActive();
80 if (!container) {
81 return { nullptr, -1 };
82 }
83 engine = GetEngine(container->GetInstanceId());
84 return { engine ? engine->GetFrontend() : nullptr, container->GetInstanceId() };
85 }
86
StringToPair(const std::string & match)87 std::pair<int32_t, int32_t> EngineHelper::StringToPair(const std::string& match)
88 {
89 std::vector<std::string> arr;
90 std::pair<int32_t, int32_t> res;
91 StringUtils::SplitStr(match, ":", arr);
92 res.first = StringUtils::StringToInt(arr[0]);
93 res.second = StringUtils::StringToInt(arr[1]);
94 return res;
95 }
96
GetPositionOnJsCode()97 std::pair<int32_t, int32_t> EngineHelper::GetPositionOnJsCode()
98 {
99 if (!AceChecker::IsPerformanceCheckEnabled()) {
100 return { 0, 0 };
101 }
102 auto jsEngine = GetCurrentEngine();
103 std::string stack;
104 CHECK_NULL_RETURN(jsEngine, std::make_pair(0, 0));
105 jsEngine->GetStackTrace(stack);
106 std::regex reg("\\d+:\\d+");
107 std::smatch match;
108 if (std::regex_search(stack, match, reg)) {
109 return StringToPair(match[0].str());
110 }
111 return { 0, 0 };
112 }
113 } // namespace OHOS::Ace
114