1 /* 2 * Copyright (c) 2023-2025 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 #include "auth_ui_state_manager.h" 17 #include "dm_anonymous.h" 18 #include "dm_dialog_manager.h" 19 #include "dm_log.h" 20 #include "json_object.h" 21 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 22 #include "multiple_user_connector.h" 23 #endif 24 namespace OHOS { 25 namespace DistributedHardware { 26 constexpr const char* UI_STATE_MSG = "uiStateMsg"; AuthUiStateManager(std::shared_ptr<IDeviceManagerServiceListener> listener)27AuthUiStateManager::AuthUiStateManager(std::shared_ptr<IDeviceManagerServiceListener> listener) : listener_(listener) 28 { 29 LOGI("AuthUiStateManager constructor"); 30 } 31 RegisterUiStateCallback(const std::string pkgName)32void AuthUiStateManager::RegisterUiStateCallback(const std::string pkgName) 33 { 34 int32_t userId = -1; 35 MultipleUserConnector::GetCallerUserId(userId); 36 ProcessInfo processInfo; 37 processInfo.userId = userId; 38 processInfo.pkgName = pkgName; 39 std::lock_guard<std::mutex> lock(pkgSetMutex_); 40 pkgSet_.emplace(processInfo); 41 } 42 UnRegisterUiStateCallback(const std::string pkgName)43void AuthUiStateManager::UnRegisterUiStateCallback(const std::string pkgName) 44 { 45 int32_t userId = -1; 46 MultipleUserConnector::GetCallerUserId(userId); 47 ProcessInfo processInfo; 48 processInfo.userId = userId; 49 processInfo.pkgName = pkgName; 50 std::lock_guard<std::mutex> lock(pkgSetMutex_); 51 if (pkgSet_.find(processInfo) == pkgSet_.end()) { 52 LOGE("AuthUiStateManager UnRegisterUiStateCallback processInfo is not exist."); 53 return; 54 } 55 pkgSet_.erase(processInfo); 56 } 57 UpdateUiState(const DmUiStateMsg msg)58void AuthUiStateManager::UpdateUiState(const DmUiStateMsg msg) 59 { 60 if (listener_ == nullptr) { 61 LOGE("AuthUiStateManager::UpdateUiState listener is null."); 62 return; 63 } 64 JsonObject jsonObj; 65 jsonObj[UI_STATE_MSG] = msg; 66 std::string paramJson = jsonObj.Dump(); 67 std::lock_guard<std::mutex> lock(pkgSetMutex_); 68 if (pkgSet_.empty()) { 69 LOGW("pkgSet_ is empty"); 70 if (msg == MSG_CANCEL_CONFIRM_SHOW) { 71 LOGW("cancel confirm dialog"); 72 DmDialogManager::GetInstance().CloseDialog(); 73 return; 74 } 75 return; 76 } 77 for (auto item : pkgSet_) { 78 listener_->OnUiCall(item, paramJson); 79 } 80 LOGI("AuthUiStateManager::UpdateUiState complete."); 81 } 82 } // namespace DistributedHardware 83 } // namespace OHOS 84