1 /*
2  * Copyright (c) 2024 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 "storage/bundle_manager_connector.h"
17 
18 #include <cstdlib>
19 #include <cstring>
20 #include <mntent.h>
21 #include <singleton.h>
22 #include <sys/statvfs.h>
23 #include <unordered_set>
24 
25 #include "iservice_registry.h"
26 #include "storage_service_errno.h"
27 #include "storage_service_log.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace StorageManager {
BundleMgrConnector()32 BundleMgrConnector::BundleMgrConnector() {}
33 
~BundleMgrConnector()34 BundleMgrConnector::~BundleMgrConnector()
35 {
36     bundleMgr_ = nullptr;
37     deathRecipient_ = nullptr;
38 }
39 
GetBundleMgrProxy()40 sptr<AppExecFwk::IBundleMgr> BundleMgrConnector::GetBundleMgrProxy()
41 {
42     std::lock_guard<std::mutex> lock(mutex_);
43     if (bundleMgr_ == nullptr) {
44         auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45         if (sam == nullptr) {
46             LOGE("BundleMgrConnector::GetBundleMgrProxy samgr == nullptr");
47             return nullptr;
48         }
49         sptr<IRemoteObject> remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
50         if (!remoteObject) {
51             LOGE("BundleMgrConnector::GetBundleMgrProxy remoteObj == nullptr");
52             return nullptr;
53         }
54         bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
55         if (bundleMgr_ == nullptr) {
56             LOGE("BundleMgrConnector::GetBundleMgrProxy bundleMgr == nullptr");
57             return nullptr;
58         }
59         deathRecipient_ = new (std::nothrow) BundleMgrDeathRecipient();
60         if (!deathRecipient_) {
61             LOGE("BundleMgrConnector::GetBundleMgrProxy failed to create death recipient");
62             bundleMgr_ = nullptr;
63             return nullptr;
64         }
65         bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_);
66     }
67     return bundleMgr_;
68 }
69 
ResetBundleMgrProxy()70 int32_t BundleMgrConnector::ResetBundleMgrProxy()
71 {
72     std::lock_guard<std::mutex> lock(mutex_);
73     if ((bundleMgr_ != nullptr) && (bundleMgr_->AsObject() != nullptr)) {
74         bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
75     }
76     bundleMgr_ = nullptr;
77     return E_OK;
78 }
79 
OnRemoteDied(const wptr<IRemoteObject> & remote)80 void BundleMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
81 {
82     DelayedSingleton<BundleMgrConnector>::GetInstance()->ResetBundleMgrProxy();
83 }
84 } // StorageManager
85 } // OHOS
86