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