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 FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_BUNDLE_MANAGER_HELPER_H
17#define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_BUNDLE_MANAGER_HELPER_H
18
19#include <string>
20#include <vector>
21
22#include "bms_death_recipient.h"
23#include "bundle_mgr_interface.h"
24#include "extension_ability_info.h"
25#include "iremote_object.h"
26#include "refbase.h"
27#include "singleton.h"
28#include "want.h"
29
30namespace OHOS {
31namespace EventFwk {
32
33class BundleManagerHelper : public DelayedSingleton<BundleManagerHelper> {
34public:
35    using IBundleMgr = OHOS::AppExecFwk::IBundleMgr;
36
37    BundleManagerHelper();
38
39    virtual ~BundleManagerHelper();
40
41    /**
42     * Checks whether it is system application.
43     *
44     * @param uid Indicates the uid of the application.
45     * @return Returns true if successful; false otherwise.
46     */
47    bool CheckIsSystemAppByUid(const uid_t uid);
48
49    bool CheckIsSystemAppByBundleName(const std::string &bundleName, const int32_t &userId);
50
51    /**
52     * Gets bundle name.
53     *
54     * @param uid Indicates the uid of the application.
55     * @return Returns the bundle name.
56     */
57    std::string GetBundleName(const uid_t uid);
58
59    /**
60     * Queries extension information.
61     *
62     * @param extensionInfo Indicates the extension information.
63     * @return Returns true if successful; false otherwise.
64     */
65    bool QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfo);
66
67    /**
68     * Queries extension information by user.
69     *
70     * @param extensionInfos Indicates the extension information.
71     * @param userId Indicates the ID of user.
72     * @return Returns true if successful; false otherwise.
73     */
74    bool QueryExtensionInfos(std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos,
75        const int32_t &userId);
76
77    /**
78     * Gets res config file.
79     *
80     * @param extension Indicates the extension information.
81     * @param profileInfos Indicates the profile information.
82     * @return Returns true if successful; false otherwise.
83     */
84    bool GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo &extension,
85        std::vector<std::string> &profileInfos);
86
87    /**
88     * Clears bundle manager helper.
89     *
90     */
91    void ClearBundleManagerHelper();
92
93    /**
94     * @brief Obtains information about all installed applications of current user.
95     * @param flag Indicates the flag used to specify information contained
96     *             in the ApplicationInfo objects that will be returned.
97     * @param appInfos Indicates all of the obtained ApplicationInfo objects.
98     * @return Returns true if the application is successfully obtained; returns false otherwise.
99     */
100    bool GetApplicationInfos(const AppExecFwk::ApplicationFlag &flag,
101        std::vector<AppExecFwk::ApplicationInfo> &appInfos);
102
103private:
104    bool GetBundleMgrProxy();
105    bool GetBundleMgrProxyAsync();
106    bool GetBundleMgrProxyInner(bool isAsync);
107
108private:
109    sptr<IBundleMgr> sptrBundleMgr_;
110    std::mutex mutex_;
111    sptr<BMSDeathRecipient> bmsDeath_;
112};
113}  // namespace EventFwk
114}  // namespace OHOS
115
116#endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_BUNDLE_MANAGER_HELPER_H