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 "net_access_policy_dialog_impl.h"
17
18 #include <atomic>
19 #include <fstream>
20 #include <memory>
21 #include <set>
22 #include <string_ex.h>
23 #include <string>
24
25 #include <ability_manager_client.h>
26 #include <message_parcel.h>
27 #include "net_mgr_log_wrapper.h"
28
29 using namespace OHOS::AAFwk;
30
31 namespace OHOS {
32 namespace NetManagerStandard {
33 namespace {
34 static constexpr int32_t INVALID_USERID = -1;
35 constexpr int32_t SIGNAL_NUM = 3;
36 sptr<IRemoteObject> g_remoteObject = nullptr;
37 uint32_t g_uid = 0;
38 std::atomic_bool g_isDialogShow = false;
39 } // namespace
40
NetAccessPolicyDialogImpl()41 NetAccessPolicyDialogImpl::NetAccessPolicyDialogImpl() : dialogConnectionCallback_(new DialogAbilityConnection()) {}
42
~NetAccessPolicyDialogImpl()43 NetAccessPolicyDialogImpl::~NetAccessPolicyDialogImpl()
44 {
45 dialogConnectionCallback_ = nullptr;
46 }
47
ConnectSystemUi(uint32_t uid)48 bool NetAccessPolicyDialogImpl::ConnectSystemUi(uint32_t uid)
49 {
50 NETMGR_LOG_I("OnAbilityConnectDone");
51
52 g_uid = uid;
53 if (g_isDialogShow) {
54 AppExecFwk::ElementName element;
55 dialogConnectionCallback_->OnAbilityConnectDone(element, g_remoteObject, INVALID_USERID);
56 NETMGR_LOG_I("network access policy dialog has been show");
57 return true;
58 }
59
60 auto abilityManager = AbilityManagerClient::GetInstance();
61 if (abilityManager == nullptr) {
62 NETMGR_LOG_E("Get abilityManager err");
63 return false;
64 }
65
66 Want want;
67 want.SetElementName("com.ohos.sceneboard", "com.ohos.sceneboard.systemdialog");
68 ErrCode result = abilityManager->ConnectAbility(want, dialogConnectionCallback_, INVALID_USERID);
69 if (result != ERR_OK) {
70 NETMGR_LOG_E("ConnectAbility err");
71 return false;
72 }
73
74 return true;
75 }
76
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)77 void NetAccessPolicyDialogImpl::DialogAbilityConnection::OnAbilityConnectDone(
78 const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
79 {
80 NETMGR_LOG_I("OnAbilityConnectDone");
81 std::lock_guard lock(mutex_);
82 MessageParcel data;
83 MessageParcel reply;
84 MessageOption option;
85
86 if (g_remoteObject == nullptr) {
87 g_remoteObject = remoteObject;
88 }
89
90 std::string parameters =
91 "{\"ability.want.params.uiExtensionType\":\"sysDialog/common\",\"sysDialogZOrder\":1, \"appUid\":";
92 std::string tmpParameters = parameters + std::to_string(g_uid) + "}";
93 data.WriteInt32(SIGNAL_NUM);
94 data.WriteString16(u"bundleName");
95 data.WriteString16(u"com.example.myapplication");
96 data.WriteString16(u"abilityName");
97 data.WriteString16(u"UIExtensionProvider");
98 data.WriteString16(u"parameters");
99 data.WriteString16(Str8ToStr16(tmpParameters));
100
101 int32_t ret = remoteObject->SendRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
102 if (ret != ERR_OK) {
103 NETMGR_LOG_E("Show power dialog is failed:%{public}d", ret);
104 return;
105 }
106 g_isDialogShow = true;
107 NETMGR_LOG_I("Show network access policy dialog is success");
108 }
109
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)110 void NetAccessPolicyDialogImpl::DialogAbilityConnection::OnAbilityDisconnectDone(
111 const AppExecFwk::ElementName& element, int resultCode)
112 {
113 NETMGR_LOG_I("OnAbilityDisconnectDone");
114 std::lock_guard lock(mutex_);
115 g_remoteObject = nullptr;
116 g_isDialogShow = false;
117 }
118
GetNetAccessPolicyDialog()119 INetAccessPolicyDialog *GetNetAccessPolicyDialog()
120 {
121 static NetAccessPolicyDialogImpl impl;
122 return &impl;
123 }
124 } // namespace NetManagerStandard
125 } // namespace OHOS
126