• 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 
18 #include <ace/xcomponent/native_interface_xcomponent.h>
19 #include <cstdint>
20 #include <cstdio>
21 #include <string>
22 
23 
24 #include <hilog/log.h>
25 // native render
26 #include <native_buffer/native_buffer.h>
27 #include <native_window/buffer_handle.h>
28 #include <native_window/external_window.h>
29 
30 #include "common/common.h"
31 
32 #include "xcomponent_manager.h"
33 
34 using namespace std;
35 
36 namespace NativeXComponentSample {
37 PluginManager PluginManager::pluginManager_;
38 AbilityInfo PluginManager::m_abilityInfo;
39 
40 const char *LOG_TAG_RECT = "TestRect";
41 
callMethod(napi_env env,napi_value obj,const std::string & methodName,napi_value * out)42 napi_status callMethod(napi_env env, napi_value obj, const std::string &methodName, napi_value *out)
43 {
44     napi_value func = nullptr;
45     napi_status ret = napi_ok;
46 
47     ret = napi_get_named_property(env, obj, methodName.data(), &func);
48     if (ret != napi_ok)
49         return ret;
50 
51     ret = napi_call_function(env, obj, func, 0, nullptr, out);
52 
53     return ret;
54 }
55 
Init(napi_env env,napi_value uiAbility,napi_value windowStage)56 void AbilityInfo::Init(napi_env env, napi_value uiAbility, napi_value windowStage)
57 {
58     napi_value ohosWindow;
59     napi_status code = callMethod(env, windowStage, "getMainWindowSync", &ohosWindow);
60     if (code != napi_ok) {
61         napi_throw_error(env, nullptr, "call getMainWindowSync failed");
62     }
63     this->env = env;
64 
65     napi_create_reference(env, uiAbility, 1, &this->uiAbility);
66     napi_create_reference(env, windowStage, 1, &this->windowStage);
67     napi_create_reference(env, ohosWindow, 1, &this->window);
68 }
69 
getWindow()70 napi_value AbilityInfo::getWindow()
71 {
72     napi_value value;
73     if (this->window) {
74         napi_status status = napi_get_reference_value(env, window, &value);
75         if (status != napi_ok) {
76             napi_throw_type_error(env, NULL, "getWindow failed");
77         }
78     }
79     return value;
80 }
81 
~AbilityInfo()82 AbilityInfo::~AbilityInfo()
83 {
84     // todo release ref
85 }
86 
87 
88 
setupNativeWindowBufferUsage(::OHNativeWindow * nativeWindow,std::uint64_t usageSetBits)89 void setupNativeWindowBufferUsage(::OHNativeWindow *nativeWindow, std::uint64_t usageSetBits)
90 {
91     std::uint64_t windowBufferUsage = 0;
92     auto getUsageRes =
93         ::OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, ::NativeWindowOperation::GET_USAGE, &windowBufferUsage);
94 
95     std::uint64_t requestedWindowBufferUsage = windowBufferUsage | usageSetBits;
96     if (requestedWindowBufferUsage != windowBufferUsage) {
97         auto setUsageRes = ::OH_NativeWindow_NativeWindowHandleOpt(
98             nativeWindow, ::NativeWindowOperation::SET_USAGE, requestedWindowBufferUsage);
99     }
100 }
101 
PluginManager()102 PluginManager::PluginManager()
103     :xcomponentMgr(new XComponentManager)
104 {
105 }
106 
~PluginManager()107 PluginManager::~PluginManager()
108 {
109     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~PluginManager");
110     delete xcomponentMgr;
111 }
112 
AttachXComponent(const std::string & id,OH_NativeXComponent * nativeXComponent,OHNativeWindow * window)113 void PluginManager::AttachXComponent(const std::string &id,
114     OH_NativeXComponent *nativeXComponent, OHNativeWindow *window)
115 {
116     this->xcomponentMgr->SetNativeXComponent(id, nativeXComponent, window);
117 }
118 
Export(napi_env env,napi_value exports)119 void PluginManager::Export(napi_env env, napi_value exports)
120 {
121     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: start execute");
122     if ((env == nullptr) || (exports == nullptr)) {
123         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: env or exports is null");
124         return;
125     }
126 
127     napi_value exportInstance = nullptr;
128     if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
129         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: napi_get_named_property fail");
130         return;
131     }
132 
133     OH_NativeXComponent *nativeXComponent = nullptr;
134     if (napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)) != napi_ok) {
135         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: napi_unwrap fail");
136         return;
137     }
138 
139     char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};
140     uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
141     if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
142         OH_LOG_Print(
143             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: OH_NativeXComponent_GetXComponentId fail");
144         return;
145     }
146 
147     this->xcomponentMgr->RegisterCallback(nativeXComponent);
148 }
149 
150 }  // namespace NativeXComponentSample