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 <ace/xcomponent/native_interface_xcomponent.h>
17 #include <cstdint>
18 #include <hilog/log.h>
19 #include <string>
20 #include "common/log_common.h"
21 #include "plugin_manager.h"
22
GetInstance()23 PluginManager *PluginManager::GetInstance()
24 {
25 static PluginManager pluginManager;
26 return &pluginManager;
27 }
28
~PluginManager()29 PluginManager::~PluginManager()
30 {
31 DRAWING_LOGI("~PluginManager");
32 for (auto iter = nativeXComponentMap_.begin(); iter != nativeXComponentMap_.end(); ++iter) {
33 if (iter->second != nullptr) {
34 delete iter->second;
35 iter->second = nullptr;
36 }
37 }
38 nativeXComponentMap_.clear();
39
40 for (auto iter = pluginRenderMap_.begin(); iter != pluginRenderMap_.end(); ++iter) {
41 if (iter->second != nullptr) {
42 delete iter->second;
43 iter->second = nullptr;
44 }
45 }
46 pluginRenderMap_.clear();
47 }
48
Export(napi_env env,napi_value exports)49 void PluginManager::Export(napi_env env, napi_value exports)
50 {
51 if ((env == nullptr) || (exports == nullptr)) {
52 DRAWING_LOGE("Export: env or exports is null");
53 return;
54 }
55
56 napi_value exportInstance = nullptr;
57 if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
58 DRAWING_LOGE("Export: napi_get_named_property fail");
59 return;
60 }
61
62 OH_NativeXComponent *nativeXComponent = nullptr;
63 if (napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)) != napi_ok) {
64 DRAWING_LOGE("Export: napi_unwrap fail");
65 return;
66 }
67
68 char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};
69 uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
70 if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
71 DRAWING_LOGE("Export: OH_NativeXComponent_GetXComponentId fail");
72 return;
73 }
74
75 std::string id(idStr);
76 auto context = PluginManager::GetInstance();
77 if ((context != nullptr) && (nativeXComponent != nullptr)) {
78 context->SetNativeXComponent(id, nativeXComponent);
79 auto render = context->GetRender(id);
80 if (render != nullptr) {
81 render->RegisterCallback(nativeXComponent);
82 render->Export(env, exports);
83 } else {
84 DRAWING_LOGE("render is nullptr");
85 }
86 }
87 }
88
SetNativeXComponent(std::string & id,OH_NativeXComponent * nativeXComponent)89 void PluginManager::SetNativeXComponent(std::string &id, OH_NativeXComponent *nativeXComponent)
90 {
91 DRAWING_LOGI("set native xComponent, ID = %{public}s.", id.c_str());
92 if (nativeXComponent == nullptr) {
93 DRAWING_LOGE("xcomponent null");
94 return;
95 }
96
97 if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) {
98 nativeXComponentMap_[id] = nativeXComponent;
99 return;
100 }
101
102 if (nativeXComponentMap_[id] != nativeXComponent) {
103 OH_NativeXComponent *tmp = nativeXComponentMap_[id];
104 delete tmp;
105 tmp = nullptr;
106 nativeXComponentMap_[id] = nativeXComponent;
107 }
108 }
109
GetRender(std::string & id)110 SampleBitMap *PluginManager::GetRender(std::string &id)
111 {
112 if (pluginRenderMap_.find(id) == pluginRenderMap_.end()) {
113 SampleBitMap *instance = SampleBitMap::GetInstance(id);
114 pluginRenderMap_[id] = instance;
115 return instance;
116 }
117 return pluginRenderMap_[id];
118 }
119
ReleaseRender(std::string & id)120 void PluginManager::ReleaseRender(std::string &id)
121 {
122 auto map = pluginRenderMap_.find(id);
123 if (map == pluginRenderMap_.end()) {
124 return;
125 }
126 pluginRenderMap_.erase(map);
127 }
128