• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::mutex BrowserProxy::mutexLock_;
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     if (instance_ == nullptr) {
33         std::lock_guard<std::mutex> lock(mutexLock_);
34         if (instance_ == nullptr) {
35             std::shared_ptr<BrowserProxy> temp = std::make_shared<BrowserProxy>();
36             instance_ = temp;
37         }
38     }
39     return instance_;
40 }
41 
SetPolicies(const AppExecFwk::ElementName & admin,const std::string & appId,const std::string & policies)42 int32_t BrowserProxy::SetPolicies(const AppExecFwk::ElementName &admin, const std::string &appId,
43     const std::string &policies)
44 {
45     EDMLOGI("BrowserProxy::SetPolicies");
46     if (appId.empty()) {
47         EDMLOGE("BrowserProxy::SetPolicies appId is empty");
48         return EdmReturnErrCode::PARAM_ERROR;
49     }
50     MessageParcel data;
51     std::uint32_t funcCode =
52         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_BROWSER_POLICIES);
53     data.WriteInterfaceToken(DESCRIPTOR);
54     data.WriteInt32(WITHOUT_USERID);
55     data.WriteParcelable(&admin);
56     data.WriteString(WITHOUT_PERMISSION_TAG);
57     data.WriteInt32(EdmConstants::SET_POLICIES_TYPE);
58     std::vector<std::string> key{appId};
59     std::vector<std::string> value{policies};
60     data.WriteStringVector(key);
61     data.WriteStringVector(value);
62     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
63 }
64 
GetPolicies(std::string & policies)65 int32_t BrowserProxy::GetPolicies(std::string &policies)
66 {
67     EDMLOGD("BrowserProxy::GetPolicies inner api");
68     if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
69         EDMLOGD("BrowserProxy::GetPolicies edm service not start.");
70         policies = "";
71         return ERR_OK;
72     }
73     return GetPolicies(nullptr, "", policies);
74 }
75 
GetPolicies(AppExecFwk::ElementName & admin,const std::string & appId,std::string & policies)76 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName &admin, const std::string &appId, std::string &policies)
77 {
78     EDMLOGD("BrowserProxy::GetPolicies");
79     if (appId.empty()) {
80         EDMLOGE("BrowserProxy::GetPolicies appId is empty.");
81         return EdmReturnErrCode::PARAM_ERROR;
82     }
83     return GetPolicies(&admin, appId, policies);
84 }
85 
GetPolicies(AppExecFwk::ElementName * admin,const std::string & appId,std::string & policies)86 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName *admin, const std::string &appId, std::string &policies)
87 {
88     MessageParcel data;
89     MessageParcel reply;
90     data.WriteInterfaceToken(DESCRIPTOR);
91     data.WriteInt32(WITHOUT_USERID);
92     data.WriteString(WITHOUT_PERMISSION_TAG);
93     if (admin == nullptr) {
94         data.WriteInt32(WITHOUT_ADMIN);
95     } else {
96         data.WriteInt32(HAS_ADMIN);
97         data.WriteParcelable(admin);
98     }
99     data.WriteString(appId);
100     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::SET_BROWSER_POLICIES, data, reply);
101     int32_t ret = ERR_INVALID_VALUE;
102     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
103     if (!blRes) {
104         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
105         return ret;
106     }
107     reply.ReadString(policies);
108     return ERR_OK;
109 }
110 
SetPolicy(const AppExecFwk::ElementName & admin,const std::string & appId,const std::string & policyName,const std::string & policyValue)111 int32_t BrowserProxy::SetPolicy(const AppExecFwk::ElementName &admin, const std::string &appId,
112     const std::string &policyName, const std::string &policyValue)
113 {
114     EDMLOGI("BrowserProxy::SetPolicy");
115     if (appId.empty()) {
116         EDMLOGE("BrowserProxy::SetPolicy appId is empty");
117         return EdmReturnErrCode::PARAM_ERROR;
118     }
119     MessageParcel data;
120     std::uint32_t funcCode =
121         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_BROWSER_POLICIES);
122     data.WriteInterfaceToken(DESCRIPTOR);
123     data.WriteInt32(WITHOUT_USERID);
124     data.WriteParcelable(&admin);
125     data.WriteString(WITHOUT_PERMISSION_TAG);
126     data.WriteInt32(EdmConstants::SET_POLICY_TYPE);
127     std::vector<std::string> params{appId, policyName, policyValue};
128     data.WriteStringVector(params);
129     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
130 }
131 
SetManagedBrowserPolicy(MessageParcel & data)132 int32_t BrowserProxy::SetManagedBrowserPolicy(MessageParcel &data)
133 {
134     EDMLOGI("BrowserProxy::SetManagedBrowserPolicy");
135     std::uint32_t funcCode =
136         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGED_BROWSER_POLICY);
137     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
138 }
139 
GetManagedBrowserPolicy(MessageParcel & data,void ** rawData,int32_t & size)140 int32_t BrowserProxy::GetManagedBrowserPolicy(MessageParcel &data, void** rawData, int32_t &size)
141 {
142     MessageParcel reply;
143     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGED_BROWSER_POLICY, data, reply);
144     int32_t ret = ERR_INVALID_VALUE;
145     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
146     if (!blRes) {
147         EDMLOGW("EnterpriseDeviceMgrProxy:GetManagedBrowserPolicy fail. %{public}d", ret);
148         return ret;
149     }
150     return GetRawData(reply, rawData, size);
151 }
152 
GetSelfManagedBrowserPolicyVersion(int32_t & version)153 int32_t BrowserProxy::GetSelfManagedBrowserPolicyVersion(int32_t &version)
154 {
155     MessageParcel data;
156     MessageParcel reply;
157     data.WriteInterfaceToken(DESCRIPTOR);
158     data.WriteInt32(WITHOUT_USERID);
159     data.WriteString(WITHOUT_PERMISSION_TAG);
160     data.WriteInt32(WITHOUT_ADMIN);
161     data.WriteString(EdmConstants::Browser::GET_MANAGED_BROWSER_VERSION);
162     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGED_BROWSER_POLICY, data, reply);
163     int32_t ret = ERR_INVALID_VALUE;
164     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
165     if (!blRes) {
166         EDMLOGW("EnterpriseDeviceMgrProxy:GetSelfManagedBrowserPolicyVersion fail. %{public}d", ret);
167         return ret;
168     }
169     reply.ReadInt32(version);
170     return ERR_OK;
171 }
172 
GetSelfManagedBrowserPolicy(void ** rawData,int32_t & size)173 int32_t BrowserProxy::GetSelfManagedBrowserPolicy(void** rawData, int32_t &size)
174 {
175     MessageParcel data;
176     MessageParcel reply;
177     data.WriteInterfaceToken(DESCRIPTOR);
178     data.WriteInt32(WITHOUT_USERID);
179     data.WriteString(WITHOUT_PERMISSION_TAG);
180     data.WriteInt32(WITHOUT_ADMIN);
181     data.WriteString(EdmConstants::Browser::GET_SELF_MANAGED_BROWSER_FILE_DATA);
182     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGED_BROWSER_POLICY, data, reply);
183     int32_t ret = ERR_INVALID_VALUE;
184     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
185     if (!blRes) {
186         EDMLOGW("EnterpriseDeviceMgrProxy:GetSelfManagedBrowserPolicy fail. %{public}d", ret);
187         return ret;
188     }
189     return GetRawData(reply, rawData, size);
190 }
191 
GetRawData(MessageParcel & reply,void ** rawData,int32_t & size)192 int32_t BrowserProxy::GetRawData(MessageParcel &reply, void** rawData, int32_t &size)
193 {
194     reply.ReadInt32(size);
195     if (size < 0 || size >= MAX_POLICY_FILE_SIZE) {
196         EDMLOGE("BrowserProxy::GetRawData fileData.size error.size: %{public}d",
197             size);
198         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
199     }
200     void* tempData = const_cast<void*>(reply.ReadRawData(size));
201     if (tempData == nullptr) {
202         EDMLOGW("EnterpriseDeviceMgrProxy:GetRawData ReadRawData fail.");
203         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
204     }
205     *rawData = malloc(size);
206     if (*rawData == nullptr) {
207         EDMLOGW("EnterpriseDeviceMgrProxy:GetManagedBrowserPolicy malloc fail.");
208         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
209     }
210     errno_t cpRet = memcpy_s(*rawData, size, tempData, size);
211     if (cpRet != EOK) {
212         EDMLOGW("EnterpriseDeviceMgrProxy:GetManagedBrowserPolicy memcpy fail.ret:%{public}d", cpRet);
213         free(*rawData);
214         *rawData = nullptr;
215         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
216     }
217     return ERR_OK;
218 }
219 } // namespace EDM
220 } // namespace OHOS
221