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 #ifndef FOUNDATION_ACE_ADAPTER_OHOS_CPP_DIALOG_CONTAINER_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_CPP_DIALOG_CONTAINER_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include "adapter/ohos/entrance/ace_container.h"
23 #include "base/resource/asset_manager.h"
24 #include "base/thread/task_executor.h"
25 #include "base/utils/noncopyable.h"
26 #include "core/common/ace_view.h"
27 #include "core/common/container.h"
28 #include "core/common/js_message_dispatcher.h"
29 #include "core/common/window.h"
30 #include "core/components/dialog/dialog_properties.h"
31 #include "core/components_ng/render/adapter/rosen_window.h"
32 
33 namespace OHOS::Ace::Platform {
34 class DialogContainer : public Container, public JsMessageDispatcher {
35     DECLARE_ACE_TYPE(DialogContainer, Container, JsMessageDispatcher);
36 
37 public:
38     explicit DialogContainer(int32_t instanceId, FrontendType type = FrontendType::DECLARATIVE_JS);
39     ~DialogContainer() override = default;
40 
Initialize()41     void Initialize() override {};
42     void Destroy() override;
43     void DestroyView() override;
44 
GetInstanceId()45     int32_t GetInstanceId() const override
46     {
47         if (aceView_) {
48             return aceView_->GetInstanceId();
49         }
50         return -1;
51     }
52 
GetFrontend()53     RefPtr<Frontend> GetFrontend() const override
54     {
55         return frontend_;
56     }
57 
GetResourceConfiguration()58     ResourceConfiguration GetResourceConfiguration() const override
59     {
60         return resourceInfo_.GetResourceConfiguration();
61     }
62 
SetResourceConfiguration(const ResourceConfiguration & config)63     void SetResourceConfiguration(const ResourceConfiguration& config)
64     {
65         resourceInfo_.SetResourceConfiguration(config);
66     }
67 
GetPlatformResRegister()68     RefPtr<PlatformResRegister> GetPlatformResRegister() const override
69     {
70         return resRegister_;
71     }
72 
GetPipelineContext()73     RefPtr<PipelineBase> GetPipelineContext() const override
74     {
75         return pipelineContext_;
76     }
77 
GetViewPosX()78     int32_t GetViewPosX() const override
79     {
80         return aceView_ ? aceView_->GetPosX() : 0;
81     }
82 
GetViewPosY()83     int32_t GetViewPosY() const override
84     {
85         return aceView_ ? aceView_->GetPosY() : 0;
86     }
87 
SetWindowId(uint32_t windowId)88     void SetWindowId(uint32_t windowId) override
89     {
90         windowId_ = windowId;
91     }
92 
GetWindowId()93     uint32_t GetWindowId() const override
94     {
95         return windowId_;
96     }
97 
GetViewWidth()98     int32_t GetViewWidth() const override
99     {
100         return aceView_ ? aceView_->GetWidth() : 0;
101     }
102 
GetViewHeight()103     int32_t GetViewHeight() const override
104     {
105         return aceView_ ? aceView_->GetHeight() : 0;
106     }
107 
GetAceView()108     RefPtr<AceView> GetAceView() const override
109     {
110         std::lock_guard<std::mutex> lock(viewMutex_);
111         return aceView_;
112     }
113 
GetView()114     void* GetView() const override
115     {
116         std::lock_guard<std::mutex> lock(viewMutex_);
117         return static_cast<void*>(AceType::RawPtr(aceView_));
118     }
119 
GetTaskExecutor()120     RefPtr<TaskExecutor> GetTaskExecutor() const override
121     {
122         return taskExecutor_;
123     }
124 
Dispatch(const std::string & group,std::vector<uint8_t> && data,int32_t id,bool replyToComponent)125     void Dispatch(
126         const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override {};
127 
DispatchPluginError(int32_t callbackId,int32_t errorCode,std::string && errorMessage)128     void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override {};
129 
DispatchSync(const std::string & group,std::vector<uint8_t> && data,uint8_t ** resData,int64_t & position)130     void DispatchSync(
131         const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, int64_t& position) const override
132     {}
133 
GetHostClassName()134     std::string GetHostClassName() const override
135     {
136         return "";
137     }
138 
139     void DumpHeapSnapshot(bool isPrivate) override;
140 
SetAssetManager(const RefPtr<AssetManager> & assetManager)141     void SetAssetManager(const RefPtr<AssetManager>& assetManager)
142     {
143         assetManager_ = assetManager;
144         if (frontend_) {
145             frontend_->SetAssetManager(assetManager);
146         }
147     }
148 
GetAssetManager()149     RefPtr<AssetManager> GetAssetManager() const override
150     {
151         return assetManager_;
152     }
153 
IsSubContainer()154     bool IsSubContainer() const override
155     {
156         return true;
157     }
158 
IsDialogContainer()159     bool IsDialogContainer() const override
160     {
161         return true;
162     }
163 
164     static void ShowToast(int32_t instanceId, const std::string& message, int32_t duration, const std::string& bottom);
165     static void ShowDialog(int32_t instanceId, const std::string& title, const std::string& message,
166         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
167         const std::set<std::string>& callbacks);
168     static void ShowDialog(int32_t instanceId, const PromptDialogAttr& dialogAttr,
169         const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback,
170         const std::set<std::string>& callbacks);
171     static void ShowActionMenu(int32_t instanceId, const std::string& title, const std::vector<ButtonInfo>& button,
172         std::function<void(int32_t, int32_t)>&& callback);
173 
174     static bool ShowToastDialogWindow(
175         int32_t instanceId, int32_t posX, int32_t posY, int32_t width, int32_t height, bool isToast = false);
176     static bool CloseWindow(int32_t instanceId);
177     static bool HideWindow(int32_t instanceId);
178 
179     static void SetUIWindow(int32_t instanceId, sptr<OHOS::Rosen::Window>& uiWindow);
180     static sptr<OHOS::Rosen::Window> GetUIWindow(int32_t instanceId);
181 
182     static void DestroyContainer(int32_t instanceId, const std::function<void()>& destroyCallback = nullptr);
183     static RefPtr<DialogContainer> GetContainer(int32_t instanceId);
184     static void SetView(const RefPtr<AceView>& view, double density, int32_t width, int32_t height,
185         sptr<OHOS::Rosen::Window>& rsWindow);
186     static void SetViewNew(const RefPtr<AceView>& view, double density, int32_t width, int32_t height,
187         sptr<OHOS::Rosen::Window>& rsWindow);
188     static bool OnBackPressed(int32_t instanceId);
189 
190     void SetFontScaleAndWeightScale(int32_t instanceId);
191     void UpdateConfiguration(const ParsedConfig& parsedConfig);
192 
193 private:
194     void InitPipelineContext(std::shared_ptr<Window> window, int32_t instanceId, double density, int32_t width,
195         int32_t height, uint32_t windowId);
196     void InitializeFrontend();
197     void InitializeCallback();
198     void InitializeTouchEventCallback();
199     void InitializeMouseEventCallback();
200     void InitializeAxisEventCallback();
201     void InitializeKeyEventCallback();
202     void InitializeRotationEventCallback();
203     void InitializeViewChangeCallback();
204     void InitializeDensityChangeCallback();
205     void InitializeSystemBarHeightChangeCallback();
206     void InitializeSurfaceDestroyCallback();
207     void InitializeDragEventCallback();
208 
209     void AttachView(std::shared_ptr<Window> window, const RefPtr<AceView>& view, double density, int32_t width,
210         int32_t height, uint32_t windowId);
211     void SetUIWindowInner(sptr<OHOS::Rosen::Window> uiWindow);
212     sptr<OHOS::Rosen::Window> GetUIWindowInner() const;
213 
214     uint32_t windowId_ = OHOS::Rosen::INVALID_WINDOW_ID;
215     int32_t instanceId_ = -1;
216     RefPtr<AceView> aceView_;
217     RefPtr<TaskExecutor> taskExecutor_;
218     RefPtr<AssetManager> assetManager_;
219     RefPtr<PlatformResRegister> resRegister_;
220     RefPtr<PipelineBase> pipelineContext_;
221     RefPtr<Frontend> frontend_;
222     FrontendType type_ = FrontendType::DECLARATIVE_JS;
223     ResourceInfo resourceInfo_;
224     sptr<OHOS::Rosen::Window> uiWindow_ = nullptr;
225     std::string windowName_;
226     WindowModal windowModal_ { WindowModal::NORMAL };
227     ColorScheme colorScheme_ { ColorScheme::FIRST_VALUE };
228     mutable std::mutex viewMutex_;
229 
230     ACE_DISALLOW_COPY_AND_MOVE(DialogContainer);
231 };
232 
233 } // namespace OHOS::Ace::Platform
234 
235 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CPP_DIALOG_CONTAINER_H
236