• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, BundleConfig &bundleConfig);
69     void Delete(const std::string &bundleName, int32_t userId);
70     sptr<IRemoteObject> CheckBMS();
71 
72 private:
73     BundleMgrProxy() = default;
74     class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
75     public:
ServiceDeathRecipient(std::weak_ptr<BundleMgrProxy> owner)76         explicit ServiceDeathRecipient(std::weak_ptr<BundleMgrProxy> owner) : owner_(owner) {}
OnRemoteDied(const wptr<IRemoteObject> & object)77         void OnRemoteDied(const wptr<IRemoteObject> &object) override
78         {
79             auto owner = owner_.lock();
80             if (owner != nullptr) {
81                 owner->OnProxyDied();
82             }
83         }
84 
85     private:
86         std::weak_ptr<BundleMgrProxy> owner_;
87     };
88     sptr<AppExecFwk::BundleMgrProxy> GetBundleMgrProxy();
89     void OnProxyDied();
90     std::pair<int, BundleConfig> ConvertToDataShareBundle(AppExecFwk::BundleInfo &bundleInfo);
91     std::pair<int, std::vector<ExtensionAbilityInfo>> ConvertExtensionAbility(AppExecFwk::BundleInfo &bundleInfo);
92     std::pair<int, std::vector<HapModuleInfo>> ConvertHapModuleInfo(AppExecFwk::BundleInfo &bundleInfo);
93     std::mutex mutex_;
94     sptr<IRemoteObject> proxy_;
95     sptr<BundleMgrProxy::ServiceDeathRecipient> deathRecipient_;
96     ConcurrentMap<std::string, BundleConfig> bundleCache_;
97     static constexpr const char *DATA_SHARE_EXTENSION_META = "ohos.extension.dataShare";
98     static constexpr const char *DATA_SHARE_PROPERTIES_META = "dataProperties";
99 };
100 } // namespace OHOS::DataShare
101 #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H
102