• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "context_transfer.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AbilityRuntime {
GetInstance()22 ContextTransfer &ContextTransfer::GetInstance()
23 {
24     static ContextTransfer contextTransfer;
25     return contextTransfer;
26 }
27 
RegisterStaticObjectCreator(const std::string & contextType,const CreateStaticObject & createFunc)28 void ContextTransfer::RegisterStaticObjectCreator(const std::string &contextType, const CreateStaticObject &createFunc)
29 {
30     TAG_LOGD(AAFwkTag::CONTEXT, "Reg static creator for %{public}s", contextType.c_str());
31     std::lock_guard<std::mutex> lock(creatorMutex_);
32     staticCreators_.emplace(contextType, createFunc);
33 }
34 
GetStaticObject(const std::string & contextType,ani_env * aniEnv,std::shared_ptr<Context> context)35 ani_object ContextTransfer::GetStaticObject(const std::string &contextType, ani_env *aniEnv,
36     std::shared_ptr<Context> context)
37 {
38     TAG_LOGD(AAFwkTag::CONTEXT, "Get static creator for %{public}s", contextType.c_str());
39     std::lock_guard<std::mutex> lock(creatorMutex_);
40     auto it = staticCreators_.find(contextType);
41     if (it != staticCreators_.end()) {
42         return it->second(aniEnv, context);
43     }
44     return nullptr;
45 }
46 
RegisterDynamicObjectCreator(const std::string & contextType,const CreateDynamicObject & createFunc)47 void ContextTransfer::RegisterDynamicObjectCreator(const std::string &contextType,
48     const CreateDynamicObject &createFunc)
49 {
50     TAG_LOGD(AAFwkTag::CONTEXT, "Reg dynamic creator for %{public}s", contextType.c_str());
51     std::lock_guard<std::mutex> lock(creatorMutex_);
52     dynamicCreators_.emplace(contextType, createFunc);
53 }
54 
GetDynamicObject(const std::string & contextType,napi_env napiEnv,std::shared_ptr<Context> context)55 napi_value ContextTransfer::GetDynamicObject(const std::string &contextType, napi_env napiEnv,
56     std::shared_ptr<Context> context)
57 {
58     TAG_LOGD(AAFwkTag::CONTEXT, "Get dynamic creator for %{public}s", contextType.c_str());
59     std::lock_guard<std::mutex> lock(creatorMutex_);
60     auto it = dynamicCreators_.find(contextType);
61     if (it != dynamicCreators_.end()) {
62         return it->second(napiEnv, context);
63     }
64     return nullptr;
65 }
66 
IsStaticCreatorExist(const std::string & contextType)67 bool ContextTransfer::IsStaticCreatorExist(const std::string &contextType)
68 {
69     std::lock_guard<std::mutex> lock(creatorMutex_);
70     auto it = staticCreators_.find(contextType);
71     return it != staticCreators_.end();
72 }
73 
IsDynamicCreatorExist(const std::string & contextType)74 bool ContextTransfer::IsDynamicCreatorExist(const std::string &contextType)
75 {
76     std::lock_guard<std::mutex> lock(creatorMutex_);
77     auto it = dynamicCreators_.find(contextType);
78     return it != dynamicCreators_.end();
79 }
80 } // namespace AbilityRuntime
81 } // namespace OHOS
82