1 /*
2  * Copyright (c) 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 AUTHORIZE_HELPER_H
17 #define AUTHORIZE_HELPER_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <memory>
22 #include <functional>
23 
24 #include "nocopyable.h"
25 #include "client_death_handler.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 enum class AuthorizeState : int32_t {
30     STATE_AUTHORIZE = 0,
31     STATE_UNAUTHORIZE = 1,
32     STATE_SELECTION_AUTHORIZE = 2,
33 };
34 
35 using AuthorizeExitCallback = std::function<void(int32_t)>;
36 
37 class AuthorizeHelper final {
38 public:
39     AuthorizeHelper();
40     ~AuthorizeHelper();
41     DISALLOW_COPY_AND_MOVE(AuthorizeHelper);
42     void Init(ClientDeathHandler& clientDeathHandler);
43     void CancelAuthorize(int32_t pid);
44     int32_t GetAuthorizePid();
45     int32_t AddAuthorizeProcess(int32_t pid, AuthorizeExitCallback exitCallback);
GetAuthorizeState()46     inline AuthorizeState GetAuthorizeState()
47     {
48         std::lock_guard<std::mutex> lock(mutex_);
49         return state_;
50     };
51     static std::shared_ptr<AuthorizeHelper> GetInstance();
52 
53 protected:
54     void OnClientDeath(int32_t pid);
55     void AuthorizeProcessExit();
56 
57 private:
58     int32_t pid_;
59     AuthorizeState state_ { AuthorizeState::STATE_UNAUTHORIZE };
60     std::atomic_bool isInit_  { false };
61     AuthorizeExitCallback exitCallback_ { nullptr };
62     static std::mutex mutex_;
63     static std::shared_ptr<AuthorizeHelper> instance_;
64 };
65 
66 #define AUTHORIZE_HELPER ::OHOS::MMI::AuthorizeHelper::GetInstance()
67 } // namespace MMI
68 } // namespace OHOS
69 #endif // AUTHORIZE_HELPER_H
70