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
16 #include "system_ability_load_callback_stub.h"
17
18 #include "errors.h"
19 #include "ipc_object_stub.h"
20 #include "ipc_types.h"
21 #include "message_parcel.h"
22 #include "refbase.h"
23 #include "sam_log.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)27 int32_t SystemAbilityLoadCallbackStub::OnRemoteRequest(uint32_t code,
28 MessageParcel& data, MessageParcel& reply, MessageOption& option)
29 {
30 HILOGI("SystemAbilityLoadCallbackStub::OnRemoteRequest, code = %{public}u", code);
31 if (!EnforceInterceToken(data)) {
32 HILOGW("SystemAbilityLoadCallbackStub::OnRemoteRequest check interface token failed!");
33 return ERR_PERMISSION_DENIED;
34 }
35 switch (code) {
36 case ON_LOAD_SYSTEM_ABILITY_SUCCESS:
37 return OnLoadSystemAbilitySuccessInner(data, reply);
38 case ON_LOAD_SYSTEM_ABILITY_FAIL:
39 return OnLoadSystemAbilityFailInner(data, reply);
40 case ON_LOAD_SYSTEM_ABILITY_COMPLETE_FOR_REMOTE:
41 return OnLoadSACompleteForRemoteInner(data, reply);
42 default:
43 HILOGW("SystemAbilityLoadCallbackStub::OnRemoteRequest unknown request code!");
44 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
45 }
46 }
47
OnLoadSystemAbilitySuccessInner(MessageParcel & data,MessageParcel & reply)48 int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilitySuccessInner(MessageParcel& data, MessageParcel& reply)
49 {
50 int32_t systemAbilityId = data.ReadInt32();
51 if (!CheckInputSystemAbilityId(systemAbilityId)) {
52 HILOGW("OnLoadSystemAbilitySuccessInner invalid systemAbilityId:%{public}d !", systemAbilityId);
53 return ERR_INVALID_VALUE;
54 }
55 sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
56 OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
57 return ERR_NONE;
58 }
59
OnLoadSystemAbilityFailInner(MessageParcel & data,MessageParcel & reply)60 int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilityFailInner(MessageParcel& data, MessageParcel& reply)
61 {
62 int32_t systemAbilityId = data.ReadInt32();
63 if (!CheckInputSystemAbilityId(systemAbilityId)) {
64 HILOGW("OnLoadSystemAbilityFailInner invalid systemAbilityId:%{public}d !", systemAbilityId);
65 return ERR_INVALID_VALUE;
66 }
67 OnLoadSystemAbilityFail(systemAbilityId);
68 return ERR_NONE;
69 }
70
OnLoadSACompleteForRemoteInner(MessageParcel & data,MessageParcel & reply)71 int32_t SystemAbilityLoadCallbackStub::OnLoadSACompleteForRemoteInner(MessageParcel& data, MessageParcel& reply)
72 {
73 std::string deviceId = data.ReadString();
74 int32_t systemAbilityId = data.ReadInt32();
75 if (!CheckInputSystemAbilityId(systemAbilityId)) {
76 HILOGW("OnLoadSACompleteForRemoteInner invalid systemAbilityId:%{public}d !", systemAbilityId);
77 return ERR_INVALID_VALUE;
78 }
79 bool ret = data.ReadBool();
80 HILOGI("OnLoadSACompleteForRemoteInner load : %{public}s", ret ? "succeed" : "failed");
81 sptr<IRemoteObject> remoteObject = ret ? data.ReadRemoteObject() : nullptr;
82 OnLoadSACompleteForRemote(deviceId, systemAbilityId, remoteObject);
83 return ERR_NONE;
84 }
85
CheckInputSystemAbilityId(int32_t systemAbilityId)86 bool SystemAbilityLoadCallbackStub::CheckInputSystemAbilityId(int32_t systemAbilityId)
87 {
88 return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
89 }
90
EnforceInterceToken(MessageParcel & data)91 bool SystemAbilityLoadCallbackStub::EnforceInterceToken(MessageParcel& data)
92 {
93 std::u16string interfaceToken = data.ReadInterfaceToken();
94 return interfaceToken == GetDescriptor();
95 }
96 }
97