1 /*
2  * Copyright (c) 2022 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_BASE_CONTEXT_H
17 #define IAM_BASE_CONTEXT_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "nocopyable.h"
25 #include "context.h"
26 #include "context_appstate_observer.h"
27 #include "context_callback.h"
28 #include "context_death_recipient.h"
29 
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
33 using namespace OHOS::AppExecFwk;
34 class BaseContext : public ScheduleNodeCallback,
35                     public Context,
36                     public std::enable_shared_from_this<BaseContext>,
37                     public ContextDeathRecipientManager,
38                     public ContextAppStateObserverManager,
39                     public NoCopyable {
40 public:
41     BaseContext(const std::string &type, uint64_t contextId, std::shared_ptr<ContextCallback> callback);
42     ~BaseContext() override;
43 
44     uint64_t GetContextId() const override;
45     std::shared_ptr<ScheduleNode> GetScheduleNode(uint64_t scheduleId) const override;
46     int32_t GetUserId() const override;
47     int32_t GetAuthType() const override;
48     std::string GetCallerName() const override;
49 
50     void OnScheduleStarted() override;
51     void OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) override;
52     void OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult) override;
53     int32_t GetLatestError() const override;
54 
55 protected:
56     void SetLatestError(int32_t error) override;
57     const char *GetDescription() const;
58     virtual bool OnStart() = 0;
59     virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr) = 0;
60     virtual bool OnStop() = 0;
61     std::shared_ptr<ContextCallback> callback_ = nullptr;
62     std::vector<std::shared_ptr<ScheduleNode>> scheduleList_;
63 
64 private:
65     bool Start() final;
66     bool Stop() final;
67     uint64_t contextId_ = INVALID_CONTEXT_ID;
68     std::string description_;
69     bool hasStarted_ = false;
70     std::mutex mutex_;
71     int32_t latestError_ = ResultCode::GENERAL_ERROR;
72 };
73 } // namespace UserAuth
74 } // namespace UserIam
75 } // namespace OHOS
76 #endif // IAM_BASE_CONTEXT_H