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 "device_settings_proxy.h"
17
18 #include "edm_log.h"
19 #include "func_code.h"
20
21 namespace OHOS {
22 namespace EDM {
23 std::shared_ptr<DeviceSettingsProxy> DeviceSettingsProxy::instance_ = nullptr;
24 std::mutex DeviceSettingsProxy::mutexLock_;
25 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
26
GetDeviceSettingsProxy()27 std::shared_ptr<DeviceSettingsProxy> DeviceSettingsProxy::GetDeviceSettingsProxy()
28 {
29 if (instance_ == nullptr) {
30 std::lock_guard<std::mutex> lock(mutexLock_);
31 if (instance_ == nullptr) {
32 std::shared_ptr<DeviceSettingsProxy> temp = std::make_shared<DeviceSettingsProxy>();
33 instance_ = temp;
34 }
35 }
36 return instance_;
37 }
38
GetScreenOffTime(const AppExecFwk::ElementName & admin,int32_t & value)39 int32_t DeviceSettingsProxy::GetScreenOffTime(const AppExecFwk::ElementName &admin, int32_t &value)
40 {
41 EDMLOGD("DeviceSettingsProxy::GetScreenOffTime");
42 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
43 if (proxy == nullptr) {
44 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
45 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
46 }
47 MessageParcel data;
48 MessageParcel reply;
49 data.WriteInterfaceToken(DESCRIPTOR);
50 data.WriteInt32(WITHOUT_USERID);
51 data.WriteInt32(HAS_ADMIN);
52 data.WriteParcelable(&admin);
53 proxy->GetPolicy(EdmInterfaceCode::SCREEN_OFF_TIME, data, reply);
54 int32_t ret = ERR_INVALID_VALUE;
55 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
56 if (!blRes) {
57 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
58 return ret;
59 }
60 reply.ReadInt32(value);
61 return ERR_OK;
62 }
63
InstallUserCertificate(const AppExecFwk::ElementName & admin,const std::vector<uint8_t> & certArray,std::string & alias,std::string & result,std::string & innerCodeMsg)64 int32_t DeviceSettingsProxy::InstallUserCertificate(const AppExecFwk::ElementName &admin,
65 const std::vector<uint8_t> &certArray, std::string &alias, std::string &result, std::string &innerCodeMsg)
66 {
67 EDMLOGD("DeviceSettingsProxy::InstallUserCertificate");
68 MessageParcel data;
69 MessageParcel reply;
70 std::uint32_t funcCode =
71 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::INSTALL_CERTIFICATE);
72 data.WriteInterfaceToken(DESCRIPTOR);
73 data.WriteInt32(WITHOUT_USERID);
74 data.WriteParcelable(&admin);
75 data.WriteUInt8Vector(certArray);
76 data.WriteString(alias);
77 ErrCode ret = EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data, reply);
78 EDMLOGI("DeviceSettingsProxy::InstallUserCertificate : %{public}d.", ret);
79 if (ret == ERR_OK) {
80 result = reply.ReadString();
81 } else if (ret == EdmReturnErrCode::MANAGED_CERTIFICATE_FAILED) {
82 int32_t certRetCode = ERR_INVALID_VALUE;
83 reply.ReadInt32(certRetCode);
84 innerCodeMsg = std::to_string(certRetCode);
85 }
86 return ret;
87 }
88
UninstallUserCertificate(const AppExecFwk::ElementName & admin,const std::string & alias,std::string & innerCodeMsg)89 int32_t DeviceSettingsProxy::UninstallUserCertificate(const AppExecFwk::ElementName &admin, const std::string &alias,
90 std::string &innerCodeMsg)
91 {
92 EDMLOGD("DeviceSettingsProxy::UninstallUserCertificate");
93 MessageParcel data;
94 MessageParcel reply;
95 std::uint32_t funcCode =
96 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::INSTALL_CERTIFICATE);
97 data.WriteInterfaceToken(DESCRIPTOR);
98 data.WriteInt32(WITHOUT_USERID);
99 data.WriteParcelable(&admin);
100 data.WriteString(alias);
101 ErrCode ret = EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data, reply);
102 if (ret == EdmReturnErrCode::MANAGED_CERTIFICATE_FAILED) {
103 int32_t certRetCode = ERR_INVALID_VALUE;
104 reply.ReadInt32(certRetCode);
105 innerCodeMsg = std::to_string(certRetCode);
106 }
107 return ret;
108 }
109 } // namespace EDM
110 } // namespace OHOS
111