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_GEOCODE_SUPPORT
17 #include "geo_convert_skeleton.h"
18 #include "common_utils.h"
19 #include "ipc_skeleton.h"
20 #include "location_log.h"
21 #include "locationhub_ipc_interface_code.h"
22 #include "permission_manager.h"
23 
24 namespace OHOS {
25 namespace Location {
InitGeoConvertHandleMap()26 void GeoConvertServiceStub::InitGeoConvertHandleMap()
27 {
28     if (geoConvertMsgHandleMap_.size() != 0) {
29         return;
30     }
31     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::IS_AVAILABLE)] =
32         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
33         return IsGeoConvertAvailableInner(data, reply, identity);
34         };
35     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_COORDINATE)] =
36         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
37         return GetAddressByCoordinateInner(data, reply, identity);
38         };
39     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_LOCATION_NAME_BY_BOUNDARY)] =
40         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
41         return GetAddressByLocationNameInner(data, reply, identity);
42         };
43     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK)] =
44         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
45         return EnableReverseGeocodingMockInner(data, reply, identity);
46         };
47     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK)] =
48         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
49         return DisableReverseGeocodingMockInner(data, reply, identity);
50         };
51     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO)] =
52         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
53         return SetGeocodingMockInfoInner(data, reply, identity);
54         };
55 }
56 
GeoConvertServiceStub()57 GeoConvertServiceStub::GeoConvertServiceStub()
58 {
59     InitGeoConvertHandleMap();
60 }
61 
ParseGeocodingMockInfos(MessageParcel & data)62 std::vector<std::shared_ptr<GeocodingMockInfo>> GeoConvertServiceStub::ParseGeocodingMockInfos(MessageParcel &data)
63 {
64     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo;
65     int arraySize = data.ReadInt32();
66     arraySize = arraySize > INPUT_ARRAY_LEN_MAX ? INPUT_ARRAY_LEN_MAX :
67         arraySize;
68     if (arraySize <= 0) {
69         return std::vector<std::shared_ptr<GeocodingMockInfo>>();
70     }
71     for (int i = 0; i < arraySize; i++) {
72         std::shared_ptr<GeocodingMockInfo> info = std::make_shared<GeocodingMockInfo>();
73         info->ReadFromParcel(data);
74         mockInfo.push_back(info);
75     }
76     return mockInfo;
77 }
78 
IsGeoConvertAvailableInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)79 int GeoConvertServiceStub::IsGeoConvertAvailableInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
80 {
81     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
82         return ERRCODE_PERMISSION_DENIED;
83     }
84     IsGeoConvertAvailable(reply);
85     return ERRCODE_SUCCESS;
86 }
87 
GetAddressByCoordinateInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)88 int GeoConvertServiceStub::GetAddressByCoordinateInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
89 {
90     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
91         return ERRCODE_PERMISSION_DENIED;
92     }
93     GetAddressByCoordinate(data, reply);
94     return ERRCODE_SUCCESS;
95 }
96 
GetAddressByLocationNameInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)97 int GeoConvertServiceStub::GetAddressByLocationNameInner(
98     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
99 {
100     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
101         return ERRCODE_PERMISSION_DENIED;
102     }
103     GetAddressByLocationName(data, reply);
104     return ERRCODE_SUCCESS;
105 }
106 
EnableReverseGeocodingMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)107 int GeoConvertServiceStub::EnableReverseGeocodingMockInner(
108     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
109 {
110     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
111         return ERRCODE_PERMISSION_DENIED;
112     }
113     EnableReverseGeocodingMock() ? reply.WriteInt32(ERRCODE_SUCCESS) :
114         reply.WriteInt32(ERRCODE_REVERSE_GEOCODING_FAIL);
115     return ERRCODE_SUCCESS;
116 }
117 
DisableReverseGeocodingMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)118 int GeoConvertServiceStub::DisableReverseGeocodingMockInner(
119     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
120 {
121     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
122         return ERRCODE_PERMISSION_DENIED;
123     }
124     DisableReverseGeocodingMock() ? reply.WriteInt32(ERRCODE_SUCCESS) :
125         reply.WriteInt32(ERRCODE_REVERSE_GEOCODING_FAIL);
126     return ERRCODE_SUCCESS;
127 }
128 
SetGeocodingMockInfoInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)129 int GeoConvertServiceStub::SetGeocodingMockInfoInner(
130     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
131 {
132     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
133         return ERRCODE_PERMISSION_DENIED;
134     }
135     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo = ParseGeocodingMockInfos(data);
136     reply.WriteInt32(SetReverseGeocodingMockInfo(mockInfo));
137     return ERRCODE_SUCCESS;
138 }
139 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)140 int GeoConvertServiceStub::OnRemoteRequest(uint32_t code,
141     MessageParcel &data, MessageParcel &reply, MessageOption &option)
142 {
143     pid_t callingPid = IPCSkeleton::GetCallingPid();
144     pid_t callingUid = IPCSkeleton::GetCallingUid();
145     AppIdentity identity;
146     identity.SetPid(callingPid);
147     identity.SetUid(callingUid);
148     LBSLOGI(GEO_CONVERT,
149         "cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid = %{public}d, timestamp = %{public}s",
150         code, option.GetFlags(), callingPid, callingUid, std::to_string(CommonUtils::GetCurrentTimeStamp()).c_str());
151     if (data.ReadInterfaceToken() != GetDescriptor()) {
152         LBSLOGE(GEO_CONVERT, "invalid token.");
153         return ERRCODE_SERVICE_UNAVAILABLE;
154     }
155     CancelIdleState();
156     int ret = ERRCODE_SUCCESS;
157     auto handleFunc = geoConvertMsgHandleMap_.find(code);
158     if (handleFunc != geoConvertMsgHandleMap_.end() && handleFunc->second != nullptr) {
159         auto memberFunc = handleFunc->second;
160         ret = memberFunc(data, reply, identity);
161     } else {
162         LBSLOGE(GEO_CONVERT, "OnReceived cmd = %{public}u, unsupport service.", code);
163         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
164     }
165     UnloadGeoConvertSystemAbility();
166     return ret;
167 }
168 } // namespace Location
169 } // namespace OHOS
170 #endif
171