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_service_stub.h"
17 #include <unistd.h>
18 #include "screen_capture_monitor_listener_proxy.h"
19 #include "media_server_manager.h"
20 #include "media_log.h"
21 #include "media_errors.h"
22 #include "ipc_skeleton.h"
23 #include "media_permission.h"
24 #include "accesstoken_kit.h"
25 #include "media_dfx.h"
26
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureMonitorServiceStub"};
29 constexpr int MAX_LIST_COUNT = 1000;
30 }
31
32 namespace OHOS {
33 namespace Media {
Create()34 sptr<ScreenCaptureMonitorServiceStub> ScreenCaptureMonitorServiceStub::Create()
35 {
36 sptr<ScreenCaptureMonitorServiceStub> screenCaptureMonitorStub =
37 new(std::nothrow) ScreenCaptureMonitorServiceStub();
38 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorStub != nullptr, nullptr,
39 "failed to new ScreenCaptureMonitorServiceStub");
40
41 int32_t ret = screenCaptureMonitorStub->Init();
42 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to screen capture monitor stub init");
43 return screenCaptureMonitorStub;
44 }
45
ScreenCaptureMonitorServiceStub()46 ScreenCaptureMonitorServiceStub::ScreenCaptureMonitorServiceStub()
47 {
48 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
49 }
50
~ScreenCaptureMonitorServiceStub()51 ScreenCaptureMonitorServiceStub::~ScreenCaptureMonitorServiceStub()
52 {
53 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
54 }
55
Init()56 int32_t ScreenCaptureMonitorServiceStub::Init()
57 {
58 screenCaptureMonitorServer_ = ScreenCaptureMonitorServer::GetInstance();
59 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr, MSERR_NO_MEMORY,
60 "failed to create ScreenCaptureMonitorServer");
61 screenCaptureMonitorStubFuncs_[SET_LISTENER_OBJ] = &ScreenCaptureMonitorServiceStub::SetListenerObject;
62 screenCaptureMonitorStubFuncs_[IS_SCREEN_CAPTURE_WORKING] =
63 &ScreenCaptureMonitorServiceStub::IsScreenCaptureWorking;
64 screenCaptureMonitorStubFuncs_[DESTROY] = &ScreenCaptureMonitorServiceStub::DestroyStub;
65 screenCaptureMonitorStubFuncs_[CLOSE_LISTENER_OBJ] = &ScreenCaptureMonitorServiceStub::CloseListenerObject;
66 screenCaptureMonitorStubFuncs_[IS_SYSTEM_SCREEN_RECORDER] =
67 &ScreenCaptureMonitorServiceStub::IsSystemScreenRecorder;
68 screenCaptureMonitorStubFuncs_[IS_SYSTEM_SCREEN_RECORDER_WORKING] =
69 &ScreenCaptureMonitorServiceStub::IsSystemScreenRecorderWorking;
70
71 return MSERR_OK;
72 }
73
DestroyStub()74 int32_t ScreenCaptureMonitorServiceStub::DestroyStub()
75 {
76 CloseListenerObject();
77 MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::SCREEN_CAPTURE_MONITOR, AsObject());
78 return MSERR_OK;
79 }
80
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)81 int ScreenCaptureMonitorServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
82 MessageOption &option)
83 {
84 MEDIA_LOGI("Stub: OnRemoteRequest of code: %{public}d is received", code);
85 auto remoteDescriptor = data.ReadInterfaceToken();
86 CHECK_AND_RETURN_RET_LOG(ScreenCaptureMonitorServiceStub::GetDescriptor() == remoteDescriptor,
87 MSERR_INVALID_OPERATION, "Invalid descriptor");
88 auto itFunc = screenCaptureMonitorStubFuncs_.find(code);
89 if (itFunc != screenCaptureMonitorStubFuncs_.end()) {
90 auto memberFunc = itFunc->second;
91 if (memberFunc != nullptr) {
92 std::lock_guard<std::mutex> lock(mutex_);
93 int32_t ret = (this->*memberFunc)(data, reply);
94 if (ret != MSERR_OK) {
95 MEDIA_LOGE("calling memberFunc is failed.");
96 }
97 return MSERR_OK;
98 }
99 }
100 MEDIA_LOGW("ScreenCaptureMonitorServiceStub: no member func supporting, applying default process");
101 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
102 }
103
SetListenerObject(const sptr<IRemoteObject> & object)104 int32_t ScreenCaptureMonitorServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
105 {
106 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set listener object is nullptr");
107 sptr<IStandardScreenCaptureMonitorListener> listener = iface_cast<IStandardScreenCaptureMonitorListener>(object);
108 CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_NO_MEMORY,
109 "failed to convert IStandardScreenCaptureMonitorListener");
110 sptr<ScreenCaptureMonitor::ScreenCaptureMonitorListener> callback =
111 new ScreenCaptureMonitorListenerCallback(listener);
112 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY,
113 "failed to new ScreenCaptureMonitorListenerCallback");
114 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr, MSERR_NO_MEMORY,
115 "screen capture monitor server is nullptr");
116 screenCaptureMonitorCallback_ = callback;
117 (void)screenCaptureMonitorServer_->SetScreenCaptureMonitorCallback(callback);
118 return MSERR_OK;
119 }
120
CloseListenerObject()121 int32_t ScreenCaptureMonitorServiceStub::CloseListenerObject()
122 {
123 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr, MSERR_OK,
124 "screenCaptureMonitorServer is nullptr");
125 (void)screenCaptureMonitorServer_->RemoveScreenCaptureMonitorCallback(screenCaptureMonitorCallback_);
126 screenCaptureMonitorCallback_ = nullptr;
127 return MSERR_OK;
128 }
129
IsScreenCaptureWorking()130 std::list<int32_t> ScreenCaptureMonitorServiceStub::IsScreenCaptureWorking()
131 {
132 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr, {}, "screen capture monitor server is nullptr");
133 return screenCaptureMonitorServer_->IsScreenCaptureWorking();
134 }
135
IsSystemScreenRecorder(int32_t pid)136 bool ScreenCaptureMonitorServiceStub::IsSystemScreenRecorder(int32_t pid)
137 {
138 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr,
139 false, "screen capture monitor server is nullptr");
140 return screenCaptureMonitorServer_->IsSystemScreenRecorder(pid);
141 }
142
IsSystemScreenRecorderWorking()143 bool ScreenCaptureMonitorServiceStub::IsSystemScreenRecorderWorking()
144 {
145 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr,
146 false, "screen capture monitor server is nullptr");
147 return screenCaptureMonitorServer_->IsSystemScreenRecorderWorking();
148 }
149
SetListenerObject(MessageParcel & data,MessageParcel & reply)150 int32_t ScreenCaptureMonitorServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
151 {
152 sptr<IRemoteObject> object = data.ReadRemoteObject();
153 reply.WriteInt32(SetListenerObject(object));
154 return MSERR_OK;
155 }
156
CloseListenerObject(MessageParcel & data,MessageParcel & reply)157 int32_t ScreenCaptureMonitorServiceStub::CloseListenerObject(MessageParcel &data, MessageParcel &reply)
158 {
159 (void)data;
160 reply.WriteInt32(CloseListenerObject());
161 return MSERR_OK;
162 }
163
IsScreenCaptureWorking(MessageParcel & data,MessageParcel & reply)164 int32_t ScreenCaptureMonitorServiceStub::IsScreenCaptureWorking(MessageParcel &data, MessageParcel &reply)
165 {
166 (void)data;
167 std::list<int32_t> pidList = IsScreenCaptureWorking();
168 int32_t size = static_cast<int32_t>(pidList.size());
169 CHECK_AND_RETURN_RET_LOG(size < MAX_LIST_COUNT, MSERR_INVALID_STATE, "content filter size exceed max range");
170 reply.WriteInt32(size);
171
172 MEDIA_LOGD("ScreenCaptureMonitorServiceStub::IsScreenCaptureWorking pid start.");
173 for (auto pid: pidList) {
174 MEDIA_LOGD("pid: %{public}d", pid);
175 reply.WriteInt32(pid);
176 }
177 MEDIA_LOGD("ScreenCaptureMonitorServiceStub::IsScreenCaptureWorking pid end.");
178 return MSERR_OK;
179 }
180
DestroyStub(MessageParcel & data,MessageParcel & reply)181 int32_t ScreenCaptureMonitorServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
182 {
183 (void)data;
184 reply.WriteInt32(DestroyStub());
185 return MSERR_OK;
186 }
187
IsSystemScreenRecorder(MessageParcel & data,MessageParcel & reply)188 int32_t ScreenCaptureMonitorServiceStub::IsSystemScreenRecorder(MessageParcel &data, MessageParcel &reply)
189 {
190 MEDIA_LOGD("ScreenCaptureMonitorServiceStub::IsSystemScreenRecorder pid start.");
191 int32_t pid = data.ReadInt32();
192
193 bool isSystem = IsSystemScreenRecorder(pid);
194 reply.WriteBool(isSystem);
195 MEDIA_LOGD("ScreenCaptureMonitorServiceStub::IsSystemScreenRecorder pid end.");
196 return MSERR_OK;
197 }
198
IsSystemScreenRecorderWorking(MessageParcel & data,MessageParcel & reply)199 int32_t ScreenCaptureMonitorServiceStub::IsSystemScreenRecorderWorking(MessageParcel &data, MessageParcel &reply)
200 {
201 MEDIA_LOGD("ScreenCaptureMonitorServiceStub::IsSystemScreenRecorder pid start.");
202 (void)data;
203
204 bool isSystemWorking = IsSystemScreenRecorderWorking();
205 reply.WriteBool(isSystemWorking);
206 MEDIA_LOGD("ScreenCaptureMonitorServiceStub::IsSystemScreenRecorder pid end.");
207 return MSERR_OK;
208 }
209 } // namespace Media
210 } // namespace OHOS