• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include "errors.h"
22 #include "hilog/log_cpp.h"
23 #include "if_local_ability_manager.h"
24 #include "ipc_types.h"
25 #include "message_option.h"
26 #include "message_parcel.h"
27 #include "parse_util.h"
28 #include "safwk_log.h"
29 #include "system_ability_definition.h"
30 
31 using namespace OHOS::HiviewDFX;
32 
33 namespace OHOS {
34 namespace {
35 const std::string TAG = "LocalAbilityManagerStub";
36 }
37 
LocalAbilityManagerStub()38 LocalAbilityManagerStub::LocalAbilityManagerStub()
39 {
40     memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::START_ABILITY_TRANSACTION)] =
41         &LocalAbilityManagerStub::StartAbilityInner;
42     memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::STOP_ABILITY_TRANSACTION)] =
43         &LocalAbilityManagerStub::StopAbilityInner;
44     memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::ACTIVE_ABILITY_TRANSACTION)] =
45         &LocalAbilityManagerStub::ActiveAbilityInner;
46     memberFuncMap_[static_cast<uint32_t>(SafwkInterfaceCode::IDLE_ABILITY_TRANSACTION)] =
47         &LocalAbilityManagerStub::IdleAbilityInner;
48 }
49 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int32_t LocalAbilityManagerStub::OnRemoteRequest(uint32_t code,
51     MessageParcel& data, MessageParcel& reply, MessageOption& option)
52 {
53     HILOGI(TAG, "code:%{public}u, flags:%{public}d", code, option.GetFlags());
54     if (!EnforceInterceToken(data)) {
55         HILOGW(TAG, "check interface token failed!");
56         return ERR_PERMISSION_DENIED;
57     }
58     auto iter = memberFuncMap_.find(code);
59     if (iter != memberFuncMap_.end()) {
60         auto memberFunc = iter->second;
61         if (memberFunc != nullptr) {
62             return (this->*memberFunc)(data, reply);
63         }
64     }
65     HILOGW(TAG, "unknown request code!");
66     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67 }
68 
StartAbilityInner(MessageParcel & data,MessageParcel & reply)69 int32_t LocalAbilityManagerStub::StartAbilityInner(MessageParcel& data, MessageParcel& reply)
70 {
71     int32_t saId = data.ReadInt32();
72     if (!CheckInputSysAbilityId(saId)) {
73         HILOGW(TAG, "read saId failed!");
74         return ERR_NULL_OBJECT;
75     }
76     std::string eventStr = data.ReadString();
77     if (eventStr.empty()) {
78         HILOGW(TAG, "LocalAbilityManagerStub::StartAbilityInner read eventStr failed!");
79         return ERR_NULL_OBJECT;
80     }
81     bool result = StartAbility(saId, eventStr);
82     HILOGI(TAG, "%{public}s to start ability", result ? "success" : "failed");
83     return ERR_NONE;
84 }
85 
StopAbilityInner(MessageParcel & data,MessageParcel & reply)86 int32_t LocalAbilityManagerStub::StopAbilityInner(MessageParcel& data, MessageParcel& reply)
87 {
88     int32_t saId = data.ReadInt32();
89     if (!CheckInputSysAbilityId(saId)) {
90         HILOGW(TAG, "read saId failed!");
91         return ERR_NULL_OBJECT;
92     }
93     std::string eventStr = data.ReadString();
94     if (eventStr.empty()) {
95         HILOGW(TAG, "LocalAbilityManagerStub::StopAbilityInner read eventStr failed!");
96         return ERR_NULL_OBJECT;
97     }
98     bool result = StopAbility(saId, eventStr);
99     HILOGI(TAG, "%{public}s to stop ability", result ? "success" : "failed");
100     return ERR_NONE;
101 }
102 
ActiveAbilityInner(MessageParcel & data,MessageParcel & reply)103 int32_t LocalAbilityManagerStub::ActiveAbilityInner(MessageParcel& data, MessageParcel& reply)
104 {
105     int32_t saId = data.ReadInt32();
106     if (!CheckInputSysAbilityId(saId)) {
107         HILOGW(TAG, "read saId failed!");
108         return ERR_NULL_OBJECT;
109     }
110     nlohmann::json activeReason = ParseUtil::StringToJsonObj(data.ReadString());
111     bool result = ActiveAbility(saId, activeReason);
112     if (!reply.WriteBool(result)) {
113         HILOGW(TAG, "ActiveAbilityInner Write result failed!");
114         return ERR_NULL_OBJECT;
115     }
116     return ERR_NONE;
117 }
118 
IdleAbilityInner(MessageParcel & data,MessageParcel & reply)119 int32_t LocalAbilityManagerStub::IdleAbilityInner(MessageParcel& data, MessageParcel& reply)
120 {
121     int32_t saId = data.ReadInt32();
122     if (!CheckInputSysAbilityId(saId)) {
123         HILOGW(TAG, "read saId failed!");
124         return ERR_NULL_OBJECT;
125     }
126     nlohmann::json idleReason = ParseUtil::StringToJsonObj(data.ReadString());
127     int32_t delayTime = 0;
128     bool result = IdleAbility(saId, idleReason, delayTime);
129     if (!reply.WriteBool(result)) {
130         HILOGW(TAG, "ActiveAbilityInner Write result failed!");
131         return ERR_NULL_OBJECT;
132     }
133     if (!reply.WriteInt32(delayTime)) {
134         HILOGW(TAG, "ActiveAbilityInner Write delayTime failed!");
135         return ERR_NULL_OBJECT;
136     }
137     return ERR_NONE;
138 }
139 
CheckInputSysAbilityId(int32_t systemAbilityId)140 bool LocalAbilityManagerStub::CheckInputSysAbilityId(int32_t systemAbilityId)
141 {
142     return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
143 }
144 
EnforceInterceToken(MessageParcel & data)145 bool LocalAbilityManagerStub::EnforceInterceToken(MessageParcel& data)
146 {
147     std::u16string interfaceToken = data.ReadInterfaceToken();
148     return interfaceToken == LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN;
149 }
150 }
151