• 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 StopAuthList(const std::vector<AuthType> &authTypeList) override;
64     void SuccessAuth(AuthType authType) override;
65 
66     void AuthResult(int32_t resultCode, int32_t at, const Attributes &finalResult);
67 protected:
68     virtual bool OnStart();
69     virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr);
70     virtual bool OnStop();
71 
72 private:
73     void SetLatestError(int32_t error) override;
74     std::shared_ptr<Context> BuildTask(const std::vector<uint8_t> &challenge,
75         AuthType authType, AuthTrustLevel authTrustLevel);
76     bool BuildSchedule();
77     bool ConnectExtension();
78     int32_t ConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr);
79     bool DisconnectExtension();
80     void End(const ResultCode &resultCode);
81     std::shared_ptr<ContextCallback> GetAuthContextCallback(AuthType authType, AuthTrustLevel authTrustLevel,
82         sptr<IamCallbackInterface> &callback);
83     void StopAllRunTask();
84     std::string BuildStartCommand();
85 
86 private:
87     struct TaskInfo {
88         AuthType authType {0};
89         std::shared_ptr<Context> task {nullptr};
90     };
91 
92     struct WidgetAuthResultInfo {
93         std::vector<uint8_t> token {};
94         AuthType authType { 0 };
95     };
96 
97     uint64_t contextId_ {0};
98     std::string description_ {""};
99     std::shared_ptr<ContextCallback> callback_ {nullptr};
100     bool hasStarted_ {false};
101 
102     int32_t latestError_ {ResultCode::GENERAL_ERROR};
103     ContextFactory::AuthWidgetContextPara para_ {};
104     std::shared_ptr<WidgetScheduleNode> schedule_ {nullptr};
105     std::recursive_mutex mutex_;
106     std::list<TaskInfo> runTaskInfoList_;
107     sptr<UIExtensionAbilityConnection> connection_ {nullptr};
108     WidgetAuthResultInfo authResultInfo_ {};
109 };
110 } // namespace UserAuth
111 } // namespace UserIam
112 } // namespace OHOS
113 #endif // IAM_WIDGET_CONTEXT_H
114