Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include "MediaKeysCapabilitiesIpc.h"
21 : #include "RialtoClientLogging.h"
22 :
23 : namespace firebolt::rialto::client
24 : {
25 : std::weak_ptr<IMediaKeysCapabilities> MediaKeysCapabilitiesIpcFactory::m_mediaKeysCapabilitiesIpc;
26 : std::mutex MediaKeysCapabilitiesIpcFactory::m_creationMutex;
27 :
28 1 : std::shared_ptr<IMediaKeysCapabilitiesIpcFactory> IMediaKeysCapabilitiesIpcFactory::createFactory()
29 : {
30 1 : std::shared_ptr<IMediaKeysCapabilitiesIpcFactory> factory;
31 :
32 : try
33 : {
34 1 : factory = std::make_shared<MediaKeysCapabilitiesIpcFactory>();
35 : }
36 0 : catch (const std::exception &e)
37 : {
38 0 : RIALTO_CLIENT_LOG_ERROR("Failed to create the media keys capabilities ipc factory, reason: %s", e.what());
39 : }
40 :
41 1 : return factory;
42 : }
43 :
44 1 : std::shared_ptr<IMediaKeysCapabilities> MediaKeysCapabilitiesIpcFactory::getMediaKeysCapabilitiesIpc() const
45 : {
46 1 : std::lock_guard<std::mutex> lock{m_creationMutex};
47 :
48 : std::shared_ptr<IMediaKeysCapabilities> mediaKeysCapabilitiesIpc =
49 1 : MediaKeysCapabilitiesIpcFactory::m_mediaKeysCapabilitiesIpc.lock();
50 :
51 1 : if (!mediaKeysCapabilitiesIpc)
52 : {
53 : try
54 : {
55 : mediaKeysCapabilitiesIpc =
56 1 : std::make_shared<client::MediaKeysCapabilitiesIpc>(IIpcClientAccessor::instance().getIpcClient());
57 : }
58 1 : catch (const std::exception &e)
59 : {
60 1 : RIALTO_CLIENT_LOG_ERROR("Failed to create the media keys capabilities ipc, reason: %s", e.what());
61 : }
62 :
63 1 : MediaKeysCapabilitiesIpcFactory::m_mediaKeysCapabilitiesIpc = mediaKeysCapabilitiesIpc;
64 : }
65 :
66 2 : return mediaKeysCapabilitiesIpc;
67 1 : }
68 :
69 21 : MediaKeysCapabilitiesIpc::MediaKeysCapabilitiesIpc(IIpcClient &ipcClient) : IpcModule(ipcClient)
70 : {
71 21 : RIALTO_CLIENT_LOG_DEBUG("entry:");
72 :
73 21 : if (!attachChannel())
74 : {
75 2 : throw std::runtime_error("Failed attach to the ipc channel");
76 : }
77 25 : }
78 :
79 38 : MediaKeysCapabilitiesIpc::~MediaKeysCapabilitiesIpc()
80 : {
81 19 : RIALTO_CLIENT_LOG_DEBUG("entry:");
82 :
83 19 : detachChannel();
84 38 : }
85 :
86 23 : bool MediaKeysCapabilitiesIpc::createRpcStubs(const std::shared_ptr<ipc::IChannel> &ipcChannel)
87 : {
88 : m_mediaKeysCapabilitiesStub =
89 23 : std::make_unique<::firebolt::rialto::MediaKeysCapabilitiesModule_Stub>(ipcChannel.get());
90 23 : if (!m_mediaKeysCapabilitiesStub)
91 : {
92 0 : return false;
93 : }
94 23 : return true;
95 : }
96 :
97 4 : std::vector<std::string> MediaKeysCapabilitiesIpc::getSupportedKeySystems()
98 : {
99 4 : if (!reattachChannelIfRequired())
100 : {
101 1 : RIALTO_CLIENT_LOG_ERROR("Reattachment of the ipc channel failed, ipc disconnected");
102 1 : return {};
103 : }
104 :
105 3 : firebolt::rialto::GetSupportedKeySystemsRequest request;
106 3 : firebolt::rialto::GetSupportedKeySystemsResponse response;
107 3 : auto ipcController = m_ipc.createRpcController();
108 3 : auto blockingClosure = m_ipc.createBlockingClosure();
109 3 : m_mediaKeysCapabilitiesStub->getSupportedKeySystems(ipcController.get(), &request, &response, blockingClosure.get());
110 :
111 : // wait for the call to complete
112 3 : blockingClosure->wait();
113 :
114 : // check the result
115 3 : if (ipcController->Failed())
116 : {
117 1 : RIALTO_CLIENT_LOG_ERROR("failed to get supported key systems due to '%s'", ipcController->ErrorText().c_str());
118 1 : return {};
119 : }
120 :
121 6 : return std::vector<std::string>{response.key_systems().begin(), response.key_systems().end()};
122 3 : }
123 :
124 5 : bool MediaKeysCapabilitiesIpc::supportsKeySystem(const std::string &keySystem)
125 : {
126 5 : if (!reattachChannelIfRequired())
127 : {
128 1 : RIALTO_CLIENT_LOG_ERROR("Reattachment of the ipc channel failed, ipc disconnected");
129 1 : return false;
130 : }
131 :
132 4 : firebolt::rialto::SupportsKeySystemRequest request;
133 : request.set_key_system(keySystem);
134 :
135 4 : firebolt::rialto::SupportsKeySystemResponse response;
136 4 : auto ipcController = m_ipc.createRpcController();
137 4 : auto blockingClosure = m_ipc.createBlockingClosure();
138 4 : m_mediaKeysCapabilitiesStub->supportsKeySystem(ipcController.get(), &request, &response, blockingClosure.get());
139 :
140 : // wait for the call to complete
141 4 : blockingClosure->wait();
142 :
143 : // check the result
144 4 : if (ipcController->Failed())
145 : {
146 1 : RIALTO_CLIENT_LOG_ERROR("failed to supports key system due to '%s'", ipcController->ErrorText().c_str());
147 1 : return false;
148 : }
149 :
150 3 : return response.is_supported();
151 4 : }
152 :
153 4 : bool MediaKeysCapabilitiesIpc::getSupportedKeySystemVersion(const std::string &keySystem, std::string &version)
154 : {
155 4 : if (!reattachChannelIfRequired())
156 : {
157 1 : RIALTO_CLIENT_LOG_ERROR("Reattachment of the ipc channel failed, ipc disconnected");
158 1 : return false;
159 : }
160 :
161 3 : firebolt::rialto::GetSupportedKeySystemVersionRequest request;
162 : request.set_key_system(keySystem);
163 :
164 3 : firebolt::rialto::GetSupportedKeySystemVersionResponse response;
165 3 : auto ipcController = m_ipc.createRpcController();
166 3 : auto blockingClosure = m_ipc.createBlockingClosure();
167 6 : m_mediaKeysCapabilitiesStub->getSupportedKeySystemVersion(ipcController.get(), &request, &response,
168 3 : blockingClosure.get());
169 :
170 : // wait for the call to complete
171 3 : blockingClosure->wait();
172 :
173 : // check the result
174 3 : if (ipcController->Failed())
175 : {
176 1 : RIALTO_CLIENT_LOG_ERROR("failed to get supported key system version due to '%s'",
177 : ipcController->ErrorText().c_str());
178 1 : return false;
179 : }
180 2 : version = response.version();
181 :
182 2 : return true;
183 3 : }
184 :
185 5 : bool MediaKeysCapabilitiesIpc::isServerCertificateSupported(const std::string &keySystem)
186 : {
187 5 : if (!reattachChannelIfRequired())
188 : {
189 1 : RIALTO_CLIENT_LOG_ERROR("Reattachment of the ipc channel failed, ipc disconnected");
190 1 : return false;
191 : }
192 :
193 4 : firebolt::rialto::IsServerCertificateSupportedRequest request;
194 : request.set_key_system(keySystem);
195 :
196 4 : firebolt::rialto::IsServerCertificateSupportedResponse response;
197 4 : auto ipcController = m_ipc.createRpcController();
198 4 : auto blockingClosure = m_ipc.createBlockingClosure();
199 8 : m_mediaKeysCapabilitiesStub->isServerCertificateSupported(ipcController.get(), &request, &response,
200 4 : blockingClosure.get());
201 :
202 : // wait for the call to complete
203 4 : blockingClosure->wait();
204 :
205 : // check the result
206 4 : if (ipcController->Failed())
207 : {
208 1 : RIALTO_CLIENT_LOG_ERROR("failed to check if server certificate is supported due to '%s'",
209 : ipcController->ErrorText().c_str());
210 1 : return false;
211 : }
212 :
213 3 : return response.is_supported();
214 4 : }
215 :
216 : }; // namespace firebolt::rialto::client
|