plugin ability、remote_message 整改 and bugfix

Signed-off-by: Yao yuchi <yaoyuchi@huawei.com>
This commit is contained in:
Yao yuchi
2022-01-28 14:52:29 +08:00
parent 8982d9ff95
commit b42702f3e2
25 changed files with 398 additions and 166 deletions
+1
View File
@@ -33,6 +33,7 @@ template("ace_ohos_standard_source_set") {
"$ace_root/adapter/ohos/entrance/file_asset_provider.cpp",
"$ace_root/adapter/ohos/entrance/flutter_ace_view.cpp",
"$ace_root/adapter/ohos/entrance/pa_container.cpp",
"$ace_root/adapter/ohos/entrance/plugin_utils_impl.cpp",
# innerkits impl
"$ace_root/adapter/ohos/entrance/ui_content_impl.cpp",
+5 -1
View File
@@ -32,12 +32,14 @@
#include "adapter/ohos/entrance/ace_container.h"
#include "adapter/ohos/entrance/capability_registry.h"
#include "adapter/ohos/entrance/flutter_ace_view.h"
#include "adapter/ohos/entrance/plugin_utils_impl.h"
#include "adapter/ohos/entrance/utils.h"
#include "base/log/log.h"
#include "base/utils/system_properties.h"
#include "core/common/container_scope.h"
#include "core/common/frontend.h"
#include "core/common/plugin_manager.h"
#include "core/common/plugin_utils.h"
namespace OHOS {
namespace Ace {
@@ -210,6 +212,9 @@ void AceAbility::OnStart(const Want& want)
AceApplicationInfo::GetInstance().SetDebug(appInfo->debug, want.GetBoolParam("debugApp", false));
auto pluginUtils = std::make_shared<PluginUtilsImpl>();
PluginManager::GetInstance().SetAceAbility(this, pluginUtils);
// create container
Platform::AceContainer::CreateContainer(abilityId_, frontendType, isArkApp, srcPath, this,
std::make_unique<AcePlatformEventCallback>([this]() { TerminateAbility(); }));
@@ -224,7 +229,6 @@ void AceAbility::OnStart(const Want& want)
container->SetResourceConfiguration(aceResCfg);
container->SetPackagePathStr(resPath);
}
PluginManager::GetInstance().SetAceAbility(this);
// create view.
auto flutterAceView = Platform::FlutterAceView::CreateView(abilityId_);
OHOS::sptr<OHOS::Rosen::Window> window = Ability::GetWindow();
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "adapter/ohos/entrance/plugin_utils_impl.h"
#include "ability_manager_client.h"
namespace OHOS::Ace {
int32_t PluginUtilsImpl::StartAbility(
const std::string& bundleName, const std::string& abilityName, const std::string& params)
{
AAFwk::Want want;
AppExecFwk::ElementName element("", bundleName, abilityName);
want.SetElement(element);
want.SetParam("params", params);
int32_t error = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
return error;
}
}
+32
View File
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_ACE_PLUGIN_UTILS_IMPL_H
#define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_ACE_PLUGIN_UTILS_IMPL_H
#include <string>
#include "core/common/plugin_utils.h"
namespace OHOS::Ace {
class PluginUtilsImpl final : public PluginUtils {
public:
PluginUtilsImpl() = default;
~PluginUtilsImpl() = default;
int32_t StartAbility(
const std::string& bundleName, const std::string& abilityName, const std::string& params) override;
};
} // namespace OHOS::Ace
#endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_ACE_PLUGIN_UTILS_IMPL_H
@@ -34,11 +34,13 @@
#include "adapter/ohos/entrance/capability_registry.h"
#include "adapter/ohos/entrance/file_asset_provider.h"
#include "adapter/ohos/entrance/flutter_ace_view.h"
#include "adapter/ohos/entrance/plugin_utils_impl.h"
#include "base/log/log.h"
#include "base/utils/system_properties.h"
#include "core/common/ace_engine.h"
#include "core/common/container_scope.h"
#include "core/common/flutter/flutter_asset_manager.h"
#include "core/common/plugin_manager.h"
namespace OHOS::Ace {
namespace {
@@ -252,6 +254,8 @@ void UIContentImpl::CommonInitialize(OHOS::Rosen::Window* window, const std::str
}
}
}
auto pluginUtils = std::make_shared<PluginUtilsImpl>();
PluginManager::GetInstance().SetAceAbility(nullptr, pluginUtils);
// create container
instanceId_ = gInstanceId.fetch_add(1, std::memory_order_relaxed);
auto container = AceType::MakeRefPtr<Platform::AceContainer>(instanceId_, FrontendType::DECLARATIVE_JS, true,
@@ -268,18 +268,9 @@ template("declarative_js_engine") {
if (defined(config.plugin_components_support) &&
config.plugin_components_support) {
if (!use_mingw_win && !use_mac) {
deps = [
"$ace_flutter_engine_root:third_party_flutter_engine_$platform",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
]
external_deps = [
"ability_runtime:ability_manager",
"ability_runtime:want",
]
sources += [ "jsview/js_plugin.cpp" ]
}
deps +=
[ "$ace_flutter_engine_root:third_party_flutter_engine_$platform" ]
sources += [ "jsview/js_plugin.cpp" ]
}
if (defined(config.use_build_in_js_engine) &&
@@ -309,7 +300,7 @@ template("declarative_js_engine") {
if (defined(config.plugin_components_support) &&
config.plugin_components_support) {
if (!use_mingw_win && !use_mac) {
external_deps += [
external_deps = [
"multimedia_image_standard:image",
"multimedia_image_standard:image_native",
]
@@ -92,6 +92,8 @@
#include "frameworks/bridge/declarative_frontend/jsview/js_toggle.h"
#if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM)
#include "frameworks/bridge/declarative_frontend/jsview/js_qrcode.h"
#endif
#ifdef PLUGIN_COMPONENT_SUPPORTED
#include "frameworks/bridge/declarative_frontend/jsview/js_plugin.h"
#endif
#include "frameworks/bridge/declarative_frontend/jsview/js_offscreen_rendering_context.h"
@@ -187,6 +189,12 @@ panda::Local<panda::JSValueRef> JsLoadDocument(panda::EcmaVM* vm, panda::Local<p
page->SetDeclarativeOnBackPressCallback([view]() { return view->FireOnBackPress(); });
page->SetDeclarativeOnPageRefreshCallback([view]() { view->MarkNeedUpdate(); });
if (page->IsUsePluginComponent()) {
if (!page->GetPluginComponentJsonData().empty()) {
view->ExecuteUpdateWithValueParams(page->GetPluginComponentJsonData());
}
}
return panda::JSValueRef::Undefined(vm);
}
@@ -818,11 +826,13 @@ static const std::unordered_map<std::string, std::function<void(BindingTarget)>>
{ "SideBarContainer", JSSideBar::JSBind },
#if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM)
{ "QRCode", JSQRCode::JSBind },
{ "PluginComponent", JSPlugin::JSBind },
#ifdef FORM_SUPPORTED
{ "FormComponent", JSForm::JSBind },
#endif
#endif
#ifdef PLUGIN_COMPONENT_SUPPORTED
{ "PluginComponent", JSPlugin::JSBind },
#endif
#if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM)
#ifdef WEB_SUPPORTED
{ "Web", JSWeb::JSBind },
@@ -128,9 +128,11 @@
#if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM)
#include "frameworks/bridge/declarative_frontend/jsview/js_web.h"
#include "frameworks/bridge/declarative_frontend/jsview/js_web_controller.h"
#include "frameworks/bridge/declarative_frontend/jsview/js_plugin.h"
#endif
#endif
#ifdef PLUGIN_COMPONENT_SUPPORTED
#include "frameworks/bridge/declarative_frontend/jsview/js_plugin.h"
#endif
#include "frameworks/bridge/declarative_frontend/jsview/js_view.h"
#include "frameworks/bridge/declarative_frontend/jsview/js_view_context.h"
#include "frameworks/bridge/declarative_frontend/jsview/js_view_register.h"
@@ -17,6 +17,9 @@
#include "base/log/log_wrapper.h"
#include "core/common/container.h"
#ifdef PLUGIN_COMPONENT_SUPPORTED
#include "core/common/plugin_manager.h"
#endif
#include "core/components/gesture_listener/gesture_listener_component.h"
#include "core/gestures/click_recognizer.h"
#include "core/pipeline/base/single_child.h"
@@ -27,9 +30,6 @@
#include "frameworks/bridge/declarative_frontend/jsview/js_pan_handler.h"
#include "frameworks/bridge/declarative_frontend/jsview/js_touch_handler.h"
#include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
#ifdef PLUGIN_COMPONENT_SUPPORTED
#include "core/common/plugin_manager.h"
#endif
namespace OHOS::Ace::Framework {
@@ -17,19 +17,10 @@
#include "base/geometry/dimension.h"
#include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
#if !(defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM))
#include "frameworks/core/components/plugin/plugin_component.h"
#else
#include "frameworks/core/components/box/box_component.h"
#endif
namespace OHOS::Ace::Framework {
#if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM)
namespace {
constexpr Dimension DEFAULT_FONT_SIZE = 30.0_px;
}
#endif
void JSPlugin::Create(const JSCallbackInfo& info)
{
if (info.Length() == 0 || !info[0]->IsObject()) {
@@ -30,4 +30,4 @@ public:
static void JsOnError(const JSCallbackInfo& info);
};
} // namespace OHOS::Ace::Framework
#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_FORM_COMPONENT_H
#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_PLUGIN_COMPONENT_H
@@ -1626,7 +1626,7 @@ void PluginFrontendDelegate::RegisterFont(const std::string& familyName, const s
}
void PluginFrontendDelegate::HandleImage(
const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback)
const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback)
{
if (src.empty() || !callback) {
return;
@@ -1638,7 +1638,6 @@ void PluginFrontendDelegate::HandleImage(
TaskExecutor::TaskType::JS);
};
pipelineContextHolder_.Get()->TryLoadImageInfo(src, std::move(loadCallback));
}
void PluginFrontendDelegate::PushJsCallbackToRenderNode(NodeId id, double ratio,
+2 -16
View File
@@ -353,22 +353,8 @@ template("ace_core_source_set") {
if (defined(config.plugin_components_support) &&
config.plugin_components_support) {
if (!use_mingw_win && !use_mac) {
deps += [
"$ace_root/frameworks/core/components/plugin:ace_core_components_plugin_$platform",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
"//foundation/appexecfwk/standard/common:libappexecfwk_common",
]
external_deps = [
"ability_runtime:app_manager",
"ability_runtime:base",
"ability_runtime:want",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"eventhandler:libeventhandler",
]
sources += [ "common/plugin_manager.cpp" ]
}
deps += [ "$ace_root/frameworks/core/components/plugin:ace_core_components_plugin_$platform" ]
sources += [ "common/plugin_manager.cpp" ]
}
include_dirs = [ "//foundation/windowmanager/interfaces/innerkits" ]
+8 -8
View File
@@ -15,10 +15,11 @@
#include "core/common/plugin_manager.h"
#include "ability_manager_client.h"
#include "base/log/log.h"
#include "core/common/plugin_utils.h"
namespace OHOS::Ace {
std::shared_ptr<PluginUtils> PluginManager::pluginUtils_ = nullptr;
PluginManager::PluginManager() {}
PluginManager::~PluginManager()
@@ -87,14 +88,13 @@ int64_t PluginManager::GetPluginSubContainerId()
return pluginSubContainerMap_.rbegin()->first + 1;
}
ErrCode PluginManager::StartAbility(
int32_t PluginManager::StartAbility(
const std::string& bundleName, const std::string& abilityName, const std::string& params)
{
AAFwk::Want want;
AppExecFwk::ElementName element("", bundleName, abilityName);
want.SetElement(element);
want.SetParam("params", params);
ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
return error;
if (!pluginUtils_) {
LOGE("PluginUtils is nullptr.");
return -1;
}
return pluginUtils_->StartAbility(bundleName, abilityName, params);
}
} // namespace OHOS::Ace
+7 -5
View File
@@ -19,9 +19,9 @@
#include <mutex>
#include <unordered_map>
#include "adapter/ohos/entrance/ace_ability.h"
#include "base/utils/macros.h"
#include "base/utils/singleton.h"
#include "core/common/plugin_utils.h"
#include "core/components/plugin/plugin_sub_container.h"
#include "frameworks/bridge/js_frontend/engine/common/js_engine_loader.h"
@@ -45,17 +45,18 @@ public:
return jsEngineLoader_;
}
void SetAceAbility(AceAbility* aceAbility)
void SetAceAbility(void* aceAbility, const std::shared_ptr<PluginUtils>& pluginUtils)
{
aceAbility_ = aceAbility;
pluginUtils_ = pluginUtils;
};
AceAbility* GetAceAbility() const
void* GetAceAbility() const
{
return aceAbility_;
};
ErrCode StartAbility(const std::string& bundleName, const std::string& abilityName, const std::string& params);
int32_t StartAbility(const std::string& bundleName, const std::string& abilityName, const std::string& params);
private:
std::mutex mutex_;
@@ -63,7 +64,8 @@ private:
std::mutex nonmatchedContainerMutex_;
std::unordered_map<std::string, RefPtr<PluginSubContainer>> nonmatchedContainerMap_;
Framework::JsEngineLoader* jsEngineLoader_ = nullptr;
AceAbility* aceAbility_ = nullptr;
void* aceAbility_ = nullptr;
static std::shared_ptr<PluginUtils> pluginUtils_;
};
} // namespace OHOS::Ace
#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLUGIN_MANAGER_H
+31
View File
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLUGIN_UTILS_H
#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLUGIN_UTILS_H
#include <string>
namespace OHOS::Ace {
class PluginUtils {
public:
PluginUtils() = default;
virtual ~PluginUtils() = default;
virtual int32_t StartAbility(
const std::string& bundleName, const std::string& abilityName, const std::string& params) = 0;
};
} // namespace OHOS::Ace
#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLUGIN_UTILS_H
@@ -77,7 +77,9 @@ void PluginSubContainer::Initialize()
LOGE("PluginSubContainer::Initialize:jsEngine is nullptr");
return;
}
jsEngine->AddExtraNativeObject("ability", PluginManager::GetInstance().GetAceAbility());
if (!PluginManager::GetInstance().GetAceAbility()) {
jsEngine->AddExtraNativeObject("ability", PluginManager::GetInstance().GetAceAbility());
}
frontend_->SetNeedDebugBreakPoint(AceApplicationInfo::GetInstance().IsNeedDebugBreakPoint());
frontend_->SetDebugVersion(AceApplicationInfo::GetInstance().IsDebugVersion());
@@ -106,7 +106,7 @@ void RosenRenderContext::PaintChild(const RefPtr<RenderNode>& child, const Offse
}
auto density = pipelineContext->GetDensity();
Offset pluginOffset = {pos.GetX() / density, pos.GetY() / density};
pluginContext->SetPluginOffset(child->GetGlobalOffset());
pluginContext->SetPluginEventOffset(child->GetGlobalOffset());
}
} else {
child->RenderWithContext(*this, rect.GetOffset());
@@ -161,6 +161,48 @@ napi_value JSPluginCallback::MakePluginTemplateObject(const PluginComponentTempl
return jsPluginTemplate;
}
void JSPluginCallback::OnPushEventInner(const OnPluginUvWorkData* workData)
{
napi_value jsCallback = nullptr;
napi_value undefined = nullptr;
napi_value jsResult = nullptr;
napi_value callbackParam[ACE_ARGS_FOUR] = {nullptr};
napi_handle_scope scope = nullptr;
std::string dataTmp("{}");
std::string extraDataTmp("{}");
if (!workData->data.empty()) {
dataTmp = workData->data;
}
if (!workData->extraData.empty()) {
extraDataTmp = workData->extraData;
}
napi_open_handle_scope(cbInfo_.env, &scope);
if (scope == nullptr) {
napi_close_handle_scope(cbInfo_.env, scope);
return;
}
PluginComponentTemplate componentTemplate;
componentTemplate.SetSource(workData->sourceName);
componentTemplate.SetAbility(workData->abilityName);
napi_open_handle_scope(cbInfo_.env, &scope);
if (scope == nullptr) {
napi_close_handle_scope(cbInfo_.env, scope);
return;
}
callbackParam[ACE_PARAM0] = AceWrapWant(cbInfo_.env, workData->want);
callbackParam[ACE_PARAM1] = MakePluginTemplateObject(componentTemplate);
callbackParam[ACE_PARAM2] = AceStringToKVObject(cbInfo_.env, dataTmp);
callbackParam[ACE_PARAM3] = AceStringToKVObject(cbInfo_.env, extraDataTmp);
napi_get_undefined(cbInfo_.env, &undefined);
napi_get_reference_value(cbInfo_.env, cbInfo_.callback, &jsCallback);
napi_call_function(cbInfo_.env, undefined, jsCallback, ACE_ARGS_FOUR, callbackParam, &jsResult);
napi_close_handle_scope(cbInfo_.env, scope);
}
void JSPluginCallback::OnPushEvent(const AAFwk::Want& want,
const PluginComponentTemplate& pluginTemplate, const std::string& data, const std::string& extraData)
{
@@ -170,26 +212,44 @@ void JSPluginCallback::OnPushEvent(const AAFwk::Want& want,
return;
}
uvWorkData_.that = (void *)this;
uvWorkData_.want = want;
uvWorkData_.sourceName = pluginTemplate.GetSource();
uvWorkData_.abilityName = pluginTemplate.GetAbility();
uvWorkData_.data = data;
uvWorkData_.extraData = extraData;
OnPushEventInner(&uvWorkData_);
}
void JSPluginCallback::OnRequestEventInner(const OnPluginUvWorkData* workData)
{
napi_value jsCallback = nullptr;
napi_value undefined = nullptr;
napi_value jsResult = nullptr;
napi_value callbackParam[ACE_ARGS_FOUR] = {nullptr};
napi_handle_scope scope = nullptr;
std::string dataTmp("{}");
std::string extraDataTmp("{}");
if (!data.empty()) {
dataTmp = data;
if (!workData->data.empty()) {
dataTmp = workData->data;
}
if (!extraData.empty()) {
extraDataTmp = extraData;
napi_open_handle_scope(cbInfo_.env, &scope);
if (scope == nullptr) {
napi_close_handle_scope(cbInfo_.env, scope);
return;
}
callbackParam[ACE_PARAM0] = AceWrapWant(cbInfo_.env, want);
callbackParam[ACE_PARAM1] = MakePluginTemplateObject(pluginTemplate);
napi_value callbackParam[ACE_ARGS_THREE] = {nullptr};
callbackParam[ACE_PARAM0] = AceWrapWant(cbInfo_.env, workData->want);
callbackParam[ACE_PARAM1] = AceWrapStringToJS(cbInfo_.env, workData->name);
callbackParam[ACE_PARAM2] = AceStringToKVObject(cbInfo_.env, dataTmp);
callbackParam[ACE_PARAM3] = AceStringToKVObject(cbInfo_.env, extraDataTmp);
napi_get_undefined(cbInfo_.env, &undefined);
napi_get_reference_value(cbInfo_.env, cbInfo_.callback, &jsCallback);
napi_call_function(cbInfo_.env, undefined, jsCallback, ACE_ARGS_FOUR, callbackParam, &jsResult);
napi_call_function(cbInfo_.env, undefined, jsCallback, ACE_ARGS_THREE, callbackParam, &jsResult);
if (AceIsTypeForNapiValue(cbInfo_.env, jsResult, napi_object)) {
SendRequestEventResult(jsResult);
}
napi_close_handle_scope(cbInfo_.env, scope);
}
void JSPluginCallback::OnRequestEvent(const AAFwk::Want& want, const std::string& name,
@@ -200,26 +260,37 @@ void JSPluginCallback::OnRequestEvent(const AAFwk::Want& want, const std::string
return;
}
uvWorkData_.that = (void *)this;
uvWorkData_.want = want;
uvWorkData_.data = data;
uvWorkData_.name = name;
OnRequestEventInner(&uvWorkData_);
}
void JSPluginCallback::OnRequestCallBackInner(const OnPluginUvWorkData* workData)
{
napi_value jsCallback = nullptr;
napi_value undefined = nullptr;
napi_value jsResult = nullptr;
std::string dataTmp("{}");
if (!data.empty()) {
dataTmp = data;
napi_handle_scope scope = nullptr;
napi_open_handle_scope(cbInfo_.env, &scope);
if (scope == nullptr) {
napi_close_handle_scope(cbInfo_.env, scope);
return;
}
PluginComponentTemplate componentTemplate;
componentTemplate.SetSource(workData->sourceName);
componentTemplate.SetAbility(workData->abilityName);
napi_value callbackParam[ACE_ARGS_TWO] = {nullptr};
napi_value callbackParam[ACE_ARGS_THREE] = {nullptr};
callbackParam[ACE_PARAM0] = AceWrapWant(cbInfo_.env, want);
callbackParam[ACE_PARAM1] = AceWrapStringToJS(cbInfo_.env, name);
callbackParam[ACE_PARAM2] = AceStringToKVObject(cbInfo_.env, dataTmp);
callbackParam[ACE_PARAM0] = AceGetCallbackErrorValue(cbInfo_.env, 0);
callbackParam[ACE_PARAM1] = MakeCallbackParamForRequest(componentTemplate, workData->data,
workData->extraData);
napi_get_undefined(cbInfo_.env, &undefined);
napi_get_reference_value(cbInfo_.env, cbInfo_.callback, &jsCallback);
napi_call_function(cbInfo_.env, undefined, jsCallback, ACE_ARGS_THREE, callbackParam, &jsResult);
if (AceIsTypeForNapiValue(cbInfo_.env, jsResult, napi_object)) {
SendRequestEventResult(jsResult);
}
napi_call_function(cbInfo_.env, undefined, jsCallback, ACE_ARGS_TWO, callbackParam, &jsResult);
napi_close_handle_scope(cbInfo_.env, scope);
}
void JSPluginCallback::OnRequestCallBack(const PluginComponentTemplate& pluginTemplate,
@@ -232,18 +303,12 @@ void JSPluginCallback::OnRequestCallBack(const PluginComponentTemplate& pluginTe
return;
}
napi_value jsCallback = nullptr;
napi_value undefined = nullptr;
napi_value jsResult = nullptr;
napi_value callbackParam[ACE_ARGS_TWO] = {nullptr};
callbackParam[ACE_PARAM0] = AceGetCallbackErrorValue(cbInfo_.env, 0);
callbackParam[ACE_PARAM1] = MakeCallbackParamForRequest(pluginTemplate, data, extraData);
napi_get_undefined(cbInfo_.env, &undefined);
napi_get_reference_value(cbInfo_.env, cbInfo_.callback, &jsCallback);
napi_call_function(cbInfo_.env, undefined, jsCallback, ACE_ARGS_TWO, callbackParam, &jsResult);
uvWorkData_.that = (void *)this;
uvWorkData_.sourceName = pluginTemplate.GetSource();
uvWorkData_.abilityName = pluginTemplate.GetAbility();
uvWorkData_.data = data;
uvWorkData_.extraData = extraData;
OnRequestCallBackInner(&uvWorkData_);
}
bool JSPluginCallback::OnEventStrictEquals(CallBackType eventType, const AAFwk::Want& want,
@@ -18,13 +18,23 @@
#include <atomic>
#include <string>
#include <uv.h>
#include "core/components/plugin/plugin_component_callback.h"
#include "js_plugin_data.h"
namespace OHOS::Ace::Napi {
using namespace OHOS::Ace;
struct OnPluginUvWorkData {
AAFwk::Want want;
std::string sourceName;
std::string abilityName;
std::string data;
std::string extraData;
std::string name;
void *that = nullptr;
};
class AceJSPluginRequestParam {
public:
AceJSPluginRequestParam(const AAFwk::Want& want, const std::string name, const std::string data,
@@ -67,11 +77,16 @@ private:
napi_value MakeCallbackParamForRequest(const PluginComponentTemplate& pluginTemplate,
const std::string& data, const std::string& extraData);
napi_value MakePluginTemplateObject(const PluginComponentTemplate& pluginTemplate);
void OnPushEventInner(const OnPluginUvWorkData* workData);
void OnRequestEventInner(const OnPluginUvWorkData* workData);
void OnRequestCallBackInner(const OnPluginUvWorkData* workData);
AAFwk::Want want_;
ACECallbackInfo cbInfo_;
CallBackType eventType_;
std::shared_ptr<AceJSPluginRequestParam> requestParam_ = nullptr;
static std::atomic_size_t uuid_;
OnPluginUvWorkData uvWorkData_;
};
} // namespace OHOS::Ace::Napi
#endif // OHOS_NAPI_ACE_PLUGIN_CALLBACK_H
@@ -38,7 +38,7 @@ JSPluginCallbackMgr& JSPluginCallbackMgr::Instance(void)
return gJSPluginCallbackMgr;
}
bool JSPluginCallbackMgr::RegisterOnEvent(CallBackType eventType, const AAFwk::Want& want,
bool JSPluginCallbackMgr::RegisterOnEvent(napi_env env, CallBackType eventType, const AAFwk::Want& want,
ACECallbackInfo& cbInfo)
{
std::lock_guard<std::mutex> lock(mutex_);
@@ -60,7 +60,7 @@ bool JSPluginCallbackMgr::RegisterOnEvent(CallBackType eventType, const AAFwk::W
}
}
bool JSPluginCallbackMgr::RegisterRequestEvent(const AAFwk::Want& want, ACECallbackInfo& cbInfo,
bool JSPluginCallbackMgr::RegisterRequestEvent(napi_env env, const AAFwk::Want& want, ACECallbackInfo& cbInfo,
const std::shared_ptr<AceJSPluginRequestParam>& param)
{
HILOG_INFO("%{public}s called.", __func__);
@@ -96,4 +96,9 @@ void JSPluginCallbackMgr::UnRegisterEvent(size_t key)
eventList_.erase(iter);
}
}
void JSPluginCallbackMgr::UnregisterCallBack(napi_env env, const AAFwk::Want& want)
{
PluginComponentManager::GetInstance()->UnregisterCallBack(want);
}
} // namespace OHOS::Ace::Napi
@@ -31,10 +31,11 @@ public:
~JSPluginCallbackMgr();
static JSPluginCallbackMgr& Instance(void);
bool RegisterOnEvent(CallBackType eventType, const AAFwk::Want& want, ACECallbackInfo& cbInfo);
bool RegisterRequestEvent(const AAFwk::Want& want, ACECallbackInfo& cbInfo,
bool RegisterOnEvent(napi_env env, CallBackType eventType, const AAFwk::Want& want, ACECallbackInfo& cbInfo);
bool RegisterRequestEvent(napi_env env, const AAFwk::Want& want, ACECallbackInfo& cbInfo,
const std::shared_ptr<AceJSPluginRequestParam>& param);
void UnRegisterEvent(size_t key);
void UnregisterCallBack(napi_env env, const AAFwk::Want& want);
private:
std::mutex mutex_;
std::map<size_t, std::shared_ptr<JSPluginCallback>> eventList_;
@@ -24,8 +24,34 @@
namespace OHOS::Ace::Napi {
constexpr int ACE_ARGS_MAX_COUNT = 10;
constexpr int ACE_ARGS_TWO = 2;
constexpr int ACE_ARGS_THREE = 3;
constexpr int ACE_PARAM0 = 0;
constexpr int ACE_PARAM1 = 1;
constexpr int ACE_PARAM2 = 2;
bool UnwrapStageWantFromJS(napi_env env, napi_value param, ACEAsyncJSCallbackInfo* asyncCallbackInfo)
{
if (!AceUnwrapWant(env, param, asyncCallbackInfo->wantStage)) {
HILOG_INFO("%{public}s called, Params(want stage) is invalid.", __func__);
return false;
}
return true;
}
bool UnwrapOwnerWantFromJS(napi_env env, napi_value param, ACEAsyncJSCallbackInfo* asyncCallbackInfo)
{
napi_value jsValue = AceGetPropertyValueByPropertyName(env, param, "owner", napi_object);
if (jsValue == nullptr) {
HILOG_INFO("%{public}s called, Params(want) is not object.", __func__);
return false;
}
if (!AceUnwrapWant(env, jsValue, asyncCallbackInfo->wantStage)) {
HILOG_INFO("%{public}s called, Params(want) is invalid.", __func__);
return false;
}
return true;
}
bool UnwrapWantFromJS(napi_env env, napi_value param, ACEAsyncJSCallbackInfo* asyncCallbackInfo)
{
@@ -39,8 +65,6 @@ bool UnwrapWantFromJS(napi_env env, napi_value param, ACEAsyncJSCallbackInfo* as
HILOG_INFO("%{public}s called, Params(want) is invalid.", __func__);
return false;
}
AppExecFwk::ElementName elem = asyncCallbackInfo->jsParamList.want.GetElement();
return true;
}
@@ -81,6 +105,15 @@ bool UnwrapPushParameters(napi_env env, napi_value param, ACEAsyncJSCallbackInfo
HILOG_INFO("%{public}s called, Params is invalid.", __func__);
return false;
}
bool isStageModelAPI = (asyncCallbackInfo->ability == nullptr);
if (isStageModelAPI) {
if (!UnwrapOwnerWantFromJS(env, param, asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, Params(ownerWant) is invalid.", __func__);
return false;
}
}
if (!UnwrapWantFromJS(env, param, asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, Params(want) is invalid.", __func__);
return false;
@@ -106,24 +139,21 @@ bool UnwrapParamForPush(napi_env env, size_t argc, napi_value* argv,
ACEAsyncJSCallbackInfo* asyncCallbackInfo)
{
HILOG_INFO("%{public}s called, argc=%{public}zu", __func__, argc);
const size_t argcMax = ACE_ARGS_TWO;
if (argc != argcMax) {
HILOG_INFO("%{public}s called, Params is invalid.", __func__);
return false;
}
if (argc == argcMax) {
if (!AceCreateAsyncCallback(env, argv[ACE_PARAM1], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the second parameter is invalid.", __func__);
return false;
}
}
if (!UnwrapPushParameters(env, argv[ACE_PARAM0], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__);
return false;
}
if (!AceCreateAsyncCallback(env, argv[ACE_PARAM1], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the last parameter is invalid.", __func__);
return false;
}
return true;
}
@@ -134,17 +164,20 @@ void JSPushWork(napi_env env, void* data)
void JSPushCompleteAsyncCallbackWork(napi_env env, napi_status status, void* data)
{
HILOG_INFO("%{public}s called.", __func__);
ACEAsyncJSCallbackInfo* asyncCallbackInfo = (ACEAsyncJSCallbackInfo*)data;
if (asyncCallbackInfo == nullptr) {
HILOG_INFO("%{public}s called. asyncCallbackInfo is null", __func__);
return;
}
PluginComponentManager::GetInstance()->Push(asyncCallbackInfo->jsParamList.want,
asyncCallbackInfo->jsParamList.paramList.GetStringValue("name"),
asyncCallbackInfo->jsParamList.paramList.GetStringValue("jsonPath"),
asyncCallbackInfo->jsParamList.paramList.GetStringValue("data"),
asyncCallbackInfo->jsParamList.paramList.GetStringValue("extraData"));
asyncCallbackInfo->error_code = 0;
AceCompleteAsyncCallbackWork(env, status, data);
}
napi_value NAPI_JSPushWrap(napi_env env, napi_callback_info info, ACEAsyncJSCallbackInfo* asyncCallbackInfo)
@@ -156,12 +189,10 @@ napi_value NAPI_JSPushWrap(napi_env env, napi_callback_info info, ACEAsyncJSCall
void* data = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data));
if (!UnwrapParamForPush(env, argc, args, asyncCallbackInfo)) {
HILOG_INFO("%{public}s called. Invoke UnwrapParamForPush fail", __func__);
return nullptr;
}
ACEAsyncParamEx asyncParamEx;
if (asyncCallbackInfo->cbInfo.callback != nullptr) {
HILOG_INFO("%{public}s called. asyncCallback.", __func__);
@@ -200,6 +231,15 @@ bool UnwrapRequestParameters(napi_env env, napi_value param, ACEAsyncJSCallbackI
HILOG_INFO("%{public}s called, Params is invalid.", __func__);
return false;
}
bool isStageModelAPI = (asyncCallbackInfo->ability == nullptr);
if (isStageModelAPI) {
if (!UnwrapOwnerWantFromJS(env, param, asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, Params(ownerWant) is invalid.", __func__);
return false;
}
}
if (!UnwrapWantFromJS(env, param, asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, Params(want) is invalid.", __func__);
return false;
@@ -221,36 +261,26 @@ bool UnwrapParamForRequest(napi_env env, size_t argc, napi_value* argv,
ACEAsyncJSCallbackInfo* asyncCallbackInfo)
{
HILOG_INFO("%{public}s called, argc=%{public}zu", __func__, argc);
const size_t argcMax = ACE_ARGS_TWO;
if (argc != argcMax) {
if (argc != ACE_ARGS_TWO) {
HILOG_INFO("%{public}s called, Params is invalid.", __func__);
return false;
}
if (argc == argcMax) {
if (!AceCreateAsyncCallback(env, argv[ACE_PARAM1], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the second parameter is invalid.", __func__);
return false;
}
}
if (!UnwrapRequestParameters(env, argv[ACE_PARAM0], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__);
return false;
}
if (!AceCreateAsyncCallback(env, argv[ACE_PARAM1], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the last parameter is invalid.", __func__);
return false;
}
return true;
}
void JSRequestWork(napi_env env, void* data)
{
HILOG_INFO("%{public}s called.", __func__);
ACEAsyncJSCallbackInfo* asyncCallbackInfo = (ACEAsyncJSCallbackInfo*)data;
if (asyncCallbackInfo == nullptr) {
HILOG_INFO("%{public}s called. asyncCallbackInfo is null", __func__);
return;
}
}
void AceRequestCompleteAsyncCallbackWork(napi_env env, napi_status status, void* data)
@@ -265,20 +295,23 @@ void AceRequestCompleteAsyncCallbackWork(napi_env env, napi_status status, void*
if (asyncCallbackInfo->ability != nullptr) {
std::shared_ptr<AAFwk::Want> pWant = asyncCallbackInfo->ability->GetWant();
std::shared_ptr<AceJSPluginRequestParam> param = std::make_shared<AceJSPluginRequestParam>(
asyncCallbackInfo->jsParamList.want,
asyncCallbackInfo->jsParamList.paramList.GetStringValue("name"),
asyncCallbackInfo->jsParamList.paramList.GetStringValue("data"),
asyncCallbackInfo->jsParamList.paramList.GetStringValue("jsonPath")
);
if (param != nullptr) {
HILOG_INFO("%{public}s called, pWant = %{public}s:%{public}s",
__func__, pWant->GetElement().GetBundleName().c_str(), pWant->GetElement().GetAbilityName().c_str());
bool ret = JSPluginCallbackMgr::Instance().RegisterRequestEvent(*pWant,
asyncCallbackInfo->cbInfo, param);
if (ret) {
asyncCallbackInfo->cbInfo.callback = nullptr;
}
asyncCallbackInfo->wantStage = *pWant;
}
std::shared_ptr<AceJSPluginRequestParam> param = std::make_shared<AceJSPluginRequestParam>(
asyncCallbackInfo->jsParamList.want,
asyncCallbackInfo->jsParamList.paramList.GetStringValue("name"),
asyncCallbackInfo->jsParamList.paramList.GetStringValue("data"),
asyncCallbackInfo->jsParamList.paramList.GetStringValue("jsonPath")
);
if (param != nullptr) {
HILOG_INFO("%{public}s called, pWant = %{public}s:%{public}s",
__func__, asyncCallbackInfo->wantStage.GetElement().GetBundleName().c_str(),
asyncCallbackInfo->wantStage.GetElement().GetAbilityName().c_str());
bool ret = JSPluginCallbackMgr::Instance().RegisterRequestEvent(env, asyncCallbackInfo->wantStage,
asyncCallbackInfo->cbInfo, param);
if (ret) {
asyncCallbackInfo->cbInfo.callback = nullptr;
}
}
@@ -342,25 +375,49 @@ bool UnwrapParamForOn(napi_env env, size_t argc, napi_value* argv,
{
HILOG_INFO("%{public}s called, argc=%{public}zu", __func__, argc);
const size_t argcMax = ACE_ARGS_TWO;
if (argc != argcMax) {
const size_t argcMax = ACE_ARGS_THREE;
if (argc > argcMax || argc < ACE_ARGS_TWO) {
HILOG_INFO("%{public}s called, Params is invalid.", __func__);
return false;
}
std::string eventName = AceUnwrapStringFromJS(env, argv[ACE_PARAM0]);
if (eventName == "push" || eventName == "request") {
asyncCallbackInfo->jsParamList.paramList.PutStringValue("eventName", eventName);
if (asyncCallbackInfo->ability == nullptr && argc != ACE_ARGS_THREE) {
HILOG_INFO("%{public}s called, Params is invalid.", __func__);
return false;
}
if (argc == ACE_ARGS_THREE || asyncCallbackInfo->ability == nullptr) {
if (!UnwrapStageWantFromJS(env, argv[ACE_PARAM0], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the owner want parameter is invalid.", __func__);
return false;
}
std::string eventName = AceUnwrapStringFromJS(env, argv[ACE_PARAM1]);
if (eventName == "push" || eventName == "request") {
asyncCallbackInfo->jsParamList.paramList.PutStringValue("eventName", eventName);
} else {
HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__);
return false;
}
if (!AceCreateAsyncCallback(env, argv[ACE_PARAM2], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the second parameter is invalid.", __func__);
return false;
}
} else {
HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__);
return false;
}
std::string eventName = AceUnwrapStringFromJS(env, argv[ACE_PARAM0]);
if (eventName == "push" || eventName == "request") {
asyncCallbackInfo->jsParamList.paramList.PutStringValue("eventName", eventName);
} else {
HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__);
return false;
}
if (!AceCreateAsyncCallback(env, argv[ACE_PARAM1], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the second parameter is invalid.", __func__);
return false;
if (!AceCreateAsyncCallback(env, argv[ACE_PARAM1], asyncCallbackInfo)) {
HILOG_INFO("%{public}s called, the second parameter is invalid.", __func__);
return false;
}
}
return true;
}
@@ -387,11 +444,13 @@ void AceOnCompleteAsyncCallbackWork(napi_env env, napi_status status, void* data
if (asyncCallbackInfo->ability != nullptr) {
std::shared_ptr<AAFwk::Want> pWant = asyncCallbackInfo->ability->GetWant();
bool ret = JSPluginCallbackMgr::Instance().RegisterOnEvent(eventCallbackType,
*pWant, asyncCallbackInfo->cbInfo);
if (ret) {
asyncCallbackInfo->cbInfo.callback = nullptr;
}
asyncCallbackInfo->wantStage = *pWant;
}
bool ret = JSPluginCallbackMgr::Instance().RegisterOnEvent(env, eventCallbackType,
asyncCallbackInfo->wantStage, asyncCallbackInfo->cbInfo);
if (ret) {
asyncCallbackInfo->cbInfo.callback = nullptr;
}
napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
AceFreeAsyncJSCallbackInfo(&asyncCallbackInfo);
@@ -415,14 +474,14 @@ napi_value NAPI_JSOnWrap(napi_env env, napi_callback_info info, ACEAsyncJSCallba
ACEAsyncParamEx asyncParamEx;
if (asyncCallbackInfo->cbInfo.callback != nullptr) {
HILOG_INFO("%{public}s called. asyncCallback.", __func__);
asyncParamEx.resource = "NAPI_JSRequestCallback";
asyncParamEx.resource = "NAPI_JSOCallback";
asyncParamEx.execute = JSOnWork;
asyncParamEx.complete = AceOnCompleteAsyncCallbackWork;
return AceExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx);
} else {
HILOG_INFO("%{public}s called. promise.", __func__);
asyncParamEx.resource = "NAPI_JSRequestPromise";
asyncParamEx.resource = "NAPI_JSOnPromise";
return nullptr;
}
}
@@ -64,6 +64,7 @@ struct ACEAsyncJSCallbackInfo {
AceFromJSParam jsParamList = {};
napi_value result = nullptr;
int error_code = 0;
AAFwk::Want wantStage = {};
};
struct ACEAsyncParamEx {
@@ -903,9 +903,10 @@ ACEAsyncJSCallbackInfo* AceCreateAsyncJSCallbackInfo(napi_env env)
napi_value abilityObj = 0;
NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj));
Ability *ability = nullptr;
NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability));
if (abilityObj) {
NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability));
}
ACEAsyncJSCallbackInfo* asyncCallbackInfo = new (std::nothrow) ACEAsyncJSCallbackInfo {
.cbInfo = {
@@ -920,7 +921,6 @@ ACEAsyncJSCallbackInfo* AceCreateAsyncJSCallbackInfo(napi_env env)
if (asyncCallbackInfo != nullptr) {
AceClearThreadReturnData(&asyncCallbackInfo->native_data);
}
return asyncCallbackInfo;
}