1 /*
2 * Copyright (c) 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 "enterprise_conn_manager.h"
17
18 #include <if_system_ability_manager.h>
19 #include <ipc_skeleton.h>
20 #include <iservice_registry.h>
21 #include <string_ex.h>
22 #include <system_ability_definition.h>
23
24 #include "ability_manager_client.h"
25 #include "ability_manager_proxy.h"
26 #include "edm_log.h"
27
28 using namespace OHOS::AAFwk;
29
30 namespace OHOS {
31 namespace EDM {
CreateAdminConnection(const AAFwk::Want & want,uint32_t code,uint32_t userId)32 sptr<IEnterpriseConnection> EnterpriseConnManager::CreateAdminConnection(const AAFwk::Want &want,
33 uint32_t code, uint32_t userId)
34 {
35 sptr<IEnterpriseConnection> connection(new (std::nothrow) EnterpriseAdminConnection(want, code, userId));
36 return connection;
37 }
38
CreateBundleConnection(const AAFwk::Want & want,uint32_t code,uint32_t userId,const std::string & bundleName)39 sptr<IEnterpriseConnection> EnterpriseConnManager::CreateBundleConnection(const AAFwk::Want &want,
40 uint32_t code, uint32_t userId, const std::string &bundleName)
41 {
42 sptr<IEnterpriseConnection> connection(new (std::nothrow) EnterpriseBundleConnection(want,
43 code, userId, bundleName));
44 return connection;
45 }
46
ConnectAbility(const sptr<IEnterpriseConnection> & connection)47 bool EnterpriseConnManager::ConnectAbility(const sptr<IEnterpriseConnection>& connection)
48 {
49 if (connection == nullptr) {
50 return false;
51 }
52 std::lock_guard<std::mutex> lock(mutex_);
53
54 if (!GetAbilityMgrProxy()) {
55 EDMLOGE("failed to get ability manager proxy!");
56 return false;
57 }
58
59 int32_t ret = abilityMgr_->ConnectAbility(connection->GetWant(), connection, nullptr, connection->GetUserId());
60 EDMLOGI("ConnectAbility over.");
61 if (ret != ERR_OK) {
62 EDMLOGE("connect failed");
63 return false;
64 }
65 return true;
66 }
67
GetAbilityMgrProxy()68 bool EnterpriseConnManager::GetAbilityMgrProxy()
69 {
70 EDMLOGI("GetAbilityMgrProxy enter");
71 if (abilityMgr_ == nullptr) {
72 sptr<ISystemAbilityManager> systemAbilityManager =
73 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
74 if (systemAbilityManager == nullptr) {
75 EDMLOGE("Failed to get system ability mgr.");
76 return false;
77 }
78
79 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
80 if (remoteObject == nullptr) {
81 EDMLOGE("Failed to get ability manager service.");
82 return false;
83 }
84
85 abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
86 if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
87 EDMLOGE("Failed to get system ability manager services ability");
88 return false;
89 }
90
91 deathRecipient_ = (new (std::nothrow) AbilityManagerDeathRecipient());
92 if (deathRecipient_ == nullptr) {
93 EDMLOGE("Failed to create AbilityManagerDeathRecipient");
94 return false;
95 }
96 if (!abilityMgr_->AsObject()->AddDeathRecipient(deathRecipient_)) {
97 EDMLOGW("Failed to add AbilityManagerDeathRecipient");
98 }
99 }
100 return true;
101 }
102
Clear()103 void EnterpriseConnManager::Clear()
104 {
105 EDMLOGI("enter");
106 std::lock_guard<std::mutex> lock(mutex_);
107
108 if ((abilityMgr_ != nullptr) && (abilityMgr_->AsObject() != nullptr)) {
109 abilityMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
110 }
111 abilityMgr_ = nullptr;
112 }
113 } // namespace EDM
114 } // namespace OHOS
115