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 "vpn_extension.h"
17 
18 #include "configuration_utils.h"
19 #include "connection_manager.h"
20 #include "js_vpn_extension.h"
21 #include "runtime.h"
22 #include "js_runtime.h"
23 #include "js_runtime_utils.h"
24 #include "vpn_extension_context.h"
25 #include "netmgr_ext_log_wrapper.h"
26 
27 using namespace OHOS::AbilityRuntime;
28 namespace OHOS {
29 namespace NetManagerStandard {
30 
31 using namespace OHOS::AppExecFwk;
32 
33 CreatorFunc VpnExtension::creator_ = nullptr;
SetCreator(const CreatorFunc & creator)34 void VpnExtension::SetCreator(const CreatorFunc& creator)
35 {
36     creator_ = creator;
37 }
38 
Create(const std::unique_ptr<OHOS::AbilityRuntime::Runtime> & runtime)39 VpnExtension* VpnExtension::Create(const std::unique_ptr<OHOS::AbilityRuntime::Runtime>& runtime)
40 {
41     if (!runtime) {
42         return new VpnExtension();
43     }
44 
45     if (creator_) {
46         return creator_(runtime);
47     }
48 
49     NETMGR_EXT_LOG_I("VpnExtension::Create runtime");
50     switch (runtime->GetLanguage()) {
51         case OHOS::AbilityRuntime::Runtime::Language::JS:
52             return JsVpnExtension::Create(runtime);
53 
54         default:
55             return new VpnExtension();
56     }
57 }
58 
Init(const std::shared_ptr<AbilityLocalRecord> & record,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)59 void VpnExtension::Init(const std::shared_ptr<AbilityLocalRecord> &record,
60     const std::shared_ptr<OHOSApplication> &application,
61     std::shared_ptr<AbilityHandler> &handler,
62     const sptr<IRemoteObject> &token)
63 {
64     ExtensionBase<VpnExtensionContext>::Init(record, application, handler, token);
65     NETMGR_EXT_LOG_I("VpnExtension begin init context");
66 }
67 
CreateAndInitContext(const std::shared_ptr<AbilityLocalRecord> & record,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)68 std::shared_ptr<VpnExtensionContext> VpnExtension::CreateAndInitContext(
69     const std::shared_ptr<AbilityLocalRecord> &record,
70     const std::shared_ptr<OHOSApplication> &application,
71     std::shared_ptr<AbilityHandler> &handler,
72     const sptr<IRemoteObject> &token)
73 {
74     std::shared_ptr<VpnExtensionContext> context =
75         ExtensionBase<VpnExtensionContext>::CreateAndInitContext(record, application, handler, token);
76     if (context == nullptr) {
77         NETMGR_EXT_LOG_E("VpnExtension::CreateAndInitContext context is nullptr");
78         return context;
79     }
80     return context;
81 }
82 
OnConfigurationUpdated(const AppExecFwk::Configuration & configuration)83 void VpnExtension::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration)
84 {
85     Extension::OnConfigurationUpdated(configuration);
86 
87     auto context = GetContext();
88     if (context == nullptr) {
89         NETMGR_EXT_LOG_E("Context is invalid.");
90         return;
91     }
92 
93     auto configUtils = std::make_shared<OHOS::AbilityRuntime::ConfigurationUtils>();
94     configUtils->UpdateGlobalConfig(configuration, context->GetResourceManager());
95 }
96 } // namespace NetManagerStandard
97 } // namespace OHOS
98