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