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