• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_session_impl.h"
17 
18 #include <transaction/rs_transaction.h>
19 #include "window_manager_hilog.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowExtensionSessionImpl"};
25 }
26 
WindowExtensionSessionImpl(const sptr<WindowOption> & option)27 WindowExtensionSessionImpl::WindowExtensionSessionImpl(const sptr<WindowOption>& option) : WindowSessionImpl(option)
28 {
29 }
30 
~WindowExtensionSessionImpl()31 WindowExtensionSessionImpl::~WindowExtensionSessionImpl()
32 {
33 }
34 
Create(const std::shared_ptr<AbilityRuntime::Context> & context,const sptr<Rosen::ISession> & iSession)35 WMError WindowExtensionSessionImpl::Create(const std::shared_ptr<AbilityRuntime::Context>& context,
36     const sptr<Rosen::ISession>& iSession)
37 {
38     if (!context || !iSession) {
39         WLOGFE("context is nullptr: %{public}u or sessionToken is nullptr: %{public}u",
40             context == nullptr, iSession == nullptr);
41         return WMError::WM_ERROR_NULLPTR;
42     }
43     hostSession_ = iSession;
44     context_ = context;
45     Connect();
46     state_ = WindowState::STATE_CREATED;
47     return WMError::WM_OK;
48 }
49 
MoveTo(int32_t x,int32_t y)50 WMError WindowExtensionSessionImpl::MoveTo(int32_t x, int32_t y)
51 {
52     WLOGFD("Id:%{public}d MoveTo %{public}d %{public}d", property_->GetPersistentId(), x, y);
53     if (IsWindowSessionInvalid()) {
54         WLOGFE("Window session invalid.");
55         return WMError::WM_ERROR_INVALID_WINDOW;
56     }
57     const auto& rect = property_->GetWindowRect();
58     WSRect wsRect = { x, y, rect.width_, rect.height_ };
59     WSError error = UpdateRect(wsRect, SizeChangeReason::MOVE);
60     return static_cast<WMError>(error);
61 }
62 
Resize(uint32_t width,uint32_t height)63 WMError WindowExtensionSessionImpl::Resize(uint32_t width, uint32_t height)
64 {
65     WLOGFD("Id:%{public}d Resize %{public}u %{public}u", property_->GetPersistentId(), width, height);
66     if (IsWindowSessionInvalid()) {
67         WLOGFE("Window session invalid.");
68         return WMError::WM_ERROR_INVALID_WINDOW;
69     }
70     const auto& rect = property_->GetWindowRect();
71     WSRect wsRect = { rect.posX_, rect.posY_, width, height };
72     WSError error = UpdateRect(wsRect, SizeChangeReason::RESIZE);
73     return static_cast<WMError>(error);
74 }
75 
TransferAbilityResult(uint32_t resultCode,const AAFwk::Want & want)76 WMError WindowExtensionSessionImpl::TransferAbilityResult(uint32_t resultCode, const AAFwk::Want& want)
77 {
78     if (state_ < WindowState::STATE_CREATED) {
79         WLOGFE("Extension invalid [name:%{public}s, id:%{public}d], state:%{public}u",
80             property_->GetWindowName().c_str(), property_->GetPersistentId(), state_);
81         return WMError::WM_ERROR_REPEAT_OPERATION;
82     }
83     return static_cast<WMError>(hostSession_->TransferAbilityResult(resultCode, want));
84 }
85 
TransferExtensionData(const AAFwk::WantParams & wantParams)86 WMError WindowExtensionSessionImpl::TransferExtensionData(const AAFwk::WantParams& wantParams)
87 {
88     if (state_ < WindowState::STATE_CREATED) {
89         WLOGFE("Extension invalid [name:%{public}s, id:%{public}d], state:%{public}u",
90             property_->GetWindowName().c_str(), property_->GetPersistentId(), state_);
91         return WMError::WM_ERROR_REPEAT_OPERATION;
92     }
93     return static_cast<WMError>(hostSession_->TransferExtensionData(wantParams));
94 }
95 
RegisterTransferComponentDataListener(const NotifyTransferComponentDataFunc & func)96 void WindowExtensionSessionImpl::RegisterTransferComponentDataListener(const NotifyTransferComponentDataFunc& func)
97 {
98     if (state_ < WindowState::STATE_CREATED) {
99         WLOGFE("Extension invalid [name:%{public}s, id:%{public}d], state:%{public}u",
100             property_->GetWindowName().c_str(), property_->GetPersistentId(), state_);
101         return;
102     }
103     notifyTransferComponentDataFunc_ = std::move(func);
104     hostSession_->NotifyRemoteReady();
105 }
106 
NotifyTransferComponentData(const AAFwk::WantParams & wantParams)107 WSError WindowExtensionSessionImpl::NotifyTransferComponentData(const AAFwk::WantParams& wantParams)
108 {
109     if (notifyTransferComponentDataFunc_) {
110         notifyTransferComponentDataFunc_(wantParams);
111     }
112     return WSError::WS_OK;
113 }
114 
SetPrivacyMode(bool isPrivacyMode)115 WMError WindowExtensionSessionImpl::SetPrivacyMode(bool isPrivacyMode)
116 {
117     WLOGFD("id : %{public}u, SetPrivacyMode, %{public}u", GetWindowId(), isPrivacyMode);
118     if (surfaceNode_ == nullptr) {
119         WLOGFE("surfaceNode_ is nullptr");
120         return WMError::WM_ERROR_NULLPTR;
121     }
122     surfaceNode_->SetSecurityLayer(isPrivacyMode);
123     RSTransaction::FlushImplicitTransaction();
124     return WMError::WM_OK;
125 }
126 
NotifyFocusStateEvent(bool focusState)127 void WindowExtensionSessionImpl::NotifyFocusStateEvent(bool focusState)
128 {
129     if (uiContent_) {
130         focusState ? uiContent_->Focus() : uiContent_->UnFocus();
131     }
132     focusState_ = focusState;
133 }
134 
NotifyFocusActiveEvent(bool isFocusActive)135 void WindowExtensionSessionImpl::NotifyFocusActiveEvent(bool isFocusActive)
136 {
137     if (uiContent_) {
138         uiContent_->SetIsFocusActive(isFocusActive);
139     }
140 }
141 
SetUIContent(const std::string & contentInfo,NativeEngine * engine,NativeValue * storage,bool isdistributed,AppExecFwk::Ability * ability)142 WMError WindowExtensionSessionImpl::SetUIContent(const std::string& contentInfo,
143     NativeEngine* engine, NativeValue* storage, bool isdistributed, AppExecFwk::Ability* ability)
144 {
145     WLOGFD("WindowExtensionSessionImpl SetUIContent: %{public}s state:%{public}u", contentInfo.c_str(), state_);
146     if (uiContent_) {
147         uiContent_->Destroy();
148     }
149     std::unique_ptr<Ace::UIContent> uiContent;
150     if (ability != nullptr) {
151         uiContent = Ace::UIContent::Create(ability);
152     } else {
153         uiContent = Ace::UIContent::Create(context_.get(), engine);
154     }
155     if (uiContent == nullptr) {
156         WLOGFE("fail to SetUIContent id: %{public}d", GetPersistentId());
157         return WMError::WM_ERROR_NULLPTR;
158     }
159     uiContent->Initialize(this, contentInfo, storage, property_->GetParentId());
160     // make uiContent available after Initialize/Restore
161     uiContent_ = std::move(uiContent);
162 
163     if (focusState_ != std::nullopt) {
164         focusState_.value() ? uiContent_->Focus() : uiContent_->UnFocus();
165     }
166 
167     uint32_t version = 0;
168     if ((context_ != nullptr) && (context_->GetApplicationInfo() != nullptr)) {
169         version = context_->GetApplicationInfo()->apiCompatibleVersion;
170     }
171     // 10 ArkUI new framework support after API10
172     if (version < 10) {
173         SetLayoutFullScreenByApiVersion(isIgnoreSafeArea_);
174         if (!isSystembarPropertiesSet_) {
175             SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR, SystemBarProperty());
176         }
177     } else if (isIgnoreSafeAreaNeedNotify_) {
178         SetLayoutFullScreenByApiVersion(isIgnoreSafeArea_);
179         isIgnoreSafeAreaNeedNotify_ = false;
180     }
181 
182     UpdateDecorEnable(true);
183     if (state_ == WindowState::STATE_SHOWN) {
184         // UIContent may be nullptr when show window, need to notify again when window is shown
185         uiContent_->Foreground();
186         UpdateTitleButtonVisibility();
187     }
188     UpdateViewportConfig(GetRect(), WindowSizeChangeReason::UNDEFINED);
189     WLOGFD("notify uiContent window size change end");
190     return WMError::WM_OK;
191 }
192 } // namespace Rosen
193 } // namespace OHOS
194