1 /*
2  * Copyright (c) 2022-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 CO_AUTH_SERVICE_H
17 #define CO_AUTH_SERVICE_H
18 
19 #include "co_auth_stub.h"
20 
21 #include <system_ability.h>
22 #include <system_ability_definition.h>
23 #include "system_ability_listener.h"
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 class CoAuthService : public SystemAbility, public CoAuthStub {
29 public:
30     static constexpr uint64_t DEFER_TIME = 2000;
31     DECLARE_SYSTEM_ABILITY(CoAuthService);
32     static std::shared_ptr<CoAuthService> GetInstance();
33 
34     CoAuthService();
35     ~CoAuthService() override = default;
36     int Dump(int fd, const std::vector<std::u16string> &args) override;
37     uint64_t ExecutorRegister(const ExecutorRegisterInfo &info, sptr<ExecutorCallbackInterface> &callback) override;
38     void ExecutorUnregister(uint64_t executorIndex) override;
39     void SetIsReady(bool isReady);
40     void SetAccessTokenReady(bool isReady);
41 
42 protected:
43     void OnStart() override;
44     void OnStop() override;
45 
46 private:
47     static void Init();
48     static void AddExecutorDeathRecipient(uint64_t executorIndex, AuthType authType,
49         std::shared_ptr<ExecutorCallbackInterface> callback);
50     void AuthServiceInit();
51     ResultCode RegisterAccessTokenListener();
52     ResultCode UnRegisterAccessTokenListener();
53     void NotifyFwkReady();
54     bool IsFwkReady();
55 
56     static std::shared_ptr<CoAuthService> instance_;
57     std::recursive_mutex mutex_;
58     bool isReady_{false};
59     bool accessTokenReady_{false};
60     sptr<SystemAbilityListener> accessTokenListener_ {nullptr};
61 };
62 } // namespace UserAuth
63 } // namespace UserIam
64 } // namespace OHOS
65 #endif // CO_AUTH_SERVICE_H