• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_ACE_CONTAINER_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_CPP_ACE_CONTAINER_H
18 
19 #include <cstddef>
20 #include <memory>
21 #include <mutex>
22 
23 #include "ability_context.h"
24 #include "native_engine/native_reference.h"
25 #include "native_engine/native_value.h"
26 
27 #include "adapter/ohos/entrance/ace_ability.h"
28 #include "adapter/ohos/entrance/platform_event_callback.h"
29 #include "base/resource/asset_manager.h"
30 #include "base/thread/task_executor.h"
31 #include "base/utils/noncopyable.h"
32 #include "core/common/ace_view.h"
33 #include "core/common/container.h"
34 #include "core/common/js_message_dispatcher.h"
35 #include "core/pipeline/pipeline_context.h"
36 
37 namespace OHOS::Ace::Platform {
38 using UIEnvCallback = std::function<void(const OHOS::Ace::RefPtr<OHOS::Ace::PipelineContext>& context)>;
39 using SharePanelCallback = std::function<void(const std::string& bundleName, const std::string& abilityName)>;
40 class ACE_FORCE_EXPORT AceContainer : public Container, public JsMessageDispatcher {
41     DECLARE_ACE_TYPE(AceContainer, Container, JsMessageDispatcher);
42 
43 public:
44     AceContainer(int32_t instanceId, FrontendType type, bool isArkApp,
45         std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility, std::unique_ptr<PlatformEventCallback> callback,
46         bool useCurrentEventRunner = false, bool useNewPipeline = false);
47     AceContainer(int32_t instanceId, FrontendType type, bool isArkApp,
48         std::weak_ptr<OHOS::AbilityRuntime::Context> runtimeContext,
49         std::weak_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo, std::unique_ptr<PlatformEventCallback> callback,
50         bool useCurrentEventRunner = false, bool isSubContainer = false, bool useNewPipeline = false);
51     ~AceContainer() override = default;
52 
53     void Initialize() override;
54 
55     void Destroy() override;
56 
57     void DestroyView() override;
58 
59     static bool Register();
60 
GetInstanceId()61     int32_t GetInstanceId() const override
62     {
63         if (aceView_) {
64             return aceView_->GetInstanceId();
65         }
66         return -1;
67     }
68 
GetFrontend()69     RefPtr<Frontend> GetFrontend() const override
70     {
71         return frontend_;
72     }
73 
SetCardFrontend(WeakPtr<Frontend> frontend,int64_t cardId)74     void SetCardFrontend(WeakPtr<Frontend> frontend, int64_t cardId) override
75     {
76         std::lock_guard<std::mutex> lock(cardFrontMutex_);
77         cardFrontendMap_.try_emplace(cardId, frontend);
78     }
79 
GetCardFrontend(int64_t cardId)80     WeakPtr<Frontend> GetCardFrontend(int64_t cardId) const override
81     {
82         std::lock_guard<std::mutex> lock(cardFrontMutex_);
83         auto it = cardFrontendMap_.find(cardId);
84         if (it != cardFrontendMap_.end()) {
85             return it->second;
86         }
87         return nullptr;
88     }
89 
SetCardPipeline(WeakPtr<PipelineBase> pipeline,int64_t cardId)90     void SetCardPipeline(WeakPtr<PipelineBase> pipeline, int64_t cardId) override
91     {
92         std::lock_guard<std::mutex> lock(cardPipelineMutex_);
93         cardPipelineMap_.try_emplace(cardId, pipeline);
94     }
95 
GetCardPipeline(int64_t cardId)96     WeakPtr<PipelineBase> GetCardPipeline(int64_t cardId) const override
97     {
98         std::lock_guard<std::mutex> lock(cardPipelineMutex_);
99         auto it = cardPipelineMap_.find(cardId);
100         if (it == cardPipelineMap_.end()) {
101             return nullptr;
102         }
103         return it->second;
104     }
105 
GetTaskExecutor()106     RefPtr<TaskExecutor> GetTaskExecutor() const override
107     {
108         return taskExecutor_;
109     }
110 
SetAssetManager(const RefPtr<AssetManager> & assetManager)111     void SetAssetManager(const RefPtr<AssetManager>& assetManager)
112     {
113         assetManager_ = assetManager;
114         if (frontend_) {
115             frontend_->SetAssetManager(assetManager);
116         }
117     }
118 
GetAssetManager()119     RefPtr<AssetManager> GetAssetManager() const override
120     {
121         return assetManager_;
122     }
123 
GetPlatformResRegister()124     RefPtr<PlatformResRegister> GetPlatformResRegister() const override
125     {
126         return resRegister_;
127     }
128 
GetPipelineContext()129     RefPtr<PipelineBase> GetPipelineContext() const override
130     {
131         return pipelineContext_;
132     }
133 
GetViewWidth()134     int32_t GetViewWidth() const override
135     {
136         return aceView_ ? aceView_->GetWidth() : 0;
137     }
138 
GetViewHeight()139     int32_t GetViewHeight() const override
140     {
141         return aceView_ ? aceView_->GetHeight() : 0;
142     }
143 
GetViewPosX()144     int32_t GetViewPosX() const override
145     {
146         return aceView_ ? aceView_->GetPosX() : 0;
147     }
148 
GetViewPosY()149     int32_t GetViewPosY() const override
150     {
151         return aceView_ ? aceView_->GetPosY() : 0;
152     }
153 
GetAceView()154     AceView* GetAceView() const
155     {
156         return aceView_;
157     }
158 
GetView()159     void* GetView() const override
160     {
161         return static_cast<void*>(aceView_);
162     }
163 
SetWindowModal(WindowModal windowModal)164     void SetWindowModal(WindowModal windowModal)
165     {
166         windowModal_ = windowModal;
167     }
168 
SetInstallationFree(bool installationFree)169     void SetInstallationFree(bool installationFree)
170     {
171         installationFree_ = installationFree;
172     }
173 
SetSharePanelCallback(SharePanelCallback && callback)174     void SetSharePanelCallback(SharePanelCallback&& callback)
175     {
176         sharePanelCallback_ = std::move(callback);
177     }
178 
SetColorScheme(ColorScheme colorScheme)179     void SetColorScheme(ColorScheme colorScheme)
180     {
181         colorScheme_ = colorScheme;
182     }
183 
GetResourceConfiguration()184     ResourceConfiguration GetResourceConfiguration() const
185     {
186         return resourceInfo_.GetResourceConfiguration();
187     }
188 
SetResourceConfiguration(const ResourceConfiguration & config)189     void SetResourceConfiguration(const ResourceConfiguration& config)
190     {
191         resourceInfo_.SetResourceConfiguration(config);
192     }
193 
GetPackagePathStr()194     std::string GetPackagePathStr() const
195     {
196         return resourceInfo_.GetPackagePath();
197     }
198 
SetPackagePathStr(const std::string & packagePath)199     void SetPackagePathStr(const std::string& packagePath)
200     {
201         resourceInfo_.SetPackagePath(packagePath);
202     }
203 
GetHapPath()204     std::string GetHapPath() const
205     {
206         return resourceInfo_.GetHapPath();
207     }
208 
209     void SetHapPath(const std::string& hapPath);
210 
211     void Dispatch(
212         const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override;
213 
DispatchSync(const std::string & group,std::vector<uint8_t> && data,uint8_t ** resData,int64_t & position)214     void DispatchSync(
215         const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, int64_t& position) const override
216     {}
217 
218     void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override;
219 
220     bool Dump(const std::vector<std::string>& params) override;
221 
222     void TriggerGarbageCollection() override;
223 
224     void DumpHeapSnapshot(bool isPrivate) override;
225 
226     void SetLocalStorage(NativeReference* storage, NativeReference* context);
227 
OnFinish()228     void OnFinish()
229     {
230         if (platformEventCallback_) {
231             platformEventCallback_->OnFinish();
232         }
233     }
234 
OnStartAbility(const std::string & address)235     void OnStartAbility(const std::string& address)
236     {
237         if (platformEventCallback_) {
238             platformEventCallback_->OnStartAbility(address);
239         }
240     }
241 
GeneratePageId()242     int32_t GeneratePageId()
243     {
244         return pageId_++;
245     }
246 
GetHostClassName()247     std::string GetHostClassName() const override
248     {
249         return "";
250     }
251 
SetSharedRuntime(void * runtime)252     void SetSharedRuntime(void* runtime) override
253     {
254         sharedRuntime_ = runtime;
255     }
256 
SetPageProfile(const std::string & pageProfile)257     void SetPageProfile(const std::string& pageProfile)
258     {
259         pageProfile_ = pageProfile;
260     }
261 
IsSubContainer()262     bool IsSubContainer() const override
263     {
264         return isSubContainer_;
265     }
266 
IsFormRender()267     bool IsFormRender() const
268     {
269         return isFormRender_;
270     }
271 
GetSharedRuntime()272     void* GetSharedRuntime() override
273     {
274         return sharedRuntime_;
275     }
276 
SetParentId(int32_t parentId)277     void SetParentId(int32_t parentId)
278     {
279         parentId_ = parentId;
280     }
281 
GetParentId()282     int32_t GetParentId() const
283     {
284         return parentId_;
285     }
286 
287     static void CreateContainer(int32_t instanceId, FrontendType type, bool isArkApp, const std::string& instanceName,
288         std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility, std::unique_ptr<PlatformEventCallback> callback,
289         bool useCurrentEventRunner = false, bool useNewPipeline = false);
290 
291     static void DestroyContainer(int32_t instanceId, const std::function<void()>& destroyCallback = nullptr);
292     static bool RunPage(int32_t instanceId, int32_t pageId, const std::string& content, const std::string& params);
293     static bool PushPage(int32_t instanceId, const std::string& content, const std::string& params);
294     static bool OnBackPressed(int32_t instanceId);
295     static void OnShow(int32_t instanceId);
296     static void OnHide(int32_t instanceId);
297     static void OnActive(int32_t instanceId);
298     static void OnInactive(int32_t instanceId);
299     static void OnNewWant(int32_t instanceId, const std::string& data);
300     static bool OnStartContinuation(int32_t instanceId);
301     static std::string OnSaveData(int32_t instanceId);
302     static bool OnRestoreData(int32_t instanceId, const std::string& data);
303     static void OnCompleteContinuation(int32_t instanceId, int result);
304     static void OnRemoteTerminated(int32_t instanceId);
305     static void OnConfigurationUpdated(int32_t instanceId, const std::string& configuration);
306     static void OnNewRequest(int32_t instanceId, const std::string& data);
307     static void AddAssetPath(int32_t instanceId, const std::string& packagePath, const std::string& hapPath,
308         const std::vector<std::string>& paths);
309     static void AddLibPath(int32_t instanceId, const std::vector<std::string>& libPath);
310     static void SetView(AceView* view, double density, int32_t width, int32_t height,
311         sptr<OHOS::Rosen::Window> rsWindow, UIEnvCallback callback = nullptr);
312     static void SetViewNew(
313         AceView* view, double density, int32_t width, int32_t height, sptr<OHOS::Rosen::Window> rsWindow);
314     static void SetUIWindow(int32_t instanceId, sptr<OHOS::Rosen::Window> uiWindow);
315     static sptr<OHOS::Rosen::Window> GetUIWindow(int32_t instanceId);
316     static OHOS::AppExecFwk::Ability* GetAbility(int32_t instanceId);
317     static void SetFontScale(int32_t instanceId, float fontScale);
318     static void SetWindowStyle(int32_t instanceId, WindowModal windowModal, ColorScheme colorScheme);
319     static std::string RestoreRouterStack(int32_t instanceId, const std::string& contentInfo);
320     static std::string GetContentInfo(int32_t instanceId);
321 
322     static RefPtr<AceContainer> GetContainer(int32_t instanceId);
323     static bool UpdatePage(int32_t instanceId, int32_t pageId, const std::string& content);
324     static void ClearEngineCache(int32_t instanceId);
325 
326     // ArkTsCard
327     static std::shared_ptr<Rosen::RSSurfaceNode> GetFormSurfaceNode(int32_t instanceId);
328 
SetWindowName(const std::string & name)329     void SetWindowName(const std::string& name)
330     {
331         windowName_ = name;
332     }
333 
GetWindowName()334     std::string& GetWindowName()
335     {
336         return windowName_;
337     }
338 
SetWindowId(uint32_t windowId)339     void SetWindowId(uint32_t windowId) override
340     {
341         windowId_ = windowId;
342     }
343 
GetWindowId()344     uint32_t GetWindowId() const override
345     {
346         return windowId_;
347     }
348 
349     void SetWindowPos(int32_t left, int32_t top);
350 
SetIsSubContainer(bool isSubContainer)351     void SetIsSubContainer(bool isSubContainer)
352     {
353         isSubContainer_ = isSubContainer;
354     }
355 
SetIsFormRender(bool isFormRender)356     void SetIsFormRender(bool isFormRender)
357     {
358         isFormRender_ = isFormRender;
359     }
360 
361     void InitializeSubContainer(int32_t parentContainerId);
362     static void SetDialogCallback(int32_t instanceId, FrontendDialogCallback callback);
363 
364     std::shared_ptr<OHOS::AbilityRuntime::Context> GetAbilityContextByModule(const std::string& bundle,
365         const std::string& module);
366 
367     void UpdateConfiguration(
368         const std::string& colorMode, const std::string& inputDevice, const std::string& languageTag);
369 
370     void NotifyConfigurationChange(bool needReloadTransition);
371 
IsUseStageModel()372     bool IsUseStageModel() const override
373     {
374         return useStageModel_;
375     }
376 
GetCardFrontendMap(std::unordered_map<int64_t,WeakPtr<Frontend>> & cardFrontendMap)377     void GetCardFrontendMap(std::unordered_map<int64_t, WeakPtr<Frontend>>& cardFrontendMap) const override
378     {
379         cardFrontendMap = cardFrontendMap_;
380     }
381 
382     void SetToken(sptr<IRemoteObject>& token);
383     sptr<IRemoteObject> GetToken();
384 
385     // ArkTSCard
386     void UpdateFormData(const std::string& data);
387     void UpdateFormSharedImage(const std::map<std::string, sptr<OHOS::AppExecFwk::FormAshmem>>& imageDataMap);
388 
389     void GetNamesOfSharedImage(std::vector<std::string>& picNameArray);
390     void UpdateSharedImage(std::vector<std::string>& picNameArray, std::vector<int32_t>& byteLenArray,
391         std::vector<int32_t>& fileDescriptorArray);
392     void GetImageDataFromAshmem(
393         const std::string& picName, Ashmem& ashmem, const RefPtr<PipelineBase>& pipelineContext, int len);
394 
395 private:
396     void InitializeFrontend();
397     void InitializeCallback();
398     void InitializeTask();
399     void InitWindowCallback();
400 
401     void AttachView(std::unique_ptr<Window> window, AceView* view, double density, int32_t width, int32_t height,
402         int32_t windowId, UIEnvCallback callback = nullptr);
403     void SetUIWindowInner(sptr<OHOS::Rosen::Window> uiWindow);
404     sptr<OHOS::Rosen::Window> GetUIWindowInner() const;
405     std::weak_ptr<OHOS::AppExecFwk::Ability> GetAbilityInner() const;
406     int32_t instanceId_ = 0;
407     AceView* aceView_ = nullptr;
408     RefPtr<TaskExecutor> taskExecutor_;
409     RefPtr<AssetManager> assetManager_;
410     RefPtr<PlatformResRegister> resRegister_;
411     RefPtr<PipelineBase> pipelineContext_;
412     RefPtr<Frontend> frontend_;
413     std::unordered_map<int64_t, WeakPtr<Frontend>> cardFrontendMap_;
414     std::unordered_map<int64_t, WeakPtr<PipelineBase>> cardPipelineMap_;
415 
416     FrontendType type_ = FrontendType::JS;
417     bool isArkApp_ = false;
418     std::unique_ptr<PlatformEventCallback> platformEventCallback_;
419     WindowModal windowModal_ { WindowModal::NORMAL };
420     ColorScheme colorScheme_ { ColorScheme::FIRST_VALUE };
421     ResourceInfo resourceInfo_;
422     std::weak_ptr<OHOS::AppExecFwk::Ability> aceAbility_;
423     std::weak_ptr<OHOS::AbilityRuntime::Context> runtimeContext_;
424     std::weak_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo_;
425     void* sharedRuntime_ = nullptr;
426     std::string pageProfile_;
427     int32_t pageId_ = 0;
428     bool useCurrentEventRunner_ = false;
429     sptr<OHOS::Rosen::Window> uiWindow_ = nullptr;
430     std::string windowName_;
431     uint32_t windowId_ = OHOS::Rosen::INVALID_WINDOW_ID;
432     sptr<IRemoteObject> token_;
433 
434     bool isSubContainer_ = false;
435     bool isFormRender_ = false;
436     int32_t parentId_ = 0;
437     bool useStageModel_ = false;
438 
439     mutable std::mutex cardFrontMutex_;
440     mutable std::mutex cardPipelineMutex_;
441     mutable std::mutex cardTokensMutex_;
442 
443     bool installationFree_ = false;
444     SharePanelCallback sharePanelCallback_ = nullptr;
445 
446     ACE_DISALLOW_COPY_AND_MOVE(AceContainer);
447 };
448 
449 } // namespace OHOS::Ace::Platform
450 
451 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CPP_ACE_CONTAINER_H
452