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