• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_CONTAINER_H
17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_CONTAINER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "adapter/preview/entrance/ace_run_args.h"
25 #include "adapter/preview/osal/fetch_manager.h"
26 #include "base/resource/asset_manager.h"
27 #include "base/thread/task_executor.h"
28 #include "base/utils/noncopyable.h"
29 #include "core/common/ace_view.h"
30 #include "core/common/container.h"
31 #include "core/common/js_message_dispatcher.h"
32 #include "core/common/platform_bridge.h"
33 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
34 #include "adapter/preview/external/ability/context.h"
35 #include "adapter/preview/external/ability/fa/fa_context.h"
36 #include "adapter/preview/external/ability/stage/stage_context.h"
37 
38 #ifndef ENABLE_ROSEN_BACKEND
39 #include "adapter/preview/entrance/flutter_ace_view.h"
40 #else
41 #include "adapter/preview/entrance/rs_ace_view.h"
42 #endif
43 
44 namespace OHOS::Ace::Platform {
45 
46 namespace {
47 // Different with mobile, we don't support multi-instances in Windows, because we only want
48 // preview UI effect, it doesn't make sense to create multi ability in one process.
49 constexpr int32_t ACE_INSTANCE_ID = 0;
50 } // namespace
51 
52 using OnRouterChangeCallback = bool (*)(const std::string currentRouterPath);
53 
54 // AceContainer is the instance have its own pipeline and thread models, it can contains multiple pages.
55 class AceContainer : public Container, public JsMessageDispatcher {
56     DECLARE_ACE_TYPE(AceContainer, Container, JsMessageDispatcher);
57 
58 public:
59     static void CreateContainer(int32_t instanceId, FrontendType type, const AceRunArgs& runArgs);
60     static void DestroyContainer(int32_t instanceId);
61 
62     static void AddAssetPath(int32_t instanceId, const std::string& packagePath, const std::vector<std::string>& paths);
63     static void SetResourcesPathAndThemeStyle(int32_t instanceId, const std::string& systemResourcesPath,
64         const std::string& appResourcesPath, const int32_t& themeId, const ColorMode& colorMode);
65 
66 #ifndef ENABLE_ROSEN_BACKEND
67     static void SetView(FlutterAceView* view, double density, int32_t width, int32_t height);
68 #else
69     static void SetView(RSAceView* view, double density, int32_t width, int32_t height, SendRenderDataCallback onRender);
70 #endif
71 
72     static void InitDeviceInfo(int32_t instanceId, const AceRunArgs& runArgs);
73     static bool RunPage(int32_t instanceId, int32_t pageId, const std::string& url, const std::string& params);
74     static RefPtr<AceContainer> GetContainerInstance(int32_t instanceId);
75     static void AddRouterChangeCallback(int32_t instanceId, const OnRouterChangeCallback& onRouterChangeCallback);
76     static void NativeOnConfigurationUpdated(int32_t instanceId);
77 
78     AceContainer(int32_t instanceId, FrontendType type, RefPtr<Context> context);
79     ~AceContainer() override = default;
80 
81     void Initialize() override;
82 
83     void Destroy() override;
84 
85     void DestroyView() override;
86 
GetInstanceId()87     int32_t GetInstanceId() const override
88     {
89         return instanceId_;
90     }
91 
GetHostClassName()92     std::string GetHostClassName() const override
93     {
94         return "";
95     }
96 
GetFrontend()97     RefPtr<Frontend> GetFrontend() const override
98     {
99         return frontend_;
100     }
101 
GetMessageBridge()102     RefPtr<PlatformBridge> GetMessageBridge() const
103     {
104         return messageBridge_;
105     }
106 
GetTaskExecutor()107     RefPtr<TaskExecutor> GetTaskExecutor() const override
108     {
109         return taskExecutor_;
110     }
111 
GetAssetManager()112     RefPtr<AssetManager> GetAssetManager() const override
113     {
114         return assetManager_;
115     }
116 
GetPlatformResRegister()117     RefPtr<PlatformResRegister> GetPlatformResRegister() const override
118     {
119         return resRegister_;
120     }
121 
GetPipelineContext()122     RefPtr<PipelineBase> GetPipelineContext() const override
123     {
124         return pipelineContext_;
125     }
126 
GetViewWidth()127     int32_t GetViewWidth() const override
128     {
129         return aceView_ ? aceView_->GetWidth() : 0;
130     }
131 
GetViewHeight()132     int32_t GetViewHeight() const override
133     {
134         return aceView_ ? aceView_->GetHeight() : 0;
135     }
136 
GetViewPosX()137     int32_t GetViewPosX() const override
138     {
139         return 0;
140     }
141 
GetViewPosY()142     int32_t GetViewPosY() const override
143     {
144         return 0;
145     }
146 
GetWindowId()147     uint32_t GetWindowId() const override
148     {
149         return 0;
150     }
151 
SetWindowId(uint32_t windowId)152     void SetWindowId(uint32_t windowId) override {}
153 
154 #ifndef ENABLE_ROSEN_BACKEND
GetAceView()155     FlutterAceView* GetAceView() const
156     {
157         return aceView_;
158     }
159 #else
GetAceView()160     RSAceView* GetAceView() const
161     {
162         return aceView_;
163     }
164 #endif
165 
GetView()166     void* GetView() const override
167     {
168         return static_cast<void*>(aceView_);
169     }
170 
SetWindowModal(WindowModal windowModal)171     void SetWindowModal(WindowModal windowModal)
172     {
173         windowModal_ = windowModal;
174     }
175 
SetColorScheme(ColorScheme colorScheme)176     void SetColorScheme(ColorScheme colorScheme)
177     {
178         colorScheme_ = colorScheme;
179     }
180 
GetType()181     FrontendType GetType() const
182     {
183         return type_;
184     }
185 
GetResourceConfiguration()186     ResourceConfiguration GetResourceConfiguration() const
187     {
188         return resourceInfo_.GetResourceConfiguration();
189     }
190 
SetResourceConfiguration(const ResourceConfiguration & config)191     void SetResourceConfiguration(const ResourceConfiguration& config)
192     {
193         resourceInfo_.SetResourceConfiguration(config);
194     }
195 
196     void UpdateResourceConfiguration(const std::string& jsonStr) override;
197 
198     void FetchResponse(const ResponseData responseData, const int32_t callbackId) const;
199 
200     void CallCurlFunction(const RequestData requestData, const int32_t callbackId) const override;
201 
202     void Dispatch(
203         const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override;
204 
DispatchSync(const std::string & group,std::vector<uint8_t> && data,uint8_t ** resData,int64_t & position)205     void DispatchSync(
206         const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, int64_t& position) const override
207     {}
208 
209     void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override;
210 
211     bool Dump(const std::vector<std::string>& params) override;
212 
213     void UpdateDeviceConfig(const DeviceConfig& deviceConfig);
214 
215     void LoadDocument(const std::string& url, const std::string& componentName);
216 
217     void RunNativeEngineLoop();
218 
219     void SetStageCardConfig(const std::string& pageProfile, const std::string& selectUrl);
220 
SetPageProfile(const std::string & pageProfile)221     void SetPageProfile(const std::string& pageProfile)
222     {
223         pageProfile_ = pageProfile;
224     }
225 
226     void InitializeStageAppConfig(const std::string& assetPath, bool formsEnabled);
227 
SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)228     void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) override
229     {
230         std::lock_guard<std::mutex> lock(cardFrontMutex_);
231         cardFrontendMap_.try_emplace(cardId, frontend);
232     }
233 
GetCardFrontend(int64_t cardId)234     WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const override
235     {
236         std::lock_guard<std::mutex> lock(cardFrontMutex_);
237         auto it = cardFrontendMap_.find(cardId);
238         if (it != cardFrontendMap_.end()) {
239             return it->second;
240         }
241         return nullptr;
242     }
243 
GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)244     void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const override
245     {
246         cardFrontendMap = cardFrontendMap_;
247     }
248 
SetCardPipeline(WeakPtr<PipelineBase> pipeline,int64_t cardId)249     void SetCardPipeline(WeakPtr<PipelineBase> pipeline, int64_t cardId) override
250     {
251         std::lock_guard<std::mutex> lock(cardPipelineMutex_);
252         cardPipelineMap_.try_emplace(cardId, pipeline);
253     }
254 
GetCardPipeline(int64_t cardId)255     WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const override
256     {
257         std::lock_guard<std::mutex> lock(cardPipelineMutex_);
258         auto it = cardPipelineMap_.find(cardId);
259         if (it == cardPipelineMap_.end()) {
260             return nullptr;
261         }
262         return it->second;
263     }
264 
265 private:
266     void InitializeFrontend();
267     void InitializeCallback();
268 
269 #ifndef ENABLE_ROSEN_BACKEND
270     void AttachView(
271         std::unique_ptr<Window> window, FlutterAceView* view, double density, int32_t width, int32_t height);
272 #else
273     void AttachView(
274         std::unique_ptr<Window> window, RSAceView* view, double density, int32_t width, int32_t height, SendRenderDataCallback onRender);
275 #endif
276 
277 #ifndef ENABLE_ROSEN_BACKEND
278     FlutterAceView* aceView_ = nullptr;
279 #else
280     RSAceView* aceView_ = nullptr;
281 #endif
282 
283     int32_t instanceId_;
284     RefPtr<TaskExecutor> taskExecutor_;
285     RefPtr<AssetManager> assetManager_;
286     RefPtr<PlatformResRegister> resRegister_;
287     RefPtr<PipelineBase> pipelineContext_;
288     RefPtr<Frontend> frontend_;
289     RefPtr<PlatformBridge> messageBridge_;
290     FrontendType type_ { FrontendType::JSON };
291     WindowModal windowModal_ { WindowModal::NORMAL };
292     ColorScheme colorScheme_ { ColorScheme::SCHEME_LIGHT };
293     ResourceInfo resourceInfo_;
294     static std::once_flag onceFlag_;
295     std::string pageProfile_;
296     std::unordered_map<int64_t, WeakPtr<Frontend>> cardFrontendMap_;
297     std::unordered_map<int64_t, WeakPtr<PipelineBase>> cardPipelineMap_;
298     mutable std::mutex cardFrontMutex_;
299     mutable std::mutex cardPipelineMutex_;
300     RefPtr<Context> context_;
301 
302     //app bar to use
303     bool installationFree_ = false;
304     int32_t labelId_;
305 
306     ACE_DISALLOW_COPY_AND_MOVE(AceContainer);
307 };
308 
309 } // namespace OHOS::Ace::Platform
310 
311 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_CONTAINER_H
312