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         ANS_LOGE("CheckLocalLiveViewAllowed is false");
56         return ERR_ANS_NON_SYSTEM_APP;
57     }
58 
59     if (!request->IsRemoveAllowed()) {
60         if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION)) {
61             request->SetRemoveAllowed(true);
62         }
63     }
64 
65     bool isHap = !AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()) &&
66         !AccessTokenHelper::IsSystemApp();
67     if (isUpdateByOwnerAllowed && isHap) {
68         if (request->GetTemplate() == nullptr) {
69             message.BranchId(EventBranchId::BRANCH_4).ErrorCode(ERR_ANS_INVALID_PARAM)
70                 .Message("Owner must has template to update", true);
71             NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
72             ANS_LOGE("Owner must has template to update.");
73             return ERR_ANS_INVALID_PARAM;
74         }
75     }
76     return ERR_OK;
77 }
78 
PublishNotificationByApp(const sptr<NotificationRequest> & request)79 ErrCode LivePublishProcess::PublishNotificationByApp(const sptr<NotificationRequest> &request)
80 {
81     ErrCode result = CommonPublishCheck(request);
82     if (result != ERR_OK) {
83         return result;
84     }
85 
86     if (request->IsInProgress() &&
87         !AccessTokenHelper::IsSystemApp() &&
88         !request->IsCommonLiveView()) {
89         request->SetInProgress(false);
90     }
91 
92     result = CommonPublishProcess(request);
93     if (result != ERR_OK) {
94         return result;
95     }
96     return ERR_OK;
97 }
98 
CheckLocalLiveViewSubscribed(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed,int32_t uid)99 bool LivePublishProcess::CheckLocalLiveViewSubscribed(
100     const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed, int32_t uid)
101 {
102     if (request->GetNotificationType() == NotificationContent::Type::LOCAL_LIVE_VIEW) {
103         return GetLiveViewSubscribeState(uid) || isUpdateByOwnerAllowed;
104     }
105     if (request->IsCommonLiveView()) {
106         std::shared_ptr<NotificationLiveViewContent> liveViewContent = nullptr;
107         liveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(
108             request->GetContent()->GetNotificationContent());
109         if (liveViewContent != nullptr && liveViewContent->GetIsOnlyLocalUpdate() &&
110             !GetLiveViewSubscribeState(uid)) {
111             ANS_LOGE("Not subscribe common live view.");
112             return false;
113         }
114     }
115     return true;
116 }
117 
CheckLocalLiveViewAllowed(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed)118 bool LivePublishProcess::CheckLocalLiveViewAllowed(
119     const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed)
120 {
121     if (request->GetNotificationType() == NotificationContent::Type::LOCAL_LIVE_VIEW) {
122         bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
123         if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
124             ANS_LOGE("Client is not a system app or subsystem");
125             return isUpdateByOwnerAllowed;
126         } else {
127             return true;
128         }
129     }
130     return true;
131 }
132 
AddLiveViewSubscriber(int32_t uid)133 void LivePublishProcess::AddLiveViewSubscriber(int32_t uid)
134 {
135     localLiveViewSubscribedList_.emplace(uid);
136 }
137 
EraseLiveViewSubsciber(int32_t uid)138 void LivePublishProcess::EraseLiveViewSubsciber(int32_t uid)
139 {
140     std::lock_guard<std::mutex> lock(liveViewMutext_);
141     localLiveViewSubscribedList_.erase(uid);
142 }
143 
GetLiveViewSubscribeState(int32_t uid)144 bool LivePublishProcess::GetLiveViewSubscribeState(int32_t uid)
145 {
146     std::lock_guard<std::mutex> lock(liveViewMutext_);
147     if (localLiveViewSubscribedList_.find(uid) == localLiveViewSubscribedList_.end()) {
148         return false;
149     }
150     return true;
151 }
152 }  // namespace Notification
153 }  // namespace OHOS
154