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 #ifndef FOUNDATION_ACE_INTERFACE_INNERKITS_FORM_RENDERER_GROUP_H
17 #define FOUNDATION_ACE_INTERFACE_INNERKITS_FORM_RENDERER_GROUP_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "form_js_info.h"
24 #include "want.h"
25 #include "event_handler.h"
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 class Context;
30 class Runtime;
31 }
32 
33 namespace AppExecFwk {
34 class Configuration;
35 class EventHandler;
36 }
37 
38 namespace Ace {
39 #ifndef ACE_EXPORT
40 #define ACE_EXPORT __attribute__((visibility("default")))
41 #endif
42 
43 class FormRenderer;
44 
45 struct FormRequest {
46     std::string compId;
47     OHOS::AAFwk::Want want;
48     OHOS::AppExecFwk::FormJsInfo formJsInfo;
49     bool isDynamic = true;
50     bool hasRelease = false;
operatorFormRequest51     bool operator() (const FormRequest& info) const
52     {
53         return compId == info.compId && formJsInfo.formId == info.formJsInfo.formId;
54     }
55 };
56 
57 /**
58  * @class FormRendererGroup
59  * FormRendererGroup interface is used to form renderer group.
60  * Provider:FormRendererGroup:runtime = 1:1:1
61  * FormRendererGroup:FormRenderer = 1:1
62  */
63 class ACE_EXPORT FormRendererGroup {
64 public:
65     static std::shared_ptr<FormRendererGroup> Create(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,
66                                                      const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime,
67                                                      std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler);
68 
69     FormRendererGroup(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,
70                       const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime,
71                       std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler);
72     ~FormRendererGroup();
73 
74     void AddForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
75     void OnUnlock();
76     void SetVisibleChange(bool isVisible);
77     void UpdateForm(const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
78     void DeleteForm();
79     void DeleteForm(const std::string& compId);
80     void ReloadForm(const AppExecFwk::FormJsInfo& formJsInfo);
81     void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
82     bool IsFormRequestsEmpty();
83     const std::vector<FormRequest>& GetAllRendererFormRequests() const;
84     std::pair<std::vector<std::string>, std::string> GetOrderedAndCurrentCompIds() const;
85     void RecycleForm(std::string& statusData) const;
86     void RecoverRenderer(const std::vector<FormRequest>& formRequests, size_t currentCompIndex);
87     void UpdateFormSizeOfFormRequests(double width, double height, float borderWidth);
88 private:
89     enum class FormRendererInitState {
90         UNINITIALIZED,
91         PRE_INITIALIZED,
92         INITIALIZED
93     };
94     void InnerAddForm(const FormRequest& formRequest);
95     void PreInitAddForm(const FormRequest& formRequest);
96     std::shared_ptr<OHOS::AbilityRuntime::Context> context_;
97     std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime_;
98     std::shared_ptr<FormRenderer> formRenderer_;
99     std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
100     std::vector<FormRequest> formRequests_;
101     std::string currentCompId_;
102     FormRendererInitState initState_ = FormRendererInitState::UNINITIALIZED;
103 };
104 }  // namespace Ace
105 }  // namespace OHOS
106 #endif  // FOUNDATION_ACE_INTERFACE_INNERKITS_FORM_RENDERER_GROUP_H
107