• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "disturb_manager.h"
17 #include <functional>
18 #include <iomanip>
19 #include <sstream>
20 #include <type_traits>
21 
22 #include "access_token_helper.h"
23 #include "ans_inner_errors.h"
24 #include "ans_log_wrapper.h"
25 #include "ans_permission_def.h"
26 #include "errors.h"
27 #include "os_account_manager_helper.h"
28 #include "ipc_skeleton.h"
29 
30 namespace OHOS {
31 namespace Notification {
DisturbManager()32 DisturbManager::DisturbManager()
33 {
34     codeAndExecuteFuncMap_ = {
35         {static_cast<uint32_t>(NotificationInterfaceCode::REMOVE_DO_NOT_DISTURB_PROFILES),
36             std::bind(&DisturbManager::HandleRemoveDoNotDisturbProfiles, this, std::placeholders::_1,
37                 std::placeholders::_2)},
38         {static_cast<uint32_t>(NotificationInterfaceCode::SET_DO_NOT_DISTURB_DATE),
39             std::bind(&DisturbManager::HandleSetDoNotDisturbDate, this, std::placeholders::_1,
40                 std::placeholders::_2)},
41         {static_cast<uint32_t>(NotificationInterfaceCode::GET_DO_NOT_DISTURB_DATE),
42             std::bind(&DisturbManager::HandleGetDoNotDisturbDate, this, std::placeholders::_1,
43                 std::placeholders::_2)},
44         {static_cast<uint32_t>(NotificationInterfaceCode::SET_DO_NOT_DISTURB_DATE_BY_USER),
45             std::bind(&DisturbManager::HandleSetDoNotDisturbDateByUser, this, std::placeholders::_1,
46                 std::placeholders::_2)},
47         {static_cast<uint32_t>(NotificationInterfaceCode::GET_DO_NOT_DISTURB_DATE_BY_USER),
48             std::bind(&DisturbManager::HandleGetDoNotDisturbDateByUser, this, std::placeholders::_1,
49                 std::placeholders::_2)},
50         {static_cast<uint32_t>(NotificationInterfaceCode::ADD_DO_NOTDISTURB_PROFILES),
51             std::bind(&DisturbManager::HandleAddDoNotDisturbProfiles, this, std::placeholders::_1,
52                 std::placeholders::_2)},
53         {static_cast<uint32_t>(NotificationInterfaceCode::GET_DONOTDISTURB_PROFILE),
54             std::bind(&DisturbManager::HandleGetDoNotDisturbProfile, this, std::placeholders::_1,
55                 std::placeholders::_2)},
56         {static_cast<uint32_t>(NotificationInterfaceCode::DOES_SUPPORT_DO_NOT_DISTURB_MODE),
57             std::bind(&DisturbManager::HandleDoesSupportDoNotDisturbMode, this, std::placeholders::_1,
58                 std::placeholders::_2)},
59     };
60     codeAndPermissionFuncMap_ = {
61         {static_cast<uint32_t>(NotificationInterfaceCode::REMOVE_DO_NOT_DISTURB_PROFILES),
62             std::bind(&DisturbManager::CheckSystemAndControllerPermission, this)},
63         {static_cast<uint32_t>(NotificationInterfaceCode::SET_DO_NOT_DISTURB_DATE),
64             std::bind(&DisturbManager::CheckSystemAndControllerPermission, this)},
65         {static_cast<uint32_t>(NotificationInterfaceCode::GET_DO_NOT_DISTURB_DATE),
66             std::bind(&DisturbManager::CheckSystemAndControllerPermission, this)},
67         {static_cast<uint32_t>(NotificationInterfaceCode::SET_DO_NOT_DISTURB_DATE_BY_USER),
68             std::bind(&DisturbManager::CheckSystemAndControllerPermission, this)},
69         {static_cast<uint32_t>(NotificationInterfaceCode::GET_DO_NOT_DISTURB_DATE_BY_USER),
70             std::bind(&DisturbManager::CheckSystemAndControllerPermission, this)},
71         {static_cast<uint32_t>(NotificationInterfaceCode::ADD_DO_NOTDISTURB_PROFILES),
72             std::bind(&DisturbManager::CheckSystemAndControllerPermission, this)},
73         {static_cast<uint32_t>(NotificationInterfaceCode::GET_DONOTDISTURB_PROFILE),
74             std::bind(&DisturbManager::CheckSystemAndControllerPermission, this)},
75     };
76 }
77 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)78 int32_t DisturbManager::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
79 {
80     ANS_LOGD("[DisturbManager] called.");
81     auto permissionChecker = codeAndPermissionFuncMap_.find(code);
82     if (permissionChecker != codeAndPermissionFuncMap_.end()) {
83         ErrCode result = permissionChecker->second();
84         if (result != ERR_OK) {
85             if (!reply.WriteInt32(result)) {
86                 return ERR_ANS_PARCELABLE_FAILED;
87             }
88             return ERR_OK;
89         }
90     }
91     auto execution = codeAndExecuteFuncMap_.find(code);
92     if (execution == codeAndExecuteFuncMap_.end()) {
93         ANS_LOGE("[OnRemoteRequest] fail: unknown code!");
94         return ERR_ANS_INVALID_PARAM;
95     }
96     ErrCode result = execution->second(data, reply);
97     if (SUCCEEDED(result)) {
98         return NO_ERROR;
99     }
100     return result;
101 }
102 
CheckSystemAndControllerPermission()103 int32_t DisturbManager::CheckSystemAndControllerPermission()
104 {
105     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
106     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
107         return ERR_ANS_NON_SYSTEM_APP;
108     }
109 
110     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
111         return ERR_ANS_PERMISSION_DENIED;
112     }
113     return ERR_OK;
114 }
115 }  // namespace Notification
116 }  // namespace OHOS
117