1 /*
2  * Copyright (c) 2024-2024 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_ABILITY_CONNECTION_REPORTER_H
17 #define OHOS_FORM_FWK_FORM_ABILITY_CONNECTION_REPORTER_H
18 
19 #include <dlfcn.h>
20 #include <mutex>
21 #include <singleton.h>
22 
23 #include "app_mgr_interface.h"
24 #include "form_ability_connection.h"
25 #include "fms_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 /**
30  * Struct FormConnectionInfo
31 */
32 struct FormConnectionInfo {
33     int32_t pid_;
34     int32_t connectCount_;
35 };
36 
37 /**
38  * @class FormAbilityConnectionReporter
39  * Form Ability Connection Reporter.
40  */
41 class FormAbilityConnectionReporter final : public DelayedRefSingleton<FormAbilityConnectionReporter> {
42     DECLARE_DELAYED_REF_SINGLETON(FormAbilityConnectionReporter)
43 public:
44     DISALLOW_COPY_AND_MOVE(FormAbilityConnectionReporter);
45 
46     /**
47      * @brief Report form ability connection info to rss.
48      * @param connection Form ability connection pointer.
49      */
50     void ReportFormAbilityConnection(const std::string &bundleName);
51 
52     /**
53      * @brief Report form ability disconnection info to rss.
54      * @param connection Form ability connection pointer.
55      */
56     void ReportFormAbilityDisconnection(const std::string &bundleName);
57 
58 private:
59     /**
60      * @brief Get app manager proxy.
61      * @return App manager proxy.
62      */
63     sptr<OHOS::AppExecFwk::IAppMgr> GetAppMgr();
64 
65     /**
66      * @brief Add form ability connection info.
67      * @param bundleName The bundle name of ability conncted with frs.
68      * @param connectionProviderKey The connection's key of form ability connection.
69      * @param infos The vector which contains running form ability process infos;
70      */
71     void AddFormAbilityConnectProcessInfo(const std::string &bundleName,
72         std::vector<AppExecFwk::RunningProcessInfo> &infos);
73 
74     /**
75      * @brief Reporter form ability connection infos to rss.
76      * @param connectionProviderKey The connection's key of form ability connection.
77      * @param isConnected True for form ability connected, false for form ability disconnected.
78      */
79     void ReportConnectionInfosToRss(const std::string &bundleName, bool isConnected);
80 
81 private:
82     std::mutex formConnectionInfoMapMutex_;
83     std::map<std::string, FormConnectionInfo> formConnectionInfoMap_;
84     sptr<OHOS::AppExecFwk::IAppMgr> appManager_ = nullptr;
85 };
86 }  // namespace AppExecFwk
87 }  // namespace OHOS
88 
89 #endif // OHOS_FORM_FWK_FORM_ABILITY_CONNECTION_REPORTER_H
90