1 /*
2  * Copyright (C) 2021-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 CONTEXT_H
17 #define CONTEXT_H
18 
19 #include "base_def.h"
20 #include "dispatcher.h"
21 #include "message.h"
22 
23 namespace utility {
24 class IContextCallback {
25 public:
26     /**
27      * @brief A destructor used to delete the <b>IContextObserver</b> instance.
28      *
29      * @since 6
30      */
31     virtual ~IContextCallback() = default;
32 
33     /**
34      * @brief Enable complete notify
35      *
36      * @param name adapter name or profile name.
37      * @param ret enable complete successful or failed.
38      * @since 6
39      */
40     virtual void OnEnable(const std::string &name, bool ret) = 0;
41 
42     /**
43      * @brief Disable complete notify
44      *
45      * @param name adapter name or profile name.
46      * @param ret disable complete successful or failed.
47      * @since 6
48      */
49     virtual void OnDisable(const std::string &name, bool ret) = 0;
50 };
51 
52 class Context {
53 public:
54     /**
55      * @brief Construct a new Context object, Usually inherited by profile services,
56      *        to Construct it's own thread Context environment.
57      *
58      * @param name Context name.
59      * @param version Context version.
60      * @since 6
61      */
Context(const std::string & name,const std::string & version)62     Context(const std::string &name, const std::string &version)
63         : name_(name), version_(version), dispatcher_(std::make_unique<Dispatcher>(name))
64     {}
~Context()65     virtual ~Context()
66     {}
67 
68     /**
69      * @brief Enable.
70      * @since 6
71      */
72     virtual void Enable() = 0;
73 
74     /**
75      * @brief Disable.
76      * @since 6
77      */
78     virtual void Disable() = 0;
79 
80     /**
81      * @brief Post enable.
82      * @since 6
83      */
PostEnable()84     virtual void PostEnable()
85     {}
86 
87     /**
88      * @brief Enable complete callback notify.
89      * @since 6
90      */
OnEnable(const std::string & name,bool ret)91     virtual void OnEnable(const std::string &name, bool ret)
92     {
93         if (callback_)
94             callback_->OnEnable(name, ret);
95     }
96 
97     /**
98      * @brief Disable complete callback notify.
99      * @since 6
100      */
OnDisable(const std::string & name,bool ret)101     virtual void OnDisable(const std::string &name, bool ret)
102     {
103         if (callback_)
104             callback_->OnDisable(name, ret);
105     }
106 
107     /**
108      * @brief register callback function.
109      * @param callback callback function.
110      * @since 6
111      */
RegisterCallback(IContextCallback & callback)112     void RegisterCallback(IContextCallback &callback)
113     {
114         callback_ = &callback;
115     }
116 
117     /**
118      * @brief Initialize the context
119      * @since 6
120      */
Initialize()121     void Initialize()
122     {
123         dispatcher_->Initialize();
124         InitializeInternal();
125     }
126 
127     /**
128      * @brief Uninitialize the context
129      * @since 6
130      */
Uninitialize()131     void Uninitialize()
132     {
133         dispatcher_->Uninitialize();
134         UninitializeInternal();
135     }
136 
137     /**
138      * @brief Get the Context's Dispatcher object
139      * @return Dispatcher pointer.
140      * @since 6
141      */
GetDispatcher()142     Dispatcher *GetDispatcher()
143     {
144         return dispatcher_.get();
145     }
146 
147     /**
148      * @brief Get Context's name.
149      * @return Context's name.
150      * @since 6
151      */
Name()152     const std::string &Name() const
153     {
154         return name_;
155     }
156 
157     /**
158      * @brief Get Context's version.
159      * @return Context's version.
160      * @since 6
161      */
Version()162     const std::string &Version() const
163     {
164         return version_;
165     }
166 
167 private:
168     /**
169      * @brief Internal initialize function, called by Initialize
170      * @since 6
171      */
InitializeInternal()172     virtual void InitializeInternal()
173     {}
174 
175     /**
176      * @brief Internal uninitialize function, called by Uninitialize
177      * @since 6
178      */
UninitializeInternal()179     virtual void UninitializeInternal()
180     {}
181 
182     std::string name_ {""};
183     std::string version_ {};
184     std::unique_ptr<Dispatcher> dispatcher_ {};
185     IContextCallback *callback_ = nullptr;
186     BT_DISALLOW_COPY_AND_ASSIGN(Context);
187 };
188 }  // namespace utility
189 
190 #endif  // CONTEXT_H