From 7ecf1624ac34ec779294a25d4269dc0c7848c9be Mon Sep 17 00:00:00 2001 From: chyyy0213 Date: Sun, 30 Jan 2022 16:27:45 +0800 Subject: [PATCH 1/2] windowstage loadContent bugfix & add createSubWindow Signed-off-by: chyyy0213 Change-Id: I374b969f04c9b7b6c64060497e5dbe86ae35f5a9 Signed-off-by: chyyy0213 --- .../include/ability_runtime/js_window_stage.h | 2 + .../src/ability_runtime/js_window_stage.cpp | 71 ++++++++++++++----- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h b/frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h index a74eb180441..bf78b4b3f74 100644 --- a/frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h +++ b/frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h @@ -41,6 +41,7 @@ public: static NativeValue* Off(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* LoadContent(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* GetWindowMode(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* CreateSubWindow(NativeEngine* engine, NativeCallbackInfo* info); virtual void AfterForeground() override; virtual void AfterBackground() override; virtual void AfterFocused() override; @@ -53,6 +54,7 @@ private: NativeValue* OffEvent(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnGetWindowMode(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnCreateSubWindow(NativeEngine& engine, NativeCallbackInfo& info); enum WindowStageEventType { VISIBLE = 1, FOCUSED, diff --git a/frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp b/frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp index 4f50cf344b5..de6dfcfeba5 100644 --- a/frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp +++ b/frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp @@ -25,8 +25,7 @@ namespace OHOS { namespace AbilityRuntime { namespace { const int CONTENT_STORAGE_ARG = 2; -constexpr size_t ARGC_TWO = 2; -constexpr size_t ARGC_THREE = 3; +constexpr size_t ARGC_ONE = 1; constexpr size_t INDEX_ONE = 1; constexpr size_t INDEX_TWO = 2; } // namespace @@ -79,6 +78,13 @@ NativeValue* JsWindowStage::GetWindowMode(NativeEngine* engine, NativeCallbackIn return (me != nullptr) ? me->OnGetWindowMode(*engine, *info) : nullptr; } +NativeValue* JsWindowStage::CreateSubWindow(NativeEngine* engine, NativeCallbackInfo* info) +{ + HILOG_INFO("JsWindowStage::CreateSubWindow is called"); + JsWindowStage* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnCreateSubWindow(*engine, *info) : nullptr; +} + void JsWindowStage::AfterForeground() { LifeCycleCallBack(WindowStageEventType::VISIBLE); @@ -175,11 +181,10 @@ NativeValue* JsWindowStage::OnGetMainWindow(NativeEngine& engine, NativeCallback "JsWindowStage::OnGetMainWindow failed.")); } }; - - NativeValue* lastParam = (info.argc == 0) ? nullptr : info.argv[0]; + NativeValue* callback = info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule( - engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, + engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); return result; } @@ -305,16 +310,14 @@ NativeValue* JsWindowStage::OnLoadContent(NativeEngine& engine, NativeCallbackIn } NativeValue* storage = nullptr; NativeValue* callBack = nullptr; - if (info.argc == ARGC_TWO) { - NativeValue* value = info.argv[INDEX_ONE]; - if (value->TypeOf() == NATIVE_FUNCTION) { - callBack = info.argv[INDEX_ONE]; - } else { - storage = info.argv[INDEX_ONE]; - } - } else if (info.argc == ARGC_THREE) { - storage = info.argv[INDEX_ONE]; - callBack = info.argv[INDEX_TWO]; + NativeValue* value1 = info.argv[INDEX_ONE]; + NativeValue* value2 = info.argv[INDEX_TWO]; + if (value1->TypeOf() == NATIVE_FUNCTION) { + callBack = value1; + } else if (value1->TypeOf() == NATIVE_OBJECT) { + storage = value1; + } else if (value2->TypeOf() == NATIVE_FUNCTION) { + callBack = value2; } contentStorage_ = static_cast(storage); AsyncTask::CompleteCallback complete = @@ -357,11 +360,43 @@ NativeValue* JsWindowStage::OnGetWindowMode(NativeEngine& engine, NativeCallback task.Resolve(engine, CreateJsValue(engine, mode)); HILOG_INFO("JsWindowStage OnGetWindowMode success"); }; + NativeValue* callback = info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr; + NativeValue* result = nullptr; + AsyncTask::Schedule( + engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); + return result; +} - NativeValue* lastParam = (info.argc == 0) ? nullptr : info.argv[0]; +NativeValue* JsWindowStage::OnCreateSubWindow(NativeEngine& engine, NativeCallbackInfo& info) +{ + HILOG_INFO("JsWindowStage::OnCreateSubWindow is called"); + if (info.argc < ARGC_ONE) { + HILOG_ERROR("JsWindowStage::OnCreateSubWindow params less than 1!"); + return engine.CreateUndefined(); + } + std::string windowName; + if (!ConvertFromJsValue(engine, info.argv[0], windowName)) { + HILOG_ERROR("Failed to convert parameter to windowName"); + return engine.CreateUndefined(); + } + AsyncTask::CompleteCallback complete = + [this, windowName](NativeEngine& engine, AsyncTask& task, int32_t status) { + sptr windowOption = new Rosen::WindowOption(); + windowOption->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW); + auto window = windowScene_->CreateWindow(windowName, windowOption); + if (window == nullptr) { + task.Reject(engine, CreateJsError(engine, static_cast(Rosen::WMError::WM_ERROR_NULLPTR), + "JsWindowStage::OnCreateSubWindow failed.")); + HILOG_ERROR("JsWindowStage window is nullptr"); + return; + } + task.Resolve(engine, CreateJsWindowObject(engine, window)); + HILOG_INFO("JsWindowStage OnCreateSubWindow success"); + }; + NativeValue* callback = info.argv[INDEX_ONE]->TypeOf() == NATIVE_FUNCTION ? info.argv[INDEX_ONE] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule( - engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); return result; } @@ -384,6 +419,8 @@ NativeValue* CreateJsWindowStage(NativeEngine& engine, *object, "getMainWindow", JsWindowStage::GetMainWindow); AbilityRuntime::BindNativeFunction(engine, *object, "getWindowMode", JsWindowStage::GetWindowMode); + AbilityRuntime::BindNativeFunction(engine, + *object, "createSubWindow", JsWindowStage::CreateSubWindow); AbilityRuntime::BindNativeFunction(engine, *object, "on", JsWindowStage::On); AbilityRuntime::BindNativeFunction(engine, *object, "off", JsWindowStage::Off); -- Gitee From e3aefe76f91554c65dab93b22531651ba410287d Mon Sep 17 00:00:00 2001 From: chyyy0213 Date: Mon, 7 Feb 2022 15:11:21 +0800 Subject: [PATCH 2/2] move windowstage from ability to window Signed-off-by: chyyy0213 Change-Id: I1bba5c56c8a1b7626269ae5ffec7e96769020f99 --- frameworks/kits/ability/native/BUILD.gn | 5 +- .../include/ability_runtime/js_window_stage.h | 76 ---- .../native/src/ability_runtime/js_ability.cpp | 5 +- .../src/ability_runtime/js_window_stage.cpp | 430 ------------------ .../window_stage_api/@ohos.window_stage.d.ts | 91 ---- 5 files changed, 5 insertions(+), 602 deletions(-) delete mode 100644 frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h delete mode 100644 frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp delete mode 100644 frameworks/kits/ability/native/src/ability_runtime/window_stage_api/@ohos.window_stage.d.ts diff --git a/frameworks/kits/ability/native/BUILD.gn b/frameworks/kits/ability/native/BUILD.gn index 72b62ccbe4a..15e1b9715ed 100644 --- a/frameworks/kits/ability/native/BUILD.gn +++ b/frameworks/kits/ability/native/BUILD.gn @@ -85,7 +85,7 @@ config("ability_public_config") { "//foundation/aafwk/standard/frameworks/kits/fmskit/native/include", "//foundation/aafwk/standard/interfaces/innerkits/form_manager/include", "//foundation/windowmanager/interfaces/innerkits/wm", - "//foundation/windowmanager/interfaces/kits/napi/window_runtime/window_napi", + "//foundation/windowmanager/interfaces/kits/napi/window_runtime/window_stage_napi", "//third_party/jsoncpp/include", "//third_party/json/include", ] @@ -215,7 +215,6 @@ ohos_shared_library("abilitykit_native") { "${SUBSYSTEM_DIR}/src/ability_runtime/js_ability.cpp", "${SUBSYSTEM_DIR}/src/ability_runtime/js_ability_context.cpp", "${SUBSYSTEM_DIR}/src/ability_runtime/js_caller_complex.cpp", - "${SUBSYSTEM_DIR}/src/ability_runtime/js_window_stage.cpp", "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/napi_dataability/src/napi_data_ability_predicates.cpp", "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/napi_resultset/src/napi_result_set.cpp", ] @@ -270,7 +269,7 @@ ohos_shared_library("abilitykit_native") { "//foundation/ace/napi:ace_napi", "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", "//foundation/graphic/standard:libwmclient", - "//foundation/windowmanager/interfaces/kits/napi/window_runtime:window_native_kit", + "//foundation/windowmanager/interfaces/kits/napi/window_runtime:windowstage_kit", "//foundation/windowmanager/wm:libwm", ] diff --git a/frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h b/frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h deleted file mode 100644 index bf78b4b3f74..00000000000 --- a/frameworks/kits/ability/native/include/ability_runtime/js_window_stage.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 INTERFACES_INNERKITS_JS_WINDOW_STAGE_H -#define INTERFACES_INNERKITS_JS_WINDOW_STAGE_H - -#include -#include -#include -#include -#include "window_scene.h" - -class NativeEngine; -class NativeValue; - -namespace OHOS { -namespace AbilityRuntime { -NativeValue* CreateJsWindowStage(NativeEngine& engine, std::shared_ptr windowScene); - -class JsWindowStage : Rosen::IWindowLifeCycle { -public: - JsWindowStage(const std::shared_ptr& windowScene, NativeValue* object) - : windowScene_(windowScene), object_(object) {} - ~JsWindowStage() = default; - static void Finalizer(NativeEngine* engine, void* data, void* hint); - static NativeValue* SetUIContent(NativeEngine* engine, NativeCallbackInfo* info); - static NativeValue* GetMainWindow(NativeEngine* engine, NativeCallbackInfo* info); - static NativeValue* On(NativeEngine* engine, NativeCallbackInfo* info); - static NativeValue* Off(NativeEngine* engine, NativeCallbackInfo* info); - static NativeValue* LoadContent(NativeEngine* engine, NativeCallbackInfo* info); - static NativeValue* GetWindowMode(NativeEngine* engine, NativeCallbackInfo* info); - static NativeValue* CreateSubWindow(NativeEngine* engine, NativeCallbackInfo* info); - virtual void AfterForeground() override; - virtual void AfterBackground() override; - virtual void AfterFocused() override; - virtual void AfterUnFocused() override; - -private: - NativeValue* OnSetUIContent(NativeEngine& engine, NativeCallbackInfo& info); - NativeValue* OnGetMainWindow(NativeEngine& engine, NativeCallbackInfo& info); - NativeValue* OnEvent(NativeEngine& engine, NativeCallbackInfo& info); - NativeValue* OffEvent(NativeEngine& engine, NativeCallbackInfo& info); - NativeValue* OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info); - NativeValue* OnGetWindowMode(NativeEngine& engine, NativeCallbackInfo& info); - NativeValue* OnCreateSubWindow(NativeEngine& engine, NativeCallbackInfo& info); - enum WindowStageEventType { - VISIBLE = 1, - FOCUSED, - UNFOCUSED, - INVISIBLE, - }; - void LifeCycleCallBack(WindowStageEventType type); - - std::shared_ptr windowScene_; - NativeValue* object_ = nullptr; - NativeEngine* engine_ = nullptr; - void* contentStorage_ = nullptr; - sptr lifecycleListener_ = nullptr; - std::map, int> eventCallbackMap_; - bool regLifeCycleListenerFlag_ = false; -}; -} // namespace AbilityRuntime -} // namespace OHOS -#endif // INTERFACES_INNERKITS_JS_WINDOW_STAGE_H diff --git a/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp b/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp index a48789d6e11..af1e58c9b25 100755 --- a/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp +++ b/frameworks/kits/ability/native/src/ability_runtime/js_ability.cpp @@ -18,13 +18,13 @@ #include "ability_runtime/js_ability.h" #include "ability_runtime/js_ability_context.h" -#include "ability_runtime/js_window_stage.h" #include "ability_start_setting.h" #include "connection_manager.h" #include "hilog_wrapper.h" #include "js_data_struct_converter.h" #include "js_runtime.h" #include "js_runtime_utils.h" +#include "js_window_stage.h" #include "napi_common_want.h" #include "napi_remote_object.h" #include "string_wrapper.h" @@ -376,7 +376,8 @@ std::unique_ptr JsAbility::CreateAppWindowStage() { HandleScope handleScope(jsRuntime_); auto &engine = jsRuntime_.GetNativeEngine(); - NativeValue *jsWindowStage = CreateJsWindowStage(engine, GetScene()); + // NativeValue *jsWindowStage = nullptr; + NativeValue *jsWindowStage = Rosen::CreateJsWindowStage(engine, GetScene()); if (jsWindowStage == nullptr) { HILOG_ERROR("Failed to create jsWindowSatge object"); return nullptr; diff --git a/frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp b/frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp deleted file mode 100644 index de6dfcfeba5..00000000000 --- a/frameworks/kits/ability/native/src/ability_runtime/js_window_stage.cpp +++ /dev/null @@ -1,430 +0,0 @@ -/* - * 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 "ability_runtime/js_window_stage.h" - -#include -#include "ability_runtime/js_ability_context.h" -#include "hilog_wrapper.h" -#include "js_runtime_utils.h" -#include "js_window.h" - -namespace OHOS { -namespace AbilityRuntime { -namespace { -const int CONTENT_STORAGE_ARG = 2; -constexpr size_t ARGC_ONE = 1; -constexpr size_t INDEX_ONE = 1; -constexpr size_t INDEX_TWO = 2; -} // namespace - -void JsWindowStage::Finalizer(NativeEngine* engine, void* data, void* hint) -{ - HILOG_INFO("JsWindowStage::Finalizer is called"); - std::unique_ptr(static_cast(data)); -} - -NativeValue* JsWindowStage::SetUIContent(NativeEngine* engine, NativeCallbackInfo* info) -{ - HILOG_INFO("JsWindowStage::SetUIContent is called"); - JsWindowStage* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnSetUIContent(*engine, *info) : nullptr; -} - -NativeValue* JsWindowStage::GetMainWindow(NativeEngine* engine, NativeCallbackInfo* info) -{ - HILOG_INFO("JsWindowStage::GetMainWindow is called"); - JsWindowStage* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnGetMainWindow(*engine, *info) : nullptr; -} - -NativeValue* JsWindowStage::On(NativeEngine* engine, NativeCallbackInfo* info) -{ - HILOG_INFO("JsWindowStage::On is called"); - JsWindowStage* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnEvent(*engine, *info) : nullptr; -} - -NativeValue* JsWindowStage::Off(NativeEngine* engine, NativeCallbackInfo* info) -{ - HILOG_INFO("JsWindowStage::Off is called"); - JsWindowStage* me = AbilityRuntime::CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OffEvent(*engine, *info) : nullptr; -} - -NativeValue* JsWindowStage::LoadContent(NativeEngine* engine, NativeCallbackInfo* info) -{ - HILOG_INFO("JsWindowStage::LoadContent is called"); - JsWindowStage* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnLoadContent(*engine, *info) : nullptr; -} - -NativeValue* JsWindowStage::GetWindowMode(NativeEngine* engine, NativeCallbackInfo* info) -{ - HILOG_INFO("JsWindowStage::GetWindowMode is called"); - JsWindowStage* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnGetWindowMode(*engine, *info) : nullptr; -} - -NativeValue* JsWindowStage::CreateSubWindow(NativeEngine* engine, NativeCallbackInfo* info) -{ - HILOG_INFO("JsWindowStage::CreateSubWindow is called"); - JsWindowStage* me = CheckParamsAndGetThis(engine, info); - return (me != nullptr) ? me->OnCreateSubWindow(*engine, *info) : nullptr; -} - -void JsWindowStage::AfterForeground() -{ - LifeCycleCallBack(WindowStageEventType::VISIBLE); -} - -void JsWindowStage::AfterBackground() -{ - LifeCycleCallBack(WindowStageEventType::INVISIBLE); -} - -void JsWindowStage::AfterFocused() -{ - LifeCycleCallBack(WindowStageEventType::FOCUSED); -} - -void JsWindowStage::AfterUnFocused() -{ - LifeCycleCallBack(WindowStageEventType::UNFOCUSED); -} - -void JsWindowStage::LifeCycleCallBack(WindowStageEventType type) -{ - HILOG_INFO("JsWindowStage::LifeCycleCallBack is called, type: %{public}d", type); - if (engine_ == nullptr) { - HILOG_INFO("JsWindowStage::LifeCycleCallBack engine_ is nullptr"); - return; - } - for (auto iter = eventCallbackMap_.begin(); iter != eventCallbackMap_.end(); iter++) { - std::shared_ptr callback = iter->first; - int argc = 1; - NativeValue* argv[1]; - argv[0] = engine_->CreateNumber((int32_t)type); - engine_->CallFunction(object_, callback->Get(), argv, argc); - } -} - -NativeValue* JsWindowStage::OnSetUIContent(NativeEngine& engine, NativeCallbackInfo& info) -{ - HILOG_INFO("JsWindowStage::OnSetUIContent is called"); - if (info.argc < 2) { // 2: minimum param nums - HILOG_ERROR("JsWindowStage::OnSetUIContent Not enough params"); - return engine.CreateUndefined(); - } - if (windowScene_ == nullptr || windowScene_->GetMainWindow() == nullptr) { - HILOG_ERROR("JsWindowStage::OnSetUIContent windowScene_ or MainWindow is nullptr"); - return engine.CreateUndefined(); - } - - // Parse info->argv[0] as abilitycontext - auto objContext = AbilityRuntime::ConvertNativeValueTo(info.argv[0]); - if (objContext == nullptr) { - HILOG_ERROR("JsWindowStage::OnSetUIContent info->argv[0] InValid"); - return engine.CreateUndefined(); - } - - auto context = static_cast*>(objContext->GetNativePointer()); - auto abilityContext = Context::ConvertTo(context->lock()); - if (abilityContext == nullptr) { - HILOG_ERROR("JsWindowStage::OnSetUIContent context is nullptr"); - return engine.CreateUndefined(); - } - - HILOG_INFO("JsWindowStage::OnSetUIContent Get context: %{public}p", abilityContext.get()); - - // Parse info->argv[1] as url - std::string contextUrl; - if (!ConvertFromJsValue(engine, info.argv[1], contextUrl)) { - HILOG_ERROR("JsWindowStage::OnSetUIContent failed to convert parameter to url"); - return engine.CreateUndefined(); - } - HILOG_INFO("JsWindowStage::OnSetUIContent Get url: %{public}s", contextUrl.c_str()); - - windowScene_->GetMainWindow()->SetUIContent(contextUrl, &engine, info.argv[CONTENT_STORAGE_ARG]); - - return engine.CreateUndefined(); -} - -NativeValue* JsWindowStage::OnGetMainWindow(NativeEngine& engine, NativeCallbackInfo& info) -{ - HILOG_INFO("JsWindowStage::OnGetMainWindow is called"); - if (windowScene_ == nullptr) { - HILOG_ERROR("JsWindowStage::OnGetMainWindow windowScene_ is nullptr"); - return engine.CreateUndefined(); - } - AsyncTask::CompleteCallback complete = - [this](NativeEngine& engine, AsyncTask& task, int32_t status) { - auto window = windowScene_->GetMainWindow(); - if (window != nullptr) { - task.Resolve(engine, OHOS::Rosen::CreateJsWindowObject(engine, window)); - HILOG_INFO("JsWindowStage::OnGetMainWindow success"); - } else { - task.Reject(engine, CreateJsError(engine, - static_cast(Rosen::WMError::WM_ERROR_NULLPTR), - "JsWindowStage::OnGetMainWindow failed.")); - } - }; - NativeValue* callback = info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr; - NativeValue* result = nullptr; - AsyncTask::Schedule( - engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, - std::move(complete), &result)); - return result; -} - -NativeValue* JsWindowStage::OnEvent(NativeEngine& engine, NativeCallbackInfo& info) -{ - HILOG_INFO("JsWindowStage::OnEvent is called"); - if (windowScene_ == nullptr) { - HILOG_ERROR("JsWindowStage::OnEvent windowScene_ is nullptr"); - return engine.CreateUndefined(); - } - - if (info.argc < 2) { // 2: minimum param nums - HILOG_ERROR("JsWindowStage::OnEvent wrong input params"); - return engine.CreateUndefined(); - } - - // Parse info->argv[0] as string - std::string eventString; - if (!ConvertFromJsValue(engine, info.argv[0], eventString)) { - HILOG_ERROR("JsWindowStage::OnEvent info->argv[0] Failed to convert parameter to string"); - return engine.CreateUndefined(); - } - if (eventString.compare("windowStageEvent") != 0) { - HILOG_ERROR("JsWindowStage::OnEvent info->argv[0] is %{public}s, InValid", - eventString.c_str()); - return engine.CreateUndefined(); - } - - NativeValue* value = info.argv[1]; - if (!value->IsCallable()) { - HILOG_ERROR("JsWindowStage::OnEvent info->argv[1] is not callable"); - return engine.CreateUndefined(); - } - - std::shared_ptr refence = nullptr; - refence.reset(engine.CreateReference(value, 1)); - eventCallbackMap_[refence] = 1; - engine_ = &engine; - - AsyncTask::CompleteCallback complete = - [this](NativeEngine& engine, AsyncTask& task, int32_t status) { - // regist lifecycle listener - if (regLifeCycleListenerFlag_ == false) { - auto window = windowScene_->GetMainWindow(); - if (window != nullptr) { - sptr listener = this; - window->RegisterLifeCycleListener(listener); - regLifeCycleListenerFlag_ = true; - } - } - task.Resolve(engine, engine.CreateUndefined()); - HILOG_INFO("JsWindowStage::OnEvent regist lifecycle success"); - }; - NativeValue* lastParam = (info.argc == 2) ? nullptr : info.argv[2]; // 2: minimum param nums - NativeValue* result = nullptr; - AsyncTask::Schedule(engine, - CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); - return result; -} - -NativeValue* JsWindowStage::OffEvent(NativeEngine& engine, NativeCallbackInfo& info) -{ - HILOG_INFO("JsWindowStage::OffEvent is called"); - if (windowScene_ == nullptr) { - HILOG_ERROR("JsWindowStage::OffEvent windowScene_ is nullptr"); - return engine.CreateUndefined(); - } - if (info.argc < 1 || info.argc > 2) { // 1: minimum param nums, 2: maximum param nums - HILOG_ERROR("JsWindowStage::OffEvent wrong input params"); - return engine.CreateUndefined(); - } - - // Parse info->argv[0] as string - std::string eventString; - if (!ConvertFromJsValue(engine, info.argv[0], eventString)) { - HILOG_ERROR("JsWindowStage::OffEvent info->argv[0] Failed to convert parameter to string"); - return engine.CreateUndefined(); - } - if (eventString.compare("windowStageEvent") != 0) { - HILOG_ERROR("JsWindowStage::OffEvent info->argv[0] is InValid"); - return engine.CreateUndefined(); - } - - if (info.argc == 1) { // 1: input param nums - HILOG_ERROR("JsWindowStage::OffEvent info.argc == 1"); - eventCallbackMap_.clear(); - return engine.CreateUndefined(); - } - - HILOG_INFO("JsWindowStage::OffEvent info.argc == 2"); - NativeValue* value = info.argv[1]; - if (value->IsCallable()) { - HILOG_INFO("JsWindowStage::OffEvent info->argv[1] is callable type"); - for (auto iter = eventCallbackMap_.begin(); iter != eventCallbackMap_.end(); iter++) { - std::shared_ptr callback = iter->first; - if (value->StrictEquals(callback->Get())) { - eventCallbackMap_.erase(iter); - break; - } - } - return engine.CreateUndefined(); - } else if (value->TypeOf() == NativeValueType::NATIVE_UNDEFINED) { - HILOG_INFO("JsWindowStage::OffEvent info->argv[1] is native undefined type"); - eventCallbackMap_.clear(); - } else { - HILOG_ERROR("JsWindowStage::OffEvent info->argv[1] is InValid param"); - } - return engine.CreateUndefined(); -} - -NativeValue* JsWindowStage::OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info) -{ - HILOG_INFO("JsWindowStage::OnLoadContent is called"); - if (info.argc <= 0 || windowScene_ == nullptr) { - HILOG_ERROR("JsWindowStage param not match or windowScene_ is nullptr"); - return engine.CreateUndefined(); - } - std::string contextUrl; - if (!ConvertFromJsValue(engine, info.argv[0], contextUrl)) { - HILOG_ERROR("Failed to convert parameter to context url"); - return engine.CreateUndefined(); - } - NativeValue* storage = nullptr; - NativeValue* callBack = nullptr; - NativeValue* value1 = info.argv[INDEX_ONE]; - NativeValue* value2 = info.argv[INDEX_TWO]; - if (value1->TypeOf() == NATIVE_FUNCTION) { - callBack = value1; - } else if (value1->TypeOf() == NATIVE_OBJECT) { - storage = value1; - } else if (value2->TypeOf() == NATIVE_FUNCTION) { - callBack = value2; - } - contentStorage_ = static_cast(storage); - AsyncTask::CompleteCallback complete = - [this, contextUrl](NativeEngine& engine, AsyncTask& task, int32_t status) { - auto win = windowScene_->GetMainWindow(); - if (win == nullptr) { - task.Reject(engine, - CreateJsError(engine, static_cast(Rosen::WMError::WM_ERROR_NULLPTR), - "JsWindowStage::OnLoadContent failed.")); - } - Rosen::WMError ret = win->SetUIContent(contextUrl, &engine, - static_cast(contentStorage_), false); - if (ret == Rosen::WMError::WM_OK) { - task.Resolve(engine, engine.CreateUndefined()); - HILOG_INFO("JsWindowStage::OnLoadContent success"); - } else { - task.Reject(engine, - CreateJsError(engine, static_cast(ret), "JsWindowStage::OnLoadContent failed.")); - } - }; - NativeValue* result = nullptr; - AsyncTask::Schedule( - engine, CreateAsyncTaskWithLastParam(engine, callBack, nullptr, std::move(complete), &result)); - return result; -} - -NativeValue* JsWindowStage::OnGetWindowMode(NativeEngine& engine, NativeCallbackInfo& info) -{ - HILOG_INFO("JsWindowStage::OnGetWindowMode is called"); - AsyncTask::CompleteCallback complete = - [this](NativeEngine& engine, AsyncTask& task, int32_t status) { - auto window = windowScene_->GetMainWindow(); - if (window == nullptr) { - task.Reject(engine, CreateJsError(engine, static_cast(Rosen::WMError::WM_ERROR_NULLPTR), - "JsWindowStage::OnGetWindowMode failed.")); - HILOG_ERROR("JsWindowStage window is nullptr"); - return; - } - Rosen::WindowMode mode = window->GetMode(); - task.Resolve(engine, CreateJsValue(engine, mode)); - HILOG_INFO("JsWindowStage OnGetWindowMode success"); - }; - NativeValue* callback = info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr; - NativeValue* result = nullptr; - AsyncTask::Schedule( - engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); - return result; -} - -NativeValue* JsWindowStage::OnCreateSubWindow(NativeEngine& engine, NativeCallbackInfo& info) -{ - HILOG_INFO("JsWindowStage::OnCreateSubWindow is called"); - if (info.argc < ARGC_ONE) { - HILOG_ERROR("JsWindowStage::OnCreateSubWindow params less than 1!"); - return engine.CreateUndefined(); - } - std::string windowName; - if (!ConvertFromJsValue(engine, info.argv[0], windowName)) { - HILOG_ERROR("Failed to convert parameter to windowName"); - return engine.CreateUndefined(); - } - AsyncTask::CompleteCallback complete = - [this, windowName](NativeEngine& engine, AsyncTask& task, int32_t status) { - sptr windowOption = new Rosen::WindowOption(); - windowOption->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW); - auto window = windowScene_->CreateWindow(windowName, windowOption); - if (window == nullptr) { - task.Reject(engine, CreateJsError(engine, static_cast(Rosen::WMError::WM_ERROR_NULLPTR), - "JsWindowStage::OnCreateSubWindow failed.")); - HILOG_ERROR("JsWindowStage window is nullptr"); - return; - } - task.Resolve(engine, CreateJsWindowObject(engine, window)); - HILOG_INFO("JsWindowStage OnCreateSubWindow success"); - }; - NativeValue* callback = info.argv[INDEX_ONE]->TypeOf() == NATIVE_FUNCTION ? info.argv[INDEX_ONE] : nullptr; - NativeValue* result = nullptr; - AsyncTask::Schedule( - engine, CreateAsyncTaskWithLastParam(engine, callback, nullptr, std::move(complete), &result)); - return result; -} - -NativeValue* CreateJsWindowStage(NativeEngine& engine, - std::shared_ptr windowScene) -{ - HILOG_INFO("JsWindowStage::CreateJsWindowStage is called"); - NativeValue* objValue = engine.CreateObject(); - NativeObject* object = AbilityRuntime::ConvertNativeValueTo(objValue); - - std::unique_ptr jsWindowStage = - std::make_unique(windowScene, objValue); - object->SetNativePointer(jsWindowStage.release(), JsWindowStage::Finalizer, nullptr); - - AbilityRuntime::BindNativeFunction(engine, - *object, "setUIContent", JsWindowStage::SetUIContent); - AbilityRuntime::BindNativeFunction(engine, - *object, "loadContent", JsWindowStage::LoadContent); - AbilityRuntime::BindNativeFunction(engine, - *object, "getMainWindow", JsWindowStage::GetMainWindow); - AbilityRuntime::BindNativeFunction(engine, - *object, "getWindowMode", JsWindowStage::GetWindowMode); - AbilityRuntime::BindNativeFunction(engine, - *object, "createSubWindow", JsWindowStage::CreateSubWindow); - AbilityRuntime::BindNativeFunction(engine, *object, "on", JsWindowStage::On); - AbilityRuntime::BindNativeFunction(engine, *object, "off", JsWindowStage::Off); - - return objValue; -} -} // namespace AbilityRuntime -} // namespace OHOS diff --git a/frameworks/kits/ability/native/src/ability_runtime/window_stage_api/@ohos.window_stage.d.ts b/frameworks/kits/ability/native/src/ability_runtime/window_stage_api/@ohos.window_stage.d.ts deleted file mode 100644 index 20c3d443921..00000000000 --- a/frameworks/kits/ability/native/src/ability_runtime/window_stage_api/@ohos.window_stage.d.ts +++ /dev/null @@ -1,91 +0,0 @@ -/* -* 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. -*/ - -import { AsyncCallback, Callback } from './basic'; - -declare namespace windowStage { - - enum WindowStageEventType { - VISIBLE = 1, - FOCUSED, - UNFOCUSED, - INVISIBLE, - } - - /** - * WindowStage - * @devices tv, phone, tablet, wearable, liteWearable. - */ - interface WindowStage { - /** - * Get main window of the stage. - * @since 8 - */ - getMainWindow(): Promise; - /** - * Loads content - * @param path path Path of the page to which the content will be loaded - * @param storage storage The data object shared within the content instance loaded by the window - * @devices tv, phone, tablet, wearable, car - * @since 8 - */ - loadContent(path: string, storage: ContenStorage, callback: AsyncCallback): void; - /** - * Loads content - * @param path path of the page to which the content will be loaded - * @devices tv, phone, tablet, wearable, car - * @since 8 - */ - loadContent(path: string, callback: AsyncCallback): void; - /** - * Loads content - * @param path path of the page to which the content will be loaded - * @devices tv, phone, tablet, wearable, car - * @since 8 - */ - loadContent(path: string, storage?: ContenStorage): Promise; - /** - * get the windowmode of current window - * @devices tv, phone, tablet, wearable, car - * @systemapi - * @since 8 - */ - getWindowMode(callback: AsyncCallback): void; - /** - * get the windowmode of current window - * @devices tv, phone, tablet, wearable, car - * @systemapi - * @since 8 - */ - getWindowMode(): Promise; - /** - * window stage event callback on. - * @since 8 - */ - on(eventType: 'windowStageEvent', callback: Callback): void; - /** - * window stage event callback off. - * @since 8 - */ - off(eventType: 'windowStageEvent', callback: Callback): void; - /** - * window stage event callback off. - * @since 8 - */ - off(eventType: 'windowStageEvent'): void; - } -} - -export default windowStage; -- Gitee