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 "plugin_manager.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 const bool REGISTER_RESULT = PluginManager::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, "global_proxy", "ohos.permission.ENTERPRISE_MANAGE_NETWORK",
34         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 ret = NetManagerStandard::NetConnClient::GetInstance().GetGlobalHttpProxy(httpProxy);
51     EDMLOGI("GlobalProxyPlugin::GetGlobalHttpProxy result = %{public}d", ret);
52     if (ret == ERR_OK) {
53         reply.WriteInt32(ERR_OK);
54         if (!httpProxy.Marshalling(reply)) {
55             EDMLOGE("GlobalProxyPlugin::httpProxy Marshalling error");
56             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
57         }
58         return ERR_OK;
59     } else {
60         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
61         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
62     }
63 }
64 } // namespace EDM
65 } // namespace OHOS
66