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
38 // LCOV_EXCL_START
CreateWindow(const Rect & rect,uint32_t parentWindowId,const std::shared_ptr<AbilityRuntime::Context> & context,const sptr<IRemoteObject> & iSession)39 sptr<Window> WindowExtensionStubImpl::CreateWindow(
40 const Rect& rect, uint32_t parentWindowId, const std::shared_ptr<AbilityRuntime::Context>& context,
41 const sptr<IRemoteObject>& iSession)
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 option->SetWindowName(windowName_);
53 option->SetWindowSessionType(WindowSessionType::EXTENSION_SESSION);
54 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
55 WLOGI("Window::Create with session.");
56 window_ = Window::Create(option, context, iSession);
57 } else {
58 WLOGI("Window::Create");
59 window_ = Window::Create(windowName_, option, context);
60 }
61 return window_.promote();
62 }
63 // LCOV_EXCL_STOP
64
SetBounds(const Rect & rect)65 void WindowExtensionStubImpl::SetBounds(const Rect& rect)
66 {
67 auto window = window_.promote();
68 if (window == nullptr) {
69 WLOGE("null window");
70 return;
71 }
72 // LCOV_EXCL_START
73 Rect orgRect = window->GetRect();
74 WLOGD("oriRect, x=%{public}d, y=%{public}d, w=%{public}d, h=%{public}d; "
75 "newRect, x=%{public}d, y=%{public}d, w=%{public}d, h=%{public}d",
76 orgRect.posX_, orgRect.posY_, orgRect.width_, orgRect.height_,
77 rect.posX_, rect.posY_, rect.width_, rect.height_);
78 if (rect.width_ != orgRect.width_ || rect.height_ != orgRect.height_) {
79 window->Resize(rect.width_, rect.height_);
80 }
81 if (rect.posX_ != orgRect.posX_ || rect.posY_ != orgRect.posY_) {
82 window->MoveTo(rect.posX_, rect.posY_);
83 }
84 // LCOV_EXCL_STOP
85 }
86
Hide()87 void WindowExtensionStubImpl::Hide()
88 {
89 auto window = window_.promote();
90 if (window != nullptr) {
91 window->Hide();
92 }
93 }
94
Show()95 void WindowExtensionStubImpl::Show()
96 {
97 auto window = window_.promote();
98 if (window != nullptr) {
99 window->Show();
100 }
101 }
102
RequestFocus()103 void WindowExtensionStubImpl::RequestFocus()
104 {
105 auto window = window_.promote();
106 if (window != nullptr) {
107 window->RequestFocus();
108 }
109 }
110
111 // LCOV_EXCL_START
GetExtensionWindow(sptr<IWindowExtensionClient> & token)112 void WindowExtensionStubImpl::GetExtensionWindow(sptr<IWindowExtensionClient>& token)
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 token_ = token;
116 if (token_ == nullptr) {
117 WLOGFE("token is null");
118 return;
119 }
120 auto window = window_.promote();
121
122 std::shared_ptr<RSSurfaceNode> node = (window != nullptr ? window->GetSurfaceNode() : nullptr);
123 if (node == nullptr) {
124 WLOGFE("node is null");
125 return;
126 }
127 token_->OnWindowReady(node);
128 WLOGI("called");
129 }
130
GetWindow() const131 sptr<Window> WindowExtensionStubImpl::GetWindow() const
132 {
133 return window_.promote();
134 }
135 // LCOV_EXCL_STOP
136 } // namespace Rosen
137 } // namespace OHOS