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 OHOS_FORM_FWK_FORM_PROVIDER_MGR_H
17 #define OHOS_FORM_FWK_FORM_PROVIDER_MGR_H
18 #include <set>
19 #include <singleton.h>
20 #include "ability_connect_callback_interface.h"
21 #include "form_provider_info.h"
22 #include "form_record.h"
23 #include "form_state_info.h"
24 #include "want.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 using Want = OHOS::AAFwk::Want;
29 /**
30  * @class FormProviderMgr
31  * Form provider manager.
32  */
33 class FormProviderMgr final : public DelayedRefSingleton<FormProviderMgr> {
34     DECLARE_DELAYED_REF_SINGLETON(FormProviderMgr)
35 public:
36     DISALLOW_COPY_AND_MOVE(FormProviderMgr);
37 
38     /**
39      * @brief handle for acquire back event from provider.
40      * @param formId The id of the form.
41      * @param providerFormInfo provider form info.
42      * @return Returns ERR_OK on success, others on failure.
43      */
44     ErrCode AcquireForm(const int64_t formId, const FormProviderInfo &formProviderInfo);
45     /**
46      * @brief handle for update form event from provider.
47      * @param formId The id of the form.
48      * @param providerFormInfo provider form info.
49      * @return Returns ERR_OK on success, others on failure.
50      */
51     ErrCode UpdateForm(const int64_t formId, const FormProviderInfo &formProviderInfo);
52     /**
53      * handle for update form event from provider.
54      *
55      * @param formId The id of the form.
56      * @param formRecord The form's record.
57      * @param formProviderData provider form info.
58      * @return Returns ERR_OK on success, others on failure.
59      */
60     ErrCode UpdateForm(const int64_t formId, FormRecord &formRecord, const FormProviderData &formProviderData);
61     /**
62      * @brief Refresh form.
63      * @param formId The form id.
64      * @param want The want of the form to request.
65      * @param isVisibleToFresh The form is visible to fresh.
66      * @return Returns ERR_OK on success, others on failure.
67      */
68     ErrCode RefreshForm(const int64_t formId, const Want &want, bool isVisibleToFresh);
69     /**
70      * @brief Connect ams for refresh form
71      * @param formId The form id.
72      * @param record Form data.
73      * @param want The want of the form.
74      * @param isCountTimerRefresh The flag of timer refresh.
75      * @return Returns ERR_OK on success, others on failure.
76      */
77     ErrCode ConnectAmsForRefresh(const int64_t formId, const FormRecord &record, const Want &want,
78         const bool isCountTimerRefresh);
79     /**
80      * @brief Connect ability manager service for refresh app permission
81      *
82      * @param formId The form id.
83      * @param want The want of the form.
84      * @return Returns ERR_OK on success, others on failure.
85      */
86     ErrCode ConnectAmsForRefreshPermission(const int64_t formId, Want &want);
87     /**
88      * @brief Notify provider form delete.
89      * @param formId The form id.
90      * @param record Form information.
91      * @return Returns ERR_OK on success, others on failure.
92      */
93     ErrCode NotifyProviderFormDelete(const int64_t formId, const FormRecord &formRecord);
94     /**
95      * @brief Notify provider forms batch delete.
96      * @param bundleName BundleName.
97      * @param bundleName AbilityName.
98      * @param formIds form id list.
99      * @return Returns ERR_OK on success, others on failure.
100      */
101     ErrCode NotifyProviderFormsBatchDelete(const std::string &bundleName, const std::string &abilityName,
102         const std::set<int64_t> &formIds);
103     /**
104      * @brief Acquire form state.
105      * @param state form state.
106      * @param provider provider info.
107      * @param wantArg The want of onAcquireFormState.
108      * @return Returns ERR_OK on success, others on failure.
109      */
110     ErrCode AcquireFormStateBack(FormState state, const std::string& provider, const Want &wantArg);
111 
112     /**
113      * @brief Acquire form data.
114      * @param wantParams Indicates the data information acquired by the form.
115      * @param requestCode Indicates the requested id.
116      * @return Returns ERR_OK on success, others on failure.
117      */
118     ErrCode AcquireFormDataBack(const AAFwk::WantParams &wantParams, int64_t requestCode);
119     /**
120      * @brief Process js message event.
121      * @param formId Indicates the unique id of form.
122      * @param record Form record.
123      * @param want information passed to supplier.
124      * @return Returns true if execute success, false otherwise.
125      */
126     int MessageEvent(const int64_t formId, const FormRecord &record, const Want &want);
127 private:
128     bool IsNeedToFresh(FormRecord &record, int64_t formId, bool isVisibleToFresh);
129 
130     FormRecord GetFormAbilityInfo(const FormRecord &record) const;
131     /**
132      * @brief Increase the timer refresh count.
133      * @param formId The form id.
134      */
135     void IncreaseTimerRefreshCount(const int64_t formId);
136     bool IsFormCached(const FormRecord &record);
137     ErrCode RefreshCheck(FormRecord &record, const int64_t formId, const Want &want);
138 
139     /**
140      * @brief Rebind form to provider by free install.
141      * @param want Indicates the want containing information about free install.
142      * @return Returns true if execute success, false otherwise.
143      */
144     ErrCode RebindByFreeInstall(const FormRecord &record, Want &want,
145         const sptr<AAFwk::IAbilityConnection> formRefreshConnection);
146 };
147 }  // namespace AppExecFwk
148 }  // namespace OHOS
149 
150 #endif // OHOS_FORM_FWK_FORM_PROVIDER_MGR_H
151