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 "scene_board_judgement.h"
20 #include "window_extension_connection.h"
21 #include "window_manager_hilog.h"
22 #include "wm_common.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowExtensionStubImpl"};
28 }
29
WindowExtensionStubImpl(const std::string & windowName)30 WindowExtensionStubImpl::WindowExtensionStubImpl(const std::string& windowName) : windowName_(windowName)
31 {
32 }
33
~WindowExtensionStubImpl()34 WindowExtensionStubImpl::~WindowExtensionStubImpl()
35 {
36 }
37
CreateWindow(const Rect & rect,uint32_t parentWindowId,const std::shared_ptr<AbilityRuntime::Context> & context,const sptr<IRemoteObject> & iSession)38 sptr<Window> WindowExtensionStubImpl::CreateWindow(
39 const Rect& rect, uint32_t parentWindowId, const std::shared_ptr<AbilityRuntime::Context>& context,
40 const sptr<IRemoteObject>& iSession)
41 {
42 sptr<WindowOption> option = new(std::nothrow) WindowOption();
43 if (option == nullptr) {
44 WLOGFE("Get option failed");
45 return nullptr;
46 }
47
48 option->SetWindowType(WindowType::WINDOW_TYPE_APP_COMPONENT);
49 option->SetWindowRect(rect);
50 option->SetParentId(parentWindowId);
51 option->SetWindowName(windowName_);
52 option->SetWindowSessionType(WindowSessionType::EXTENSION_SESSION);
53 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
54 WLOGI("Window::Create with session.");
55 window_ = Window::Create(option, context, iSession);
56 } else {
57 WLOGI("Window::Create");
58 window_ = Window::Create(windowName_, option, context);
59 }
60 return window_.promote();
61 }
62
SetBounds(const Rect & rect)63 void WindowExtensionStubImpl::SetBounds(const Rect& rect)
64 {
65 auto window = window_.promote();
66 if (window == nullptr) {
67 WLOGE("null window");
68 return;
69 }
70 Rect orgRect = window->GetRect();
71 WLOGD("oriRect, x = %{public}d, y = %{public}d, w = %{public}d, h = %{public}d", orgRect.posX_,
72 orgRect.posY_, orgRect.width_, orgRect.height_);
73 WLOGD("newRect, x = %{public}d, y = %{public}d, w = %{public}d, h = %{public}d", rect.posX_,
74 rect.posY_, rect.width_, rect.height_);
75 if (rect.width_ != orgRect.width_ || rect.height_ != orgRect.height_) {
76 window->Resize(rect.width_, rect.height_);
77 }
78 if (rect.posX_ != orgRect.posX_ || rect.posY_ != orgRect.posY_) {
79 window->MoveTo(rect.posX_, rect.posY_);
80 }
81 }
82
Hide()83 void WindowExtensionStubImpl::Hide()
84 {
85 auto window = window_.promote();
86 if (window != nullptr) {
87 window->Hide();
88 }
89 }
90
Show()91 void WindowExtensionStubImpl::Show()
92 {
93 auto window = window_.promote();
94 if (window != nullptr) {
95 window->Show();
96 }
97 }
98
RequestFocus()99 void WindowExtensionStubImpl::RequestFocus()
100 {
101 auto window = window_.promote();
102 if (window != nullptr) {
103 window->RequestFocus();
104 }
105 }
106
GetExtensionWindow(sptr<IWindowExtensionClient> & token)107 void WindowExtensionStubImpl::GetExtensionWindow(sptr<IWindowExtensionClient>& token)
108 {
109 std::lock_guard<std::mutex> lock(mutex_);
110 token_ = token;
111 if (token_ == nullptr) {
112 WLOGFE("token is null");
113 return;
114 }
115 auto window = window_.promote();
116
117 std::shared_ptr<RSSurfaceNode> node = (window != nullptr ? window->GetSurfaceNode() : nullptr);
118 if (node == nullptr) {
119 WLOGFE("node is null");
120 return;
121 }
122 token_->OnWindowReady(node);
123 WLOGI("called");
124 }
125
GetWindow() const126 sptr<Window> WindowExtensionStubImpl::GetWindow() const
127 {
128 return window_.promote();
129 }
130 } // namespace Rosen
131 } // namespace OHOS