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_APPEXECFWK_SERVICES_D_BUNDLEMGR_INCLUDE_DISTRIBUTED_BMS_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_D_BUNDLEMGR_INCLUDE_DISTRIBUTED_BMS_H
18 
19 #include <memory>
20 
21 #include "bundle_info.h"
22 #include "bundle_mgr_interface.h"
23 #include "dbms_device_manager.h"
24 #include "distributed_bms_host.h"
25 #include "distributed_monitor.h"
26 #include "if_system_ability_manager.h"
27 #include "iremote_object.h"
28 #include "resource_manager.h"
29 #include "singleton.h"
30 #include "system_ability.h"
31 namespace OHOS {
32 namespace AppExecFwk {
33 class DistributedBms : public SystemAbility, public DistributedBmsHost {
34     DECLARE_DELAYED_SINGLETON(DistributedBms);
35     DECLARE_SYSTEM_ABILITY(DistributedBms);
36 
37 public:
38     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
39 
40     OHOS::sptr<DbmsDeviceManager> GetDeviceManager();
41     /**
42      * @brief get remote ability info
43      * @param elementName Indicates the elementName.
44      * @param remoteAbilityInfo Indicates the remote ability info.
45      * @return Returns true when get remote ability info success; returns false otherwise.
46      */
47     int32_t GetRemoteAbilityInfo(
48         const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo) override;
49 
50     /**
51      * @brief get remote ability info
52      * @param elementName Indicates the elementName.
53      * @param localeInfo Indicates the localeInfo.
54      * @param remoteAbilityInfo Indicates the remote ability info.
55      * @return Returns true when get remote ability info success; returns false otherwise.
56      */
57     int32_t GetRemoteAbilityInfo(const OHOS::AppExecFwk::ElementName &elementName, const std::string &localeInfo,
58         RemoteAbilityInfo &remoteAbilityInfo) override;
59     /**
60      * @brief get remote ability infos
61      * @param elementNames Indicates the elementNames.
62      * @param remoteAbilityInfos Indicates the remote ability infos.
63      * @return Returns true when get remote ability info success; returns false otherwise.
64      */
65     int32_t GetRemoteAbilityInfos(
66         const std::vector<ElementName> &elementNames, std::vector<RemoteAbilityInfo> &remoteAbilityInfos) override;
67 
68     /**
69      * @brief get remote ability infos
70      * @param elementNames Indicates the elementNames.
71      * @param localeInfo Indicates the localeInfo.
72      * @param remoteAbilityInfos Indicates the remote ability infos.
73      * @return Returns true when get remote ability info success; returns false otherwise.
74      */
75     int32_t GetRemoteAbilityInfos(const std::vector<ElementName> &elementNames, const std::string &localeInfo,
76         std::vector<RemoteAbilityInfo> &remoteAbilityInfos) override;
77 
78     /**
79      * @brief get ability info
80      * @param elementName Indicates the elementName.
81      * @param remoteAbilityInfo Indicates the remote ability info.
82      * @return Returns true when get remote ability info success; returns false otherwise.
83      */
84     int32_t GetAbilityInfo(
85         const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo) override;
86 
87     /**
88      * @brief get ability info
89      * @param elementName Indicates the elementName.
90      * @param localeInfo Indicates the localeInfo.
91      * @param remoteAbilityInfo Indicates the remote ability info.
92      * @return Returns true when get remote ability info success; returns false otherwise.
93      */
94     int32_t GetAbilityInfo(const OHOS::AppExecFwk::ElementName &elementName, const std::string &localeInfo,
95         RemoteAbilityInfo &remoteAbilityInfo) override;
96     /**
97      * @brief get ability infos
98      * @param elementNames Indicates the elementNames.
99      * @param remoteAbilityInfos Indicates the remote ability infos.
100      * @return Returns true when get remote ability info success; returns false otherwise.
101      */
102     int32_t GetAbilityInfos(
103         const std::vector<ElementName> &elementNames, std::vector<RemoteAbilityInfo> &remoteAbilityInfos) override;
104 
105     /**
106      * @brief get ability infos
107      * @param elementNames Indicates the elementNames.
108      * @param localeInfo Indicates the localeInfo.
109      * @param remoteAbilityInfos Indicates the remote ability infos.
110      * @return Returns true when get remote ability info success; returns false otherwise.
111      */
112     int32_t GetAbilityInfos(const std::vector<ElementName> &elementNames, const std::string &localeInfo,
113         std::vector<RemoteAbilityInfo> &remoteAbilityInfos) override;
114 
115     bool GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName,
116         DistributedBundleInfo &distributedBundleInfo) override;
117 
118     /**
119      * @brief get distributedBundleName based on a given accessTokenId and networkId.
120      * @param networkId Indicates the networkId of remote device.
121      * @param accessTokenId AccessTokenId of the application
122      * @param bundleNames distributed bundle name.
123      * @return Returns ERR_OK on success, others on failure when get distributed bundle name.
124      */
125     int32_t GetDistributedBundleName(const std::string &networkId,  uint32_t accessTokenId,
126         std::string &bundleName) override;
127 
128     int32_t GetUdidByNetworkId(const std::string &networkId, std::string &udid);
129     int32_t GetUuidByNetworkId(const std::string &netWorkId, std::string &uuid);
130 
131     /**
132      * @brief Start the bundle manager service.
133      * @return
134      */
135     virtual void OnStart() override;
136     /**
137      * @brief Stop the bundle manager service.
138      * @return
139      */
140     virtual void OnStop() override;
141 
142 private:
143     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgr_;
144     std::shared_ptr<DbmsDeviceManager> dbmsDeviceManager_;
145     std::shared_ptr<DistributedMonitor> distributedSub_;
146     std::mutex bundleMgrMutex_;
147     std::mutex dbmsDeviceManagerMutex_;
148 
149     void Init();
150     void InitDeviceManager();
151     bool GetMediaBase64(std::unique_ptr<uint8_t[]> &data, int64_t fileLength,
152         std::string &imageType, std::string &value);
153     std::unique_ptr<unsigned char[]> LoadResourceFile(std::string &path, int &len);
154     std::unique_ptr<char[]> EncodeBase64(std::unique_ptr<uint8_t[]> &data, int srcLen);
155     int32_t GetAbilityIconByContent(
156         const AbilityInfo &abilityInfo, int32_t userId, RemoteAbilityInfo &remoteAbilityInfo);
157     int32_t Base64WithoutCompress(std::unique_ptr<uint8_t[]> &imageContent, size_t imageContentSize,
158         RemoteAbilityInfo &remoteAbilityInfo);
159     bool VerifySystemApp();
160     bool VerifyTokenNative(Security::AccessToken::AccessTokenID callerToken);
161     bool VerifyTokenShell(Security::AccessToken::AccessTokenID callerToken);
162     bool VerifyCallingPermission(const std::string &permissionName);
163 };
164 }  // namespace AppExecFwk
165 }  // namespace OHOS
166 #endif  // FOUNDATION_APPEXECFWK_SERVICES_D_BUNDLEMGR_INCLUDE_DISTRIBUTED_BMS_H