• 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 #include "device_manager_service_notify.h"
17 #include "dm_anonymous.h"
18 #include "dm_error_type.h"
19 #include "dm_log.h"
20 
21 namespace OHOS {
22 namespace DistributedHardware {
23 DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerServiceNotify);
24 namespace {
25 constexpr uint32_t MAX_CALLBACK_LEN = 1000;
26 const static std::map<DmCommonNotifyEvent, DmCommonNotifyEvent> unRegNotifyEventMap_ = {
27     {DmCommonNotifyEvent::UN_REG_DEVICE_STATE, DmCommonNotifyEvent::REG_DEVICE_STATE},
28     {DmCommonNotifyEvent::UN_REG_DEVICE_SCREEN_STATE, DmCommonNotifyEvent::REG_DEVICE_SCREEN_STATE},
29     {DmCommonNotifyEvent::UN_REG_REMOTE_DEVICE_TRUST_CHANGE, DmCommonNotifyEvent::REG_REMOTE_DEVICE_TRUST_CHANGE},
30     {DmCommonNotifyEvent::UN_REG_CREDENTIAL_AUTH_STATUS_NOTIFY, DmCommonNotifyEvent::REG_CREDENTIAL_AUTH_STATUS_NOTIFY}
31 };
32 
33 const static std::set<DmCommonNotifyEvent> regNotifyEventSet_ = {
34     DmCommonNotifyEvent::REG_DEVICE_STATE,
35     DmCommonNotifyEvent::REG_DEVICE_SCREEN_STATE,
36     DmCommonNotifyEvent::REG_REMOTE_DEVICE_TRUST_CHANGE,
37     DmCommonNotifyEvent::REG_CREDENTIAL_AUTH_STATUS_NOTIFY
38 };
39 }
40 
RegisterCallBack(int32_t dmCommonNotifyEvent,const ProcessInfo & processInfo)41 int32_t DeviceManagerServiceNotify::RegisterCallBack(int32_t dmCommonNotifyEvent, const ProcessInfo &processInfo)
42 {
43     LOGI("start event %{public}d pkgName: %{public}s.", dmCommonNotifyEvent, processInfo.pkgName.c_str());
44     if (processInfo.pkgName.empty()) {
45         LOGE("Invalid parameter, pkgName is empty.");
46         return ERR_DM_INPUT_PARA_INVALID;
47     }
48     DmCommonNotifyEvent notifyEvent = static_cast<DmCommonNotifyEvent>(dmCommonNotifyEvent);
49     std::lock_guard<std::mutex> autoLock(callbackLock_);
50     if (unRegNotifyEventMap_.find(notifyEvent) != unRegNotifyEventMap_.end()) {
51         if (callbackMap_.find(unRegNotifyEventMap_.at(notifyEvent)) == callbackMap_.end()) {
52             LOGI("notifyEvent is not exist %{public}d.", unRegNotifyEventMap_.at(notifyEvent));
53             return DM_OK;
54         }
55         callbackMap_.at(unRegNotifyEventMap_.at(notifyEvent)).erase(processInfo);
56         return DM_OK;
57     }
58     if (regNotifyEventSet_.find(notifyEvent) == regNotifyEventSet_.end()) {
59         LOGE("notifyEvent is not support %{public}d.", notifyEvent);
60         return ERR_DM_INPUT_PARA_INVALID;
61     }
62     if (callbackMap_.find(notifyEvent) == callbackMap_.end()) {
63         std::set<ProcessInfo> processInfoSet;
64         processInfoSet.insert(processInfo);
65         callbackMap_[notifyEvent] = processInfoSet;
66         return DM_OK;
67     }
68     if (callbackMap_[notifyEvent].size() >= MAX_CALLBACK_LEN) {
69         LOGE("too many callbacks dmCommonNotifyEvent: %{public}d, pkgName: %{public}s", dmCommonNotifyEvent,
70             processInfo.pkgName.c_str());
71         return ERR_DM_FAILED;
72     }
73     callbackMap_.at(notifyEvent).insert(processInfo);
74     return DM_OK;
75 }
76 
GetCallBack(int32_t dmCommonNotifyEvent,std::set<ProcessInfo> & processInfos)77 void DeviceManagerServiceNotify::GetCallBack(int32_t dmCommonNotifyEvent, std::set<ProcessInfo> &processInfos)
78 {
79     LOGI("start event %{public}d.", dmCommonNotifyEvent);
80     DmCommonNotifyEvent notifyEvent = static_cast<DmCommonNotifyEvent>(dmCommonNotifyEvent);
81     std::lock_guard<std::mutex> autoLock(callbackLock_);
82     if (regNotifyEventSet_.find(notifyEvent) == regNotifyEventSet_.end()) {
83         LOGE("notifyEvent is not support %{public}d.", notifyEvent);
84         return;
85     }
86     if (callbackMap_.find(notifyEvent) == callbackMap_.end()) {
87         LOGE("callback is empty %{public}d.", notifyEvent);
88         return;
89     }
90     processInfos = callbackMap_.at(notifyEvent);
91 }
92 
ClearDiedProcessCallback(const ProcessInfo & processInfo)93 void DeviceManagerServiceNotify::ClearDiedProcessCallback(const ProcessInfo &processInfo)
94 {
95     LOGI("start pkgName %{public}s.", processInfo.pkgName.c_str());
96     std::lock_guard<std::mutex> autoLock(callbackLock_);
97     for (auto iter = callbackMap_.begin(); iter != callbackMap_.end();) {
98         iter->second.erase(processInfo);
99         if (iter->second.empty()) {
100             iter = callbackMap_.erase(iter);
101         } else {
102             ++iter;
103         }
104     }
105 }
106 } // namespace DistributedHardware
107 } // namespace OHOS
108