• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "system_bundle_installer.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_mgr_service.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
SystemBundleInstaller()23 SystemBundleInstaller::SystemBundleInstaller()
24 {
25     APP_LOGI("system bundle installer instance is created");
26 }
27 
~SystemBundleInstaller()28 SystemBundleInstaller::~SystemBundleInstaller()
29 {
30     APP_LOGI("system bundle installer instance is destroyed");
31 }
32 
InstallSystemBundle(const std::string & filePath,InstallParam & installParam,Constants::AppType appType)33 bool SystemBundleInstaller::InstallSystemBundle(
34     const std::string &filePath,
35     InstallParam &installParam,
36     Constants::AppType appType)
37 {
38     MarkPreBundleSyeEventBootTag(true);
39     ErrCode result = InstallBundle(filePath, installParam, appType);
40     if (result != ERR_OK) {
41         APP_LOGE("install system bundle fail, error: %{public}d", result);
42         return false;
43     }
44     return true;
45 }
46 
InstallSystemSharedBundle(InstallParam & installParam,bool isOTA,Constants::AppType appType)47 bool SystemBundleInstaller::InstallSystemSharedBundle(
48     InstallParam &installParam,
49     bool isOTA,
50     Constants::AppType appType)
51 {
52     MarkPreBundleSyeEventBootTag(!isOTA);
53     std::vector<std::string> bundlePaths{};
54     ErrCode result = InstallBundle(bundlePaths, installParam, appType);
55     if (result != ERR_OK) {
56         APP_LOGE("install system bundle fail, error: %{public}d", result);
57         return false;
58     }
59     return true;
60 }
61 
OTAInstallSystemBundle(const std::vector<std::string> & filePaths,InstallParam & installParam,Constants::AppType appType)62 bool SystemBundleInstaller::OTAInstallSystemBundle(
63     const std::vector<std::string> &filePaths,
64     InstallParam &installParam,
65     Constants::AppType appType)
66 {
67     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
68     if (dataMgr == nullptr) {
69         APP_LOGE("Get dataMgr shared_ptr nullptr");
70         return false;
71     }
72     bool ret = true;
73     for (auto allUserId : dataMgr->GetAllUser()) {
74         installParam.userId = allUserId;
75         MarkPreBundleSyeEventBootTag(false);
76         otaInstall_ = true;
77         ErrCode errCode = InstallBundle(filePaths, installParam, appType);
78         if ((errCode != ERR_OK) && (errCode != ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON)) {
79             APP_LOGE("install system bundle fail, error: %{public}d", errCode);
80             ret = false;
81         }
82         ResetInstallProperties();
83     }
84 
85     return ret;
86 }
87 
UninstallSystemBundle(const std::string & bundleName)88 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName)
89 {
90     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
91     if (dataMgr == nullptr) {
92         APP_LOGE("Get dataMgr shared_ptr nullptr");
93         return false;
94     }
95 
96     InstallParam installParam;
97     for (auto userId : dataMgr->GetAllUser()) {
98         installParam.userId = userId;
99         installParam.needSavePreInstallInfo = true;
100         installParam.isPreInstallApp = true;
101         installParam.noSkipsKill = false;
102         installParam.needSendEvent = false;
103         MarkPreBundleSyeEventBootTag(false);
104         ErrCode result = UninstallBundle(bundleName, installParam);
105         if (result != ERR_OK) {
106             APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
107         }
108 
109         ResetInstallProperties();
110     }
111     return true;
112 }
113 
UninstallSystemBundle(const std::string & bundleName,bool isKeepData)114 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, bool isKeepData)
115 {
116     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
117     if (dataMgr == nullptr) {
118         APP_LOGE("Get dataMgr shared_ptr nullptr!");
119         return false;
120     }
121 
122     InstallParam installParam;
123     for (auto userId : dataMgr->GetAllUser()) {
124         installParam.userId = userId;
125         installParam.needSavePreInstallInfo = true;
126         installParam.isPreInstallApp = true;
127         installParam.noSkipsKill = false;
128         installParam.needSendEvent = false;
129         installParam.isKeepData = isKeepData;
130         MarkPreBundleSyeEventBootTag(false);
131         ErrCode result = UninstallBundle(bundleName, installParam);
132         if (result != ERR_OK) {
133             APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
134         }
135 
136         ResetInstallProperties();
137     }
138     return true;
139 }
140 
UninstallSystemBundle(const std::string & bundleName,const std::string & modulePackage)141 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, const std::string &modulePackage)
142 {
143     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
144     if (dataMgr == nullptr) {
145         APP_LOGE("Get dataMgr shared_ptr nullptr");
146         return false;
147     }
148 
149     InstallParam installParam;
150     for (auto userId : dataMgr->GetAllUser()) {
151         installParam.userId = userId;
152         installParam.needSavePreInstallInfo = true;
153         installParam.isPreInstallApp = true;
154         installParam.noSkipsKill = false;
155         installParam.needSendEvent = false;
156         MarkPreBundleSyeEventBootTag(false);
157         ErrCode result = UninstallBundle(bundleName, modulePackage, installParam);
158         if (result != ERR_OK) {
159             APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
160         }
161 
162         ResetInstallProperties();
163     }
164 
165     return true;
166 }
167 }  // namespace AppExecFwk
168 }  // namespace OHOS
169