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 "global_proxy_plugin.h"
17
18 #include "edm_ipc_interface_code.h"
19 #include "edm_log.h"
20 #include "func_code_utils.h"
21 #include "http_proxy_serializer.h"
22 #include "net_conn_client.h"
23 #include "iplugin_manager.h"
24
25 namespace OHOS {
26 namespace EDM {
27 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(GlobalProxyPlugin::GetPlugin());
28
InitPlugin(std::shared_ptr<IPluginTemplate<GlobalProxyPlugin,NetManagerStandard::HttpProxy>> ptr)29 void GlobalProxyPlugin::InitPlugin(
30 std::shared_ptr<IPluginTemplate<GlobalProxyPlugin, NetManagerStandard::HttpProxy>> ptr)
31 {
32 EDMLOGI("GlobalProxyPlugin InitPlugin...");
33 ptr->InitAttribute(EdmInterfaceCode::GLOBAL_PROXY, PolicyName::POLICY_GLOBAL_PROXY,
34 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_NETWORK, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
35 ptr->SetSerializer(HttpProxySerializer::GetInstance());
36 ptr->SetOnHandlePolicyListener(&GlobalProxyPlugin::OnSetPolicy, FuncOperateType::SET);
37 }
38
OnSetPolicy(NetManagerStandard::HttpProxy & httpProxy)39 ErrCode GlobalProxyPlugin::OnSetPolicy(NetManagerStandard::HttpProxy &httpProxy)
40 {
41 int32_t ret = NetManagerStandard::NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
42 EDMLOGI("GlobalProxyPlugin::SetGlobalHttpProxy set result = %{public}d", ret);
43 return ret == ERR_OK ? ERR_OK : EdmReturnErrCode::SYSTEM_ABNORMALLY;
44 }
45
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)46 ErrCode GlobalProxyPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
47 int32_t userId)
48 {
49 NetManagerStandard::HttpProxy httpProxy;
50 int32_t accountId = data.ReadInt32();
51 httpProxy.SetUserId(accountId);
52 int32_t ret = NetManagerStandard::NetConnClient::GetInstance().GetGlobalHttpProxy(httpProxy);
53 EDMLOGI("GlobalProxyPlugin::GetGlobalHttpProxy result = %{public}d", ret);
54 if (ret == ERR_OK) {
55 reply.WriteInt32(ERR_OK);
56 if (!httpProxy.Marshalling(reply)) {
57 EDMLOGE("GlobalProxyPlugin::httpProxy Marshalling error");
58 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
59 }
60 return ERR_OK;
61 } else {
62 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
63 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
64 }
65 }
66 } // namespace EDM
67 } // namespace OHOS
68