1 /*
2 * Copyright (c) 2021 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_installer.h"
17
18 #include <cinttypes>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_installer_manager.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25
BundleInstaller(const int64_t installerId,const std::shared_ptr<EventHandler> & handler,const sptr<IStatusReceiver> & statusReceiver)26 BundleInstaller::BundleInstaller(const int64_t installerId, const std::shared_ptr<EventHandler> &handler,
27 const sptr<IStatusReceiver> &statusReceiver)
28 : installerId_(installerId), handler_(handler), statusReceiver_(statusReceiver)
29 {
30 APP_LOGI("create bundle installer instance, the installer id is %{public}" PRId64 "", installerId_);
31 }
32
~BundleInstaller()33 BundleInstaller::~BundleInstaller()
34 {
35 APP_LOGI("destroy bundle installer instance, the installer id is %{public}" PRId64 "", installerId_);
36 }
37
Install(const std::string & bundleFilePath,const InstallParam & installParam)38 void BundleInstaller::Install(const std::string &bundleFilePath, const InstallParam &installParam)
39 {
40 auto resultCode = InstallBundle(bundleFilePath, installParam, Constants::AppType::THIRD_PARTY_APP);
41 statusReceiver_->OnFinished(resultCode, "");
42 SendRemoveEvent();
43 }
44
Uninstall(const std::string & bundleName,const InstallParam & installParam)45 void BundleInstaller::Uninstall(const std::string &bundleName, const InstallParam &installParam)
46 {
47 auto resultCode = UninstallBundle(bundleName, installParam);
48 statusReceiver_->OnFinished(resultCode, "");
49 SendRemoveEvent();
50 }
51
Uninstall(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam)52 void BundleInstaller::Uninstall(
53 const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam)
54 {
55 auto resultCode = UninstallBundle(bundleName, modulePackage, installParam);
56 statusReceiver_->OnFinished(resultCode, "");
57 SendRemoveEvent();
58 }
59
UpdateInstallerState(const InstallerState state)60 void BundleInstaller::UpdateInstallerState(const InstallerState state)
61 {
62 APP_LOGI("UpdateInstallerState in bundleInstaller state %{public}d", state);
63 SetInstallerState(state);
64 if (statusReceiver_) {
65 statusReceiver_->OnStatusNotify(static_cast<int>(state));
66 }
67 }
68
SendRemoveEvent() const69 void BundleInstaller::SendRemoveEvent() const
70 {
71 if (auto handler = handler_.lock()) {
72 uint32_t eventId = static_cast<uint32_t>(BundleInstallerManager::REMOVE_BUNDLE_INSTALLER);
73 handler->SendEvent(InnerEvent::Get(eventId, installerId_));
74 } else {
75 APP_LOGE("fail to remove %{public}" PRId64 " installer due to handler is expired", installerId_);
76 }
77 }
78
79 } // namespace AppExecFwk
80 } // namespace OHOS