• 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 #ifndef OHOS_ABILITY_RUNTIME_CONTEXT_TRANSFER_H
17 #define OHOS_ABILITY_RUNTIME_CONTEXT_TRANSFER_H
18 
19 #include <memory>
20 
21 #include "ani.h"
22 #include "context.h"
23 #include "native_engine/native_engine.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using CreateStaticObject = std::function<ani_object (ani_env *aniEnv, std::shared_ptr<Context> context)>;
28 using CreateDynamicObject = std::function<napi_value (napi_env napiEnv, std::shared_ptr<Context> context)>;
29 class ContextTransfer {
30 public:
31     static ContextTransfer &GetInstance();
32     ContextTransfer() = default;
33     ~ContextTransfer() = default;
34 
35     void RegisterStaticObjectCreator(const std::string &contextType, const CreateStaticObject &createFunc);
36     ani_object GetStaticObject(const std::string &contextType, ani_env *aniEnv, std::shared_ptr<Context> context);
37 
38     void RegisterDynamicObjectCreator(const std::string &contextType, const CreateDynamicObject &createFunc);
39     napi_value GetDynamicObject(const std::string &contextType, napi_env napiEnv, std::shared_ptr<Context> context);
40 
41     bool IsStaticCreatorExist(const std::string &contextType);
42     bool IsDynamicCreatorExist(const std::string &contextType);
43 
44 private:
45     ContextTransfer(const ContextTransfer&) = delete;
46     ContextTransfer(ContextTransfer&&) = delete;
47     ContextTransfer& operator=(const ContextTransfer&) = delete;
48     ContextTransfer& operator=(ContextTransfer&&) = delete;
49 
50     std::mutex creatorMutex_;
51     std::unordered_map<std::string, CreateStaticObject> staticCreators_;
52     std::unordered_map<std::string, CreateDynamicObject> dynamicCreators_;
53 };
54 } // namespace AbilityRuntime
55 } // namespace OHOS
56 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_TRANSFER_H
57