• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include "plugin_manager.h"
17 #include <ace/xcomponent/native_interface_xcomponent.h>
18 #include <cstdint>
19 #include <cstdio>
20 #include <hilog/log.h>
21 #include <string>
22 
23 #include "../common/common.h"
24 
25 namespace NativeXComponentSample {
26 PluginManager PluginManager::pluginManager_;
27 
~PluginManager()28 PluginManager::~PluginManager()
29 {
30     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~PluginManager");
31     for (auto iter = nativeXComponentMap_.begin(); iter != nativeXComponentMap_.end(); ++iter) {
32         if (iter->second != nullptr) {
33             delete iter->second;
34             iter->second = nullptr;
35         }
36     }
37     nativeXComponentMap_.clear();
38 
39     for (auto iter = pluginRenderMap_.begin(); iter != pluginRenderMap_.end(); ++iter) {
40         if (iter->second != nullptr) {
41             delete iter->second;
42             iter->second = nullptr;
43         }
44     }
45     pluginRenderMap_.clear();
46 }
47 
GetContext(napi_env env,napi_callback_info info)48 napi_value PluginManager::GetContext(napi_env env, napi_callback_info info)
49 {
50     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "PluginManager", "GetContext in");
51     if ((env == nullptr) || (info == nullptr)) {
52         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "GetContext env or info is null");
53         return nullptr;
54     }
55 
56     size_t argCnt = 1;
57     napi_value args[1] = { nullptr };
58     if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
59         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "GetContext napi_get_cb_info failed");
60     }
61 
62     if (argCnt != 1) {
63         napi_throw_type_error(env, NULL, "Wrong number of arguments");
64         return nullptr;
65     }
66 
67     napi_valuetype valuetype;
68     if (napi_typeof(env, args[0], &valuetype) != napi_ok) {
69         napi_throw_type_error(env, NULL, "napi_typeof failed");
70         return nullptr;
71     }
72 
73     if (valuetype != napi_number) {
74         napi_throw_type_error(env, NULL, "Wrong type of arguments");
75         return nullptr;
76     }
77 
78     int64_t value;
79     if (napi_get_value_int64(env, args[0], &value) != napi_ok) {
80         napi_throw_type_error(env, NULL, "napi_get_value_int64 failed");
81         return nullptr;
82     }
83 
84     napi_value exports;
85     if (napi_create_object(env, &exports) != napi_ok) {
86         napi_throw_type_error(env, NULL, "napi_create_object failed");
87         return nullptr;
88     }
89 
90     return exports;
91 }
92 
Export(napi_env env,napi_value exports)93 void PluginManager::Export(napi_env env, napi_value exports)
94 {
95     if ((env == nullptr) || (exports == nullptr)) {
96         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: env or exports is null");
97         return;
98     }
99 
100     napi_value exportInstance = nullptr;
101     if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
102         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: napi_get_named_property fail");
103         return;
104     }
105 
106     OH_NativeXComponent* nativeXComponent = nullptr;
107     if (napi_unwrap(env, exportInstance, reinterpret_cast<void**>(&nativeXComponent)) != napi_ok) {
108         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: napi_unwrap fail");
109         return;
110     }
111 
112     char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
113     uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
114     if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
115         OH_LOG_Print(
116             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: OH_NativeXComponent_GetXComponentId fail");
117         return;
118     }
119 
120     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "PluginManager", "idStr: %{public}s", idStr);
121 
122     std::string id(idStr);
123     auto context = PluginManager::GetInstance();
124     if ((context != nullptr) && (nativeXComponent != nullptr)) {
125         context->SetNativeXComponent(id, nativeXComponent);
126         auto render = context->GetRender(id);
127         if (render != nullptr) {
128             render->RegisterCallback(nativeXComponent);
129             render->Export(env, exports);
130         }
131     }
132 }
133 
SetNativeXComponent(std::string & id,OH_NativeXComponent * nativeXComponent)134 void PluginManager::SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent)
135 {
136     if (nativeXComponent == nullptr) {
137         return;
138     }
139 
140     if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) {
141         nativeXComponentMap_[id] = nativeXComponent;
142         return;
143     }
144 
145     if (nativeXComponentMap_[id] != nativeXComponent) {
146         OH_NativeXComponent* tmp = nativeXComponentMap_[id];
147         delete tmp;
148         tmp = nullptr;
149         nativeXComponentMap_[id] = nativeXComponent;
150     }
151 }
152 
GetRender(std::string & id)153 PluginRender* PluginManager::GetRender(std::string& id)
154 {
155     if (pluginRenderMap_.find(id) == pluginRenderMap_.end()) {
156         PluginRender* instance = PluginRender::GetInstance(id);
157         pluginRenderMap_[id] = instance;
158         return instance;
159     }
160 
161     return pluginRenderMap_[id];
162 }
163 } // namespace NativeXComponentSample
164