• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
55     int32_t systemAbilityId = -1;
56     bool ret = data.ReadInt32(systemAbilityId);
57     if (!ret) {
58         HILOGW("OnLoadSystemAbilitySuccessInner read SA:%{public}d fail!", systemAbilityId);
59         return ERR_INVALID_VALUE;
60     }
61     HILOGD("OnLoadSystemAbilitySuccessInner, SA:%{public}d", systemAbilityId);
62     if (!CheckInputSystemAbilityId(systemAbilityId)) {
63         HILOGW("OnLoadSystemAbilitySuccessInner invalid SA:%{public}d !", systemAbilityId);
64         return ERR_INVALID_VALUE;
65     }
66     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
67     if (remoteObject == nullptr) {
68         HILOGW("OnLoadSystemAbilitySuccessInner read remoteObject fail, SA:%{public}d!", systemAbilityId);
69     }
70     int64_t begin = OHOS::GetTickCount();
71     OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
72     HILOGW("onloadsasuc SA:%{public}d spend %{public}" PRId64 " ms", systemAbilityId, GetTickCount() - begin);
73     return ERR_NONE;
74 }
75 
OnLoadSystemAbilityFailInner(MessageParcel & data,MessageParcel & reply)76 int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilityFailInner(MessageParcel& data, MessageParcel& reply)
77 {
78     int32_t systemAbilityId = -1;
79     bool ret = data.ReadInt32(systemAbilityId);
80     if (!ret) {
81         HILOGW("OnLoadSystemAbilityFailInner read SA:%{public}d fail!", systemAbilityId);
82         return ERR_INVALID_VALUE;
83     }
84     HILOGI("OnLoadSystemAbilityFailInner, SA:%{public}d", systemAbilityId);
85     if (!CheckInputSystemAbilityId(systemAbilityId)) {
86         HILOGW("OnLoadSystemAbilityFailInner invalid SA:%{public}d !", systemAbilityId);
87         return ERR_INVALID_VALUE;
88     }
89     int64_t begin = OHOS::GetTickCount();
90     OnLoadSystemAbilityFail(systemAbilityId);
91     HILOGW("onloadsafail SA:%{public}d spend %{public}" PRId64 " ms", systemAbilityId, GetTickCount() - begin);
92     return ERR_NONE;
93 }
94 
OnLoadSACompleteForRemoteInner(MessageParcel & data,MessageParcel & reply)95 int32_t SystemAbilityLoadCallbackStub::OnLoadSACompleteForRemoteInner(MessageParcel& data, MessageParcel& reply)
96 {
97     std::string deviceId = data.ReadString();
98     int32_t systemAbilityId = -1;
99     bool ret = data.ReadInt32(systemAbilityId);
100     if (!ret) {
101         HILOGW("OnLoadSACompleteForRemoteInner read SA:%{public}d fail!", systemAbilityId);
102         return ERR_INVALID_VALUE;
103     }
104     if (!CheckInputSystemAbilityId(systemAbilityId)) {
105         HILOGW("OnLoadSACompleteForRemoteInner invalid SA:%{public}d !", systemAbilityId);
106         return ERR_INVALID_VALUE;
107     }
108     ret = data.ReadBool();
109     HILOGI("OnLoadSACompleteForRemoteInner load SA:%{public}d %{public}s",
110         systemAbilityId, ret ? "succeed" : "failed");
111     sptr<IRemoteObject> remoteObject = ret ? data.ReadRemoteObject() : nullptr;
112     int64_t begin = OHOS::GetTickCount();
113     OnLoadSACompleteForRemote(deviceId, systemAbilityId, remoteObject);
114     HILOGW("onloadRemoteSa SA:%{public}d spend %{public}" PRId64 " ms", systemAbilityId, GetTickCount() - begin);
115     return ERR_NONE;
116 }
117 
CheckInputSystemAbilityId(int32_t systemAbilityId)118 bool SystemAbilityLoadCallbackStub::CheckInputSystemAbilityId(int32_t systemAbilityId)
119 {
120     return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
121 }
122 
EnforceInterceToken(MessageParcel & data)123 bool SystemAbilityLoadCallbackStub::EnforceInterceToken(MessageParcel& data)
124 {
125     std::u16string interfaceToken = data.ReadInterfaceToken();
126     return interfaceToken == GetDescriptor();
127 }
128 }
129