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 "browser_proxy.h"
17
18 #include "edm_constants.h"
19 #include "edm_log.h"
20 #include "func_code.h"
21 #include "securec.h"
22
23 namespace OHOS {
24 namespace EDM {
25 std::shared_ptr<BrowserProxy> BrowserProxy::instance_ = nullptr;
26 std::once_flag BrowserProxy::flag_;
27 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
28 constexpr int32_t MAX_POLICY_FILE_SIZE = 134217728; // 128 * 1024 * 1024
29
GetBrowserProxy()30 std::shared_ptr<BrowserProxy> BrowserProxy::GetBrowserProxy()
31 {
32 std::call_once(flag_, []() {
33 if (instance_ == nullptr) {
34 instance_ = std::make_shared<BrowserProxy>();
35 }
36 });
37 return instance_;
38 }
39
GetPolicies(std::string & policies)40 int32_t BrowserProxy::GetPolicies(std::string &policies)
41 {
42 EDMLOGD("BrowserProxy::GetPolicies inner api");
43 if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
44 EDMLOGD("BrowserProxy::GetPolicies edm service not start.");
45 policies = "";
46 return ERR_OK;
47 }
48 return GetPolicies(nullptr, "", policies);
49 }
50
GetPolicies(AppExecFwk::ElementName & admin,const std::string & appId,std::string & policies)51 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName &admin, const std::string &appId, std::string &policies)
52 {
53 EDMLOGD("BrowserProxy::GetPolicies");
54 if (appId.empty()) {
55 EDMLOGE("BrowserProxy::GetPolicies appId is empty.");
56 return EdmReturnErrCode::PARAM_ERROR;
57 }
58 return GetPolicies(&admin, appId, policies);
59 }
60
GetPolicies(AppExecFwk::ElementName * admin,const std::string & appId,std::string & policies)61 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName *admin, const std::string &appId, std::string &policies)
62 {
63 MessageParcel data;
64 MessageParcel reply;
65 data.WriteInterfaceToken(DESCRIPTOR);
66 data.WriteInt32(WITHOUT_USERID);
67 data.WriteString(WITHOUT_PERMISSION_TAG);
68 if (admin == nullptr) {
69 data.WriteInt32(WITHOUT_ADMIN);
70 } else {
71 data.WriteInt32(HAS_ADMIN);
72 data.WriteParcelable(admin);
73 }
74 data.WriteString(appId);
75 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::SET_BROWSER_POLICIES, data, reply);
76 int32_t ret = ERR_INVALID_VALUE;
77 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
78 if (!blRes) {
79 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
80 return ret;
81 }
82 reply.ReadString(policies);
83 return ERR_OK;
84 }
85
SetPolicy(const AppExecFwk::ElementName & admin,const std::string & appId,const std::string & policyName,const std::string & policyValue)86 int32_t BrowserProxy::SetPolicy(const AppExecFwk::ElementName &admin, const std::string &appId,
87 const std::string &policyName, const std::string &policyValue)
88 {
89 EDMLOGI("BrowserProxy::SetPolicy");
90 if (appId.empty()) {
91 EDMLOGE("BrowserProxy::SetPolicy appId is empty");
92 return EdmReturnErrCode::PARAM_ERROR;
93 }
94 MessageParcel data;
95 std::uint32_t funcCode =
96 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_BROWSER_POLICIES);
97 data.WriteInterfaceToken(DESCRIPTOR);
98 data.WriteInt32(WITHOUT_USERID);
99 data.WriteParcelable(&admin);
100 data.WriteString(WITHOUT_PERMISSION_TAG);
101 std::vector<std::string> params{appId, policyName, policyValue};
102 data.WriteStringVector(params);
103 return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
104 }
105
SetManagedBrowserPolicy(MessageParcel & data)106 int32_t BrowserProxy::SetManagedBrowserPolicy(MessageParcel &data)
107 {
108 EDMLOGI("BrowserProxy::SetManagedBrowserPolicy");
109 std::uint32_t funcCode =
110 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGED_BROWSER_POLICY);
111 return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
112 }
113
GetManagedBrowserPolicy(MessageParcel & data,void ** rawData,int32_t & size)114 int32_t BrowserProxy::GetManagedBrowserPolicy(MessageParcel &data, void** rawData, int32_t &size)
115 {
116 MessageParcel reply;
117 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGED_BROWSER_POLICY, data, reply);
118 int32_t ret = ERR_INVALID_VALUE;
119 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
120 if (!blRes) {
121 EDMLOGW("EnterpriseDeviceMgrProxy:GetManagedBrowserPolicy fail. %{public}d", ret);
122 return ret;
123 }
124 return GetRawData(reply, rawData, size);
125 }
126
GetSelfManagedBrowserPolicyVersion(int32_t & version)127 int32_t BrowserProxy::GetSelfManagedBrowserPolicyVersion(int32_t &version)
128 {
129 MessageParcel data;
130 MessageParcel reply;
131 data.WriteInterfaceToken(DESCRIPTOR);
132 data.WriteInt32(WITHOUT_USERID);
133 data.WriteString(WITHOUT_PERMISSION_TAG);
134 data.WriteInt32(WITHOUT_ADMIN);
135 data.WriteString(EdmConstants::Browser::GET_MANAGED_BROWSER_VERSION);
136 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGED_BROWSER_POLICY, data, reply);
137 int32_t ret = ERR_INVALID_VALUE;
138 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
139 if (!blRes) {
140 EDMLOGW("EnterpriseDeviceMgrProxy:GetSelfManagedBrowserPolicyVersion fail. %{public}d", ret);
141 return ret;
142 }
143 reply.ReadInt32(version);
144 return ERR_OK;
145 }
146
GetSelfManagedBrowserPolicy(void ** rawData,int32_t & size)147 int32_t BrowserProxy::GetSelfManagedBrowserPolicy(void** rawData, int32_t &size)
148 {
149 MessageParcel data;
150 MessageParcel reply;
151 data.WriteInterfaceToken(DESCRIPTOR);
152 data.WriteInt32(WITHOUT_USERID);
153 data.WriteString(WITHOUT_PERMISSION_TAG);
154 data.WriteInt32(WITHOUT_ADMIN);
155 data.WriteString(EdmConstants::Browser::GET_SELF_MANAGED_BROWSER_FILE_DATA);
156 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGED_BROWSER_POLICY, data, reply);
157 int32_t ret = ERR_INVALID_VALUE;
158 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
159 if (!blRes) {
160 EDMLOGW("EnterpriseDeviceMgrProxy:GetSelfManagedBrowserPolicy fail. %{public}d", ret);
161 return ret;
162 }
163 return GetRawData(reply, rawData, size);
164 }
165
GetRawData(MessageParcel & reply,void ** rawData,int32_t & size)166 int32_t BrowserProxy::GetRawData(MessageParcel &reply, void** rawData, int32_t &size)
167 {
168 reply.ReadInt32(size);
169 if (size < 0 || size >= MAX_POLICY_FILE_SIZE) {
170 EDMLOGE("BrowserProxy::GetRawData fileData.size error.size: %{public}d",
171 size);
172 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
173 }
174 void* tempData = const_cast<void*>(reply.ReadRawData(size));
175 if (tempData == nullptr) {
176 EDMLOGW("EnterpriseDeviceMgrProxy:GetRawData ReadRawData fail.");
177 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
178 }
179 *rawData = malloc(size);
180 if (*rawData == nullptr) {
181 EDMLOGW("EnterpriseDeviceMgrProxy:GetRawData malloc fail.");
182 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
183 }
184 errno_t cpRet = memcpy_s(*rawData, size, tempData, size);
185 if (cpRet != EOK) {
186 EDMLOGW("EnterpriseDeviceMgrProxy:GetRawData memcpy fail.ret:%{public}d", cpRet);
187 free(*rawData);
188 *rawData = nullptr;
189 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
190 }
191 return ERR_OK;
192 }
193 } // namespace EDM
194 } // namespace OHOS
195