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
22 namespace OHOS {
23 namespace EDM {
24 std::shared_ptr<BrowserProxy> BrowserProxy::instance_ = nullptr;
25 std::mutex BrowserProxy::mutexLock_;
26 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
27
GetBrowserProxy()28 std::shared_ptr<BrowserProxy> BrowserProxy::GetBrowserProxy()
29 {
30 if (instance_ == nullptr) {
31 std::lock_guard<std::mutex> lock(mutexLock_);
32 if (instance_ == nullptr) {
33 std::shared_ptr<BrowserProxy> temp = std::make_shared<BrowserProxy>();
34 instance_ = temp;
35 }
36 }
37 return instance_;
38 }
39
SetPolicies(const AppExecFwk::ElementName & admin,const std::string & appId,const std::string & policies)40 int32_t BrowserProxy::SetPolicies(const AppExecFwk::ElementName &admin, const std::string &appId,
41 const std::string &policies)
42 {
43 EDMLOGI("BrowserProxy::SetPolicies");
44 if (appId.empty()) {
45 EDMLOGE("BrowserProxy::SetPolicies appId is empty");
46 return EdmReturnErrCode::PARAM_ERROR;
47 }
48 MessageParcel data;
49 std::uint32_t funcCode =
50 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_BROWSER_POLICIES);
51 data.WriteInterfaceToken(DESCRIPTOR);
52 data.WriteInt32(WITHOUT_USERID);
53 data.WriteParcelable(&admin);
54 std::vector<std::string> key{appId};
55 std::vector<std::string> value{policies};
56 data.WriteStringVector(key);
57 data.WriteStringVector(value);
58 return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
59 }
60
GetPolicies(std::string & policies)61 int32_t BrowserProxy::GetPolicies(std::string &policies)
62 {
63 EDMLOGD("BrowserProxy::GetPolicies inner api");
64 if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
65 EDMLOGD("BrowserProxy::GetPolicies edm service not start.");
66 policies = "";
67 return ERR_OK;
68 }
69 return GetPolicies(nullptr, "", policies);
70 }
71
GetPolicies(AppExecFwk::ElementName & admin,const std::string & appId,std::string & policies)72 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName &admin, const std::string &appId, std::string &policies)
73 {
74 EDMLOGD("BrowserProxy::GetPolicies");
75 if (appId.empty()) {
76 EDMLOGE("BrowserProxy::GetPolicies appId is empty.");
77 return EdmReturnErrCode::PARAM_ERROR;
78 }
79 return GetPolicies(&admin, appId, policies);
80 }
81
GetPolicies(AppExecFwk::ElementName * admin,const std::string & appId,std::string & policies)82 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName *admin, const std::string &appId, std::string &policies)
83 {
84 MessageParcel data;
85 MessageParcel reply;
86 data.WriteInterfaceToken(DESCRIPTOR);
87 data.WriteInt32(WITHOUT_USERID);
88 if (admin == nullptr) {
89 data.WriteInt32(WITHOUT_ADMIN);
90 } else {
91 data.WriteInt32(HAS_ADMIN);
92 data.WriteParcelable(admin);
93 }
94 data.WriteString(appId);
95 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::SET_BROWSER_POLICIES, data, reply);
96 int32_t ret = ERR_INVALID_VALUE;
97 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
98 if (!blRes) {
99 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
100 return ret;
101 }
102 reply.ReadString(policies);
103 return ERR_OK;
104 }
105 } // namespace EDM
106 } // namespace OHOS
107