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_manager_proxy.h"
17 #include "edm_log.h"
18 #include "func_code.h"
19 
20 namespace OHOS {
21 namespace EDM {
22 std::shared_ptr<LocationManagerProxy> LocationManagerProxy::instance_ = nullptr;
23 std::mutex LocationManagerProxy::mutexLock_;
24 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
25 
GetLocationManagerProxy()26 std::shared_ptr<LocationManagerProxy> LocationManagerProxy::GetLocationManagerProxy()
27 {
28     if (instance_ == nullptr) {
29         std::lock_guard<std::mutex> lock(mutexLock_);
30         if (instance_ == nullptr) {
31             std::shared_ptr<LocationManagerProxy> temp = std::make_shared<LocationManagerProxy>();
32             instance_ = temp;
33         }
34     }
35     return instance_;
36 }
37 
SetLocationPolicy(const AppExecFwk::ElementName & admin,LocationPolicy locationPolicy)38 int32_t LocationManagerProxy::SetLocationPolicy(const AppExecFwk::ElementName &admin, LocationPolicy locationPolicy)
39 {
40     EDMLOGD("LocationManagerProxy::SetLocationPolicy");
41     MessageParcel data;
42     MessageParcel reply;
43     data.WriteInterfaceToken(DESCRIPTOR);
44     data.WriteInt32(WITHOUT_USERID);
45     data.WriteParcelable(&admin);
46     data.WriteString(WITHOUT_PERMISSION_TAG);
47     data.WriteInt32(static_cast<int32_t>(locationPolicy));
48     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::LOCATION_POLICY);
49     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
50 }
51 
GetLocationPolicy(const AppExecFwk::ElementName * admin,LocationPolicy & locationPolicy)52 int32_t LocationManagerProxy::GetLocationPolicy(const AppExecFwk::ElementName *admin, LocationPolicy &locationPolicy)
53 {
54     EDMLOGD("LocationManagerProxy::GetLocationPolicy");
55     MessageParcel data;
56     MessageParcel reply;
57     data.WriteInterfaceToken(DESCRIPTOR);
58     data.WriteInt32(WITHOUT_USERID);
59     data.WriteString(WITHOUT_PERMISSION_TAG);
60     if (admin != nullptr) {
61         data.WriteInt32(HAS_ADMIN);
62         data.WriteParcelable(admin);
63     } else {
64         if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
65             locationPolicy = LocationPolicy::DEFAULT_LOCATION_SERVICE;
66             return ERR_OK;
67         }
68         data.WriteInt32(WITHOUT_ADMIN);
69     }
70     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::LOCATION_POLICY, data, reply);
71     int32_t ret = ERR_INVALID_VALUE;
72     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
73     if (!blRes) {
74         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
75         return ret;
76     }
77     locationPolicy = (LocationPolicy)reply.ReadInt32();
78     return ERR_OK;
79 }
80 } // namespace EDM
81 } // namespace OHOS