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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/media/cdm_storage_manager.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/task/thread_pool.h"
#include "base/types/pass_key.h"
#include "content/browser/media/cdm_storage_common.h"
#include "content/public/common/content_features.h"
#include "media/cdm/cdm_type.h"
#include "media/mojo/mojom/cdm_storage.mojom.h"
namespace content {
namespace {
const char kUmaPrefix[] = "Media.EME.CdmStorageManager.";
const char kGetSizeForFileError[] = "GetSizeForFileError.";
const char kGetSizeForStorageKeyError[] = "GetSizeForStorageKeyError.";
const char kGetSizeForTimeFrameError[] = "GetSizeForTimeFrameError.";
const char kWriteFileError[] = "WriteFileError.";
const char kReadFileError[] = "ReadFileError.";
const char kDatabaseOpenErrorNoPeriod[] = "DatabaseOpenError";
const char kDatabaseOpenError[] = "DatabaseOpenError.";
const char kDatabaseSizeName[] = "CurrentDatabaseUsageKB.";
constexpr uint64_t kBytesPerKB = 1024;
constexpr int kMinDatabaseSizeKB = 0;
// Used for histogram reporting, the max size of the database we expect in KB.
constexpr uint64_t kMaxDatabaseSizeKB = 512000 * 10;
constexpr int kSizeKBBuckets = 1000;
// Creates a task runner suitable for running SQLite database operations.
scoped_refptr<base::SequencedTaskRunner> CreateDatabaseTaskRunner() {
// We use a SequencedTaskRunner so that there is a global ordering to a
// storage key's directory operations.
return base::ThreadPool::CreateSequencedTaskRunner({
// Needed for file I/O.
base::MayBlock(),
// Reasonable compromise, given that a few database operations are
// blocking, while most operations are not. We should be able to do better
// when we get scheduling APIs on the Web Platform.
base::TaskPriority::USER_VISIBLE,
// Needed to allow for clearing site data on shutdown.
base::TaskShutdownBehavior::BLOCK_SHUTDOWN,
});
}
} // namespace
CdmStorageManager::CdmStorageManager(const base::FilePath& path)
: path_(path), db_(CreateDatabaseTaskRunner(), path_) {}
CdmStorageManager::~CdmStorageManager() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void CdmStorageManager::Open(const std::string& file_name,
OpenCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (file_name.empty()) {
DVLOG(1) << "No file specified.";
ReportDatabaseOpenError(CdmStorageOpenError::kNoFileSpecified);
std::move(callback).Run(Status::kFailure, mojo::NullAssociatedRemote());
return;
}
if (!CdmFileImpl::IsValidName(file_name)) {
DVLOG(1) << "Invalid name of file.";
ReportDatabaseOpenError(CdmStorageOpenError::kInvalidFileName);
std::move(callback).Run(Status::kFailure, mojo::NullAssociatedRemote());
return;
}
db_.AsyncCall(&CdmStorageDatabase::EnsureOpen)
.Then(base::BindOnce(&CdmStorageManager::DidOpenFile,
weak_factory_.GetWeakPtr(),
receivers_.current_context().storage_key,
receivers_.current_context().cdm_type, file_name,
std::move(callback)));
}
void CdmStorageManager::GetUsagePerAllStorageKeys(
base::OnceCallback<void(const CdmStorageKeyUsageSize&)> callback,
base::Time begin,
base::Time end) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::GetUsagePerAllStorageKeys)
.WithArgs(begin, end)
.Then(std::move(callback));
}
void CdmStorageManager::DeleteDataForStorageKey(
const blink::StorageKey& storage_key,
base::OnceCallback<void(bool)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DeleteData(base::NullCallback(), storage_key, base::Time::Min(),
base::Time::Max(), std::move(callback));
}
void CdmStorageManager::OpenCdmStorage(
const CdmStorageBindingContext& binding_context,
mojo::PendingReceiver<media::mojom::CdmStorage> receiver) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
receivers_.Add(this, std::move(receiver), binding_context);
}
void CdmStorageManager::ReadFile(
const blink::StorageKey& storage_key,
const media::CdmType& cdm_type,
const std::string& file_name,
base::OnceCallback<void(std::optional<std::vector<uint8_t>>)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::ReadFile)
.WithArgs(storage_key, cdm_type, file_name)
.Then(base::BindOnce(&CdmStorageManager::DidReadFile,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void CdmStorageManager::WriteFile(const blink::StorageKey& storage_key,
const media::CdmType& cdm_type,
const std::string& file_name,
const std::vector<uint8_t>& data,
base::OnceCallback<void(bool)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::WriteFile)
.WithArgs(storage_key, cdm_type, file_name, data)
.Then(base::BindOnce(&CdmStorageManager::DidWriteFile,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void CdmStorageManager::GetSizeForFile(
const blink::StorageKey& storage_key,
const media::CdmType& cdm_type,
const std::string& file_name,
base::OnceCallback<void(uint64_t)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::GetSizeForFile)
.WithArgs(storage_key, cdm_type, file_name)
.Then(base::BindOnce(&CdmStorageManager::DidGetSize,
weak_factory_.GetWeakPtr(), std::move(callback),
kGetSizeForFileError));
}
void CdmStorageManager::GetSizeForStorageKey(
const blink::StorageKey& storage_key,
const base::Time begin,
const base::Time end,
base::OnceCallback<void(uint64_t)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::GetSizeForStorageKey)
.WithArgs(storage_key, begin, end)
.Then(base::BindOnce(&CdmStorageManager::DidGetSize,
weak_factory_.GetWeakPtr(), std::move(callback),
kGetSizeForStorageKeyError));
}
void CdmStorageManager::GetSizeForTimeFrame(
const base::Time begin,
const base::Time end,
base::OnceCallback<void(uint64_t)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::GetSizeForTimeFrame)
.WithArgs(begin, end)
.Then(base::BindOnce(&CdmStorageManager::DidGetSize,
weak_factory_.GetWeakPtr(), std::move(callback),
kGetSizeForTimeFrameError));
}
void CdmStorageManager::DeleteFile(const blink::StorageKey& storage_key,
const media::CdmType& cdm_type,
const std::string& file_name,
base::OnceCallback<void(bool)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::DeleteFile)
.WithArgs(storage_key, cdm_type, file_name)
.Then(std::move(callback));
}
void CdmStorageManager::DeleteData(
const StoragePartition::StorageKeyMatcherFunction& storage_key_matcher,
const blink::StorageKey& storage_key,
const base::Time begin,
const base::Time end,
base::OnceCallback<void(bool)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
db_.AsyncCall(&CdmStorageDatabase::DeleteData)
.WithArgs(storage_key_matcher, storage_key, begin, end)
.Then(std::move(callback));
}
void CdmStorageManager::OnFileReceiverDisconnect(
const std::string& name,
const media::CdmType& cdm_type,
const blink::StorageKey& storage_key,
base::PassKey<CdmFileImpl> pass_key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto count = cdm_files_.erase(CdmFileId(name, cdm_type, storage_key));
DCHECK_GT(count, 0u);
}
void CdmStorageManager::DidOpenFile(const blink::StorageKey& storage_key,
const media::CdmType& cdm_type,
const std::string& file_name,
OpenCallback callback,
CdmStorageOpenError error) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (error != CdmStorageOpenError::kOk) {
ReportDatabaseOpenError(error);
std::move(callback).Run(Status::kFailure, mojo::NullAssociatedRemote());
return;
}
// Check whether this CDM file is in-use.
CdmFileId id(file_name, cdm_type, storage_key);
if (base::Contains(cdm_files_, id)) {
std::move(callback).Run(Status::kInUse, mojo::NullAssociatedRemote());
return;
}
// File was opened successfully, so create the binding and return success.
mojo::PendingAssociatedRemote<media::mojom::CdmFile> cdm_file;
cdm_files_.emplace(id, std::make_unique<CdmFileImpl>(
this, storage_key, cdm_type, file_name,
cdm_file.InitWithNewEndpointAndPassReceiver()));
std::move(callback).Run(Status::kSuccess, std::move(cdm_file));
}
void CdmStorageManager::DidReadFile(
base::OnceCallback<void(std::optional<std::vector<uint8_t>>)> callback,
std::optional<std::vector<uint8_t>> data) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::UmaHistogramBoolean(
GetCdmStorageManagerHistogramName(kReadFileError, in_memory()),
!data.has_value());
if (!database_size_reported_) {
db_.AsyncCall(&CdmStorageDatabase::GetDatabaseSize)
.Then(base::BindOnce(&CdmStorageManager::DidGetDatabaseSize,
weak_factory_.GetWeakPtr()));
database_size_reported_ = true;
}
std::move(callback).Run(data);
}
void CdmStorageManager::DidWriteFile(base::OnceCallback<void(bool)> callback,
bool success) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::UmaHistogramBoolean(
GetCdmStorageManagerHistogramName(kWriteFileError, in_memory()),
!success);
std::move(callback).Run(success);
}
void CdmStorageManager::DidGetSize(base::OnceCallback<void(uint64_t)> callback,
const std::string& operation,
std::optional<uint64_t> size) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::UmaHistogramBoolean(
GetCdmStorageManagerHistogramName(operation, in_memory()),
!size.has_value());
std::move(callback).Run(size.value_or(0));
}
void CdmStorageManager::DidGetDatabaseSize(const uint64_t size) {
// One time report DatabaseSize.
base::UmaHistogramCustomCounts(
GetCdmStorageManagerHistogramName(kDatabaseSizeName, in_memory()),
size / kBytesPerKB, kMinDatabaseSizeKB, kMaxDatabaseSizeKB,
kSizeKBBuckets);
}
// TODO(crbug.com/40272342) Investigate if we can propagate the SQL errors.
// Investigate adding delete functionality to 'MojoCdmHelper::CloseCdmFileIO' to
// close database on CdmFileIO closure.
void CdmStorageManager::ReportDatabaseOpenError(CdmStorageOpenError error) {
// General Errors without distinguishing incognito or not.
base::UmaHistogramEnumeration(
std::string{kUmaPrefix} + std::string{kDatabaseOpenErrorNoPeriod}, error);
// Histogram split by incognito and non-incognito.
base::UmaHistogramEnumeration(
GetCdmStorageManagerHistogramName(kDatabaseOpenError, in_memory()),
error);
}
} // namespace content
|