1 /*
2 * Copyright (c) 2021-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 "system_ability_status_change_stub.h"
17
18 #include "errors.h"
19 #include "ipc_object_stub.h"
20 #include "ipc_types.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "refbase.h"
24 #include "sam_log.h"
25 #include "system_ability_definition.h"
26
27 namespace OHOS {
SystemAbilityStatusChangeStub()28 SystemAbilityStatusChangeStub::SystemAbilityStatusChangeStub()
29 {
30 memberFuncMap_[ON_ADD_SYSTEM_ABILITY] =
31 &SystemAbilityStatusChangeStub::OnAddSystemAbilityInner;
32 memberFuncMap_[ON_REMOVE_SYSTEM_ABILITY] =
33 &SystemAbilityStatusChangeStub::OnRemoveSystemAbilityInner;
34 }
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t SystemAbilityStatusChangeStub::OnRemoteRequest(uint32_t code,
37 MessageParcel& data, MessageParcel& reply, MessageOption& option)
38 {
39 HILOGI("SystemAbilityStatusChangeStub::code:%{public}u, flags:%{public}d", code, option.GetFlags());
40 if (!EnforceInterceToken(data)) {
41 HILOGW("check interface token failed!");
42 return ERR_PERMISSION_DENIED;
43 }
44 auto iter = memberFuncMap_.find(code);
45 if (iter != memberFuncMap_.end()) {
46 auto memberFunc = iter->second;
47 if (memberFunc != nullptr) {
48 return (this->*memberFunc)(data, reply);
49 }
50 }
51 HILOGW("unknown request code!");
52 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
53 }
54
OnAddSystemAbilityInner(MessageParcel & data,MessageParcel & reply)55 int32_t SystemAbilityStatusChangeStub::OnAddSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
56 {
57 int32_t systemAbilityId = data.ReadInt32();
58 if (!CheckInputSysAbilityId(systemAbilityId)) {
59 HILOGW("read systemAbilityId failed!");
60 return ERR_NULL_OBJECT;
61 }
62 std::string deviceId = data.ReadString();
63 OnAddSystemAbility(systemAbilityId, deviceId);
64 return ERR_NONE;
65 }
66
OnRemoveSystemAbilityInner(MessageParcel & data,MessageParcel & reply)67 int32_t SystemAbilityStatusChangeStub::OnRemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
68 {
69 int32_t systemAbilityId = data.ReadInt32();
70 if (!CheckInputSysAbilityId(systemAbilityId)) {
71 HILOGW("read systemAbilityId failed!");
72 return ERR_NULL_OBJECT;
73 }
74 std::string deviceId = data.ReadString();
75 OnRemoveSystemAbility(systemAbilityId, deviceId);
76 return ERR_NONE;
77 }
78
CheckInputSysAbilityId(int32_t systemAbilityId)79 bool SystemAbilityStatusChangeStub::CheckInputSysAbilityId(int32_t systemAbilityId)
80 {
81 return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
82 }
83
EnforceInterceToken(MessageParcel & data)84 bool SystemAbilityStatusChangeStub::EnforceInterceToken(MessageParcel& data)
85 {
86 std::u16string interfaceToken = data.ReadInterfaceToken();
87 return interfaceToken == GetDescriptor();
88 }
89 }
90