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); 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 51 private: 52 int32_t pid_; 53 AuthorizeState state_ { AuthorizeState::STATE_UNAUTHORIZE }; 54 std::atomic_bool isInit_ { false }; 55 AuthorizeExitCallback exitCallback_ { nullptr }; 56 static std::mutex mutex_; 57 static std::shared_ptr<AuthorizeHelper> instance_; 58 }; 59 60 #define AUTHORIZE_HELPER ::OHOS::MMI::AuthorizeHelper::GetInstance() 61 } // namespace MMI 62 } // namespace OHOS 63 #endif // AUTHORIZE_HELPER_H 64