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 #ifdef FEATURE_PASSIVE_SUPPORT
17 #include "passive_ability_skeleton.h"
18
19 #include "ipc_object_stub.h"
20 #include "ipc_skeleton.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23
24 #include "common_utils.h"
25 #include "location.h"
26 #include "location_log.h"
27 #include "work_record.h"
28 #include "locationhub_ipc_interface_code.h"
29 #include "permission_manager.h"
30
31 namespace OHOS {
32 namespace Location {
InitPassiveMsgHandleMap()33 void PassiveAbilityStub::InitPassiveMsgHandleMap()
34 {
35 if (PassiveMsgHandleMap_.size() != 0) {
36 return;
37 }
38 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::SEND_LOCATION_REQUEST)] =
39 [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
40 return SendLocationRequestInner(data, reply, identity);
41 };
42 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::SET_ENABLE)] =
43 [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
44 return SetEnableInner(data, reply, identity);
45 };
46 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::ENABLE_LOCATION_MOCK)] =
47 [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
48 return EnableMockInner(data, reply, identity);
49 };
50 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::DISABLE_LOCATION_MOCK)] =
51 [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
52 return DisableMockInner(data, reply, identity);
53 };
54 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS)] =
55 [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
56 return SetMockedLocationsInner(data, reply, identity);
57 };
58 }
59
PassiveAbilityStub()60 PassiveAbilityStub::PassiveAbilityStub()
61 {
62 InitPassiveMsgHandleMap();
63 }
64
SendLocationRequestInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)65 int PassiveAbilityStub::SendLocationRequestInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
66 {
67 if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
68 return ERRCODE_PERMISSION_DENIED;
69 }
70 std::unique_ptr<WorkRecord> workrecord = WorkRecord::Unmarshalling(data);
71 reply.WriteInt32(SendLocationRequest(*workrecord));
72 return ERRCODE_SUCCESS;
73 }
74
SetEnableInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)75 int PassiveAbilityStub::SetEnableInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
76 {
77 if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
78 return ERRCODE_PERMISSION_DENIED;
79 }
80 reply.WriteInt32(SetEnable(data.ReadBool()));
81 return ERRCODE_SUCCESS;
82 }
83
EnableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)84 int PassiveAbilityStub::EnableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
85 {
86 if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
87 return ERRCODE_PERMISSION_DENIED;
88 }
89 reply.WriteInt32(EnableMock());
90 return ERRCODE_SUCCESS;
91 }
92
DisableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)93 int PassiveAbilityStub::DisableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
94 {
95 if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
96 return ERRCODE_PERMISSION_DENIED;
97 }
98 reply.WriteInt32(DisableMock());
99 return ERRCODE_SUCCESS;
100 }
101
SetMockedLocationsInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)102 int PassiveAbilityStub::SetMockedLocationsInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
103 {
104 if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
105 return ERRCODE_PERMISSION_DENIED;
106 }
107 SendMessage(static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS), data, reply);
108 isMessageRequest_ = true;
109 return ERRCODE_SUCCESS;
110 }
111
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)112 int PassiveAbilityStub::OnRemoteRequest(uint32_t code,
113 MessageParcel &data, MessageParcel &reply, MessageOption &option)
114 {
115 pid_t callingPid = IPCSkeleton::GetCallingPid();
116 pid_t callingUid = IPCSkeleton::GetCallingUid();
117 AppIdentity identity;
118 identity.SetPid(callingPid);
119 identity.SetUid(callingUid);
120 LBSLOGD(PASSIVE, "OnRemoteRequest cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
121 code, option.GetFlags(), callingPid, callingUid);
122
123 if (data.ReadInterfaceToken() != GetDescriptor()) {
124 LBSLOGE(PASSIVE, "invalid token.");
125 return ERRCODE_SERVICE_UNAVAILABLE;
126 }
127 CancelIdleState();
128 int ret = ERRCODE_SUCCESS;
129 isMessageRequest_ = false;
130 auto handleFunc = PassiveMsgHandleMap_.find(code);
131 if (handleFunc != PassiveMsgHandleMap_.end() && handleFunc->second != nullptr) {
132 auto memberFunc = handleFunc->second;
133 ret = memberFunc(data, reply, identity);
134 } else {
135 LBSLOGE(PASSIVE, "OnReceived cmd = %{public}u, unsupport service.", code);
136 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
137 }
138 if (!isMessageRequest_) {
139 UnloadPassiveSystemAbility();
140 }
141 return ret;
142 }
143 } // namespace Location
144 } // namespace OHOS
145 #endif // FEATURE_PASSIVE_SUPPORT
146