1 /*
2 * Copyright (c) 2024 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 "location_log.h"
17 #include "cj_lambda.h"
18 #include "cached_locations_callback.h"
19 #include "location.h"
20
21 namespace OHOS {
22 namespace GeoLocationManager {
CachedLocationsCallback()23 CachedLocationsCallback::CachedLocationsCallback()
24 {
25 remoteDied_ = false;
26 }
27
CachedLocationsCallback(int64_t callbackId)28 CachedLocationsCallback::CachedLocationsCallback(int64_t callbackId)
29 {
30 remoteDied_ = false;
31 this->callbackId_ = callbackId;
32 auto cFunc = reinterpret_cast<void(*)(CJLocationArr locations)>(callbackId);
33 callback_ = [ lambda = CJLambda::Create(cFunc)](const std::vector<std::unique_ptr<Location::Location>>& locations)
34 -> void { lambda(LocationVectorToCJLocationArr(locations)); };
35 }
36
~CachedLocationsCallback()37 CachedLocationsCallback::~CachedLocationsCallback()
38 {
39 }
40
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int CachedLocationsCallback::OnRemoteRequest(
42 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
43 {
44 LBSLOGD(Location::CACHED_LOCATIONS_CALLBACK, "CachedLocationsCallback::OnRemoteRequest!");
45 if (data.ReadInterfaceToken() != GetDescriptor()) {
46 LBSLOGE(Location::CACHED_LOCATIONS_CALLBACK, "invalid token.");
47 return -1;
48 }
49 if (remoteDied_) {
50 LBSLOGD(Location::CACHED_LOCATIONS_CALLBACK, "Failed to `%{public}s`,Remote service is died!", __func__);
51 return -1;
52 }
53
54 switch (code) {
55 case Location::ICachedLocationsCallback::RECEIVE_CACHED_LOCATIONS_EVENT: {
56 int size = data.ReadInt32();
57 if (size > 0 && size < Location::MAXIMUM_CACHE_LOCATIONS) {
58 std::vector<std::unique_ptr<Location::Location>> locations(size);
59 for (int i = 0; i < size; i++) {
60 locations.push_back(Location::Location::UnmarshallingMakeUnique(data));
61 }
62 OnCacheLocationsReport(locations);
63 }
64 break;
65 }
66 default: {
67 IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68 break;
69 }
70 }
71 return 0;
72 }
73
IsRemoteDied()74 bool CachedLocationsCallback::IsRemoteDied()
75 {
76 return remoteDied_;
77 }
78
OnCacheLocationsReport(const std::vector<std::unique_ptr<Location::Location>> & locations)79 void CachedLocationsCallback::OnCacheLocationsReport(const std::vector<std::unique_ptr<Location::Location>>& locations)
80 {
81 LBSLOGD(Location::CACHED_LOCATIONS_CALLBACK, "CachedLocationsCallback::OnCacheLocationsReport");
82 std::unique_lock<std::mutex> guard(mutex_);
83 if (callback_ != nullptr) {
84 callback_(locations);
85 }
86 }
87 }
88 }