1 /*
2  * Copyright (c) 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_ABILITY_FORM_FWK_SERVICES_INCLUDE_FORM_SHARE_MGR_H
17 #define FOUNDATION_ABILITY_FORM_FWK_SERVICES_INCLUDE_FORM_SHARE_MGR_H
18 
19 #include <map>
20 #include <shared_mutex>
21 #include <singleton.h>
22 #include "ability_info.h"
23 #include "fms_log_wrapper.h"
24 #include "form_distributed_client.h"
25 #include "form_event_handler.h"
26 #include "form_free_install_operator.h"
27 #include "form_item_info.h"
28 #include "form_serial_queue.h"
29 #include "form_share_info.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 using WantParams = OHOS::AAFwk::WantParams;
34 /**
35  * @class FormShareMgr
36  * Form share manager.
37  */
38 class FormShareMgr final : public std::enable_shared_from_this<FormShareMgr>,
39                            public FormEventTimeoutObserver {
40 public:
41     DISALLOW_COPY_AND_MOVE(FormShareMgr);
42 
SetEventHandler(const std::shared_ptr<FormEventHandler> & handler)43     void SetEventHandler(const std::shared_ptr<FormEventHandler> &handler)
44     {
45         if (eventHandler_ != nullptr || handler == nullptr) {
46             return;
47         }
48         eventHandler_ = handler;
49         eventHandler_->RegisterEventTimeoutObserver(shared_from_this());
50     }
51 
SetSerialQueue(const std::shared_ptr<FormSerialQueue> & serialQueue)52     void SetSerialQueue(const std::shared_ptr<FormSerialQueue> &serialQueue)
53     {
54         if (serialQueue == nullptr) {
55             return;
56         }
57         serialQueue_ = serialQueue;
58     }
59 
60     /**
61      * @brief Share form by formID and deviceID.
62      * @param formId Indicates the unique id of form.
63      * @param deviceId Indicates the remote device ID.
64      * @param callerToken Indicates the host client.
65      * @param requestCode The request code of this share form.
66      * @return Returns ERR_OK on success, others on failure.
67      */
68     int32_t ShareForm(int64_t formId, const std::string &deviceId, const sptr<IRemoteObject> &callerToken,
69         int64_t requestCode);
70 
71     /**
72      * @brief Receive form sharing information from remote.
73      * @param info Indicates form sharing information.
74      * @return Returns ERR_OK on success, others on failure.
75      */
76     int32_t RecvFormShareInfoFromRemote(const FormShareInfo &info);
77 
78     /**
79      * @brief Acquire share form data from form provider.
80      * @param formId The Id of the from.
81      * @param remoteDeviceId Indicates the remote device ID.
82      * @param want Indicates the want containing information about sharing information and sharing data.
83      * @param remoteObject Form provider proxy object.
84      */
85     void AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId, const Want &want,
86         const sptr<IRemoteObject> &remoteObject);
87 
88     /**
89      * @brief Handle data shared by provider.
90      * @param formId The Id of the from.
91      * @param remoteDeviceId Indicates the remote device ID.
92      * @param wantParams Indicates the data shared by the provider.
93      * @param requestCode The request code of this share form.
94      * @param result Indicates the result of parsing the shared data.
95      */
96     void HandleProviderShareData(int64_t formId, const std::string &remoteDeviceId,
97         const AAFwk::WantParams &wantParams, int64_t requestCode, const bool &result);
98 
99     /**
100      * @brief Add provider data.
101      * @param want The want of the form to add.
102      * @param wantParams WantParams of the request.
103      */
104     void AddProviderData(const Want &want, WantParams &wantParams);
105 
106     /**
107      * @brief Handle form share timeout.
108      * @param eventId Event id.
109      */
110     void HandleFormShareInfoTimeout(int64_t eventId);
111 
112     /**
113      * @brief Handle free install timeout.
114      * @param eventId Event id.
115      */
116     void HandleFreeInstallTimeout(int64_t eventId);
117 
118     /**
119      * @brief Free install was finished.
120      * @param FormFreeInstallOperator Indicates the free install operator object.
121      * @param resultCode Indicates the free install results.
122      * @param formShareInfoKey Indicates the form share info key.
123      */
124     void OnInstallFinished(const std::shared_ptr<FormFreeInstallOperator> &freeInstallOperator,
125         int32_t resultCode, const std::string &formShareInfoKey);
126 
127     /**
128      * @brief Send form to share asyn result.
129      * @param requestCode The request code of this share form.
130      * @param result Indicates form to share asyn result.
131      */
132     void SendResponse(int64_t requestCode, int32_t result);
133 
134     /**
135      * @brief Whether the added form is a share form.
136      * @param want The want of the form to add.
137      * @return Returns true is a shared form, false is not.
138      */
139     bool IsShareForm(const Want &want);
140 private:
141     std::string MakeFormShareInfoKey(const FormShareInfo &info);
142     std::string MakeFormShareInfoKey(const Want &want);
143     void RemoveFormShareInfo(const std::string &formShareInfoKey);
144     void FinishFreeInstallTask(const std::shared_ptr<FormFreeInstallOperator> &freeInstallOperator);
145     bool IsExistFormPackage(const std::string &bundleName, const std::string &moduleName);
146     bool CheckFormShareInfo(const FormShareInfo &info);
147     void StartFormUser(const FormShareInfo &info);
148     int32_t HandleRecvFormShareInfoFromRemoteTask(const FormShareInfo &info);
149     int32_t CheckFormPackage(const FormShareInfo &info, const std::string &formShareInfoKey);
150     void OnEventTimeoutResponse(int64_t msg, int64_t eventId) override;
151 private:
152     DECLARE_DELAYED_SINGLETON(FormShareMgr);
153     std::shared_ptr<FormEventHandler> eventHandler_ = nullptr;
154     std::shared_ptr<FormDistributedClient> formDmsClient_ = nullptr;
155     // map for <formShareInfoKey, FormShareInfo>
156     std::map<std::string, FormShareInfo> shareInfo_;
157     // map for <eventId, formShareInfoKey>
158     std::map<int64_t, std::string> eventMap_;
159     // map for <eventId, std::shared_ptr<FormFreeInstallOperator>>
160     std::map<int64_t, std::shared_ptr<FormFreeInstallOperator>> freeInstallOperatorMap_;
161     // map for <requestCode, formHostClient>
162     std::map<int64_t, sptr<IRemoteObject>> requestMap_;
163     mutable std::shared_mutex shareInfoMapMutex_ {};
164     mutable std::shared_mutex eventMapMutex_ {};
165     mutable std::shared_mutex freeInstallMapMutex_ {};
166     mutable std::shared_mutex requestMapMutex_ {};
167     std::shared_ptr<FormSerialQueue> serialQueue_ = nullptr;
168 };
169 
170 } // namespace AppExecFwk
171 } // namespace OHOS
172 #endif // FOUNDATION_ABILITY_FORM_FWK_SERVICES_INCLUDE_FORM_SHARE_MGR_H
173