1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/display/manager/content_protection_key_manager.h"
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "ui/display/display_features.h"
#include "ui/display/manager/util/display_manager_util.h"
namespace display {
namespace {
// Length of the key as expected to come from the server.
// This is the same length that the kernel also expects.
constexpr size_t kHdcpKeySize = 285;
display::DisplaySnapshot* GetDisplayWithIdIfHdcpCapableAndKeyNeeded(
const std::vector<raw_ptr<display::DisplaySnapshot, VectorExperimental>>&
displays_states,
int64_t display_id) {
for (display::DisplaySnapshot* display : displays_states) {
if (display->display_id() == display_id) {
uint32_t protection_mask;
bool is_hdcp_capable =
GetContentProtectionMethods(display->type(), &protection_mask) &&
protection_mask & display::kContentProtectionMethodHdcpAll;
if (is_hdcp_capable && display->has_content_protection_key()) {
return display;
}
break;
}
}
return nullptr;
}
} // namespace
ContentProtectionKeyManager::ContentProtectionKeyManager() = default;
ContentProtectionKeyManager::~ContentProtectionKeyManager() = default;
void ContentProtectionKeyManager::SetKeyIfRequired(
const std::vector<raw_ptr<DisplaySnapshot, VectorExperimental>>&
displays_states,
int64_t display_id,
KeySetCallback on_key_set) {
DCHECK(!on_key_set.is_null());
// TODO(markyacoub): Remove this flag once the feature is fully launched.
if (!features::IsHdcpKeyProvisioningRequired()) {
std::move(on_key_set).Run(false);
return;
}
if (!GetDisplayWithIdIfHdcpCapableAndKeyNeeded(displays_states, display_id)) {
std::move(on_key_set).Run(false);
return;
}
pending_display_callbacks_[display_id] = std::move(on_key_set);
// If we already learnt that we need a key and already fetched the key, go
// ahead and inject it into the kernel.
if (!cached_provisioned_key_.empty()) {
InjectKeyToKernel(display_id);
return;
}
// If there are no pending displays nor we have the key, it means we haven't
// fetched the key from the server yet.
if (displays_pending_set_key_.empty()) {
displays_pending_set_key_.insert(display_id);
FetchKeyFromServer();
// If the list isn't empty, it means we're in the process of fetching the
// key from the server already. Just add the pending display to the list and
// we'll set it later when the key is fetched.
} else {
displays_pending_set_key_.insert(display_id);
}
}
void ContentProtectionKeyManager::FetchKeyFromServer() {
DCHECK(!provisioned_key_request_.is_null());
provisioned_key_request_.Run(
base::BindOnce(&ContentProtectionKeyManager::OnKeyFetchedFromServer,
weak_ptr_factory_.GetWeakPtr()));
}
void ContentProtectionKeyManager::OnKeyFetchedFromServer(
const std::string& key) {
if (key.size()) {
// This is the size of the key that we expect from the server as of now.
DCHECK_EQ(key.size(), kHdcpKeySize);
cached_provisioned_key_ = key;
for (int64_t display_id : displays_pending_set_key_) {
InjectKeyToKernel(display_id);
}
} else {
LOG(ERROR) << "Fetched an empty HDCP key from widevine server";
for (int64_t display_id : displays_pending_set_key_) {
TriggerPendingCallbacks(display_id, false);
}
}
displays_pending_set_key_.clear();
}
void ContentProtectionKeyManager::InjectKeyToKernel(int64_t display_id) {
DCHECK(native_display_delegate_);
native_display_delegate_->SetHdcpKeyProp(
display_id, cached_provisioned_key_,
base::BindOnce(&ContentProtectionKeyManager::OnKeyInjectedToKernel,
weak_ptr_factory_.GetWeakPtr(), display_id));
}
void ContentProtectionKeyManager::OnKeyInjectedToKernel(int64_t display_id,
bool success) {
LOG_IF(ERROR, !success) << "Failed to Inject the HDCP Key to Display #"
<< display_id;
TriggerPendingCallbacks(display_id, success);
}
void ContentProtectionKeyManager::TriggerPendingCallbacks(int64_t display_id,
bool is_key_set) {
CHECK(base::Contains(pending_display_callbacks_, display_id));
KeySetCallback callback = std::move(pending_display_callbacks_[display_id]);
DCHECK(!callback.is_null());
std::move(callback).Run(is_key_set);
pending_display_callbacks_.erase(display_id);
}
} // namespace display
|