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 "sync_rule/user_status_listener.h"
17 
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "utils_log.h"
21 
22 namespace OHOS::FileManagement::CloudSync {
23 
UserStatusSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,std::shared_ptr<UserStatusListener> listener)24 UserStatusSubscriber::UserStatusSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
25     std::shared_ptr<UserStatusListener> listener) : EventFwk::CommonEventSubscriber(subscribeInfo), listener_(listener)
26 {
27 }
28 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)29 void UserStatusSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
30 {
31     auto action = eventData.GetWant().GetAction();
32     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
33         LOGI("user unlocked");
34         listener_->NotifyUserUnlocked();
35         return;
36     }
37     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
38         LOGI("account logout");
39         listener_->DoCleanVideoCache();
40         return;
41     }
42 }
43 
UserStatusListener(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)44 UserStatusListener::UserStatusListener(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
45 {
46     dataSyncManager_ = dataSyncManager;
47 }
48 
~UserStatusListener()49 UserStatusListener::~UserStatusListener()
50 {
51     Stop();
52 }
53 
AddObserver(std::shared_ptr<IUserStatusObserver> observer)54 void UserStatusListener::AddObserver(std::shared_ptr<IUserStatusObserver> observer)
55 {
56     std::lock_guard<std::mutex> lck(obsVecMutex_);
57     observers_.push_back(observer);
58 }
59 
NotifyUserUnlocked()60 void UserStatusListener::NotifyUserUnlocked()
61 {
62     std::lock_guard<std::mutex> lck(obsVecMutex_);
63     for (auto &observer : observers_) {
64         observer->OnUserUnlocked();
65     }
66 }
67 
Start()68 void UserStatusListener::Start()
69 {
70     /* subscribe user unlock event */
71     EventFwk::MatchingSkills matchingSkills;
72     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
73     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
74     EventFwk::CommonEventSubscribeInfo info(matchingSkills);
75     commonEventSubscriber_ = std::make_shared<UserStatusSubscriber>(info, shared_from_this());
76     auto subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_);
77     LOGI("Subscriber end, SubscribeResult = %{public}d", subRet);
78 }
79 
Stop()80 void UserStatusListener::Stop()
81 {
82     if (commonEventSubscriber_ != nullptr) {
83         EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_);
84         commonEventSubscriber_ = nullptr;
85     }
86 }
87 
DoCleanVideoCache()88 void UserStatusListener::DoCleanVideoCache()
89 {
90     dataSyncManager_->CleanVideoCache();
91 }
92 } // namespace OHOS::FileManagement::CloudSync