• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef IAM_WIDGET_CONTEXT_H
17 #define IAM_WIDGET_CONTEXT_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <list>
24 #include <vector>
25 
26 #include "auth_common.h"
27 #include "extension_manager_client.h"
28 #include "authentication_impl.h"
29 #include "context.h"
30 #include "context_factory.h"
31 #include "in_process_call_wrapper.h"
32 #include "iam_callback_interface.h"
33 #include "nocopyable.h"
34 #include "widget_schedule_node.h"
35 #include "ui_extension_ability_connection.h"
36 
37 namespace OHOS {
38 namespace UserIam {
39 namespace UserAuth {
40 class WidgetContext : public WidgetScheduleNodeCallback,
41                       public Context,
42                       public std::enable_shared_from_this<WidgetContext>,
43                       public NoCopyable {
44 public:
45     WidgetContext(uint64_t contextId, const ContextFactory::AuthWidgetContextPara &para,
46         std::shared_ptr<ContextCallback> callback);
47     ~WidgetContext() override;
48 
49     // Context API
50     bool Start() override;
51     bool Stop() override;
52     uint64_t GetContextId() const override;
53     ContextType GetContextType() const override;
54     std::shared_ptr<ScheduleNode> GetScheduleNode(uint64_t scheduleId) const override;
55     uint32_t GetTokenId() const override;
56     int32_t GetLatestError() const override;
57 
58     // WidgetScheduleNodeCallback API
59     bool LaunchWidget() override;
60     void ExecuteAuthList(const std::set<AuthType> &authTypeList) override;
61     void EndAuthAsCancel() override;
62     void EndAuthAsNaviPin() override;
63     void EndAuthAsWidgetParaInvalid() override;
64     void StopAuthList(const std::vector<AuthType> &authTypeList) override;
65     void SuccessAuth(AuthType authType) override;
66 
67     void AuthResult(int32_t resultCode, int32_t at, const Attributes &finalResult);
68 protected:
69     virtual bool OnStart();
70     virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr);
71     virtual bool OnStop();
72 
73 private:
74     void SetLatestError(int32_t error) override;
75     std::shared_ptr<Context> BuildTask(const std::vector<uint8_t> &challenge,
76         AuthType authType, AuthTrustLevel authTrustLevel);
77     bool BuildSchedule();
78     bool ConnectExtension();
79     int32_t ConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr);
80     bool DisconnectExtension();
81     void End(const ResultCode &resultCode);
82     std::shared_ptr<ContextCallback> GetAuthContextCallback(AuthType authType, AuthTrustLevel authTrustLevel,
83         sptr<IamCallbackInterface> &callback);
84     void StopAllRunTask();
85     std::string BuildStartCommand();
86     std::shared_ptr<UserIam::UserAuth::IamHitraceHelper> connectAbilityHitrace_ {nullptr};
87 
88 private:
89     struct TaskInfo {
90         AuthType authType {0};
91         std::shared_ptr<Context> task {nullptr};
92     };
93 
94     struct WidgetAuthResultInfo {
95         std::vector<uint8_t> token {};
96         AuthType authType { 0 };
97     };
98 
99     uint64_t contextId_ {0};
100     std::string description_ {""};
101     std::shared_ptr<ContextCallback> callerCallback_ {nullptr};
102     bool hasStarted_ {false};
103 
104     int32_t latestError_ {ResultCode::GENERAL_ERROR};
105     ContextFactory::AuthWidgetContextPara para_ {};
106     std::shared_ptr<WidgetScheduleNode> schedule_ {nullptr};
107     std::recursive_mutex mutex_;
108     std::list<TaskInfo> runTaskInfoList_;
109     sptr<UIExtensionAbilityConnection> connection_ {nullptr};
110     WidgetAuthResultInfo authResultInfo_ {};
111 };
112 } // namespace UserAuth
113 } // namespace UserIam
114 } // namespace OHOS
115 #endif // IAM_WIDGET_CONTEXT_H
116