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