0

Implement PrintSettings::NewInvalidCookie() and use it where applicable

Currently, printing code use PrintSettings::NewCookie() to generate new
valid document cookies. When printing code need an invalid cookie, they
often use 0 as the magical invalid value. Instead of doing this, add
PrintSettings::NewInvalidCookie() and use it where applicable, so
printing code has a standard way to setting an invalid cookie that is
also easier to understand.

This is prep work for changing the cookie from an int to a
base::UnguessableToken.

Bug: 1286556
Change-Id: Ie33c8337a41dadcf597dfd9c12f7c6bd9958cf44
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4658496
Code-Coverage: Findit <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Alan Screen <awscreen@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1164832}
This commit is contained in:
Lei Zhang
2023-06-30 19:11:52 +00:00
committed by Chromium LUCI CQ
parent 5caf5c22bf
commit eb6768213e
7 changed files with 22 additions and 9 deletions

@ -433,7 +433,7 @@ void PrintViewManagerBase::GetDefaultPrintSettingsReply(
set_cookie(params->document_cookie);
std::move(callback).Run(std::move(params));
} else {
set_cookie(0);
set_cookie(PrintSettings::NewInvalidCookie());
std::move(callback).Run(nullptr);
}
}
@ -460,7 +460,7 @@ void PrintViewManagerBase::ScriptedPrintReply(
set_cookie(params->params->document_cookie);
std::move(callback).Run(std::move(params));
} else {
set_cookie(0);
set_cookie(PrintSettings::NewInvalidCookie());
std::move(callback).Run(nullptr);
}
}
@ -619,7 +619,8 @@ void PrintViewManagerBase::GetDefaultPrintSettings(
auto callback_wrapper =
base::BindOnce(&PrintViewManagerBase::GetDefaultPrintSettingsReply,
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
std::unique_ptr<PrinterQuery> printer_query = queue_->PopPrinterQuery(0);
std::unique_ptr<PrinterQuery> printer_query =
queue_->PopPrinterQuery(PrintSettings::NewInvalidCookie());
if (!printer_query) {
printer_query =
queue_->CreatePrinterQuery(render_frame_host->GetGlobalId());
@ -1212,7 +1213,7 @@ void PrintViewManagerBase::ReleasePrinterQuery() {
if (!current_cookie)
return;
set_cookie(0);
set_cookie(PrintSettings::NewInvalidCookie());
PrintJobManager* print_job_manager = g_browser_process->print_job_manager();
// May be NULL in tests.

@ -143,7 +143,7 @@ void PrinterQuery::GetSettingsDone(base::OnceClosure callback,
cookie_ = PrintSettings::NewCookie();
} else {
// Failure.
cookie_ = 0;
cookie_ = PrintSettings::NewInvalidCookie();
}
std::move(callback).Run();

@ -20,6 +20,7 @@
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/service_process_host.h"
#include "printing/common/metafile_utils.h"
#include "printing/print_settings.h"
#include "printing/printing_utils.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
@ -362,7 +363,7 @@ mojom::PrintCompositor* PrintCompositeClient::CreateCompositeRequest(
void PrintCompositeClient::RemoveCompositeRequest(int cookie) {
DCHECK_EQ(document_cookie_, cookie);
compositor_.reset();
document_cookie_ = 0;
document_cookie_ = PrintSettings::NewInvalidCookie();
initiator_frame_ = nullptr;
// Reset state of the client.

@ -54,7 +54,7 @@ void RenderParamsFromPrintSettings(const PrintSettings& settings,
params->rasterize_pdf = settings.rasterize_pdf();
params->rasterize_pdf_dpi = settings.rasterize_pdf_dpi();
// Always use an invalid cookie.
params->document_cookie = 0;
params->document_cookie = PrintSettings::NewInvalidCookie();
params->selection_only = settings.selection_only();
params->supports_alpha_blend = settings.supports_alpha_blend();
params->should_print_backgrounds = settings.should_print_backgrounds();

@ -271,8 +271,9 @@ bool MockPrinter::SaveBitmap(unsigned int page,
void MockPrinter::CreateDocumentCookie() {
EXPECT_FALSE(document_cookie_.has_value());
document_cookie_ =
use_invalid_settings_ ? 0 : printing::PrintSettings::NewCookie();
document_cookie_ = use_invalid_settings_
? printing::PrintSettings::NewInvalidCookie()
: printing::PrintSettings::NewCookie();
}
void MockPrinter::SetPrintParams(printing::mojom::PrintParams* params) {

@ -535,11 +535,17 @@ void PrintSettings::SetCustomMargins(
margin_type_ = mojom::MarginType::kCustomMargins;
}
// static
int PrintSettings::NewCookie() {
// A cookie of 0 is used to mark a document as unassigned, count from 1.
return cookie_seq.GetNext() + 1;
}
// static
int PrintSettings::NewInvalidCookie() {
return 0;
}
void PrintSettings::SetOrientation(bool landscape) {
if (landscape_ != landscape) {
landscape_ = landscape;

@ -297,6 +297,10 @@ class COMPONENT_EXPORT(PRINTING) PrintSettings {
// is correctly associated with its corresponding `PrintedDocument`.
static int NewCookie();
// Creates an invalid cookie for use in situations where the cookie needs to
// be marked as invalid.
static int NewInvalidCookie();
private:
#if BUILDFLAG(IS_MAC)
static constexpr int kMacDeviceUnitsPerInch = 72;