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     data.WriteString(WITHOUT_PERMISSION_TAG);
55     data.WriteInt32(EdmConstants::SET_POLICIES_TYPE);
56     std::vector<std::string> key{appId};
57     std::vector<std::string> value{policies};
58     data.WriteStringVector(key);
59     data.WriteStringVector(value);
60     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
61 }
62 
GetPolicies(std::string & policies)63 int32_t BrowserProxy::GetPolicies(std::string &policies)
64 {
65     EDMLOGD("BrowserProxy::GetPolicies inner api");
66     if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
67         EDMLOGD("BrowserProxy::GetPolicies edm service not start.");
68         policies = "";
69         return ERR_OK;
70     }
71     return GetPolicies(nullptr, "", policies);
72 }
73 
GetPolicies(AppExecFwk::ElementName & admin,const std::string & appId,std::string & policies)74 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName &admin, const std::string &appId, std::string &policies)
75 {
76     EDMLOGD("BrowserProxy::GetPolicies");
77     if (appId.empty()) {
78         EDMLOGE("BrowserProxy::GetPolicies appId is empty.");
79         return EdmReturnErrCode::PARAM_ERROR;
80     }
81     return GetPolicies(&admin, appId, policies);
82 }
83 
GetPolicies(AppExecFwk::ElementName * admin,const std::string & appId,std::string & policies)84 int32_t BrowserProxy::GetPolicies(AppExecFwk::ElementName *admin, const std::string &appId, std::string &policies)
85 {
86     MessageParcel data;
87     MessageParcel reply;
88     data.WriteInterfaceToken(DESCRIPTOR);
89     data.WriteInt32(WITHOUT_USERID);
90     data.WriteString(WITHOUT_PERMISSION_TAG);
91     if (admin == nullptr) {
92         data.WriteInt32(WITHOUT_ADMIN);
93     } else {
94         data.WriteInt32(HAS_ADMIN);
95         data.WriteParcelable(admin);
96     }
97     data.WriteString(appId);
98     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::SET_BROWSER_POLICIES, data, reply);
99     int32_t ret = ERR_INVALID_VALUE;
100     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
101     if (!blRes) {
102         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
103         return ret;
104     }
105     reply.ReadString(policies);
106     return ERR_OK;
107 }
108 
SetPolicy(const AppExecFwk::ElementName & admin,const std::string & appId,const std::string & policyName,const std::string & policyValue)109 int32_t BrowserProxy::SetPolicy(const AppExecFwk::ElementName &admin, const std::string &appId,
110     const std::string &policyName, const std::string &policyValue)
111 {
112     EDMLOGI("BrowserProxy::SetPolicy");
113     if (appId.empty()) {
114         EDMLOGE("BrowserProxy::SetPolicy appId is empty");
115         return EdmReturnErrCode::PARAM_ERROR;
116     }
117     MessageParcel data;
118     std::uint32_t funcCode =
119         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_BROWSER_POLICIES);
120     data.WriteInterfaceToken(DESCRIPTOR);
121     data.WriteInt32(WITHOUT_USERID);
122     data.WriteParcelable(&admin);
123     data.WriteString(WITHOUT_PERMISSION_TAG);
124     data.WriteInt32(EdmConstants::SET_POLICY_TYPE);
125     std::vector<std::string> params{appId, policyName, policyValue};
126     data.WriteStringVector(params);
127     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
128 }
129 } // namespace EDM
130 } // namespace OHOS
131