1 /*
2 * Copyright (c) 2022 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 "window_extension_stub_impl.h"
17
18 #include "js_window_extension.h"
19 #include "window_extension_connection.h"
20 #include "window_manager_hilog.h"
21 #include "wm_common.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowExtensionStubImpl"};
27 }
28
WindowExtensionStubImpl(const std::string & windowName)29 WindowExtensionStubImpl::WindowExtensionStubImpl(const std::string& windowName) : windowName_(windowName)
30 {
31 }
32
~WindowExtensionStubImpl()33 WindowExtensionStubImpl::~WindowExtensionStubImpl()
34 {
35 if (window_ != nullptr) {
36 window_->Destroy();
37 }
38 }
39
CreateWindow(const Rect & rect,uint32_t parentWindowId,const std::shared_ptr<AbilityRuntime::Context> & context)40 sptr<Window> WindowExtensionStubImpl::CreateWindow(
41 const Rect& rect, uint32_t parentWindowId, const std::shared_ptr<AbilityRuntime::Context>& context)
42 {
43 sptr<WindowOption> option = new(std::nothrow) WindowOption();
44 if (option == nullptr) {
45 WLOGFE("Get option failed");
46 return nullptr;
47 }
48
49 option->SetWindowType(WindowType::WINDOW_TYPE_APP_COMPONENT);
50 option->SetWindowRect(rect);
51 option->SetParentId(parentWindowId);
52 WLOGFI("Window::Create");
53 window_ = Window::Create(windowName_, option, context);
54 return window_;
55 }
56
SetBounds(const Rect & rect)57 void WindowExtensionStubImpl::SetBounds(const Rect& rect)
58 {
59 if (window_ == nullptr) {
60 return;
61 }
62 Rect orgRect = window_->GetRect();
63 if (rect.width_ != orgRect.width_ || rect.height_ != orgRect.height_) {
64 window_->Resize(rect.width_, rect.height_);
65 }
66 if (rect.posX_ != orgRect.posX_ || rect.posY_ != orgRect.posY_) {
67 window_->MoveTo(rect.posX_, rect.posY_);
68 }
69 }
70
Hide()71 void WindowExtensionStubImpl::Hide()
72 {
73 if (window_ != nullptr) {
74 window_->Hide();
75 }
76 }
77
Show()78 void WindowExtensionStubImpl::Show()
79 {
80 if (window_ != nullptr) {
81 window_->Show();
82 }
83 }
84
RequestFocus()85 void WindowExtensionStubImpl::RequestFocus()
86 {
87 if (window_ != nullptr) {
88 window_->RequestFocus();
89 }
90 }
91
GetExtensionWindow(sptr<IWindowExtensionClient> & token)92 void WindowExtensionStubImpl::GetExtensionWindow(sptr<IWindowExtensionClient>& token)
93 {
94 token_ = token;
95 if (token_ == nullptr) {
96 WLOGFE("token is null");
97 return;
98 }
99
100 std::shared_ptr<RSSurfaceNode> node = (window_ != nullptr ? window_->GetSurfaceNode() : nullptr);
101 if (node == nullptr) {
102 WLOGFE("node is null");
103 return;
104 }
105 token_->OnWindowReady(node);
106 WLOGFI("called");
107 }
108
GetWindow() const109 sptr<Window> WindowExtensionStubImpl::GetWindow() const
110 {
111 return window_;
112 }
113 } // namespace Rosen
114 } // namespace OHOS