From 3688c657d17eb11ac120f377c7fcfd5172f915af Mon Sep 17 00:00:00 2001 From: chyyy Date: Sun, 16 Jan 2022 17:20:23 +0800 Subject: [PATCH] add load content Change-Id: I412a0b9023849a5cb5149a638c71feabb9802156 Signed-off-by: chyyy --- interfaces/innerkits/wm/window.h | 7 ++- interfaces/innerkits/wm/window_scene.h | 5 +- .../kits/napi/window_runtime/js_window.cpp | 53 +++++++++++++++++++ .../kits/napi/window_runtime/js_window.h | 3 ++ .../napi/window_runtime/js_window_manager.cpp | 52 +++++++++++++----- .../napi/window_runtime/js_window_utils.h | 2 + wm/BUILD.gn | 5 ++ wm/include/static_call.h | 2 +- wm/include/window_impl.h | 7 ++- wm/src/static_call.cpp | 4 +- wm/src/window.cpp | 4 +- wm/src/window_impl.cpp | 35 ++++++++++-- wm/src/window_scene.cpp | 5 +- wm/test/systemtest/window_subwindow_test.cpp | 2 +- wm/test/systemtest/window_test_utils.cpp | 2 +- wm/test/unittest/mock_static_call.h | 3 +- 16 files changed, 156 insertions(+), 35 deletions(-) diff --git a/interfaces/innerkits/wm/window.h b/interfaces/innerkits/wm/window.h index 4ba32e0a8b..665b3e1b16 100644 --- a/interfaces/innerkits/wm/window.h +++ b/interfaces/innerkits/wm/window.h @@ -24,6 +24,7 @@ #include #include #include +#include "foundation/appexecfwk/standard/kits/appkit/native/ability_runtime/context/context.h" #include "wm_common.h" #include "window_option.h" #include "window_life_cycle_interface.h" @@ -35,7 +36,7 @@ namespace OHOS::AppExecFwk { } namespace OHOS::AbilityRuntime { -class AbilityContext; + class AbilityContext; } namespace OHOS { @@ -48,7 +49,7 @@ public: class Window : public RefBase { public: static sptr Create(const std::string& windowName, - sptr& option, const std::shared_ptr& abilityContext = nullptr); + sptr& option, const std::shared_ptr& context = nullptr); static sptr Find(const std::string& windowName); virtual std::shared_ptr GetSurfaceNode() const = 0; @@ -86,6 +87,8 @@ public: virtual void RegisterWindowChangeListener(sptr& listener) = 0; virtual WMError SetUIContent(std::shared_ptr context, std::string& contentInfo, NativeEngine* engine, NativeValue* storage, bool isdistributed = false) = 0; + virtual WMError SetUIContent(const std::string& contentInfo, NativeEngine* engine, + NativeValue* storage, bool isdistributed = false) = 0; virtual const std::string& GetContentInfo() = 0; }; } diff --git a/interfaces/innerkits/wm/window_scene.h b/interfaces/innerkits/wm/window_scene.h index fb647ebe02..073ec4718c 100644 --- a/interfaces/innerkits/wm/window_scene.h +++ b/interfaces/innerkits/wm/window_scene.h @@ -18,7 +18,6 @@ #include #include -#include #include #include "window.h" @@ -34,7 +33,7 @@ public: WindowScene() = default; ~WindowScene(); - WMError Init(int32_t displayId, std::shared_ptr& abilityContext, + WMError Init(int32_t displayId, const std::shared_ptr& context, sptr& listener, sptr option = nullptr); sptr CreateWindow(const std::string& windowName, sptr& option) const; @@ -55,7 +54,7 @@ private: sptr mainWindow_ = nullptr; int32_t displayId_ = DEFAULT_DISPLAY_ID; - std::shared_ptr abilityContext_ = nullptr; + std::shared_ptr context_ = nullptr; }; } // namespace Rosen } // namespace OHOS diff --git a/interfaces/kits/napi/window_runtime/js_window.cpp b/interfaces/kits/napi/window_runtime/js_window.cpp index 1d9f66cc66..53581baf1f 100644 --- a/interfaces/kits/napi/window_runtime/js_window.cpp +++ b/interfaces/kits/napi/window_runtime/js_window.cpp @@ -104,6 +104,13 @@ NativeValue* JsWindow::UnRegisterWindowCallback(NativeEngine* engine, NativeCall return (me != nullptr) ? me->OnUnRegisterWindowCallback(*engine, *info) : nullptr; } +NativeValue* JsWindow::LoadContent(NativeEngine* engine, NativeCallbackInfo* info) +{ + WLOGFI("JsWindow::LoadContent is called"); + JsWindow* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnLoadContent(*engine, *info) : nullptr; +} + NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info) { WLOGFI("JsWindow::OnShow is called"); @@ -395,6 +402,51 @@ NativeValue* JsWindow::OnUnRegisterWindowCallback(NativeEngine& engine, NativeCa return engine.CreateUndefined(); } +NativeValue* JsWindow::OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info) +{ + WLOGFI("JsWindow::OnLoadContent is called"); + if (windowToken_ == nullptr || info.argc <= 0) { + WLOGFE("JsWindow windowToken_ is nullptr"); + return engine.CreateUndefined(); + } + std::string contextUrl; + if (!ConvertFromJsValue(engine, info.argv[0], contextUrl)) { + WLOGFE("Failed to convert parameter to context url"); + return engine.CreateUndefined(); + } + 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]; + } + contentStorage_ = static_cast(storage); + AsyncTask::CompleteCallback complete = + [this, contextUrl](NativeEngine& engine, AsyncTask& task, int32_t status) { + WMError ret = windowToken_->SetUIContent(contextUrl, &engine, + static_cast(contentStorage_), false); + if (ret == WMError::WM_OK) { + task.Resolve(engine, engine.CreateUndefined()); + WLOGFI("JsWindow::OnLoadContent success"); + } else { + task.Reject(engine, + CreateJsError(engine, static_cast(ret), "JsWindow::OnLoadContent failed.")); + } + }; + + NativeValue* result = nullptr; + AsyncTask::Schedule( + engine, CreateAsyncTaskWithLastParam(engine, callBack, nullptr, std::move(complete), &result)); + return result; +} + NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr& window) { WLOGFI("JsWindow::CreateJsWindow is called"); @@ -414,6 +466,7 @@ NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr& window) BindNativeFunction(engine, *object, "getProperties", JsWindow::GetProperties); BindNativeFunction(engine, *object, "on", JsWindow::RegisterWindowCallback); BindNativeFunction(engine, *object, "off", JsWindow::UnRegisterWindowCallback); + BindNativeFunction(engine, *object, "loadContent", JsWindow::LoadContent); return objValue; } } // namespace Rosen diff --git a/interfaces/kits/napi/window_runtime/js_window.h b/interfaces/kits/napi/window_runtime/js_window.h index dceeea194a..b449dccad9 100644 --- a/interfaces/kits/napi/window_runtime/js_window.h +++ b/interfaces/kits/napi/window_runtime/js_window.h @@ -40,6 +40,7 @@ public: static NativeValue* GetProperties(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* RegisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* UnRegisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* LoadContent(NativeEngine* engine, NativeCallbackInfo* info); private: NativeValue* OnShow(NativeEngine& engine, NativeCallbackInfo& info); @@ -52,8 +53,10 @@ private: NativeValue* OnGetProperties(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnRegisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnUnRegisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info); sptr windowToken_ = nullptr; sptr windowListener_ = nullptr; + void* contentStorage_ = nullptr; }; } // namespace Rosen } // namespace OHOS diff --git a/interfaces/kits/napi/window_runtime/js_window_manager.cpp b/interfaces/kits/napi/window_runtime/js_window_manager.cpp index 1e883acb1b..f4aaf9a701 100644 --- a/interfaces/kits/napi/window_runtime/js_window_manager.cpp +++ b/interfaces/kits/napi/window_runtime/js_window_manager.cpp @@ -51,43 +51,71 @@ public: } private: + std::weak_ptr context_; + bool GetNativeContext(NativeValue* nativeContext) + { + if (nativeContext != nullptr) { + // Parse info->argv[0] as abilitycontext + auto objContext = AbilityRuntime::ConvertNativeValueTo(nativeContext); + if (objContext == nullptr) { + return false; + } + auto context = static_cast*>(objContext->GetNativePointer()); + context_ = context->lock(); + } + return true; + } NativeValue* OnCreateWindow(NativeEngine& engine, NativeCallbackInfo& info) { WLOGFI("JsOnCreateWindow is called"); - - if (info.argc < ARGC_TWO) { - WLOGFE("OnCreateWindow MUST set windowname"); + if (info.argc <= 0) { + WLOGFE("parames num not match!"); return engine.CreateUndefined(); } + NativeValue* nativeString = nullptr; + NativeValue* nativeContext = nullptr; + NativeValue* nativeType = nullptr; + NativeValue* callback = nullptr; + if (info.argv[0]->TypeOf() == NATIVE_STRING) { + nativeString = info.argv[0]; + nativeType = info.argv[ARGC_ONE]; + callback = (info.argc == ARGC_TWO) ? nullptr : info.argv[INDEX_TWO]; + } else { + nativeContext = info.argv[0]; + nativeString = info.argv[ARGC_ONE]; + nativeType = info.argv[ARGC_TWO]; + callback = (info.argc == ARGC_THREE) ? nullptr : info.argv[INDEX_THREE]; + } std::string windowName; - if (!ConvertFromJsValue(engine, info.argv[0], windowName)) { + if (!ConvertFromJsValue(engine, nativeString, windowName)) { WLOGFE("Failed to convert parameter to windowName"); return engine.CreateUndefined(); } - NativeNumber* nativeType = ConvertNativeValueTo(info.argv[1]); - if (nativeType == nullptr) { + NativeNumber* type = ConvertNativeValueTo(nativeType); + if (type == nullptr) { WLOGFE("Failed to convert parameter to windowType"); return engine.CreateUndefined(); } - WindowType winType = static_cast(static_cast(*nativeType)); + WindowType winType = static_cast(static_cast(*type)); + if (!GetNativeContext(nativeContext)) { + return engine.CreateUndefined(); + } AsyncTask::CompleteCallback complete = - [windowName, winType](NativeEngine& engine, AsyncTask& task, int32_t status) { + [weak = context_, windowName, winType](NativeEngine& engine, AsyncTask& task, int32_t status) { sptr windowOption = new WindowOption(); windowOption->SetWindowType(winType); - sptr window = Window::Create(windowName, windowOption); + sptr window = Window::Create(windowName, windowOption, weak.lock()); if (window != nullptr) { task.Resolve(engine, CreateJsWindowObject(engine, window)); - WLOGFI("JsWindowManager::OnCreateWindow success"); } else { task.Reject(engine, CreateJsError(engine, static_cast(WMError::WM_ERROR_NULLPTR), "JsWindowManager::OnCreateWindow failed.")); } }; - NativeValue* lastParam = (info.argc == ARGC_TWO) ? nullptr : info.argv[INDEX_TWO]; 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; } diff --git a/interfaces/kits/napi/window_runtime/js_window_utils.h b/interfaces/kits/napi/window_runtime/js_window_utils.h index a00c712ddc..5d209d739f 100644 --- a/interfaces/kits/napi/window_runtime/js_window_utils.h +++ b/interfaces/kits/napi/window_runtime/js_window_utils.h @@ -25,8 +25,10 @@ namespace Rosen { namespace { constexpr size_t ARGC_ONE = 1; constexpr size_t ARGC_TWO = 2; + constexpr size_t ARGC_THREE = 3; constexpr int32_t INDEX_ONE = 1; constexpr int32_t INDEX_TWO = 2; + constexpr int32_t INDEX_THREE = 3; } NativeValue* CreateJsWindowPropertiesObject(NativeEngine& engine, sptr& window); diff --git a/wm/BUILD.gn b/wm/BUILD.gn index b0cb7b4e40..226606a668 100644 --- a/wm/BUILD.gn +++ b/wm/BUILD.gn @@ -136,6 +136,11 @@ ohos_shared_library("libwm") { # weston adapter "//foundation/windowmanager/adapter:libwmadapter", + + # context + + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/kits:app_context", ] external_deps = [ diff --git a/wm/include/static_call.h b/wm/include/static_call.h index fae17a46e0..d5aab8395f 100644 --- a/wm/include/static_call.h +++ b/wm/include/static_call.h @@ -27,7 +27,7 @@ class StaticCall { WM_DECLARE_SINGLE_INSTANCE_BASE(StaticCall); public: virtual sptr CreateWindow(const std::string& windowName, - sptr& option, std::shared_ptr abilityContext = nullptr); + sptr& option, std::shared_ptr context = nullptr); protected: StaticCall() = default; private: diff --git a/wm/include/window_impl.h b/wm/include/window_impl.h index d7a7ac2415..5a323707f4 100644 --- a/wm/include/window_impl.h +++ b/wm/include/window_impl.h @@ -59,7 +59,7 @@ public: virtual WMError SetSystemBarProperty(WindowType type, const SystemBarProperty& property) override; WMError Create(const std::string& parentName, - const std::shared_ptr& abilityContext = nullptr); + const std::shared_ptr& context = nullptr); virtual WMError Destroy() override; virtual WMError Show() override; virtual WMError Hide() override; @@ -81,6 +81,8 @@ public: void UpdateFocusStatus(bool focused); virtual void UpdateConfiguration(const std::shared_ptr& configuration) override; + virtual WMError SetUIContent(const std::string& contentInfo, NativeEngine* engine, + NativeValue* storage, bool isdistributed) override; virtual WMError SetUIContent(std::shared_ptr context, std::string& contentInfo, NativeEngine* engine, NativeValue* storage, bool isdistributed) override; virtual const std::string& GetContentInfo() override; @@ -132,7 +134,8 @@ private: std::shared_ptr surfaceNode_; std::string name_; std::unique_ptr uiContent_; - std::shared_ptr abilityContext_; + std::shared_ptr abilityContext_; // give up when context offer getToken + std::shared_ptr context_; const float STATUS_BAR_RATIO = 0.07; const float NAVIGATION_BAR_RATIO = 0.07; const float SYSTEM_ALARM_WINDOW_WIDTH_RATIO = 0.8; diff --git a/wm/src/static_call.cpp b/wm/src/static_call.cpp index 102612b895..bc59da6c49 100644 --- a/wm/src/static_call.cpp +++ b/wm/src/static_call.cpp @@ -20,9 +20,9 @@ namespace Rosen { WM_IMPLEMENT_SINGLE_INSTANCE(StaticCall) sptr StaticCall::CreateWindow(const std::string& windowName, - sptr& option, std::shared_ptr abilityContext) + sptr& option, std::shared_ptr context) { - return Window::Create(windowName, option, abilityContext); + return Window::Create(windowName, option, context); } } // namespace Rosen } // namespace OHOS \ No newline at end of file diff --git a/wm/src/window.cpp b/wm/src/window.cpp index b054d93884..db52ba1e1b 100644 --- a/wm/src/window.cpp +++ b/wm/src/window.cpp @@ -24,7 +24,7 @@ namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "WindowImpl"}; } sptr Window::Create(const std::string& windowName, sptr& option, - const std::shared_ptr& abilityContext) + const std::shared_ptr& context) { if (windowName.empty()) { WLOGFE("window name is empty"); @@ -40,7 +40,7 @@ sptr Window::Create(const std::string& windowName, sptr& o } option->SetWindowName(windowName); sptr windowImpl = new WindowImpl(option); - WMError error = windowImpl->Create(option->GetParentName(), abilityContext); + WMError error = windowImpl->Create(option->GetParentName(), context); if (error != WMError::WM_OK) { return nullptr; } diff --git a/wm/src/window_impl.cpp b/wm/src/window_impl.cpp index a0d471b558..33ee62089e 100644 --- a/wm/src/window_impl.cpp +++ b/wm/src/window_impl.cpp @@ -192,6 +192,7 @@ WMError WindowImpl::SetUIContent(std::shared_ptr std::string& contentInfo, NativeEngine* engine, NativeValue* storage, bool isdistributed) { WLOGFI("SetUIContent"); + WLOGFI("contentInfo: %{public}s, context:%{public}p", contentInfo.c_str(), context.get()); uiContent_ = Ace::UIContent::Create(context.get(), engine); if (uiContent_ == nullptr) { WLOGFE("fail to SetUIContent id: %{public}d", property_->GetWindowId()); @@ -205,6 +206,28 @@ WMError WindowImpl::SetUIContent(std::shared_ptr return WMError::WM_OK; } +WMError WindowImpl::SetUIContent(const std::string& contentInfo, + NativeEngine* engine, NativeValue* storage, bool isdistributed) +{ + WLOGFI("SetUIContent"); + if (context_.get() == nullptr) { + WLOGFE("SetUIContent context_ is nullptr id: %{public}d", property_->GetWindowId()); + return WMError::WM_ERROR_NULLPTR; + } + WLOGFI("contentInfo: %{public}s, context_:%{public}p", contentInfo.c_str(), context_.get()); + uiContent_ = Ace::UIContent::Create(context_.get(), engine); + if (uiContent_ == nullptr) { + WLOGFE("fail to SetUIContent id: %{public}d", property_->GetWindowId()); + return WMError::WM_ERROR_NULLPTR; + } + if (isdistributed) { + uiContent_->Restore(this, contentInfo, storage); + } else { + uiContent_->Initialize(this, contentInfo, storage); + } + return WMError::WM_OK; +} + const std::string& WindowImpl::GetContentInfo() { WLOGFI("GetContentInfo"); @@ -237,8 +260,7 @@ WMError WindowImpl::SetSystemBarProperty(WindowType type, const SystemBarPropert return ret; } -WMError WindowImpl::Create(const std::string& parentName, - const std::shared_ptr& abilityContext) +WMError WindowImpl::Create(const std::string& parentName, const std::shared_ptr& context) { WLOGFI("[Client] Window Create"); #ifdef _NEW_RENDERSERVER_ @@ -269,9 +291,11 @@ WMError WindowImpl::Create(const std::string& parentName, WLOGFE("create window failed with errCode:%{public}d", static_cast(ret)); return ret; } - if (abilityContext != nullptr) { - ret = SingletonContainer::Get().SaveAbilityToken(abilityContext->GetAbilityToken(), windowId); - abilityContext_ = abilityContext; + context_ = context; + // FIX ME: use context_ + abilityContext_ = AbilityRuntime::Context::ConvertTo(context); + if (abilityContext_ != nullptr) { + ret = SingletonContainer::Get().SaveAbilityToken(abilityContext_->GetAbilityToken(), windowId); if (ret != WMError::WM_OK) { WLOGFE("SaveAbilityToken failed with errCode:%{public}d", static_cast(ret)); return ret; @@ -503,6 +527,7 @@ void WindowImpl::ConsumeKeyEvent(std::shared_ptr& keyEvent) WLOGI("ConsumeKeyEvent keyEvent is consumed"); return; } + // FIX ME: use context_ if (abilityContext_ != nullptr) { WLOGI("ConsumeKeyEvent ability TerminateSelf"); abilityContext_->TerminateSelf(); diff --git a/wm/src/window_scene.cpp b/wm/src/window_scene.cpp index 5215694fee..c316443246 100644 --- a/wm/src/window_scene.cpp +++ b/wm/src/window_scene.cpp @@ -38,11 +38,10 @@ WindowScene::~WindowScene() } } -WMError WindowScene::Init(int32_t displayId, std::shared_ptr& abilityContext, +WMError WindowScene::Init(int32_t displayId, const std::shared_ptr& context, sptr& listener, sptr option) { displayId_ = displayId; - abilityContext_ = abilityContext; if (option == nullptr) { option = new WindowOption(); } @@ -54,7 +53,7 @@ WMError WindowScene::Init(int32_t displayId, std::shared_ptr().CreateWindow( - MAIN_WINDOW_ID + std::to_string(count++), option, abilityContext_); + MAIN_WINDOW_ID + std::to_string(count++), option, context); #endif if (mainWindow_ == nullptr) { return WMError::WM_ERROR_NULLPTR; diff --git a/wm/test/systemtest/window_subwindow_test.cpp b/wm/test/systemtest/window_subwindow_test.cpp index 3aa5f96627..cd1bbd1adf 100644 --- a/wm/test/systemtest/window_subwindow_test.cpp +++ b/wm/test/systemtest/window_subwindow_test.cpp @@ -15,7 +15,7 @@ // gtest #include - +#include #include "window.h" #include "window_life_cycle_interface.h" #include "window_option.h" diff --git a/wm/test/systemtest/window_test_utils.cpp b/wm/test/systemtest/window_test_utils.cpp index a761514717..2a252e384a 100644 --- a/wm/test/systemtest/window_test_utils.cpp +++ b/wm/test/systemtest/window_test_utils.cpp @@ -14,7 +14,7 @@ */ #include "window_test_utils.h" - +#include namespace OHOS { namespace Rosen { Rect WindowTestUtils::screenRect_ = {0, 0, 0, 0}; diff --git a/wm/test/unittest/mock_static_call.h b/wm/test/unittest/mock_static_call.h index fa14ff0b80..ec0518dda4 100644 --- a/wm/test/unittest/mock_static_call.h +++ b/wm/test/unittest/mock_static_call.h @@ -18,6 +18,7 @@ #include #include "ability_context_impl.h" +#include "foundation/appexecfwk/standard/kits/appkit/native/ability_runtime/context/context.h" #include "static_call.h" namespace OHOS { @@ -25,7 +26,7 @@ namespace Rosen { class MockStaticCall : public StaticCall { public: MOCK_METHOD3(CreateWindow, sptr(const std::string& windowName, - sptr& option, std::shared_ptr abilityContext)); + sptr& option, std::shared_ptr abilityContext)); }; } } // namespace OHOS -- Gitee