• 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 CrossAppSharedConfig {
41     std::string resourcePath;
42     ProxyDataProfileInfo profile;
43 };
44 
45 struct HapModuleInfo {
46     std::string resourcePath;
47     std::string hapPath;
48     std::string moduleName;
49     std::vector<ProxyData> proxyDatas;
50     CrossAppSharedConfig crossAppSharedConfig;
51 };
52 
53 struct ExtensionAbilityInfo {
54     AppExecFwk::ExtensionAbilityType type = AppExecFwk::ExtensionAbilityType::UNSPECIFIED;
55     std::string readPermission;
56     std::string writePermission;
57     std::string uri;
58     std::string resourcePath;
59     std::string hapPath;
60     ProfileConfig profileInfo;
61 };
62 
63 struct BundleConfig {
64     std::string name;
65     bool singleton = false;
66     bool isSystemApp = false;
67     std::vector<HapModuleInfo> hapModuleInfos;
68     std::vector<ExtensionAbilityInfo> extensionInfos;
69 };
70 
71 struct BundleInfo {
72     std::string bundleName;
73     std::string appIdentifier;
74     int32_t userId;
75     int32_t appIndex;
76     uint32_t tokenId;
77 };
78 
79 class BundleMgrProxy final : public std::enable_shared_from_this<BundleMgrProxy> {
80 public:
81     BundleMgrProxy() = default;
82     ~BundleMgrProxy();
83     static std::shared_ptr<BundleMgrProxy> GetInstance();
84     int GetBundleInfoFromBMS(const std::string &bundleName, int32_t userId,
85         BundleConfig &bundleConfig, int32_t appIndex = 0);
86     int GetBundleInfoFromBMSWithCheck(const std::string &bundleName, int32_t userId,
87         BundleConfig &bundleConfig, int32_t appIndex = 0);
88     void Delete(const std::string &bundleName, int32_t userId, int32_t appIndex);
89     sptr<IRemoteObject> CheckBMS();
90     std::pair<int, std::string> GetCallerAppIdentifier(const std::string &bundleName, int32_t userId);
91 private:
92     class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
93     public:
ServiceDeathRecipient(std::weak_ptr<BundleMgrProxy> owner)94         explicit ServiceDeathRecipient(std::weak_ptr<BundleMgrProxy> owner) : owner_(owner) {}
OnRemoteDied(const wptr<IRemoteObject> & object)95         void OnRemoteDied(const wptr<IRemoteObject> &object) override
96         {
97             auto owner = owner_.lock();
98             if (owner != nullptr) {
99                 owner->OnProxyDied();
100             }
101         }
102 
103     private:
104         std::weak_ptr<BundleMgrProxy> owner_;
105     };
106     sptr<AppExecFwk::BundleMgrProxy> GetBundleMgrProxy();
107     void OnProxyDied();
108     std::pair<int, BundleConfig> ConvertToDataShareBundle(AppExecFwk::BundleInfo &bundleInfo);
109     std::pair<int, std::vector<ExtensionAbilityInfo>> ConvertExtensionAbility(AppExecFwk::BundleInfo &bundleInfo);
110     std::pair<int, std::vector<HapModuleInfo>> ConvertHapModuleInfo(AppExecFwk::BundleInfo &bundleInfo);
111     std::mutex mutex_;
112     sptr<IRemoteObject> proxy_;
113     sptr<BundleMgrProxy::ServiceDeathRecipient> deathRecipient_;
114     ConcurrentMap<std::string, BundleConfig> bundleCache_;
115     ConcurrentMap<std::string, std::string> callerInfoCache_;
116     static constexpr const char *DATA_SHARE_EXTENSION_META = "ohos.extension.dataShare";
117     static constexpr const char *DATA_SHARE_PROPERTIES_META = "dataProperties";
118 };
119 } // namespace OHOS::DataShare
120 #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H
121