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