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_CLIENT_H 17 #define IAM_WIDGET_CLIENT_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <set> 22 #include <vector> 23 24 #include "authentication_impl.h" 25 #include "imodal_callback.h" 26 #include "iwidget_callback.h" 27 #include "widget_json.h" 28 #include "widget_schedule_node.h" 29 30 namespace OHOS { 31 namespace UserIam { 32 namespace UserAuth { 33 class WidgetClient { 34 public: 35 static WidgetClient &Instance(); 36 ~WidgetClient() = default; 37 // sets 38 void SetWidgetSchedule(uint64_t contextId, const std::shared_ptr<WidgetScheduleNode> &schedule); 39 void SetWidgetParam(const WidgetParamInner ¶m); 40 void SetAuthTypeList(const std::vector<AuthType> &authTypeList); 41 void SetWidgetCallback(const sptr<IWidgetCallback> &callback); 42 void SetAuthTokenId(uint32_t tokenId); 43 uint32_t GetAuthTokenId(); 44 45 // interaction with widget 46 ResultCode OnNotice(NoticeType type, const std::string &eventData); 47 void ReportWidgetResult(int32_t result, AuthType authType, 48 int32_t lockoutDuration, int32_t remainAttempts, bool skipLockedBiometricAuth); 49 void ReportWidgetTip(int32_t tipType, AuthType authType, std::vector<uint8_t> tipInfo, 50 bool skipLockedBiometricAuth); 51 52 // others 53 void SetPinSubType(const PinSubType &subType); 54 void SetSensorInfo(const std::string &info); 55 void Reset(); 56 void ForceStopAuth(); 57 58 // extra info 59 void SetChallenge(const std::vector<uint8_t> &challenge); 60 void SetCallingBundleName(const std::string &callingBundleName); 61 62 private: 63 WidgetClient() = default; 64 void SendCommand(const WidgetCommand &command); 65 bool GetAuthTypeList(const WidgetNotice ¬ice, std::vector<AuthType> &authTypeList); 66 bool IsValidNoticeType(const WidgetNotice ¬ice); 67 void ProcessNotice(const WidgetNotice ¬ice, std::vector<AuthType> &authTypeList); 68 void InsertScheduleNode(uint64_t contextId, std::shared_ptr<WidgetScheduleNode> &scheduleNode); 69 void RemoveScheduleNode(uint64_t contextId); 70 std::shared_ptr<WidgetScheduleNode> GetScheduleNode(uint64_t contextId); 71 void ClearSchedule(uint64_t contextId); 72 void WidgetLoad(uint64_t contextId, std::vector<AuthType> &authTypeList); 73 void WidgetRelease(uint64_t contextId, std::vector<AuthType> &authTypeList); 74 75 private: 76 std::shared_ptr<WidgetScheduleNode> schedule_ {nullptr}; 77 uint64_t widgetContextId_ {0}; 78 WidgetParamInner widgetParam_ {}; 79 std::vector<AuthType> authTypeList_ {}; 80 sptr<IWidgetCallback> widgetCallback_ {nullptr}; 81 std::string pinSubType_ {""}; 82 std::string sensorInfo_ {""}; 83 uint32_t authTokenId_ {0}; 84 std::vector<uint8_t> challenge_ {}; 85 std::string callingBundleName_ {""}; 86 std::recursive_mutex mutex_; 87 /* <contextId_, schedule_> */ 88 std::map<uint64_t, std::shared_ptr<WidgetScheduleNode>> scheduleMap_; 89 std::vector<AuthType> loadedAuthTypeList_ {}; 90 }; 91 } // namespace UserAuth 92 } // namespace UserIam 93 } // namespace OHOS 94 #endif // IAM_WIDGET_CLIENT_H