1 /*
2  * Copyright (c) 2023-2024 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 "base_context.h"
30 #include "context.h"
31 #include "context_appstate_observer.h"
32 #include "context_death_recipient.h"
33 #include "context_factory.h"
34 #include "in_process_call_wrapper.h"
35 #include "iam_callback_interface.h"
36 #include "nocopyable.h"
37 #include "widget_json.h"
38 #include "widget_schedule_node.h"
39 #include "ui_extension_ability_connection.h"
40 
41 namespace OHOS {
42 namespace UserIam {
43 namespace UserAuth {
44 using namespace OHOS::AppExecFwk;
45 class WidgetContext : public WidgetScheduleNodeCallback,
46                       public Context,
47                       public std::enable_shared_from_this<WidgetContext>,
48                       public ContextDeathRecipientManager,
49                       public ContextAppStateObserverManager,
50                       public NoCopyable {
51 public:
52     WidgetContext(uint64_t contextId, const ContextFactory::AuthWidgetContextPara &para,
53         std::shared_ptr<ContextCallback> callback);
54     ~WidgetContext() override;
55 
56     // Context API
57     bool Start() override;
58     bool Stop() override;
59     uint64_t GetContextId() const override;
60     ContextType GetContextType() const override;
61     std::shared_ptr<ScheduleNode> GetScheduleNode(uint64_t scheduleId) const override;
62     uint32_t GetTokenId() const override;
63     int32_t GetLatestError() const override;
64     int32_t GetUserId() const override;
65 
66     // WidgetScheduleNodeCallback API
67     bool LaunchWidget() override;
68     void ExecuteAuthList(const std::set<AuthType> &authTypeList, bool endAfterFirstFail,
69         AuthIntent authIntent) override;
70     void EndAuthAsCancel() override;
71     void EndAuthAsNaviPin() override;
72     void EndAuthAsWidgetParaInvalid() override;
73     void StopAuthList(const std::vector<AuthType> &authTypeList) override;
74     void SuccessAuth(AuthType authType) override;
75     bool AuthWidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad,
76         AuthType &rotateAuthType) override;
77     void AuthWidgetReloadInit() override;
78 
79     void AuthResult(int32_t resultCode, int32_t authType, const Attributes &finalResult);
80     void AuthTipInfo(int32_t tipInfo, int32_t authType, const Attributes &extraInfo);
81 protected:
82     virtual bool OnStart();
83     virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr);
84     virtual bool OnStop();
85 
86 private:
87     struct WidgetRotatePara {
88         bool isReload {false};
89         uint32_t orientation {0};
90         uint32_t needRotate {0};
91         uint32_t alreadyLoad {0};
92         AuthType rotateAuthType {0};
93     };
94     void SetLatestError(int32_t error) override;
95     std::shared_ptr<Context> BuildTask(const std::vector<uint8_t> &challenge,
96         AuthType authType, AuthTrustLevel authTrustLevel, bool endAfterFirstFail, AuthIntent authIntent);
97     bool BuildSchedule();
98     bool ConnectExtension(const WidgetRotatePara &widgetRotatePara);
99     int32_t ConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr);
100     bool DisconnectExtension();
101     void End(const ResultCode &resultCode);
102     std::shared_ptr<ContextCallback> GetAuthContextCallback(AuthType authType, AuthTrustLevel authTrustLevel,
103         sptr<IamCallbackInterface> &callback);
104     void StopAllRunTask();
105     std::string BuildStartCommand(const WidgetRotatePara &widgetRotatePara);
106     void ProcessRotatePara(WidgetCmdParameters &widgetCmdParameters, const WidgetRotatePara &widgetRotatePara);
107     bool isValidRotate(const WidgetRotatePara &widgetRotatePara);
108     std::string GetCallingBundleName();
109 
110 private:
111     struct TaskInfo {
112         AuthType authType {0};
113         std::shared_ptr<Context> task {nullptr};
114     };
115 
116     struct WidgetAuthResultInfo {
117         std::vector<uint8_t> token {};
118         AuthType authType {0};
119         uint64_t credentialDigest;
120         uint16_t credentialCount;
121         int64_t pinExpiredInfo;
122     };
123 
124     uint64_t contextId_ {0};
125     std::string description_ {""};
126     std::shared_ptr<ContextCallback> callerCallback_ {nullptr};
127     bool hasStarted_ {false};
128 
129     int32_t latestError_ {ResultCode::GENERAL_ERROR};
130     ContextFactory::AuthWidgetContextPara para_ {};
131     std::shared_ptr<WidgetScheduleNode> schedule_ {nullptr};
132     std::recursive_mutex mutex_;
133     std::list<TaskInfo> runTaskInfoList_;
134     sptr<UIExtensionAbilityConnection> connection_ {nullptr};
135     WidgetAuthResultInfo authResultInfo_ {};
136     int32_t faceReload_ {0};
137     uint32_t widgetRotateOrientation_ {0};
138     uint32_t widgetAlreadyLoad_ {0};
139 };
140 } // namespace UserAuth
141 } // namespace UserIam
142 } // namespace OHOS
143 #endif // IAM_WIDGET_CONTEXT_H
144