• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "live_publish_process.h"
17 
18 #include "access_token_helper.h"
19 #include "advanced_notification_service.h"
20 #include "ans_log_wrapper.h"
21 #include "ans_const_define.h"
22 #include "ipc_skeleton.h"
23 #include "notification_content.h"
24 #include "notification_live_view_content.h"
25 #include "os_account_manager_helper.h"
26 
27 #include "../advanced_notification_inline.cpp"
28 
29 namespace OHOS {
30 namespace Notification {
31 std::shared_ptr<LivePublishProcess> LivePublishProcess::instance_;
32 std::mutex LivePublishProcess::instanceMutex_;
33 
GetInstance()34 std::shared_ptr<LivePublishProcess> LivePublishProcess::GetInstance()
35 {
36     std::lock_guard<std::mutex> lock(instanceMutex_);
37 
38     if (instance_ == nullptr) {
39         instance_ = std::make_shared<LivePublishProcess>();
40         if (instance_ == nullptr) {
41             ANS_LOGE("Failed to create LivePublishProcess instance");
42             return nullptr;
43         }
44     }
45     return instance_;
46 }
47 
PublishPreWork(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed)48 ErrCode LivePublishProcess::PublishPreWork(const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed)
49 {
50     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_1, EventBranchId::BRANCH_1);
51     if (!CheckLocalLiveViewAllowed(request, isUpdateByOwnerAllowed)) {
52         message.BranchId(EventBranchId::BRANCH_3).ErrorCode(ERR_ANS_NON_SYSTEM_APP)
53             .Message("CheckLocalLiveViewAllowed is false", true);
54         NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
55         return ERR_ANS_NON_SYSTEM_APP;
56     }
57 
58     if (!request->IsRemoveAllowed()) {
59         if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION)) {
60             request->SetRemoveAllowed(true);
61         }
62     }
63 
64     bool isHap = !AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()) &&
65         !AccessTokenHelper::IsSystemApp();
66     if (isUpdateByOwnerAllowed && isHap) {
67         if (request->GetTemplate() == nullptr) {
68             message.BranchId(EventBranchId::BRANCH_4).ErrorCode(ERR_ANS_INVALID_PARAM)
69                 .Message("Owner must has template to update", true);
70             NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
71             return ERR_ANS_INVALID_PARAM;
72         }
73     }
74     return ERR_OK;
75 }
76 
PublishNotificationByApp(const sptr<NotificationRequest> & request)77 ErrCode LivePublishProcess::PublishNotificationByApp(const sptr<NotificationRequest> &request)
78 {
79     ErrCode result = CommonPublishCheck(request);
80     if (result != ERR_OK) {
81         return result;
82     }
83 
84     if (request->IsInProgress() &&
85         !AccessTokenHelper::IsSystemApp() &&
86         !request->IsCommonLiveView()) {
87         request->SetInProgress(false);
88     }
89 
90     result = CommonPublishProcess(request);
91     if (result != ERR_OK) {
92         return result;
93     }
94     return ERR_OK;
95 }
96 
CheckLocalLiveViewSubscribed(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed,int32_t uid)97 bool LivePublishProcess::CheckLocalLiveViewSubscribed(
98     const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed, int32_t uid)
99 {
100     if (request->GetNotificationType() == NotificationContent::Type::LOCAL_LIVE_VIEW) {
101         return GetLiveViewSubscribeState(uid) || isUpdateByOwnerAllowed;
102     }
103     if (request->IsCommonLiveView()) {
104         std::shared_ptr<NotificationLiveViewContent> liveViewContent = nullptr;
105         liveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(
106             request->GetContent()->GetNotificationContent());
107         if (liveViewContent != nullptr && liveViewContent->GetIsOnlyLocalUpdate() &&
108             !GetLiveViewSubscribeState(uid)) {
109             ANS_LOGE("Not subscribe common live view.");
110             return false;
111         }
112     }
113     return true;
114 }
115 
CheckLocalLiveViewAllowed(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed)116 bool LivePublishProcess::CheckLocalLiveViewAllowed(
117     const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed)
118 {
119     if (request->GetNotificationType() == NotificationContent::Type::LOCAL_LIVE_VIEW) {
120         bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
121         if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
122             ANS_LOGE("Client is not a system app or subsystem");
123             return isUpdateByOwnerAllowed;
124         } else {
125             return true;
126         }
127     }
128     return true;
129 }
130 
AddLiveViewSubscriber(int32_t uid)131 void LivePublishProcess::AddLiveViewSubscriber(int32_t uid)
132 {
133     localLiveViewSubscribedList_.emplace(uid);
134 }
135 
EraseLiveViewSubsciber(int32_t uid)136 void LivePublishProcess::EraseLiveViewSubsciber(int32_t uid)
137 {
138     std::lock_guard<std::mutex> lock(liveViewMutext_);
139     localLiveViewSubscribedList_.erase(uid);
140 }
141 
GetLiveViewSubscribeState(int32_t uid)142 bool LivePublishProcess::GetLiveViewSubscribeState(int32_t uid)
143 {
144     std::lock_guard<std::mutex> lock(liveViewMutext_);
145     if (localLiveViewSubscribedList_.find(uid) == localLiveViewSubscribedList_.end()) {
146         return false;
147     }
148     return true;
149 }
150 }  // namespace Notification
151 }  // namespace OHOS
152