• 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/entrance/ace_view_preview.h"
26 #include "adapter/preview/external/ability/stage/stage_pkg_context_info.h"
27 #include "adapter/preview/osal/fetch_manager.h"
28 #include "base/resource/asset_manager.h"
29 #include "base/thread/task_executor.h"
30 #include "base/utils/noncopyable.h"
31 #include "core/common/ace_view.h"
32 #include "core/common/container.h"
33 #include "core/common/js_message_dispatcher.h"
34 #include "core/common/platform_bridge.h"
35 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
36 #include "core/event/crown_event.h"
37 
38 #include <refbase.h>
39 
40 namespace OHOS::Rosen {
41     class Window;
42 }
43 
44 namespace OHOS::AbilityRuntime {
45     class Context;
46 }
47 
48 namespace OHOS::Ace::Platform {
49 
50 namespace {
51 // Different with mobile, we don't support multi-instances in Windows, because we only want
52 // preview UI effect, it doesn't make sense to create multi ability in one process.
53 constexpr int32_t ACE_INSTANCE_ID = 0;
54 } // namespace
55 
56 using UIEnvCallback = std::function<void(const OHOS::Ace::RefPtr<PipelineContext>& context)>;
57 using OnRouterChangeCallback = bool (*)(const std::string currentRouterPath);
58 
59 // AceContainer is the instance have its own pipeline and thread models, it can contains multiple pages.
60 class AceContainer : public Container, public JsMessageDispatcher {
61     DECLARE_ACE_TYPE(AceContainer, Container, JsMessageDispatcher);
62 
63 public:
64     static void CreateContainer(
65         int32_t instanceId, FrontendType type, bool useNewPipeline, bool useCurrentEventRunner = false);
66     static void DestroyContainer(int32_t instanceId);
67 
68     static void AddAssetPath(int32_t instanceId, const std::string& packagePath, const std::vector<std::string>& paths);
69     static void SetResourcesPathAndThemeStyle(int32_t instanceId, const std::string& systemResourcesPath,
70         const std::string& hmsResourcesPath, const std::string& appResourcesPath, const int32_t& themeId,
71         const ColorMode& colorMode);
72 
73 #ifndef ENABLE_ROSEN_BACKEND
74     static void SetView(AceViewPreview* view, double density, int32_t width, int32_t height);
75 #else
76     static void SetView(AceViewPreview* view, sptr<Rosen::Window> rsWindow, double density, int32_t width,
77         int32_t height, UIEnvCallback callback);
78 #endif
79 
80     static UIContentErrorCode RunPage(
81         int32_t instanceId, const std::string& url, const std::string& params, bool isNamedRouter = false);
82     static RefPtr<AceContainer> GetContainerInstance(int32_t instanceId);
83     static void AddRouterChangeCallback(int32_t instanceId, const OnRouterChangeCallback& onRouterChangeCallback);
84     static void NativeOnConfigurationUpdated(int32_t instanceId);
85 
86     AceContainer(int32_t instanceId, FrontendType type, bool useNewPipeline, bool useCurrentEventRunner = false);
87     ~AceContainer() override = default;
88 
89     void Initialize() override;
90 
91     void Destroy() override;
92 
93     void DestroyView() override;
94 
GetInstanceId()95     int32_t GetInstanceId() const override
96     {
97         return instanceId_;
98     }
99 
GetHostClassName()100     std::string GetHostClassName() const override
101     {
102         return "";
103     }
104 
GetFrontend()105     RefPtr<Frontend> GetFrontend() const override
106     {
107         return frontend_;
108     }
109 
GetMessageBridge()110     RefPtr<PlatformBridge> GetMessageBridge() const
111     {
112         return messageBridge_;
113     }
114 
GetTaskExecutor()115     RefPtr<TaskExecutor> GetTaskExecutor() const override
116     {
117         return taskExecutor_;
118     }
119 
GetAssetManager()120     RefPtr<AssetManager> GetAssetManager() const override
121     {
122         return assetManager_;
123     }
124 
GetPlatformResRegister()125     RefPtr<PlatformResRegister> GetPlatformResRegister() const override
126     {
127         return resRegister_;
128     }
129 
GetPipelineContext()130     RefPtr<PipelineBase> GetPipelineContext() const override
131     {
132         return pipelineContext_;
133     }
134 
GetViewWidth()135     int32_t GetViewWidth() const override
136     {
137         return aceView_ ? aceView_->GetWidth() : 0;
138     }
139 
GetViewHeight()140     int32_t GetViewHeight() const override
141     {
142         return aceView_ ? aceView_->GetHeight() : 0;
143     }
144 
GetViewPosX()145     int32_t GetViewPosX() const override
146     {
147         return 0;
148     }
149 
GetViewPosY()150     int32_t GetViewPosY() const override
151     {
152         return 0;
153     }
154 
GetWindowId()155     uint32_t GetWindowId() const override
156     {
157         return 0;
158     }
159 
SetWindowId(uint32_t windowId)160     void SetWindowId(uint32_t windowId) override {}
161 
WindowIsShow()162     bool WindowIsShow() const override
163     {
164         return true;
165     }
166 
GetAceView()167     RefPtr<AceView> GetAceView() const override
168     {
169         return aceView_;
170     }
171 
GetView()172     void* GetView() const override
173     {
174         return static_cast<void*>(AceType::RawPtr(aceView_));
175     }
176 
SetWindowModal(WindowModal windowModal)177     void SetWindowModal(WindowModal windowModal)
178     {
179         windowModal_ = windowModal;
180     }
181 
SetColorScheme(ColorScheme colorScheme)182     void SetColorScheme(ColorScheme colorScheme)
183     {
184         colorScheme_ = colorScheme;
185     }
186 
GetType()187     FrontendType GetType() const
188     {
189         return type_;
190     }
191 
GetResourceConfiguration()192     ResourceConfiguration GetResourceConfiguration() const override
193     {
194         return resourceInfo_.GetResourceConfiguration();
195     }
196 
SetResourceConfiguration(const ResourceConfiguration & config)197     void SetResourceConfiguration(const ResourceConfiguration& config)
198     {
199         resourceInfo_.SetResourceConfiguration(config);
200     }
201 
202     void UpdateResourceConfiguration(const std::string& jsonStr) override;
203 
204     void FetchResponse(const ResponseData responseData, const int32_t callbackId) const;
205 
206     void CallCurlFunction(const RequestData requestData, const int32_t callbackId) const override;
207 
208     void Dispatch(
209         const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override;
210 
DispatchSync(const std::string & group,std::vector<uint8_t> && data,uint8_t ** resData,int64_t & position)211     void DispatchSync(
212         const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, int64_t& position) const override
213     {}
214 
215     void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override;
216 
217     void UpdateDeviceConfig(const DeviceConfig& deviceConfig);
218 
219     void LoadDocument(const std::string& url, const std::string& componentName);
220 
221     void RunNativeEngineLoop();
222 
223     void SetStageCardConfig(const std::string& pageProfile, const std::string& selectUrl);
224 
225     void SetPkgContextInfo(const RefPtr<StagePkgContextInfo>& PkgContextInfo);
226 
SetPageProfile(const std::string & pageProfile)227     void SetPageProfile(const std::string& pageProfile)
228     {
229         pageProfile_ = pageProfile;
230     }
231 
232     void InitializeAppConfig(const std::string& assetPath, const std::string& bundleName,
233         const std::string& moduleName, const std::string& compileMode);
234 
SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)235     void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) override
236     {
237         std::lock_guard<std::mutex> lock(cardFrontMutex_);
238         cardFrontendMap_.try_emplace(cardId, frontend);
239     }
240 
GetCardFrontend(int64_t cardId)241     WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const override
242     {
243         std::lock_guard<std::mutex> lock(cardFrontMutex_);
244         auto it = cardFrontendMap_.find(cardId);
245         if (it != cardFrontendMap_.end()) {
246             return it->second;
247         }
248         return nullptr;
249     }
250 
GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)251     void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const override
252     {
253         cardFrontendMap = cardFrontendMap_;
254     }
255 
SetCardPipeline(WeakPtr<PipelineBase> pipeline,int64_t cardId)256     void SetCardPipeline(WeakPtr<PipelineBase> pipeline, int64_t cardId) override
257     {
258         std::lock_guard<std::mutex> lock(cardPipelineMutex_);
259         cardPipelineMap_.try_emplace(cardId, pipeline);
260     }
261 
GetCardPipeline(int64_t cardId)262     WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const override
263     {
264         std::lock_guard<std::mutex> lock(cardPipelineMutex_);
265         auto it = cardPipelineMap_.find(cardId);
266         if (it == cardPipelineMap_.end()) {
267             return nullptr;
268         }
269         return it->second;
270     }
271 
272     static std::string GetContentInfo(int32_t instanceId, ContentInfoType type);
SetSharedRuntime(void * runtime)273     void SetSharedRuntime(void* runtime) override
274     {
275         sharedRuntime_ = runtime;
276     }
SetInstallationFree(bool installationFree)277     void SetInstallationFree(bool installationFree)
278     {
279         installationFree_ = installationFree;
280     }
281 
SetLabelId(int32_t labelId)282     void SetLabelId(int32_t labelId)
283     {
284         labelId_ = labelId;
285     }
SetComponentModeFlag(bool isComponentMode)286     static void SetComponentModeFlag(bool isComponentMode)
287     {
288         isComponentMode_ = isComponentMode;
289     }
290 
SetContainerSdkPath(const std::string & containerSdkPath)291     void SetContainerSdkPath(const std::string& containerSdkPath)
292     {
293         containerSdkPath_ = containerSdkPath;
294     }
295 
296     void NotifyConfigurationChange(bool, const ConfigurationChange& configurationChange) override;
297 
298     // Support to execute the ets code mocked by developer
SetMockModuleList(const std::map<std::string,std::string> & mockJsonInfo)299     void SetMockModuleList(const std::map<std::string, std::string>& mockJsonInfo)
300     {
301         mockJsonInfo_ = mockJsonInfo;
302     }
303 
SetBundleName(const std::string & bundleName)304     void SetBundleName(const std::string& bundleName)
305     {
306         bundleName_ = bundleName;
307     }
308 
SetModuleName(const std::string & moduleName)309     void SetModuleName(const std::string& moduleName)
310     {
311         moduleName_ = moduleName;
312     }
313 
RegisterCrownEventCallback(CrownEventCallback && callback)314     void RegisterCrownEventCallback(CrownEventCallback&& callback)
315     {
316         ACE_DCHECK(callback);
317         crownEventCallback_ = std::move(callback);
318     }
319 
320     void SetLocalStorage(NativeReference* storage, const std::shared_ptr<OHOS::AbilityRuntime::Context>& context);
321     std::shared_ptr<OHOS::AbilityRuntime::Context> GetAbilityContextByModule(
322         const std::string& bundle, const std::string& module);
323     void SetAbilityContext(const std::weak_ptr<OHOS::AbilityRuntime::Context>& context);
RecordResAdapter(const std::string & key)324     void RecordResAdapter(const std::string& key)
325     {
326         resAdapterRecord_.emplace(key);
327     }
328 
GetResourceInfo()329     const ResourceInfo& GetResourceInfo() const
330     {
331         return resourceInfo_;
332     }
333 private:
334     void InitializeFrontend();
335     void InitializeCallback();
336     void SetHspBufferTrackerCallback();
337     // Support to execute the ets code mocked by developer
338     void SetMockModuleListToJsEngine();
339 
340 #ifndef ENABLE_ROSEN_BACKEND
341     void AttachView(
342         std::shared_ptr<Window> window, AceViewPreview* view, double density, int32_t width, int32_t height);
343 #else
344     void AttachView(std::shared_ptr<Window> window, AceViewPreview* view, double density, int32_t width, int32_t height,
345         UIEnvCallback callback);
346 #endif
347 
348     RefPtr<AceViewPreview> aceView_ = nullptr;
349     int32_t instanceId_;
350     RefPtr<TaskExecutor> taskExecutor_;
351     RefPtr<AssetManager> assetManager_;
352     RefPtr<PlatformResRegister> resRegister_;
353     RefPtr<PipelineBase> pipelineContext_;
354     RefPtr<Frontend> frontend_;
355     RefPtr<PlatformBridge> messageBridge_;
356     FrontendType type_ { FrontendType::JSON };
357     WindowModal windowModal_ { WindowModal::NORMAL };
358     ColorScheme colorScheme_ { ColorScheme::SCHEME_LIGHT };
359     ResourceInfo resourceInfo_;
360     static std::once_flag onceFlag_;
361     std::string pageProfile_;
362     std::unordered_map<int64_t, WeakPtr<Frontend>> cardFrontendMap_;
363     std::unordered_map<int64_t, WeakPtr<PipelineBase>> cardPipelineMap_;
364     mutable std::mutex cardFrontMutex_;
365     mutable std::mutex cardPipelineMutex_;
366     void* sharedRuntime_ = nullptr;
367     std::string bundleName_;
368     std::string moduleName_;
369     RefPtr<StagePkgContextInfo> PkgContextInfo_;
370     std::weak_ptr<OHOS::AbilityRuntime::Context> runtimeContext_;
371     std::unordered_set<std::string> resAdapterRecord_;
372 
373     // Support to execute the ets code mocked by developer
374     std::map<std::string, std::string> mockJsonInfo_;
375 
376     // app bar to use
377     bool installationFree_ = false;
378     int32_t labelId_;
379     static bool isComponentMode_;
380     std::string containerSdkPath_;
381     CrownEventCallback crownEventCallback_;
382     friend class WindowFreeContainer;
383 
384     ACE_DISALLOW_COPY_AND_MOVE(AceContainer);
385 };
386 
387 } // namespace OHOS::Ace::Platform
388 
389 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_CONTAINER_H
390