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 }
30
31 namespace OHOS {
32 namespace Media {
Create()33 sptr<ScreenCaptureMonitorServiceStub> ScreenCaptureMonitorServiceStub::Create()
34 {
35 sptr<ScreenCaptureMonitorServiceStub> screenCaptureMonitorStub =
36 new(std::nothrow) ScreenCaptureMonitorServiceStub();
37 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorStub != nullptr, nullptr,
38 "failed to new ScreenCaptureMonitorServiceStub");
39
40 int32_t ret = screenCaptureMonitorStub->Init();
41 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to screen capture monitor stub init");
42 StatisticEventWriteBundleName("create", "ScreenCaptureMonitorServiceStub");
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
67 return MSERR_OK;
68 }
69
DestroyStub()70 int32_t ScreenCaptureMonitorServiceStub::DestroyStub()
71 {
72 CloseListenerObject();
73 MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::SCREEN_CAPTURE_MONITOR, AsObject());
74 return MSERR_OK;
75 }
76
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)77 int ScreenCaptureMonitorServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
78 MessageOption &option)
79 {
80 MEDIA_LOGI("Stub: OnRemoteRequest of code: %{public}d is received", code);
81 auto remoteDescriptor = data.ReadInterfaceToken();
82 CHECK_AND_RETURN_RET_LOG(ScreenCaptureMonitorServiceStub::GetDescriptor() == remoteDescriptor,
83 MSERR_INVALID_OPERATION, "Invalid descriptor");
84 auto itFunc = screenCaptureMonitorStubFuncs_.find(code);
85 if (itFunc != screenCaptureMonitorStubFuncs_.end()) {
86 auto memberFunc = itFunc->second;
87 if (memberFunc != nullptr) {
88 std::lock_guard<std::mutex> lock(mutex_);
89 int32_t ret = (this->*memberFunc)(data, reply);
90 if (ret != MSERR_OK) {
91 MEDIA_LOGE("calling memberFunc is failed.");
92 }
93 return MSERR_OK;
94 }
95 }
96 MEDIA_LOGW("ScreenCaptureMonitorServiceStub: no member func supporting, applying default process");
97 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
98 }
99
SetListenerObject(const sptr<IRemoteObject> & object)100 int32_t ScreenCaptureMonitorServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
101 {
102 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set listener object is nullptr");
103 sptr<IStandardScreenCaptureMonitorListener> listener = iface_cast<IStandardScreenCaptureMonitorListener>(object);
104 CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_NO_MEMORY,
105 "failed to convert IStandardScreenCaptureMonitorListener");
106 sptr<ScreenCaptureMonitor::ScreenCaptureMonitorListener> callback =
107 new ScreenCaptureMonitorListenerCallback(listener);
108 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY,
109 "failed to new ScreenCaptureMonitorListenerCallback");
110 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr, MSERR_NO_MEMORY,
111 "screen capture monitor server is nullptr");
112 screenCaptureMonitorCallback_ = callback;
113 (void)screenCaptureMonitorServer_->SetScreenCaptureMonitorCallback(callback);
114 return MSERR_OK;
115 }
116
CloseListenerObject()117 int32_t ScreenCaptureMonitorServiceStub::CloseListenerObject()
118 {
119 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorCallback_ != nullptr, MSERR_OK,
120 "screenCaptureMonitorCallback_ is nullptr");
121 (void)screenCaptureMonitorServer_->RemoveScreenCaptureMonitorCallback(screenCaptureMonitorCallback_);
122 screenCaptureMonitorCallback_ = nullptr;
123 return MSERR_OK;
124 }
125
IsScreenCaptureWorking()126 int32_t ScreenCaptureMonitorServiceStub::IsScreenCaptureWorking()
127 {
128 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorServer_ != nullptr, MSERR_NO_MEMORY,
129 "screen capture monitor server is nullptr");
130 return screenCaptureMonitorServer_->IsScreenCaptureWorking();
131 }
132
SetListenerObject(MessageParcel & data,MessageParcel & reply)133 int32_t ScreenCaptureMonitorServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
134 {
135 sptr<IRemoteObject> object = data.ReadRemoteObject();
136 reply.WriteInt32(SetListenerObject(object));
137 return MSERR_OK;
138 }
139
CloseListenerObject(MessageParcel & data,MessageParcel & reply)140 int32_t ScreenCaptureMonitorServiceStub::CloseListenerObject(MessageParcel &data, MessageParcel &reply)
141 {
142 (void)data;
143 reply.WriteInt32(CloseListenerObject());
144 return MSERR_OK;
145 }
146
IsScreenCaptureWorking(MessageParcel & data,MessageParcel & reply)147 int32_t ScreenCaptureMonitorServiceStub::IsScreenCaptureWorking(MessageParcel &data, MessageParcel &reply)
148 {
149 (void)data;
150 reply.WriteInt32(IsScreenCaptureWorking());
151 return MSERR_OK;
152 }
153
DestroyStub(MessageParcel & data,MessageParcel & reply)154 int32_t ScreenCaptureMonitorServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
155 {
156 (void)data;
157 reply.WriteInt32(DestroyStub());
158 return MSERR_OK;
159 }
160 } // namespace Media
161 } // namespace OHOS