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 
48     void OnScheduleStarted() override;
49     void OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) override;
50     void OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult) override;
51     int32_t GetLatestError() const override;
52 
53 protected:
54     void SetLatestError(int32_t error) override;
55     const char *GetDescription() const;
56     virtual bool OnStart() = 0;
57     virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr) = 0;
58     virtual bool OnStop() = 0;
59     std::shared_ptr<ContextCallback> callback_ = nullptr;
60     std::vector<std::shared_ptr<ScheduleNode>> scheduleList_;
61 
62 private:
63     bool Start() final;
64     bool Stop() final;
65     uint64_t contextId_ = INVALID_CONTEXT_ID;
66     std::string description_;
67     bool hasStarted_ = false;
68     std::mutex mutex_;
69     int32_t latestError_ = ResultCode::GENERAL_ERROR;
70 };
71 } // namespace UserAuth
72 } // namespace UserIam
73 } // namespace OHOS
74 #endif // IAM_BASE_CONTEXT_H