1 /*
2  * Copyright (c) 2023 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 DATASHARESERVICE_BUNDLEMGR_PROXY_H
17 #define DATASHARESERVICE_BUNDLEMGR_PROXY_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "bundle_info.h"
23 #include "bundlemgr/bundle_mgr_proxy.h"
24 #include "concurrent_map.h"
25 #include "data_share_profile_config.h"
26 
27 namespace OHOS::DataShare {
28 struct ProfileConfig {
29     ProfileInfo profile;
30     int resultCode = 0;
31 };
32 
33 struct ProxyData {
34     std::string uri;
35     std::string requiredReadPermission;
36     std::string requiredWritePermission;
37     ProfileConfig profileInfo;
38 };
39 
40 struct HapModuleInfo {
41     std::string resourcePath;
42     std::string hapPath;
43     std::string moduleName;
44     std::vector<ProxyData> proxyDatas;
45 };
46 
47 struct ExtensionAbilityInfo {
48     AppExecFwk::ExtensionAbilityType type = AppExecFwk::ExtensionAbilityType::UNSPECIFIED;
49     std::string readPermission;
50     std::string writePermission;
51     std::string uri;
52     std::string resourcePath;
53     std::string hapPath;
54     ProfileConfig profileInfo;
55 };
56 
57 struct BundleConfig {
58     std::string name;
59     bool singleton = false;
60     std::vector<HapModuleInfo> hapModuleInfos;
61     std::vector<ExtensionAbilityInfo> extensionInfos;
62 };
63 
64 class BundleMgrProxy final : public std::enable_shared_from_this<BundleMgrProxy> {
65 public:
66     ~BundleMgrProxy();
67     static std::shared_ptr<BundleMgrProxy> GetInstance();
68     int GetBundleInfoFromBMS(const std::string &bundleName, int32_t userId,
69         BundleConfig &bundleConfig, int32_t appIndex = 0);
70     void Delete(const std::string &bundleName, int32_t userId, int32_t appIndex);
71     sptr<IRemoteObject> CheckBMS();
72 
73 private:
74     BundleMgrProxy() = default;
75     class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
76     public:
ServiceDeathRecipient(std::weak_ptr<BundleMgrProxy> owner)77         explicit ServiceDeathRecipient(std::weak_ptr<BundleMgrProxy> owner) : owner_(owner) {}
OnRemoteDied(const wptr<IRemoteObject> & object)78         void OnRemoteDied(const wptr<IRemoteObject> &object) override
79         {
80             auto owner = owner_.lock();
81             if (owner != nullptr) {
82                 owner->OnProxyDied();
83             }
84         }
85 
86     private:
87         std::weak_ptr<BundleMgrProxy> owner_;
88     };
89     sptr<AppExecFwk::BundleMgrProxy> GetBundleMgrProxy();
90     void OnProxyDied();
91     std::pair<int, BundleConfig> ConvertToDataShareBundle(AppExecFwk::BundleInfo &bundleInfo);
92     std::pair<int, std::vector<ExtensionAbilityInfo>> ConvertExtensionAbility(AppExecFwk::BundleInfo &bundleInfo);
93     std::pair<int, std::vector<HapModuleInfo>> ConvertHapModuleInfo(AppExecFwk::BundleInfo &bundleInfo);
94     std::mutex mutex_;
95     sptr<IRemoteObject> proxy_;
96     sptr<BundleMgrProxy::ServiceDeathRecipient> deathRecipient_;
97     ConcurrentMap<std::string, BundleConfig> bundleCache_;
98     static constexpr const char *DATA_SHARE_EXTENSION_META = "ohos.extension.dataShare";
99     static constexpr const char *DATA_SHARE_PROPERTIES_META = "dataProperties";
100 };
101 } // namespace OHOS::DataShare
102 #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H
103