• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::CurrentIdSafely();
38     if (argc > 0) {
39         const auto& obj = argv[0];
40         if (obj && obj->IsObject(runtime) && obj->HasProperty(runtime, "getInstanceId")) {
41             auto getIdFunc = obj->GetProperty(runtime, "getInstanceId");
42             auto retId = getIdFunc->Call(runtime, obj, {}, 0);
43             if (retId->IsInt32(runtime)) {
44                 currentInstance = retId->ToInt32(runtime);
45             }
46         }
47     }
48 #ifdef PLUGIN_COMPONENT_SUPPORTED
49     if (currentInstance >= MIN_PLUGIN_SUBCONTAINER_ID) {
50         currentInstance = PluginManager::GetInstance().GetPluginParentContainerId(currentInstance);
51     }
52 #endif
53     auto it = contexts_.find(currentInstance);
54     if (it != contexts_.end()) {
55         return it->second;
56     }
57 
58     // Try to get the active container.
59     auto container = Container::GetActive();
60     if (container) {
61         currentInstance = container->GetInstanceId();
62         it = contexts_.find(currentInstance);
63         if (it != contexts_.end()) {
64             return it->second;
65         }
66     }
67 
68     return runtime->NewUndefined();
69 }
70 
InitContextModule(const std::shared_ptr<JsRuntime> & runtime,std::shared_ptr<JsValue> moduleObj)71 void JsiContextModule::InitContextModule(const std::shared_ptr<JsRuntime>& runtime, std::shared_ptr<JsValue> moduleObj)
72 {
73 #ifndef PREVIEW
74     moduleObj->SetProperty(runtime, "getContext", runtime->NewFunction(JsiContextModule::GetContext));
75 #endif
76 }
77 
AddContext(int32_t key,const std::shared_ptr<JsValue> & value)78 void JsiContextModule::AddContext(int32_t key, const std::shared_ptr<JsValue>& value)
79 {
80     if (contexts_.find(key) != contexts_.end()) {
81         LOGW("Context exists for key %d", key);
82         return;
83     }
84     contexts_.emplace(key, value);
85 }
86 
RemoveContext(int32_t key)87 void JsiContextModule::RemoveContext(int32_t key)
88 {
89     auto it = contexts_.find(key);
90     if (it != contexts_.end()) {
91         contexts_.erase(it);
92     }
93 }
94 
95 } // namespace OHOS::Ace::Framework
96