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 "frameworks/bridge/declarative_frontend/engine/bindings.h"
19 #include "frameworks/core/common/container.h"
20
21 #ifdef PLUGIN_COMPONENT_SUPPORTED
22 #include "core/common/plugin_manager.h"
23 #endif
24 namespace OHOS::Ace::Framework {
25
26 thread_local std::unordered_map<int32_t, std::shared_ptr<JsValue>> JsiContextModule::contexts_;
27
GetInstance()28 JsiContextModule* JsiContextModule::GetInstance()
29 {
30 static JsiContextModule instance;
31 return &instance;
32 }
33
GetContext(const std::shared_ptr<JsRuntime> & runtime,const std::shared_ptr<JsValue> & thisObj,const std::vector<std::shared_ptr<JsValue>> & argv,int32_t argc)34 std::shared_ptr<JsValue> JsiContextModule::GetContext(const std::shared_ptr<JsRuntime>& runtime,
35 const std::shared_ptr<JsValue>& thisObj, const std::vector<std::shared_ptr<JsValue>>& argv, int32_t argc)
36 {
37 int32_t currentInstance = Container::CurrentId();
38 if (thisObj && thisObj->IsObject(runtime) && thisObj->HasProperty(runtime, "getInstanceId")) {
39 auto getIdFunc = thisObj->GetProperty(runtime, "getInstanceId");
40 auto retId = getIdFunc->Call(runtime, thisObj, {}, 0);
41 if (retId->IsInt32(runtime)) {
42 currentInstance = retId->ToInt32(runtime);
43 }
44 }
45 #ifdef PLUGIN_COMPONENT_SUPPORTED
46 if (currentInstance >= MIN_PLUGIN_SUBCONTAINER_ID) {
47 currentInstance = PluginManager::GetInstance().GetPluginParentContainerId(currentInstance);
48 }
49 #endif
50 LOGD("Current ID is %{public}d", currentInstance);
51 auto it = contexts_.find(currentInstance);
52 if (it == contexts_.end()) {
53 LOGW("Context with ID %{public}d not found!", currentInstance);
54 return runtime->NewUndefined();
55 }
56 return it->second;
57 }
58
InitContextModule(const std::shared_ptr<JsRuntime> & runtime,std::shared_ptr<JsValue> moduleObj)59 void JsiContextModule::InitContextModule(const std::shared_ptr<JsRuntime>& runtime, std::shared_ptr<JsValue> moduleObj)
60 {
61 #ifndef PREVIEW
62 moduleObj->SetProperty(runtime, "getContext", runtime->NewFunction(JsiContextModule::GetContext));
63 #endif
64 }
65
AddContext(int32_t key,const std::shared_ptr<JsValue> & value)66 void JsiContextModule::AddContext(int32_t key, const std::shared_ptr<JsValue>& value)
67 {
68 if (contexts_.find(key) != contexts_.end()) {
69 LOGW("Context exists for key %d", key);
70 return;
71 }
72 contexts_.emplace(key, value);
73 }
74
RemoveContext(int32_t key)75 void JsiContextModule::RemoveContext(int32_t key)
76 {
77 auto it = contexts_.find(key);
78 if (it != contexts_.end()) {
79 contexts_.erase(it);
80 } else {
81 LOGW("Context with key %{public}d does not exist!", key);
82 }
83 }
84
85 } // namespace OHOS::Ace::Framework
86