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_controller_stub.h"
17 #include "media_server_manager.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "avsharedmemory_ipc.h"
21 #include "media_utils.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureControllerStub"};
25 }
26
27 namespace OHOS {
28 namespace Media {
29
Create()30 sptr<ScreenCaptureControllerStub> ScreenCaptureControllerStub::Create()
31 {
32 MEDIA_LOGI("ScreenCaptureControllerStub::Create() start");
33 sptr<ScreenCaptureControllerStub> screenCaptureControllerStub = new(std::nothrow) ScreenCaptureControllerStub();
34 CHECK_AND_RETURN_RET_LOG(screenCaptureControllerStub != nullptr, nullptr,
35 "failed to new ScreenCaptureControllerStub");
36
37 int32_t ret = screenCaptureControllerStub->Init();
38 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to screenCapture controller stub init");
39 return screenCaptureControllerStub;
40 }
41
ScreenCaptureControllerStub()42 ScreenCaptureControllerStub::ScreenCaptureControllerStub()
43 {
44 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
45 }
46
~ScreenCaptureControllerStub()47 ScreenCaptureControllerStub::~ScreenCaptureControllerStub()
48 {
49 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
50 }
51
Init()52 int32_t ScreenCaptureControllerStub::Init()
53 {
54 MEDIA_LOGI("ScreenCaptureControllerStub::Init() start");
55 screenCaptureControllerServer_ = ScreenCaptureControllerServer::Create();
56 CHECK_AND_RETURN_RET_LOG(screenCaptureControllerServer_ != nullptr, MSERR_NO_MEMORY,
57 "failed to create screenCaptureControllerServer Service");
58 screenCaptureControllerStubFuncs_[REPORT_USER_CHOICE] =
59 &ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice;
60 screenCaptureControllerStubFuncs_[DESTROY] = &ScreenCaptureControllerStub::DestroyStub;
61
62 return MSERR_OK;
63 }
64
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)65 int ScreenCaptureControllerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
66 MessageOption &option)
67 {
68 MEDIA_LOGD("ScreenCaptureControllerStub: OnRemoteRequest of code: %{public}u is received", code);
69
70 auto remoteDescriptor = data.ReadInterfaceToken();
71 if (ScreenCaptureControllerStub::GetDescriptor() != remoteDescriptor) {
72 MEDIA_LOGE("Invalid descriptor");
73 return MSERR_INVALID_OPERATION;
74 }
75
76 auto itFunc = screenCaptureControllerStubFuncs_.find(code);
77 if (itFunc != screenCaptureControllerStubFuncs_.end()) {
78 auto memberFunc = itFunc->second;
79 if (memberFunc != nullptr) {
80 int32_t ret = (this->*memberFunc)(data, reply);
81 if (ret != MSERR_OK) {
82 MEDIA_LOGE("Calling memberFunc is failed.");
83 }
84 return MSERR_OK;
85 }
86 }
87 MEDIA_LOGW("ScreenCaptureControllerStub: no member func supporting, applying default process");
88
89 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
90 }
91
DestroyStub()92 int32_t ScreenCaptureControllerStub::DestroyStub()
93 {
94 screenCaptureControllerServer_ = nullptr;
95 MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::SCREEN_CAPTURE_CONTROLLER, AsObject());
96 return MSERR_OK;
97 }
98
DestroyStub(MessageParcel & data,MessageParcel & reply)99 int32_t ScreenCaptureControllerStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
100 {
101 (void)data;
102 reply.WriteInt32(DestroyStub());
103 return MSERR_OK;
104 }
105
ReportAVScreenCaptureUserChoice(int32_t sessionId,std::string choice)106 int32_t ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice(int32_t sessionId, std::string choice)
107 {
108 MEDIA_LOGI("ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice start 2");
109 CHECK_AND_RETURN_RET_LOG(screenCaptureControllerServer_ != nullptr, false,
110 "screen capture controller server is nullptr");
111 int32_t appUid = IPCSkeleton::GetCallingUid();
112 std::string appName = GetClientBundleName(appUid);
113 if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
114 .compare(appName) != 0) {
115 MEDIA_LOGE("ScreenCaptureControllerStub called by app name %{public}s", appName.c_str());
116 return MSERR_INVALID_OPERATION;
117 }
118 return screenCaptureControllerServer_->ReportAVScreenCaptureUserChoice(sessionId, choice);
119 }
120
ReportAVScreenCaptureUserChoice(MessageParcel & data,MessageParcel & reply)121 int32_t ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice(MessageParcel &data, MessageParcel &reply)
122 {
123 MEDIA_LOGI("ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice start 1");
124 CHECK_AND_RETURN_RET_LOG(screenCaptureControllerServer_ != nullptr, MSERR_INVALID_STATE,
125 "screen capture controller server is nullptr");
126 (void)data;
127 int32_t sessionId = data.ReadInt32();
128 std::string choice = data.ReadString();
129 int32_t ret = ReportAVScreenCaptureUserChoice(sessionId, choice);
130 reply.WriteInt32(ret);
131 return MSERR_OK;
132 }
133
134
135 } // namespace Media
136 } // namespace OHOS