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
OTAInstallSystemBundle(const std::vector<std::string> & filePaths,InstallParam & installParam,Constants::AppType appType)47 bool SystemBundleInstaller::OTAInstallSystemBundle(
48 const std::vector<std::string> &filePaths,
49 InstallParam &installParam,
50 Constants::AppType appType)
51 {
52 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
53 if (dataMgr == nullptr) {
54 APP_LOGE("Get dataMgr shared_ptr nullptr");
55 return false;
56 }
57
58 for (auto allUserId : dataMgr->GetAllUser()) {
59 installParam.userId = allUserId;
60 MarkPreBundleSyeEventBootTag(false);
61 ErrCode result = InstallBundle(filePaths, installParam, appType);
62 if (result != ERR_OK) {
63 APP_LOGW("install system bundle fail, error: %{public}d", result);
64 }
65
66 ResetInstallProperties();
67 }
68
69 return true;
70 }
71
UninstallSystemBundle(const std::string & bundleName)72 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName)
73 {
74 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
75 if (dataMgr == nullptr) {
76 APP_LOGE("Get dataMgr shared_ptr nullptr");
77 return false;
78 }
79
80 InstallParam installParam;
81 for (auto userId : dataMgr->GetAllUser()) {
82 installParam.userId = userId;
83 installParam.needSavePreInstallInfo = true;
84 installParam.isPreInstallApp = true;
85 installParam.noSkipsKill = false;
86 installParam.needSendEvent = false;
87 MarkPreBundleSyeEventBootTag(false);
88 ErrCode result = UninstallBundle(bundleName, installParam);
89 if (result != ERR_OK) {
90 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
91 }
92
93 ResetInstallProperties();
94 }
95 return true;
96 }
97
UninstallSystemBundle(const std::string & bundleName,bool isKeepData)98 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, bool isKeepData)
99 {
100 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
101 if (dataMgr == nullptr) {
102 APP_LOGE("Get dataMgr shared_ptr nullptr!");
103 return false;
104 }
105
106 InstallParam installParam;
107 for (auto userId : dataMgr->GetAllUser()) {
108 installParam.userId = userId;
109 installParam.needSavePreInstallInfo = true;
110 installParam.isPreInstallApp = true;
111 installParam.noSkipsKill = false;
112 installParam.needSendEvent = false;
113 installParam.isKeepData = isKeepData;
114 MarkPreBundleSyeEventBootTag(false);
115 ErrCode result = UninstallBundle(bundleName, installParam);
116 if (result != ERR_OK) {
117 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
118 }
119
120 ResetInstallProperties();
121 }
122 return true;
123 }
124
UninstallSystemBundle(const std::string & bundleName,const std::string & modulePackage)125 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, const std::string &modulePackage)
126 {
127 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
128 if (dataMgr == nullptr) {
129 APP_LOGE("Get dataMgr shared_ptr nullptr");
130 return false;
131 }
132
133 InstallParam installParam;
134 for (auto userId : dataMgr->GetAllUser()) {
135 installParam.userId = userId;
136 installParam.needSavePreInstallInfo = true;
137 installParam.isPreInstallApp = true;
138 installParam.noSkipsKill = false;
139 installParam.needSendEvent = false;
140 MarkPreBundleSyeEventBootTag(false);
141 ErrCode result = UninstallBundle(bundleName, modulePackage, installParam);
142 if (result != ERR_OK) {
143 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
144 }
145
146 ResetInstallProperties();
147 }
148
149 return true;
150 }
151 } // namespace AppExecFwk
152 } // namespace OHOS
153