1 /*
2  * Copyright (c) 2023 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_callback_adapter_impl.h"
17 
18 #include "location_proxy_adapter_impl.h"
19 #include "nweb_log.h"
20 
21 namespace OHOS::NWeb {
LocationCallbackImpl(std::shared_ptr<LocationCallbackAdapter> adapter)22 LocationCallbackImpl::LocationCallbackImpl(
23     std::shared_ptr<LocationCallbackAdapter> adapter)
24     : locationCallbackAdapter_(adapter) {}
25 
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)26 int LocationCallbackImpl::OnRemoteRequest(uint32_t code,
27     OHOS::MessageParcel& data,
28     OHOS::MessageParcel& reply,
29     OHOS::MessageOption& option)
30 {
31     if (data.ReadInterfaceToken() != GetDescriptor()) {
32         WVLOG_E("LocationCallbackImpl invalid token.");
33         return -1;
34     }
35     if (locationCallbackAdapter_ == nullptr) {
36         WVLOG_E("LocationCallbackImpl adapter is nullptr.");
37         return -1;
38     }
39     switch (code) {
40         case RECEIVE_LOCATION_INFO_EVENT: {
41             std::unique_ptr<OHOS::Location::Location> location =
42                 OHOS::Location::Location::Unmarshalling(data);
43             OnLocationReport(location);
44             break;
45         }
46         case RECEIVE_ERROR_INFO_EVENT: {
47             int32_t errorCode = data.ReadInt32();
48             OnErrorReport(errorCode);
49             break;
50         }
51         case RECEIVE_LOCATION_STATUS_EVENT: {
52             int32_t status = data.ReadInt32();
53             OnLocatingStatusChange(status);
54             break;
55         }
56         default: {
57             WVLOG_E("locationCallback receive error code:%{public}u", code);
58             break;
59         }
60     }
61     return 0;
62 }
63 
OnLocationReport(const std::unique_ptr<OHOS::Location::Location> & location)64 void LocationCallbackImpl::OnLocationReport(
65     const std::unique_ptr<OHOS::Location::Location>& location)
66 {
67     std::unique_ptr<OHOS::Location::Location> tempLocation =
68         std::make_unique<OHOS::Location::Location>(*location);
69     std::shared_ptr<LocationInfo> locationInfo =
70         std::make_shared<LocationInfoImpl>(tempLocation);
71     if (locationCallbackAdapter_ != nullptr) {
72         locationCallbackAdapter_->OnLocationReport(locationInfo);
73     }
74 }
75 
OnLocatingStatusChange(const int status)76 void LocationCallbackImpl::OnLocatingStatusChange(const int status)
77 {
78     if (locationCallbackAdapter_ != nullptr) {
79         locationCallbackAdapter_->OnLocatingStatusChange(status);
80     }
81 }
82 
OnErrorReport(const int errorCode)83 void LocationCallbackImpl::OnErrorReport(const int errorCode)
84 {
85     if (locationCallbackAdapter_ != nullptr) {
86         locationCallbackAdapter_->OnErrorReport(errorCode);
87     }
88 }
89 }
90