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 "local_ability_manager_stub.h"
17
18 #include <cstdint>
19 #include <utility>
20
21 #include "errors.h"
22 #include "hilog/log_cpp.h"
23 #include "if_local_ability_manager.h"
24 #include "ipc_types.h"
25 #include "message_option.h"
26 #include "message_parcel.h"
27 #include "safwk_log.h"
28 #include "system_ability_definition.h"
29
30 using namespace OHOS::HiviewDFX;
31
32 namespace OHOS {
33 namespace {
34 const std::string TAG = "LocalAbilityManagerStub";
35 }
36
LocalAbilityManagerStub()37 LocalAbilityManagerStub::LocalAbilityManagerStub()
38 {
39 memberFuncMap_[START_ABILITY_TRANSACTION] =
40 &LocalAbilityManagerStub::StartAbilityInner;
41 }
42
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)43 int32_t LocalAbilityManagerStub::OnRemoteRequest(uint32_t code,
44 MessageParcel& data, MessageParcel& reply, MessageOption& option)
45 {
46 HILOGI(TAG, "code:%{public}u, flags:%{public}d", code, option.GetFlags());
47 if (!EnforceInterceToken(data)) {
48 HILOGW(TAG, "check interface token failed!");
49 return ERR_PERMISSION_DENIED;
50 }
51 auto iter = memberFuncMap_.find(code);
52 if (iter != memberFuncMap_.end()) {
53 auto memberFunc = iter->second;
54 if (memberFunc != nullptr) {
55 return (this->*memberFunc)(data, reply);
56 }
57 }
58 HILOGW(TAG, "unknown request code!");
59 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61
StartAbilityInner(MessageParcel & data,MessageParcel & reply)62 int32_t LocalAbilityManagerStub::StartAbilityInner(MessageParcel& data, MessageParcel& reply)
63 {
64 int32_t saId = data.ReadInt32();
65 if (!CheckInputSysAbilityId(saId)) {
66 HILOGW(TAG, "read saId failed!");
67 return ERR_NULL_OBJECT;
68 }
69
70 bool result = StartAbility(saId);
71 HILOGI(TAG, "%{public}s to start ability", result ? "success" : "failed");
72 return ERR_NONE;
73 }
74
CheckInputSysAbilityId(int32_t systemAbilityId)75 bool LocalAbilityManagerStub::CheckInputSysAbilityId(int32_t systemAbilityId)
76 {
77 return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID);
78 }
79
EnforceInterceToken(MessageParcel & data)80 bool LocalAbilityManagerStub::EnforceInterceToken(MessageParcel& data)
81 {
82 std::u16string interfaceToken = data.ReadInterfaceToken();
83 return interfaceToken == LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN;
84 }
85 }
86