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_log.h"
18 #include "nlohmann/json.hpp"
19 
20 namespace OHOS {
21 namespace DistributedHardware {
22 constexpr const char* UI_STATE_MSG = "uiStateMsg";
AuthUiStateManager(std::shared_ptr<IDeviceManagerServiceListener> listener)23 AuthUiStateManager::AuthUiStateManager(std::shared_ptr<IDeviceManagerServiceListener> listener) : listener_(listener)
24 {
25     LOGI("AuthUiStateManager constructor");
26 }
27 
RegisterUiStateCallback(const std::string pkgName)28 void AuthUiStateManager::RegisterUiStateCallback(const std::string pkgName)
29 {
30     std::lock_guard<std::mutex> lock(pkgSetMutex_);
31     pkgSet_.emplace(pkgName);
32 }
33 
UnRegisterUiStateCallback(const std::string pkgName)34 void AuthUiStateManager::UnRegisterUiStateCallback(const std::string pkgName)
35 {
36     std::lock_guard<std::mutex> lock(pkgSetMutex_);
37     if (pkgSet_.find(pkgName) == pkgSet_.end()) {
38         LOGE("AuthUiStateManager UnRegisterUiStateCallback pkgName is not exist.");
39         return;
40     }
41     pkgSet_.erase(pkgName);
42 }
43 
UpdateUiState(const DmUiStateMsg msg)44 void AuthUiStateManager::UpdateUiState(const DmUiStateMsg msg)
45 {
46     if (listener_ == nullptr) {
47         LOGE("AuthUiStateManager::UpdateUiState listener is null.");
48         return;
49     }
50     nlohmann::json jsonObj;
51     jsonObj[UI_STATE_MSG] = msg;
52     std::string paramJson = jsonObj.dump();
53     std::lock_guard<std::mutex> lock(pkgSetMutex_);
54     for (auto pkgName : pkgSet_) {
55         listener_->OnUiCall(pkgName, paramJson);
56     }
57     LOGI("AuthUiStateManager::UpdateUiState complete.");
58 }
59 } // namespace DistributedHardware
60 } // namespace OHOS
61