1 /*
2 * Copyright (c) 2021-2023 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 "local_ability_manager_stub.h"
17
18 #include <cstdint>
19 #include <utility>
20 #include <cinttypes>
21
22 #include "errors.h"
23 #include "hilog/log_cpp.h"
24 #include "if_local_ability_manager.h"
25 #include "ipc_types.h"
26 #include "message_option.h"
27 #include "message_parcel.h"
28 #include "parse_util.h"
29 #include "safwk_log.h"
30 #include "datetime_ex.h"
31 #include "system_ability_definition.h"
32
33 using namespace OHOS::HiviewDFX;
34
35 namespace OHOS {
36 namespace {
37 const std::string TAG = "LocalAbilityManagerStub";
38 }
39
LocalAbilityManagerStub()40 LocalAbilityManagerStub::LocalAbilityManagerStub()
41 {
42 memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::START_ABILITY_TRANSACTION)] =
43 &LocalAbilityManagerStub::StartAbilityInner;
44 memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::STOP_ABILITY_TRANSACTION)] =
45 &LocalAbilityManagerStub::StopAbilityInner;
46 memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::ACTIVE_ABILITY_TRANSACTION)] =
47 &LocalAbilityManagerStub::ActiveAbilityInner;
48 memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::IDLE_ABILITY_TRANSACTION)] =
49 &LocalAbilityManagerStub::IdleAbilityInner;
50 memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::SEND_STRATEGY_TO_SA_TRANSACTION)] =
51 &LocalAbilityManagerStub::SendStrategyToSAInner;
52 }
53
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int32_t LocalAbilityManagerStub::OnRemoteRequest(uint32_t code,
55 MessageParcel& data, MessageParcel& reply, MessageOption& option)
56 {
57 HILOGD(TAG, "code:%{public}u, flags:%{public}d", code, option.GetFlags());
58 if (!EnforceInterceToken(data)) {
59 HILOGW(TAG, "check interface token failed!");
60 return ERR_PERMISSION_DENIED;
61 }
62 auto iter = memberFuncMap_.find(code);
63 if (iter != memberFuncMap_.end()) {
64 auto memberFunc = iter->second;
65 if (memberFunc != nullptr) {
66 return (this->*memberFunc)(data, reply);
67 }
68 }
69 HILOGW(TAG, "unknown request code!");
70 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71 }
72
StartAbilityInner(MessageParcel & data,MessageParcel & reply)73 int32_t LocalAbilityManagerStub::StartAbilityInner(MessageParcel& data, MessageParcel& reply)
74 {
75 int32_t saId = -1;
76 bool ret = data.ReadInt32(saId);
77 if (!ret) {
78 return ERR_NULL_OBJECT;
79 }
80 if (!CheckInputSysAbilityId(saId)) {
81 HILOGW(TAG, "read saId failed!");
82 return ERR_NULL_OBJECT;
83 }
84 int64_t begin = GetTickCount();
85 std::string eventStr = data.ReadString();
86 if (eventStr.empty()) {
87 HILOGW(TAG, "LocalAbilityManagerStub::StartAbilityInner read eventStr failed!");
88 return ERR_NULL_OBJECT;
89 }
90 bool result = StartAbility(saId, eventStr);
91 HILOGI(TAG, "%{public}s to start SA:%{public}d, eventStrLen:%{public}zu, spend:%{public}" PRId64 " ms",
92 result ? "success" : "failed", saId, eventStr.length(), (GetTickCount() - begin));
93 return ERR_NONE;
94 }
95
StopAbilityInner(MessageParcel & data,MessageParcel & reply)96 int32_t LocalAbilityManagerStub::StopAbilityInner(MessageParcel& data, MessageParcel& reply)
97 {
98 int32_t saId = -1;
99 bool ret = data.ReadInt32(saId);
100 if (!ret) {
101 return ERR_NULL_OBJECT;
102 }
103 if (!CheckInputSysAbilityId(saId)) {
104 HILOGW(TAG, "read saId failed!");
105 return ERR_NULL_OBJECT;
106 }
107 int64_t begin = GetTickCount();
108 std::string eventStr = data.ReadString();
109 if (eventStr.empty()) {
110 HILOGW(TAG, "StopAbilityInner read eventStr failed!");
111 return ERR_NULL_OBJECT;
112 }
113 bool result = StopAbility(saId, eventStr);
114 HILOGI(TAG, "%{public}s to stop SA:%{public}d, eventStrLen:%{public}zu, spend:%{public}" PRId64 " ms",
115 result ? "success" : "failed", saId, eventStr.length(), (GetTickCount() - begin));
116 return ERR_NONE;
117 }
118
ActiveAbilityInner(MessageParcel & data,MessageParcel & reply)119 int32_t LocalAbilityManagerStub::ActiveAbilityInner(MessageParcel& data, MessageParcel& reply)
120 {
121 int32_t saId = -1;
122 bool ret = data.ReadInt32(saId);
123 if (!ret) {
124 return ERR_NULL_OBJECT;
125 }
126 if (!CheckInputSysAbilityId(saId)) {
127 HILOGW(TAG, "read saId failed!");
128 return ERR_NULL_OBJECT;
129 }
130 int64_t begin = GetTickCount();
131 nlohmann::json activeReason = ParseUtil::StringToJsonObj(data.ReadString());
132 bool result = ActiveAbility(saId, activeReason);
133 if (!reply.WriteBool(result)) {
134 HILOGW(TAG, "ActiveAbilityInner Write result failed!");
135 return ERR_NULL_OBJECT;
136 }
137 HILOGI(TAG, "%{public}s to Active SA:%{public}d, spend:%{public}" PRId64 " ms",
138 result ? "success" : "failed", saId, (GetTickCount() - begin));
139 return ERR_NONE;
140 }
141
IdleAbilityInner(MessageParcel & data,MessageParcel & reply)142 int32_t LocalAbilityManagerStub::IdleAbilityInner(MessageParcel& data, MessageParcel& reply)
143 {
144 int32_t saId = -1;
145 bool ret = data.ReadInt32(saId);
146 if (!ret) {
147 return ERR_NULL_OBJECT;
148 }
149 if (!CheckInputSysAbilityId(saId)) {
150 HILOGW(TAG, "read saId failed!");
151 return ERR_NULL_OBJECT;
152 }
153 int64_t begin = GetTickCount();
154 nlohmann::json idleReason = ParseUtil::StringToJsonObj(data.ReadString());
155 int32_t delayTime = 0;
156 bool result = IdleAbility(saId, idleReason, delayTime);
157 if (!reply.WriteBool(result)) {
158 HILOGW(TAG, "ActiveAbilityInner Write result failed!");
159 return ERR_NULL_OBJECT;
160 }
161 if (!reply.WriteInt32(delayTime)) {
162 HILOGW(TAG, "ActiveAbilityInner Write delayTime failed!");
163 return ERR_NULL_OBJECT;
164 }
165 HILOGI(TAG, "%{public}s to Idle SA:%{public}d, delayTime:%{public}d spend:%{public}" PRId64 " ms",
166 result ? "success" : "failed", saId, delayTime, (GetTickCount() - begin));
167 return ERR_NONE;
168 }
169
SendStrategyToSAInner(MessageParcel & data,MessageParcel & reply)170 int32_t LocalAbilityManagerStub::SendStrategyToSAInner(MessageParcel& data, MessageParcel& reply)
171 {
172 int32_t type = -1;
173 bool ret = data.ReadInt32(type);
174 if (!ret) {
175 return ERR_NULL_OBJECT;
176 }
177 int32_t saId = -1;
178 ret = data.ReadInt32(saId);
179 if (!ret) {
180 return ERR_NULL_OBJECT;
181 }
182 if (!CheckInputSysAbilityId(saId)) {
183 HILOGW(TAG, "read saId failed!");
184 return ERR_NULL_OBJECT;
185 }
186 int32_t level = -1;
187 ret = data.ReadInt32(level);
188 if (!ret) {
189 return ERR_NULL_OBJECT;
190 }
191 std::string aciton;
192 ret = data.ReadString(aciton);
193 if (!ret) {
194 return ERR_NULL_OBJECT;
195 }
196 bool result = SendStrategyToSA(type, saId, level, aciton);
197 if (!reply.WriteBool(result)) {
198 HILOGW(TAG, "SendStrategyToSA Write result failed!");
199 return ERR_NULL_OBJECT;
200 }
201 HILOGD(TAG, "SendStrategyToSA called %{public}s ", result ? "success" : "failed");
202 return ERR_NONE;
203 }
204
CheckInputSysAbilityId(int32_t systemAbilityId)205 bool LocalAbilityManagerStub::CheckInputSysAbilityId(int32_t systemAbilityId)
206 {
207 return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
208 }
209
EnforceInterceToken(MessageParcel & data)210 bool LocalAbilityManagerStub::EnforceInterceToken(MessageParcel& data)
211 {
212 std::u16string interfaceToken = data.ReadInterfaceToken();
213 return interfaceToken == LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN;
214 }
215 }
216