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 <cinttypes>
19 #include "errors.h"
20 #include "ipc_object_stub.h"
21 #include "ipc_types.h"
22 #include "message_parcel.h"
23 #include "refbase.h"
24 #include "datetime_ex.h"
25 #include "sam_log.h"
26
27 namespace OHOS {
28 namespace {
29 constexpr int32_t FIRST_SYS_ABILITY_ID = 0x00000000;
30 constexpr int32_t LAST_SYS_ABILITY_ID = 0x00ffffff;
31 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t SystemAbilityLoadCallbackStub::OnRemoteRequest(uint32_t code,
33 MessageParcel& data, MessageParcel& reply, MessageOption& option)
34 {
35 HILOGD("SystemAbilityLoadCallbackStub::OnRemoteRequest, code = %{public}u", code);
36 if (!EnforceInterceToken(data)) {
37 HILOGW("SystemAbilityLoadCallbackStub::OnRemoteRequest check interface token failed!");
38 return ERR_PERMISSION_DENIED;
39 }
40 switch (code) {
41 case ON_LOAD_SYSTEM_ABILITY_SUCCESS:
42 return OnLoadSystemAbilitySuccessInner(data, reply);
43 case ON_LOAD_SYSTEM_ABILITY_FAIL:
44 return OnLoadSystemAbilityFailInner(data, reply);
45 case ON_LOAD_SYSTEM_ABILITY_COMPLETE_FOR_REMOTE:
46 return OnLoadSACompleteForRemoteInner(data, reply);
47 default:
48 HILOGW("SystemAbilityLoadCallbackStub::OnRemoteRequest unknown request code!");
49 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50 }
51 }
52
OnLoadSystemAbilitySuccessInner(MessageParcel & data,MessageParcel & reply)53 int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilitySuccessInner(MessageParcel& data, MessageParcel& reply)
54 __attribute__((no_sanitize("cfi")))
55 {
56 int32_t systemAbilityId = -1;
57 bool ret = data.ReadInt32(systemAbilityId);
58 if (!ret) {
59 HILOGW("OnLoadSystemAbilitySuccessInner read SA:%{public}d fail!", systemAbilityId);
60 return ERR_INVALID_VALUE;
61 }
62 HILOGD("OnLoadSystemAbilitySuccessInner, SA:%{public}d", systemAbilityId);
63 if (!CheckInputSystemAbilityId(systemAbilityId)) {
64 HILOGW("OnLoadSystemAbilitySuccessInner invalid SA:%{public}d !", systemAbilityId);
65 return ERR_INVALID_VALUE;
66 }
67 sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
68 if (remoteObject == nullptr) {
69 HILOGW("OnLoadSystemAbilitySuccessInner read remoteObject fail, SA:%{public}d!", systemAbilityId);
70 }
71 int64_t begin = OHOS::GetTickCount();
72 OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
73 HILOGW("OnLoadSaSucInner SA:%{public}d spend %{public}" PRId64 "ms", systemAbilityId, GetTickCount() - begin);
74 return ERR_NONE;
75 }
76
OnLoadSystemAbilityFailInner(MessageParcel & data,MessageParcel & reply)77 int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilityFailInner(MessageParcel& data, MessageParcel& reply)
78 {
79 int32_t systemAbilityId = -1;
80 bool ret = data.ReadInt32(systemAbilityId);
81 if (!ret) {
82 HILOGW("OnLoadSystemAbilityFailInner read SA:%{public}d fail!", systemAbilityId);
83 return ERR_INVALID_VALUE;
84 }
85 HILOGI("OnLoadSystemAbilityFailInner, SA:%{public}d", systemAbilityId);
86 if (!CheckInputSystemAbilityId(systemAbilityId)) {
87 HILOGW("OnLoadSystemAbilityFailInner invalid SA:%{public}d !", systemAbilityId);
88 return ERR_INVALID_VALUE;
89 }
90 int64_t begin = OHOS::GetTickCount();
91 OnLoadSystemAbilityFail(systemAbilityId);
92 HILOGW("OnLoadSaFailInner SA:%{public}d spend %{public}" PRId64 "ms", systemAbilityId, GetTickCount() - begin);
93 return ERR_NONE;
94 }
95
OnLoadSACompleteForRemoteInner(MessageParcel & data,MessageParcel & reply)96 int32_t SystemAbilityLoadCallbackStub::OnLoadSACompleteForRemoteInner(MessageParcel& data, MessageParcel& reply)
97 {
98 std::string deviceId = data.ReadString();
99 int32_t systemAbilityId = -1;
100 bool ret = data.ReadInt32(systemAbilityId);
101 if (!ret) {
102 HILOGW("OnLoadSACompleteForRemoteInner read SA:%{public}d fail!", systemAbilityId);
103 return ERR_INVALID_VALUE;
104 }
105 if (!CheckInputSystemAbilityId(systemAbilityId)) {
106 HILOGW("OnLoadSACompleteForRemoteInner invalid SA:%{public}d !", systemAbilityId);
107 return ERR_INVALID_VALUE;
108 }
109 ret = data.ReadBool();
110 HILOGI("OnLoadSACompleteForRemoteInner load SA:%{public}d %{public}s",
111 systemAbilityId, ret ? "succeed" : "failed");
112 sptr<IRemoteObject> remoteObject = ret ? data.ReadRemoteObject() : nullptr;
113 int64_t begin = OHOS::GetTickCount();
114 OnLoadSACompleteForRemote(deviceId, systemAbilityId, remoteObject);
115 HILOGW("OnLoadRemoteSaInner SA:%{public}d spend %{public}" PRId64 "ms", systemAbilityId, GetTickCount() - begin);
116 return ERR_NONE;
117 }
118
CheckInputSystemAbilityId(int32_t systemAbilityId)119 bool SystemAbilityLoadCallbackStub::CheckInputSystemAbilityId(int32_t systemAbilityId)
120 {
121 return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
122 }
123
EnforceInterceToken(MessageParcel & data)124 bool SystemAbilityLoadCallbackStub::EnforceInterceToken(MessageParcel& data)
125 {
126 std::u16string interfaceToken = data.ReadInterfaceToken();
127 return interfaceToken == GetDescriptor();
128 }
129 }
130