1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
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 "account_observer.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "os_account_manager.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "AccountObserver"};
23 }
24 using namespace OHOS;
25 namespace OHOS {
26 namespace Media {
27
GetInstance()28 AccountObserver& AccountObserver::GetInstance()
29 {
30 static AccountObserver instance;
31 instance.Init();
32 return instance;
33 }
34
AccountObserver()35 AccountObserver::AccountObserver()
36 {
37 MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
38 }
39
~AccountObserver()40 AccountObserver::~AccountObserver()
41 {
42 UnregisterObserver();
43 MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
44 }
45
RegisterAccountObserverCallBack(std::weak_ptr<AccountObserverCallBack> callback)46 bool AccountObserver::RegisterAccountObserverCallBack(std::weak_ptr<AccountObserverCallBack> callback)
47 {
48 std::unique_lock<std::mutex> lock(mutex_);
49 auto callbackPtr = callback.lock();
50 if (callbackPtr) {
51 accountObserverCallBack_ = callback;
52 return true;
53 }
54 MEDIA_LOGI("0x%{public}06" PRIXPTR "AccountObserver CallBack is null", FAKE_POINTER(this));
55 return false;
56 }
57
UnregisterAccountObserverCallBack()58 void AccountObserver::UnregisterAccountObserverCallBack()
59 {
60 if (!accountObserverCallBack_.expired()) {
61 accountObserverCallBack_.reset();
62 }
63 }
64
OnAccountsSwitch()65 bool AccountObserver::OnAccountsSwitch()
66 {
67 std::unique_lock<std::mutex> lock(mutex_);
68 auto callbackPtr = accountObserverCallBack_.lock();
69 if (callbackPtr) {
70 MEDIA_LOGI("0x%{public}06" PRIXPTR " Stop and Release CallBack", FAKE_POINTER(this));
71 return callbackPtr->StopAndRelease(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER_SWITCHES);
72 } else {
73 MEDIA_LOGI("0x%{public}06" PRIXPTR "AccountObserver CallBack is null", FAKE_POINTER(this));
74 }
75 return true;
76 }
77
Init()78 bool AccountObserver::Init()
79 {
80 if (isAccountListenerDied_.load()) {
81 MEDIA_LOGI("0x%{public}06" PRIXPTR " AccountObserver Init, Register Observer", FAKE_POINTER(this));
82 UnregisterObserver();
83 if (RegisterObserver()) {
84 isAccountListenerDied_.store(false);
85 }
86 } else {
87 MEDIA_LOGI("AccountObserver exist : %{public}d", isAccountListenerDied_.load());
88 }
89 return true;
90 }
91
RegisterObserver()92 bool AccountObserver::RegisterObserver()
93 {
94 MEDIA_LOGI("AccountObserver Register");
95 std::unique_lock<std::mutex> lock(mutex_);
96 AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo;
97 osAccountSubscribeInfo.SetOsAccountSubscribeType(AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED);
98 osAccountSubscribeInfo.SetName("ScreenCaptureAccountSubscriber");
99 accountListener_ = std::make_shared<AccountListener>(osAccountSubscribeInfo);
100 if (accountListener_ == nullptr) {
101 MEDIA_LOGE("make AccountListener failed");
102 return false;
103 }
104 ErrCode errCode = AccountSA::OsAccountManager::SubscribeOsAccount(accountListener_);
105 CHECK_AND_RETURN_RET_LOG(errCode == ERR_OK, false, "subscribe failed, error code: %{public}d", errCode);
106
107 return true;
108 }
109
UnregisterObserver()110 void AccountObserver::UnregisterObserver()
111 {
112 MEDIA_LOGI("AccountObserver Unregister");
113 std::unique_lock<std::mutex> lock(mutex_);
114 if (accountListener_) {
115 AccountSA::OsAccountManager::UnsubscribeOsAccount(accountListener_);
116 }
117 isAccountListenerDied_.store(true);
118 }
119 } // namespace Media
120 } // namespace OHOS