• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_[GET_CONFIG_PARAM] =
61         &ScreenCaptureControllerStub::GetAVScreenCaptureConfigurableParameters;
62     screenCaptureControllerStubFuncs_[DESTROY] = &ScreenCaptureControllerStub::DestroyStub;
63 
64     return MSERR_OK;
65 }
66 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int ScreenCaptureControllerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
68     MessageOption &option)
69 {
70     MEDIA_LOGD("ScreenCaptureControllerStub: OnRemoteRequest of code: %{public}u is received", code);
71 
72     auto remoteDescriptor = data.ReadInterfaceToken();
73     if (ScreenCaptureControllerStub::GetDescriptor() != remoteDescriptor) {
74         MEDIA_LOGE("Invalid descriptor");
75         return MSERR_INVALID_OPERATION;
76     }
77 
78     auto itFunc = screenCaptureControllerStubFuncs_.find(code);
79     if (itFunc != screenCaptureControllerStubFuncs_.end()) {
80         auto memberFunc = itFunc->second;
81         if (memberFunc != nullptr) {
82             int32_t ret = (this->*memberFunc)(data, reply);
83             if (ret != MSERR_OK) {
84                 MEDIA_LOGE("Calling memberFunc is failed.");
85             }
86             return MSERR_OK;
87         }
88     }
89     MEDIA_LOGW("ScreenCaptureControllerStub: no member func supporting, applying default process");
90 
91     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
92 }
93 
DestroyStub()94 int32_t ScreenCaptureControllerStub::DestroyStub()
95 {
96     screenCaptureControllerServer_ = nullptr;
97     MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::SCREEN_CAPTURE_CONTROLLER, AsObject());
98     return MSERR_OK;
99 }
100 
DestroyStub(MessageParcel & data,MessageParcel & reply)101 int32_t ScreenCaptureControllerStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
102 {
103     (void)data;
104     reply.WriteInt32(DestroyStub());
105     return MSERR_OK;
106 }
107 
ReportAVScreenCaptureUserChoice(int32_t sessionId,std::string choice)108 int32_t ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice(int32_t sessionId, std::string choice)
109 {
110     MEDIA_LOGI("ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice start 2");
111     CHECK_AND_RETURN_RET_LOG(screenCaptureControllerServer_ != nullptr, false,
112         "screen capture controller server is nullptr");
113     int32_t appUid = IPCSkeleton::GetCallingUid();
114     std::string appName = GetClientBundleName(appUid);
115     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
116             .compare(appName) != 0) {
117         MEDIA_LOGE("ScreenCaptureControllerStub called by app name %{public}s", appName.c_str());
118         return MSERR_INVALID_OPERATION;
119     }
120     return screenCaptureControllerServer_->ReportAVScreenCaptureUserChoice(sessionId, choice);
121 }
122 
ReportAVScreenCaptureUserChoice(MessageParcel & data,MessageParcel & reply)123 int32_t ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice(MessageParcel &data, MessageParcel &reply)
124 {
125     MEDIA_LOGI("ScreenCaptureControllerStub::ReportAVScreenCaptureUserChoice start 1");
126     CHECK_AND_RETURN_RET_LOG(screenCaptureControllerServer_ != nullptr, MSERR_INVALID_STATE,
127         "screen capture controller server is nullptr");
128     (void)data;
129     int32_t sessionId = data.ReadInt32();
130     std::string choice = data.ReadString();
131     int32_t ret = ReportAVScreenCaptureUserChoice(sessionId, choice);
132     reply.WriteInt32(ret);
133     return MSERR_OK;
134 }
135 
GetAVScreenCaptureConfigurableParameters(int32_t sessionId,std::string & resultStr)136 int32_t ScreenCaptureControllerStub::GetAVScreenCaptureConfigurableParameters(int32_t sessionId, std::string &resultStr)
137 {
138     MEDIA_LOGI("ScreenCaptureControllerStub::GetAVScreenCaptureConfigurableParameters start");
139     CHECK_AND_RETURN_RET_LOG(screenCaptureControllerServer_ != nullptr, false,
140         "screen capture controller server is nullptr");
141     int32_t appUid = IPCSkeleton::GetCallingUid();
142     std::string appName = GetClientBundleName(appUid);
143     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
144             .compare(appName) != 0) {
145         MEDIA_LOGE("ScreenCaptureControllerStub called by app name %{public}s", appName.c_str());
146         return MSERR_INVALID_OPERATION;
147     }
148     return screenCaptureControllerServer_->GetAVScreenCaptureConfigurableParameters(sessionId, resultStr);
149 }
150 
GetAVScreenCaptureConfigurableParameters(MessageParcel & data,MessageParcel & reply)151 int32_t ScreenCaptureControllerStub::GetAVScreenCaptureConfigurableParameters(MessageParcel &data, MessageParcel &reply)
152 {
153     MEDIA_LOGI("ScreenCaptureControllerStub::GetAVScreenCaptureConfigurableParameters start");
154     CHECK_AND_RETURN_RET_LOG(screenCaptureControllerServer_ != nullptr, MSERR_INVALID_STATE,
155         "screen capture controller server is nullptr");
156     (void)data;
157     int32_t sessionId = data.ReadInt32();
158     std::string resultStr;
159     int32_t ret = GetAVScreenCaptureConfigurableParameters(sessionId, resultStr);
160     MEDIA_LOGI("ScreenCaptureControllerStub::GetAVScreenCaptureConfigurableParameters start resultStr: %{public}s",
161         resultStr.c_str());
162     reply.WriteInt32(ret);
163     reply.WriteString(resultStr);
164     return MSERR_OK;
165 }
166 
167 
168 } // namespace Media
169 } // namespace OHOS