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 #include "window_extension_connection_ohos.h"
16
17 #include <functional>
18 #include <memory>
19
20 #include "base/utils/utils.h"
21 #include "common/rs_color.h"
22 #include "element_name.h"
23 #include "render_service_client/core/ui/rs_surface_node.h"
24
25 #include "base/memory/ace_type.h"
26 #include "base/memory/referenced.h"
27 #include "core/common/ace_engine.h"
28 #include "core/common/container.h"
29 #include "core/common/container_scope.h"
30 #include "frameworks/base/json/json_util.h"
31 #include "frameworks/core/components_v2/ability_component/render_ability_component.h"
32
33 #ifdef OS_ACCOUNT_EXISTS
34 #include "os_account_manager.h"
35 #endif
36
37 namespace OHOS::Ace {
38
39 class ConnectionCallback : public Rosen::IWindowExtensionCallback {
40 public:
41 ACE_DISALLOW_COPY_AND_MOVE(ConnectionCallback);
ConnectionCallback(WeakPtr<RenderNode> node,int32_t instanceId)42 ConnectionCallback(WeakPtr<RenderNode> node, int32_t instanceId) : node_(std::move(node)), instanceId_(instanceId)
43 {}
44 ~ConnectionCallback() override = default;
OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode> & rsSurfaceNode)45 void OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode>& rsSurfaceNode) override
46 {
47 LOGI("OnWindowReady and ready to connect extension");
48 auto nodeStrong = node_.Upgrade();
49 if (!nodeStrong || !rsSurfaceNode) {
50 LOGI("cannot replace sureface node because the render node or surfacenode is empty");
51 return;
52 }
53 rsSurfaceNode->CreateNodeInRenderThread();
54 auto rect = nodeStrong->GetPaintRect();
55 auto size = rect.GetSize();
56 rsSurfaceNode->SetBounds(0, 0, static_cast<float>(size.Width()), static_cast<float>(size.Height()));
57 if (!originNode_) {
58 originNode_ = nodeStrong->GetRSNode();
59 }
60 nodeStrong->SyncRSNode(std::static_pointer_cast<RSNode>(rsSurfaceNode));
61 nodeStrong->MarkNeedLayout();
62 auto task = [weak = node_, instanceId = instanceId_]() {
63 ContainerScope scope(instanceId);
64 auto node = weak.Upgrade();
65 CHECK_NULL_VOID_NOLOG(node);
66 auto ability = AceType::DynamicCast<V2::RenderAbilityComponent>(node);
67 if (ability) {
68 ability->FireConnect();
69 }
70 };
71 PostTaskToUI(std::move(task));
72 }
73
OnExtensionDisconnected()74 void OnExtensionDisconnected() override
75 {
76 LOGI("window extension disconnect");
77 auto ability = AceType::DynamicCast<V2::RenderAbilityComponent>(node_.Upgrade());
78 CHECK_NULL_VOID(ability);
79 if (originNode_) {
80 ability->SyncRSNode(originNode_);
81 }
82
83 auto task = [weak = node_, instanceId = instanceId_]() {
84 ContainerScope scope(instanceId);
85 auto node = weak.Upgrade();
86 CHECK_NULL_VOID_NOLOG(node);
87 auto ability = AceType::DynamicCast<V2::RenderAbilityComponent>(node);
88 if (ability) {
89 ability->FireDisconnect();
90 }
91 };
92 PostTaskToUI(std::move(task));
93 }
94
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & event)95 void OnKeyEvent(const std::shared_ptr<MMI::KeyEvent>& event) override {}
OnPointerEvent(const std::shared_ptr<MMI::PointerEvent> & event)96 void OnPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event) override {}
OnBackPress()97 void OnBackPress() override {}
98
99 private:
PostTaskToUI(const std::function<void ()> && task) const100 void PostTaskToUI(const std::function<void()>&& task) const
101 {
102 CHECK_NULL_VOID(task);
103 auto container = AceEngine::Get().GetContainer(instanceId_);
104 CHECK_NULL_VOID(container);
105 auto context = container->GetPipelineContext();
106 CHECK_NULL_VOID(context);
107 auto taskExecutor = context->GetTaskExecutor();
108 CHECK_NULL_VOID(taskExecutor);
109 taskExecutor->PostTask(task, TaskExecutor::TaskType::UI);
110 }
111
112 std::shared_ptr<RSNode> originNode_;
113 WeakPtr<RenderNode> node_;
114 int32_t instanceId_ = -1;
115 };
116
RectConverter(const Rect & rect,Rosen::Rect & rosenRect)117 void RectConverter(const Rect& rect, Rosen::Rect& rosenRect)
118 {
119 rosenRect.posX_ = static_cast<int>(rect.GetOffset().GetX());
120 rosenRect.posY_ = static_cast<int>(rect.GetOffset().GetY());
121 rosenRect.width_ = static_cast<uint32_t>(rect.GetSize().Width());
122 rosenRect.height_ = static_cast<uint32_t>(rect.GetSize().Height());
123 }
124
WantConverter(const std::string & want,AppExecFwk::ElementName & element)125 void WantConverter(const std::string& want, AppExecFwk::ElementName& element)
126 {
127 auto json = JsonUtil::ParseJsonString(want);
128 element.SetAbilityName(json->GetValue("abilityName")->GetString());
129 element.SetBundleName(json->GetValue("bundleName")->GetString());
130 }
131
ConnectExtension(const std::string & want,const Rect & rect,WeakPtr<RenderNode> node,int32_t windowId)132 void WindowExtensionConnectionAdapterOhos::ConnectExtension(
133 const std::string& want, const Rect& rect, WeakPtr<RenderNode> node, int32_t windowId)
134 {
135 LOGI("ConnectExtension rect: %{public}s", rect.ToString().c_str());
136 #if defined(ENABLE_ROSEN_BACKEND) && defined(OS_ACCOUNT_EXISTS)
137 LOGI("connect to windows extension begin");
138 if (!windowExtension_) {
139 windowExtension_ = std::make_unique<Rosen::WindowExtensionConnection>();
140 }
141 std::vector<int32_t> userIds;
142 ErrCode code = AccountSA::OsAccountManager::QueryActiveOsAccountIds(userIds);
143 if (code != ERR_OK) {
144 LOGE("fail to queryAccountId, so cannot connect extension");
145 return;
146 }
147 Rosen::Rect rosenRect;
148 RectConverter(rect, rosenRect);
149 AppExecFwk::ElementName element;
150 WantConverter(want, element);
151
152 int32_t instanceId = -1;
153 auto container = Container::Current();
154 if (container) {
155 instanceId = container->GetInstanceId();
156 }
157 sptr<Rosen::IWindowExtensionCallback> callback = new ConnectionCallback(node, instanceId);
158 windowExtension_->ConnectExtension(element, rosenRect, userIds.front(), windowId, callback);
159 #else
160 LOGI("unrosen engine doesn't support ability component");
161 #endif
162 }
163
RemoveExtension()164 void WindowExtensionConnectionAdapterOhos::RemoveExtension()
165 {
166 if (windowExtension_) {
167 LOGI("remove extension");
168 windowExtension_->DisconnectExtension();
169 } else {
170 LOGI("ability doesn't connect to window extension. remove extension fail");
171 }
172 }
173
Show()174 void WindowExtensionConnectionAdapterOhos::Show()
175 {
176 LOGI("show WindowExtensionConnectionAdapterOhos");
177 if (windowExtension_) {
178 windowExtension_->Show();
179 } else {
180 LOGI("ability doesn't connect to window extension. show extension fail");
181 }
182 }
183
Hide()184 void WindowExtensionConnectionAdapterOhos::Hide()
185 {
186 LOGI("hide WindowExtensionConnectionAdapterOhos");
187 if (windowExtension_) {
188 windowExtension_->Hide();
189 } else {
190 LOGI("ability doesn't connect to window extension. show extension fail");
191 }
192 }
193
UpdateRect(const Rect & rect)194 void WindowExtensionConnectionAdapterOhos::UpdateRect(const Rect& rect)
195 {
196 if (windowExtension_) {
197 Rosen::Rect rosenRect;
198 RectConverter(rect, rosenRect);
199 LOGI("UpdateRect rect: %{public}s", rect.ToString().c_str());
200 windowExtension_->SetBounds(rosenRect);
201 } else {
202 LOGI("ability doesn't connect to window extension.cannot update rect region ");
203 }
204 }
205 } // namespace OHOS::Ace
206