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 "common.h"
17 #include "want.h"
18 #include "parameter.h"
19 #include "connection.h"
20 #include "ipc_skeleton.h"
21 #include "scene_board_judgement.h"
22 #include "extension_manager_client.h"
23
24 using namespace std;
25 using namespace OHOS;
26 using namespace OHOS::HDC::AUTH;
27 using namespace OHOS::AAFwk;
28
ConnectExtensionAbility(void)29 static sptr<HdcdConnection> ConnectExtensionAbility(void)
30 {
31 Want want;
32 string bundle;
33 string ability;
34
35 sptr<HdcdConnection> con = new(std::nothrow) HdcdConnection();
36 if (!con) {
37 AUTH_LOGE("alloc mem failed");
38 return nullptr;
39 }
40
41 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
42 bundle = "com.ohos.sceneboard";
43 ability = "com.ohos.sceneboard.systemdialog";
44 } else {
45 bundle = "com.ohos.systemui";
46 ability = "com.ohos.systemui.dialog";
47 }
48 want.SetElementName(bundle, ability);
49
50 std::string identity = IPCSkeleton::ResetCallingIdentity();
51 ErrCode ret = ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, con, nullptr, -1);
52 IPCSkeleton::SetCallingIdentity(identity);
53
54 AUTH_LOGI("connect bundle(%s) ability(%s) ret(%d)", bundle.c_str(), ability.c_str(), ret);
55
56 return (ret == ERR_OK) ? con : nullptr;
57 }
58
WaitDialogResult(void)59 static bool WaitDialogResult(void)
60 {
61 if (WaitParameter("persist.hdc.daemon.auth_result", "auth_result:*", WAIT_USER_PERMIT_TIMEOUT) != 0) {
62 AUTH_LOGE("wait user permit failed");
63 return false;
64 }
65 AUTH_LOGE("wait user permit over");
66 return true;
67 }
68
GetUserPermit(void)69 static int GetUserPermit(void)
70 {
71 auto con = ConnectExtensionAbility();
72 if (!con) {
73 AUTH_LOGE("connect ability failed");
74 return USER_PERMIT_ERR_CON_ABILITY_FAIL;
75 }
76 if (!con->GetShowDialogResult()) {
77 AUTH_LOGE("show dialog failed");
78 return USER_PERMIT_ERR_SHOW_DIALOG_FAIL;
79 }
80 if (!WaitDialogResult()) {
81 AUTH_LOGE("wait ability result failed");
82 return USER_PERMIT_ERR_WAIT_DIALOG_FAIL;
83 }
84
85 return USER_PERMIT_SUCCESS;
86 }
87
main(int argc,char ** argv)88 int main(int argc, char** argv)
89 {
90 // ohos not support, direct success
91 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
92 if (SetParameter("persist.hdc.daemon.auth_result", "auth_result:2") != 0) {
93 AUTH_LOGW("set auth_result failed");
94 return USER_PERMIT_ERR_SET_AUTH_RESULT_FAIL;
95 }
96
97 AUTH_LOGF("always forever permit for ohos");
98 return USER_PERMIT_SUCCESS;
99 }
100
101 return GetUserPermit();
102 }
103