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