1 /*
2  * Copyright (c) 2021-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 #include "bundle_mgr_slite_feature.h"
17 
18 #include "bundlems_log.h"
19 #include "ohos_init.h"
20 #include "samgr_lite.h"
21 #include "securec.h"
22 #include "utils.h"
23 #include "want_utils.h"
24 
25 namespace OHOS {
26 BundleMgrSliteFeatureImpl g_bmsSliteImpl = {
27     DEFAULT_IUNKNOWN_ENTRY_BEGIN,
28     .Install = BundleMgrSliteFeature::Install,
29     .Uninstall = BundleMgrSliteFeature::Uninstall,
30     .QueryAbilityInfo = BundleMgrSliteFeature::QueryAbilityInfo,
31     .GetBundleInfo = BundleMgrSliteFeature::GetBundleInfo,
32     .GetBundleInfos = BundleMgrSliteFeature::GetBundleInfos,
33     .GetInstallState = BundleMgrSliteFeature::GetInstallState,
34     .GetBundleSize = BundleMgrSliteFeature::GetBundleSize,
35     .RegisterInstallerCallback = BundleMgrSliteFeature::RegisterInstallerCallback,
36     .UpdateBundleInfoList = BundleMgrSliteFeature::UpdateBundleInfoList,
37     .GetBundleInfosNoReplication = BundleMgrSliteFeature::GetBundleInfosNoReplication,
38     .QueryAbilityInfos = BundleMgrSliteFeature::QueryAbilityInfos,
39     .RegisterEvent = BundleMgrSliteFeature::RegisterEvent,
40     .UnregisterEvent = BundleMgrSliteFeature::UnregisterEvent,
41     .InitPreAppInfo = BundleMgrSliteFeature::InitPreAppInfo,
42     .InsertPreAppInfo = BundleMgrSliteFeature::InsertPreAppInfo,
43     .SetPreAppInfo = BundleMgrSliteFeature::SetPreAppInfo,
44     DEFAULT_IUNKNOWN_ENTRY_END
45 };
46 
Init()47 void BundleMgrSliteFeature::Init()
48 {
49     SamgrLite *samgrLite = SAMGR_GetInstance();
50     CHECK_NULLPTR_RETURN(samgrLite, "BundleMgrSliteFeature", "get samgr error");
51     BOOL result = samgrLite->RegisterFeature(BMS_SERVICE, BundleMgrSliteFeature::GetInstance());
52     if (result == FALSE) {
53         PRINTE("BundleMgrSliteFeature", "bms register feature failure");
54         return;
55     }
56     g_bmsSliteImpl.bms = BundleMgrSliteFeature::GetInstance();
57     auto publicApi = GET_IUNKNOWN(g_bmsSliteImpl);
58     CHECK_NULLPTR_RETURN(publicApi, "BundleMgrSliteFeatureLite", "publicApi is nullptr");
59     BOOL apiResult = samgrLite->RegisterFeatureApi(BMS_SERVICE, BMS_SLITE_FEATURE, publicApi);
60     PRINTI("BundleMgrSliteFeature", "bms feature init %{public}s", apiResult ? "success" : "failure");
61 }
62 
BundleMgrSliteFeature()63 BundleMgrSliteFeature::BundleMgrSliteFeature() : Feature(), identity_()
64 {
65     this->Feature::GetName = BundleMgrSliteFeature::GetFeatureName;
66     this->Feature::OnInitialize = BundleMgrSliteFeature::OnFeatureInitialize;
67     this->Feature::OnStop = BundleMgrSliteFeature::OnFeatureStop;
68     this->Feature::OnMessage = BundleMgrSliteFeature::OnFeatureMessage;
69 }
70 
GetFeatureName(Feature * feature)71 const char *BundleMgrSliteFeature::GetFeatureName(Feature *feature)
72 {
73     return BMS_SLITE_FEATURE;
74 }
75 
GetIdentity()76 Identity *BundleMgrSliteFeature::GetIdentity()
77 {
78     return &identity_;
79 }
80 
OnFeatureInitialize(Feature * feature,Service * parent,Identity identity)81 void BundleMgrSliteFeature::OnFeatureInitialize(Feature *feature, Service *parent, Identity identity)
82 {
83     CHECK_NULLPTR_RETURN(feature, "BundleMgrSliteFeature", "initialize fail");
84     (static_cast<BundleMgrSliteFeature *>(feature))->identity_ = identity;
85 }
86 
OnFeatureStop(Feature * feature,Identity identity)87 void BundleMgrSliteFeature::OnFeatureStop(Feature *feature, Identity identity)
88 {
89 }
90 
OnFeatureMessage(Feature * feature,Request * request)91 BOOL BundleMgrSliteFeature::OnFeatureMessage(Feature *feature, Request *request)
92 {
93     if (feature == nullptr || request == nullptr) {
94         return FALSE;
95     }
96     if (request->msgId == BMS_INSTALL_MSG) {
97         OHOS::GtManagerService::GetInstance().Install(nullptr, nullptr, nullptr);
98     }
99     return TRUE;
100 }
101 
Install(const char * hapPath,const InstallParam * installParam,InstallerCallback installerCallback)102 bool BundleMgrSliteFeature::Install(const char *hapPath, const InstallParam *installParam,
103     InstallerCallback installerCallback)
104 {
105     return OHOS::GtManagerService::GetInstance().Install(hapPath, installParam, installerCallback);
106 }
107 
Uninstall(const char * bundleName,const InstallParam * installParam,InstallerCallback installerCallback)108 bool BundleMgrSliteFeature::Uninstall(const char *bundleName, const InstallParam *installParam,
109     InstallerCallback installerCallback)
110 {
111     return OHOS::GtManagerService::GetInstance().Uninstall(bundleName, installParam, installerCallback);
112 }
113 
QueryAbilityInfo(const Want * want,AbilityInfo * abilityInfo)114 uint8_t BundleMgrSliteFeature::QueryAbilityInfo(const Want *want, AbilityInfo *abilityInfo)
115 {
116     return OHOS::GtManagerService::GetInstance().QueryAbilityInfo(want, abilityInfo);
117 }
118 
GetBundleInfo(const char * bundleName,int32_t flags,BundleInfo * bundleInfo)119 uint8_t BundleMgrSliteFeature::GetBundleInfo(const char *bundleName, int32_t flags, BundleInfo *bundleInfo)
120 {
121     return OHOS::GtManagerService::GetInstance().GetBundleInfo(bundleName, flags, *bundleInfo);
122 }
123 
GetBundleInfos(const int32_t flags,BundleInfo ** bundleInfos,int32_t * len)124 uint8_t BundleMgrSliteFeature::GetBundleInfos(const int32_t flags, BundleInfo **bundleInfos, int32_t *len)
125 {
126     return OHOS::GtManagerService::GetInstance().GetBundleInfos(flags, bundleInfos, len);
127 }
128 
GetInstallState(const char * bundleName,InstallState * installState,uint8_t * installProcess)129 bool BundleMgrSliteFeature::GetInstallState(const char *bundleName, InstallState *installState, uint8_t *installProcess)
130 {
131     return OHOS::GtManagerService::GetInstance().GetInstallState(bundleName, installState, installProcess);
132 }
133 
GetBundleSize(const char * bundleName)134 uint32_t BundleMgrSliteFeature::GetBundleSize(const char *bundleName)
135 {
136     return OHOS::GtManagerService::GetInstance().GetBundleSize(bundleName);
137 }
138 
RegisterInstallerCallback(InstallerCallback installerCallback)139 bool BundleMgrSliteFeature::RegisterInstallerCallback(InstallerCallback installerCallback)
140 {
141     return OHOS::GtManagerService::GetInstance().RegisterInstallerCallback(installerCallback);
142 }
143 
UpdateBundleInfoList()144 void BundleMgrSliteFeature::UpdateBundleInfoList()
145 {
146     OHOS::GtManagerService::GetInstance().UpdateBundleInfoList();
147 }
148 
GetBundleInfosNoReplication(const int flags,BundleInfo ** bundleInfos,int32_t * len)149 uint8_t BundleMgrSliteFeature::GetBundleInfosNoReplication(const int flags, BundleInfo **bundleInfos, int32_t *len)
150 {
151     return OHOS::GtManagerService::GetInstance().GetBundleInfosNoReplication(flags, bundleInfos, len);
152 }
153 
QueryAbilityInfos(const Want * want,AbilityInfo ** abilityInfo,int32_t * len)154 uint8_t BundleMgrSliteFeature::QueryAbilityInfos(const Want *want, AbilityInfo **abilityInfo, int32_t *len)
155 {
156     return OHOS::GtManagerService::GetInstance().QueryAbilityInfos(want, abilityInfo, len);
157 }
158 
RegisterEvent(InstallerCallback installerCallback)159 bool BundleMgrSliteFeature::RegisterEvent(InstallerCallback installerCallback)
160 {
161     return OHOS::GtManagerService::GetInstance().RegisterEvent(installerCallback);
162 }
163 
UnregisterEvent(InstallerCallback installerCallback)164 bool BundleMgrSliteFeature::UnregisterEvent(InstallerCallback installerCallback)
165 {
166     return OHOS::GtManagerService::GetInstance().UnregisterEvent(installerCallback);
167 }
168 
InitPreAppInfo()169 PreAppList *BundleMgrSliteFeature::InitPreAppInfo()
170 {
171     return OHOS::GtManagerService::GetInstance().InitPreAppInfo();
172 }
173 
InsertPreAppInfo(const char * filePath,PreAppList * list)174 void BundleMgrSliteFeature::InsertPreAppInfo(const char *filePath, PreAppList *list)
175 {
176     OHOS::GtManagerService::GetInstance().InsertPreAppInfo(filePath, list);
177 }
178 
SetPreAppInfo(PreAppList * list)179 void BundleMgrSliteFeature::SetPreAppInfo(PreAppList *list)
180 {
181     OHOS::GtManagerService::GetInstance().SetPreAppInfo(list);
182 }
183 } // namespace OHOS
184