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 #include "frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_context_module.h"
16
17 #include "base/log/log.h"
18 #include "base/subwindow/subwindow_manager.h"
19 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
20 #include "frameworks/core/common/container.h"
21
22 #ifdef PLUGIN_COMPONENT_SUPPORTED
23 #include "core/common/plugin_manager.h"
24 #endif
25 namespace OHOS::Ace::Framework {
26
27 thread_local std::unordered_map<int32_t, std::shared_ptr<JsValue>> JsiContextModule::contexts_;
28
GetInstance()29 JsiContextModule* JsiContextModule::GetInstance()
30 {
31 static JsiContextModule instance;
32 return &instance;
33 }
34
GetInstanceIdByThis(const std::shared_ptr<JsRuntime> & runtime,const std::vector<std::shared_ptr<JsValue>> & argv,int32_t argc)35 int32_t JsiContextModule::GetInstanceIdByThis(
36 const std::shared_ptr<JsRuntime>& runtime, const std::vector<std::shared_ptr<JsValue>>& argv, int32_t argc)
37 {
38 if (argc <= 0) {
39 return INSTANCE_ID_UNDEFINED;
40 }
41 const auto& obj = argv[0];
42 if (!obj || !obj->IsObject(runtime) || !obj->HasProperty(runtime, "getInstanceId")) {
43 return INSTANCE_ID_UNDEFINED;
44 }
45 auto getIdFunc = obj->GetProperty(runtime, "getInstanceId");
46 auto retId = getIdFunc->Call(runtime, obj, {}, 0);
47 if (!retId->IsInt32(runtime)) {
48 return INSTANCE_ID_UNDEFINED;
49 }
50 return retId->ToInt32(runtime);
51 }
52
GetContext(const std::shared_ptr<JsRuntime> & runtime,const std::shared_ptr<JsValue> & thisObj,const std::vector<std::shared_ptr<JsValue>> & argv,int32_t argc)53 std::shared_ptr<JsValue> JsiContextModule::GetContext(const std::shared_ptr<JsRuntime>& runtime,
54 const std::shared_ptr<JsValue>& thisObj, const std::vector<std::shared_ptr<JsValue>>& argv, int32_t argc)
55 {
56 int32_t currentInstance = GetInstanceIdByThis(runtime, argv, argc);
57 if (currentInstance < 0) {
58 currentInstance = Container::CurrentIdSafely();
59 }
60
61 #ifdef PLUGIN_COMPONENT_SUPPORTED
62 if (currentInstance >= MIN_PLUGIN_SUBCONTAINER_ID) {
63 currentInstance = PluginManager::GetInstance().GetPluginParentContainerId(currentInstance);
64 }
65 #endif
66
67 if (currentInstance >= MIN_SUBCONTAINER_ID && currentInstance < MIN_PLUGIN_SUBCONTAINER_ID) {
68 currentInstance = SubwindowManager::GetInstance()->GetParentContainerId(currentInstance);
69 }
70
71 auto it = contexts_.find(currentInstance);
72 if (it != contexts_.end()) {
73 return it->second;
74 }
75 int32_t currentInstanceBak = currentInstance;
76
77 // Try to get the active container.
78 auto container = Container::GetActive();
79 if (container) {
80 currentInstance = container->GetInstanceId();
81 it = contexts_.find(currentInstance);
82 if (it != contexts_.end()) {
83 return it->second;
84 } else {
85 TAG_LOGW(AceLogTag::ACE_DEFAULT_DOMAIN, "Context not found, id:%{public}d, active:%{public}d",
86 currentInstanceBak, currentInstance);
87 }
88 }
89
90 return runtime->NewUndefined();
91 }
92
InitContextModule(const std::shared_ptr<JsRuntime> & runtime,std::shared_ptr<JsValue> moduleObj)93 void JsiContextModule::InitContextModule(const std::shared_ptr<JsRuntime>& runtime, std::shared_ptr<JsValue> moduleObj)
94 {
95 #ifndef PREVIEW
96 moduleObj->SetProperty(runtime, "getContext", runtime->NewFunction(JsiContextModule::GetContext));
97 #endif
98 }
99
AddContext(int32_t key,const std::shared_ptr<JsValue> & value)100 void JsiContextModule::AddContext(int32_t key, const std::shared_ptr<JsValue>& value)
101 {
102 if (contexts_.find(key) != contexts_.end()) {
103 LOGW("Context exists for key %d", key);
104 return;
105 }
106 contexts_.emplace(key, value);
107 }
108
RemoveContext(int32_t key)109 void JsiContextModule::RemoveContext(int32_t key)
110 {
111 auto it = contexts_.find(key);
112 if (it != contexts_.end()) {
113 contexts_.erase(it);
114 }
115 }
116
117 } // namespace OHOS::Ace::Framework
118