• 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 #include "work_conn_manager.h"
16 
17 #include <if_system_ability_manager.h>
18 #include <ipc_skeleton.h>
19 #include <iservice_registry.h>
20 #include <string_ex.h>
21 #include <system_ability_definition.h>
22 
23 #include "ability_manager_client.h"
24 #include "work_sched_common.h"
25 
26 using namespace std;
27 using namespace OHOS::AAFwk;
28 
29 namespace OHOS {
30 namespace WorkScheduler {
AddConnInfo(string & workId,sptr<WorkSchedulerConnection> & connection)31 void WorkConnManager::AddConnInfo(string &workId, sptr<WorkSchedulerConnection> &connection)
32 {
33     std::lock_guard<std::mutex> lock(connMapMutex_);
34     connMap_.emplace(workId, connection);
35 }
36 
RemoveConnInfo(string & workId)37 void WorkConnManager::RemoveConnInfo(string &workId)
38 {
39     std::lock_guard<std::mutex> lock(connMapMutex_);
40     connMap_.erase(workId);
41 }
42 
GetConnInfo(string & workId)43 sptr<WorkSchedulerConnection> WorkConnManager::GetConnInfo(string &workId)
44 {
45     std::lock_guard<std::mutex> lock(connMapMutex_);
46     if (connMap_.count(workId) > 0) {
47         return connMap_.at(workId);
48     }
49     return nullptr;
50 }
51 
StartWork(shared_ptr<WorkStatus> workStatus)52 bool WorkConnManager::StartWork(shared_ptr<WorkStatus> workStatus)
53 {
54     WS_HILOGD("StartWork, id: %{public}s, bundleName: %{public}s, abilityName: %{public}s",
55         workStatus->workId_.c_str(), workStatus->bundleName_.c_str(), workStatus->abilityName_.c_str());
56     sptr<ISystemAbilityManager> systemAbilityManager =
57         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58     if (systemAbilityManager == nullptr) {
59         WS_HILOGI("Failed to get system ability manager service.");
60         return false;
61     }
62     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
63     if (remoteObject == nullptr) {
64         WS_HILOGI("Failed to ability manager service.");
65         return false;
66     }
67     sptr<AAFwk::IAbilityManager> abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
68     if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
69         WS_HILOGI("Failed to get ability manager services object");
70         return false;
71     }
72 
73     WS_HILOGI("Begin to connect bundle:%{public}s, abilityName:%{public}s, userId:%{public}d",
74         workStatus->bundleName_.c_str(), workStatus->abilityName_.c_str(), workStatus->userId_);
75     sptr<WorkSchedulerConnection> connection(new (std::nothrow) WorkSchedulerConnection(workStatus->workInfo_));
76     if (connection == nullptr) {
77         WS_HILOGI("Failed to new connection.");
78         return false;
79     }
80 
81     Want want;
82     want.SetElementName(workStatus->bundleName_, workStatus->abilityName_);
83     int ret = abilityMgr_->ConnectAbility(want, connection, nullptr, workStatus->userId_);
84     if (ret != ERR_OK) {
85         WS_HILOGE("connect failed");
86         return false;
87     }
88     AddConnInfo(workStatus->workId_, connection);
89     return true;
90 }
91 
DisConnect(sptr<WorkSchedulerConnection> connect)92 bool WorkConnManager::DisConnect(sptr<WorkSchedulerConnection> connect)
93 {
94     sptr<ISystemAbilityManager> systemAbilityManager =
95         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
96     if (systemAbilityManager == nullptr) {
97         WS_HILOGI("Failed to get system ability manager service.");
98         return false;
99     }
100     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
101     if (remoteObject == nullptr) {
102         WS_HILOGI("Failed to ability manager service.");
103         return false;
104     }
105     sptr<AAFwk::IAbilityManager> abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
106     if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
107         WS_HILOGI("Failed to  get ability manager services object.");
108         return false;
109     }
110     int ret = abilityMgr_->DisconnectAbility(connect);
111     if (ret != ERR_OK) {
112         WS_HILOGE("disconnect failed");
113         return false;
114     }
115     return true;
116 }
117 
StopWork(shared_ptr<WorkStatus> workStatus)118 bool WorkConnManager::StopWork(shared_ptr<WorkStatus> workStatus)
119 {
120     bool ret = false;
121     sptr<WorkSchedulerConnection> conn = GetConnInfo(workStatus->workId_);
122     if (conn != nullptr) {
123         conn->StopWork();
124         ret = DisConnect(conn);
125     } else {
126         WS_HILOGD("connection is null");
127     }
128     RemoveConnInfo(workStatus->workId_);
129     return ret;
130 }
131 } // namespace WorkScheduler
132 } // namespace OHOS