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 "screen_capture_monitor_client.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureMonitorClient"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
Create(const sptr<IStandardScreenCaptureMonitorService> & ipcProxy)26 std::shared_ptr<ScreenCaptureMonitorClient> ScreenCaptureMonitorClient::Create(
27     const sptr<IStandardScreenCaptureMonitorService> &ipcProxy)
28 {
29     CHECK_AND_RETURN_RET_LOG(ipcProxy != nullptr, nullptr, "ipcProxy is nullptr..");
30     std::shared_ptr<ScreenCaptureMonitorClient> scmClient = std::make_shared<ScreenCaptureMonitorClient>(ipcProxy);
31     CHECK_AND_RETURN_RET_LOG(scmClient != nullptr, nullptr, "failed to new ScreenCaptureMonitorClient..");
32     int32_t ret = scmClient->CreateListenerObject();
33     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to create listener object..");
34     return scmClient;
35 }
36 
ScreenCaptureMonitorClient(const sptr<IStandardScreenCaptureMonitorService> & ipcProxy)37 ScreenCaptureMonitorClient::ScreenCaptureMonitorClient(const sptr<IStandardScreenCaptureMonitorService> &ipcProxy)
38     : screenCaptureMonitorProxy_(ipcProxy)
39 {
40     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
41 }
42 
~ScreenCaptureMonitorClient()43 ScreenCaptureMonitorClient::~ScreenCaptureMonitorClient()
44 {
45     {
46         std::lock_guard<std::mutex> lock(mutex_);
47         if (screenCaptureMonitorProxy_ != nullptr) {
48             (void)screenCaptureMonitorProxy_->DestroyStub();
49             screenCaptureMonitorProxy_ = nullptr;
50         }
51         screenCaptureMonitorClientCallbacks_.clear();
52     }
53     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
54 }
55 
MediaServerDied()56 void ScreenCaptureMonitorClient::MediaServerDied()
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     screenCaptureMonitorProxy_ = nullptr;
60     listenerStub_ = nullptr;
61 }
62 
CreateListenerObject()63 int32_t ScreenCaptureMonitorClient::CreateListenerObject()
64 {
65     std::lock_guard<std::mutex> lock(mutex_);
66     listenerStub_ = new(std::nothrow) ScreenCaptureMonitorListenerStub();
67     CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY,
68         "failed to new ScreenCaptureMonitorListenerStub object");
69     CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorProxy_ != nullptr, MSERR_NO_MEMORY,
70         "ScreenCaptureMonitor service does not exist.");
71     sptr<IRemoteObject> object = listenerStub_->AsObject();
72     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "listener object is nullptr.");
73     MEDIA_LOGD("SetListenerObject");
74     listenerStubIPCExist_ = true;
75     return screenCaptureMonitorProxy_->SetListenerObject(object);
76 }
77 
CloseListenerObject()78 int32_t ScreenCaptureMonitorClient::CloseListenerObject()
79 {
80     listenerStubIPCExist_ = false;
81     MEDIA_LOGD("CloseListenerObject");
82     return screenCaptureMonitorProxy_->CloseListenerObject();
83 }
84 
IsScreenCaptureWorking()85 int32_t ScreenCaptureMonitorClient::IsScreenCaptureWorking()
86 {
87     std::lock_guard<std::mutex> lock(mutex_);
88     CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorProxy_ != nullptr, MSERR_NO_MEMORY,
89         "ScreenCaptureMonitor service does not exist.");
90     return screenCaptureMonitorProxy_->IsScreenCaptureWorking();
91 }
92 
RegisterScreenCaptureMonitorListener(sptr<ScreenCaptureMonitor::ScreenCaptureMonitorListener> listener)93 void ScreenCaptureMonitorClient::RegisterScreenCaptureMonitorListener(
94     sptr<ScreenCaptureMonitor::ScreenCaptureMonitorListener> listener)
95 {
96     std::lock_guard<std::mutex> lock(mutex_);
97     if (!listenerStubIPCExist_) {
98         CreateListenerObject();
99     }
100     CHECK_AND_RETURN_LOG(listener != nullptr, "input param listener is nullptr.");
101     CHECK_AND_RETURN_LOG(listenerStub_ != nullptr, "listenerStub_ is nullptr.");
102     MEDIA_LOGD("RegisterScreenCaptureMonitorListener");
103     screenCaptureMonitorClientCallbacks_.insert(listener);
104     listenerStub_->RegisterScreenCaptureMonitorListener(listener);
105 }
106 
UnregisterScreenCaptureMonitorListener(sptr<ScreenCaptureMonitor::ScreenCaptureMonitorListener> listener)107 void ScreenCaptureMonitorClient::UnregisterScreenCaptureMonitorListener(
108     sptr<ScreenCaptureMonitor::ScreenCaptureMonitorListener> listener)
109 {
110     std::lock_guard<std::mutex> lock(mutex_);
111     CHECK_AND_RETURN_LOG(listener != nullptr, "input param listener is nullptr.");
112     CHECK_AND_RETURN_LOG(listenerStub_ != nullptr, "listenerStub_ is nullptr.");
113     MEDIA_LOGD("UnregisterScreenCaptureMonitorListener");
114     listenerStub_->UnregisterScreenCaptureMonitorListener(listener);
115     screenCaptureMonitorClientCallbacks_.erase(listener);
116     if (listenerStubIPCExist_ && screenCaptureMonitorClientCallbacks_.size() == 0) {
117         MEDIA_LOGD("No listener left,CloseListenerObject");
118         CloseListenerObject();
119     }
120 }
121 } // namespace Media
122 } // namespace OHOS