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 <hisysevent.h>
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 "work_sched_hilog.h"
26 #include "errors.h"
27
28 using namespace std;
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::HiviewDFX;
31
32 namespace OHOS {
33 namespace WorkScheduler {
AddConnInfo(string & workId,sptr<WorkSchedulerConnection> & connection)34 void WorkConnManager::AddConnInfo(string &workId, sptr<WorkSchedulerConnection> &connection)
35 {
36 std::lock_guard<std::mutex> lock(connMapMutex_);
37 connMap_.emplace(workId, connection);
38 }
39
RemoveConnInfo(string & workId)40 void WorkConnManager::RemoveConnInfo(string &workId)
41 {
42 std::lock_guard<std::mutex> lock(connMapMutex_);
43 connMap_.erase(workId);
44 }
45
GetConnInfo(string & workId)46 sptr<WorkSchedulerConnection> WorkConnManager::GetConnInfo(string &workId)
47 {
48 std::lock_guard<std::mutex> lock(connMapMutex_);
49 if (connMap_.count(workId) > 0) {
50 return connMap_.at(workId);
51 }
52 return nullptr;
53 }
54
StartWork(shared_ptr<WorkStatus> workStatus)55 bool WorkConnManager::StartWork(shared_ptr<WorkStatus> workStatus)
56 {
57 WS_HILOGD("Start Work with id: %{public}s, bundleName: %{public}s, abilityName: %{public}s",
58 workStatus->workId_.c_str(), workStatus->bundleName_.c_str(), workStatus->abilityName_.c_str());
59 sptr<ISystemAbilityManager> systemAbilityManager =
60 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61 if (systemAbilityManager == nullptr) {
62 WS_HILOGE("Failed to get system ability manager service.");
63 return false;
64 }
65 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
66 if (remoteObject == nullptr) {
67 WS_HILOGE("Failed to ability manager service.");
68 return false;
69 }
70 sptr<AAFwk::IAbilityManager> abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
71 if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
72 WS_HILOGE("Failed to get ability manager services object");
73 return false;
74 }
75
76 WS_HILOGI("Begin to connect bundle:%{public}s, abilityName:%{public}s, userId:%{public}d",
77 workStatus->bundleName_.c_str(), workStatus->abilityName_.c_str(), workStatus->userId_);
78 sptr<WorkSchedulerConnection> connection(new (std::nothrow) WorkSchedulerConnection(workStatus->workInfo_));
79 if (connection == nullptr) {
80 WS_HILOGE("Failed to new connection.");
81 return false;
82 }
83
84 Want want;
85 want.SetElementName(workStatus->bundleName_, workStatus->abilityName_);
86 int32_t ret = abilityMgr_->ConnectAbility(want, connection, nullptr, workStatus->userId_);
87 if (ret != ERR_OK) {
88 WS_HILOGE("connect failed");
89 return false;
90 }
91 AddConnInfo(workStatus->workId_, connection);
92
93 // Notify work add event to battery statistics
94 WriteStartWorkEvent(workStatus);
95
96 return true;
97 }
98
DisConnect(sptr<WorkSchedulerConnection> connect)99 bool WorkConnManager::DisConnect(sptr<WorkSchedulerConnection> connect)
100 {
101 sptr<ISystemAbilityManager> systemAbilityManager =
102 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103 if (systemAbilityManager == nullptr) {
104 WS_HILOGE("Failed to get system ability manager service.");
105 return false;
106 }
107 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
108 if (remoteObject == nullptr) {
109 WS_HILOGE("Failed to ability manager service.");
110 return false;
111 }
112 sptr<AAFwk::IAbilityManager> abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
113 if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
114 WS_HILOGE("Failed to get ability manager services object.");
115 return false;
116 }
117 int32_t ret = abilityMgr_->DisconnectAbility(connect);
118 if (ret != ERR_OK) {
119 WS_HILOGE("disconnect failed");
120 return false;
121 }
122 return true;
123 }
124
StopWork(shared_ptr<WorkStatus> workStatus)125 bool WorkConnManager::StopWork(shared_ptr<WorkStatus> workStatus)
126 {
127 bool ret = false;
128 sptr<WorkSchedulerConnection> conn = GetConnInfo(workStatus->workId_);
129 if (conn != nullptr) {
130 conn->StopWork();
131 ret = DisConnect(conn);
132 } else {
133 WS_HILOGE("connection is null");
134 }
135 RemoveConnInfo(workStatus->workId_);
136
137 // Notify work remove event to battery statistics
138 int32_t pid = IPCSkeleton::GetCallingPid();
139 HiSysEvent::Write("WORKSCHEDULER", "WORK_STOP", HiSysEvent::EventType::STATISTIC, "UID",
140 workStatus->uid_, "PID", pid, "NAME", workStatus->bundleName_, "WORKID", workStatus->workId_);
141
142 return ret;
143 }
144
WriteStartWorkEvent(shared_ptr<WorkStatus> workStatus)145 void WorkConnManager::WriteStartWorkEvent(shared_ptr<WorkStatus> workStatus)
146 {
147 int32_t pid = IPCSkeleton::GetCallingPid();
148 string conditions = "";
149 if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::NETWORK) > 0) {
150 conditions.append("NETWORK-");
151 }
152
153 if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::CHARGER) > 0) {
154 conditions.append("CHARGER-");
155 }
156
157 if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::BATTERY_STATUS) > 0) {
158 conditions.append("BATTERY_STATUS-");
159 }
160
161 if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::BATTERY_LEVEL) > 0) {
162 conditions.append("BATTERY_LEVEL-");
163 }
164
165 if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::STORAGE) > 0) {
166 conditions.append("STORAGE-");
167 }
168
169 if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::TIMER) > 0) {
170 conditions.append("TIMER-");
171 }
172 conditions.pop_back();
173
174 string type = "Repeat";
175 if (!workStatus->workInfo_->IsRepeat()) {
176 type = "Not Repeat";
177 }
178
179 HiSysEvent::Write("WORKSCHEDULER", "WORK_START", HiSysEvent::EventType::STATISTIC, "UID",
180 workStatus->uid_, "PID", pid, "NAME", workStatus->bundleName_, "WORKID", workStatus->workId_, "TRIGGER",
181 conditions, "TYPE", type, "INTERVAL", workStatus->workInfo_->GetTimeInterval());
182 }
183 } // namespace WorkScheduler
184 } // namespace OHOS
185