Skip to content

Commit

Permalink
Print Preview: Simplify DestinationMatch
Browse files Browse the repository at this point in the history
Simplify DestinationMatch - now that cloud printing is gone, all
origins are always passed in the constructor and skipVirtualDestinations
was always set to true.

Also remove various type/origin conversion functions and replace with
adding a type_ field and getter in the Destination class.

Bug: 1162164
Change-Id: I25bac3f3ba4f72af77123041e779bcd84b7ed9eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3584723
Reviewed-by: Demetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/main@{#992331}
  • Loading branch information
Rebekah Potter authored and Chromium LUCI CQ committed Apr 14, 2022
1 parent 0387a9a commit d7df575
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 139 deletions.
35 changes: 35 additions & 0 deletions chrome/browser/resources/print_preview/data/destination.ts
Expand Up @@ -33,6 +33,18 @@ export enum DestinationOrigin {
CROS = 'chrome_os',
}

/**
* Printer types for capabilities and printer list requests.
* Must match PrinterType in printing/mojom/print.mojom
*/
export enum PrinterType {
PRIVET_PRINTER_DEPRECATED = 0,
EXTENSION_PRINTER = 1,
PDF_PRINTER = 2,
LOCAL_PRINTER = 3,
CLOUD_PRINTER_DEPRECATED = 4
}

// <if expr="chromeos_ash or chromeos_lacros">
/**
* Enumeration specifying whether a destination is provisional and the reason
Expand Down Expand Up @@ -197,6 +209,8 @@ export class Destination {
private printerStatusRetryTimerMs_: number = 3000;
// </if>

private type_: PrinterType;

/**
* List of capability types considered color.
*/
Expand All @@ -219,6 +233,7 @@ export class Destination {
this.extensionId_ = (params && params.extensionId) || '';
this.extensionName_ = (params && params.extensionName) || '';
this.location_ = (params && params.location) || '';
this.type_ = this.computeType_(id, origin);
// <if expr="chromeos_ash or chromeos_lacros">
this.provisionalType_ =
(params && params.provisionalType) || DestinationProvisionalType.NONE;
Expand All @@ -231,6 +246,26 @@ export class Destination {
// </if>
}

private computeType_(id: string, origin: DestinationOrigin): PrinterType {
// <if expr="chromeos_ash or chromeos_lacros">
if (id === GooglePromotedDestinationId.SAVE_TO_DRIVE_CROS) {
return PrinterType.PDF_PRINTER;
}
// </if>

if (id === GooglePromotedDestinationId.SAVE_AS_PDF) {
return PrinterType.PDF_PRINTER;
}

return origin === DestinationOrigin.EXTENSION ?
PrinterType.EXTENSION_PRINTER :
PrinterType.LOCAL_PRINTER;
}

get type(): PrinterType {
return this.type_;
}

get id(): string {
return this.id_;
}
Expand Down
86 changes: 3 additions & 83 deletions chrome/browser/resources/print_preview/data/destination_match.ts
Expand Up @@ -2,115 +2,35 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {assert} from 'chrome://resources/js/assert_ts.js';
import {Destination, DestinationOrigin, GooglePromotedDestinationId, RecentDestination} from './destination.js';

/**
* Printer types for capabilities and printer list requests.
* Must match PrinterType in printing/mojom/print.mojom
*/
export enum PrinterType {
PRIVET_PRINTER_DEPRECATED = 0,
EXTENSION_PRINTER = 1,
PDF_PRINTER = 2,
LOCAL_PRINTER = 3,
CLOUD_PRINTER_DEPRECATED = 4
}

export function originToType(origin: DestinationOrigin): PrinterType {
if (origin === DestinationOrigin.LOCAL || origin === DestinationOrigin.CROS) {
return PrinterType.LOCAL_PRINTER;
}
assert(origin === DestinationOrigin.EXTENSION);
return PrinterType.EXTENSION_PRINTER;
}

export function getPrinterTypeForDestination(
destination: (Destination|RecentDestination)): PrinterType {
// <if expr="chromeos_ash or chromeos_lacros">
if (destination.id === GooglePromotedDestinationId.SAVE_TO_DRIVE_CROS) {
return PrinterType.PDF_PRINTER;
}
// </if>

if (destination.id === GooglePromotedDestinationId.SAVE_AS_PDF) {
return PrinterType.PDF_PRINTER;
}
return originToType(destination.origin);
}
import {Destination, PrinterType} from './destination.js';

export class DestinationMatch {
private origins_: DestinationOrigin[];

private idRegExp_: RegExp|null;

private displayNameRegExp_: RegExp|null;

private skipVirtualDestinations_: boolean;

/**
* A set of key parameters describing a destination used to determine
* if two destinations are the same.
* @param origins Match destinations from these origins.
* @param idRegExp Match destination's id.
* @param displayNameRegExp Match destination's displayName.
* @param skipVirtualDestinations Whether to ignore virtual
* destinations, for example, Save as PDF.
*/
constructor(
origins: DestinationOrigin[], idRegExp: RegExp|null,
displayNameRegExp: RegExp|null, skipVirtualDestinations: boolean) {
this.origins_ = origins;
constructor(idRegExp: RegExp|null, displayNameRegExp: RegExp|null) {
this.idRegExp_ = idRegExp;
this.displayNameRegExp_ = displayNameRegExp;
this.skipVirtualDestinations_ = skipVirtualDestinations;
}

matchOrigin(origin: DestinationOrigin): boolean {
return this.origins_.includes(origin);
}

matchIdAndOrigin(id: string, origin: DestinationOrigin): boolean {
return this.matchOrigin(origin) && !!this.idRegExp_ &&
this.idRegExp_.test(id);
}

match(destination: Destination): boolean {
if (!this.matchOrigin(destination.origin)) {
return false;
}
if (this.idRegExp_ && !this.idRegExp_.test(destination.id)) {
return false;
}
if (this.displayNameRegExp_ &&
!this.displayNameRegExp_.test(destination.displayName)) {
return false;
}
if (this.skipVirtualDestinations_ &&
this.isVirtualDestination_(destination)) {
if (destination.type === PrinterType.PDF_PRINTER) {
return false;
}
return true;
}

/**
* @return Whether {@code destination} is virtual, in terms of
* destination selection.
*/
private isVirtualDestination_(destination: Destination): boolean {
// <if expr="chromeos_ash or chromeos_lacros">
if (destination.id === GooglePromotedDestinationId.SAVE_TO_DRIVE_CROS) {
return true;
}
// </if>

return destination.id === GooglePromotedDestinationId.SAVE_AS_PDF;
}

/**
* @return The printer types that correspond to this destination match.
*/
getTypes(): Set<PrinterType> {
return new Set(this.origins_.map(originToType));
}
}
68 changes: 46 additions & 22 deletions chrome/browser/resources/print_preview/data/destination_store.ts
Expand Up @@ -13,11 +13,11 @@ import {NativeLayerCros, NativeLayerCrosImpl, PrinterSetupResponse} from '../nat

// </if>
import {Cdd, MediaSizeOption} from './cdd.js';
import {createDestinationKey, createRecentDestinationKey, Destination, DestinationOrigin, GooglePromotedDestinationId, RecentDestination} from './destination.js';
import {createDestinationKey, createRecentDestinationKey, Destination, DestinationOrigin, GooglePromotedDestinationId, PrinterType, RecentDestination} from './destination.js';
// <if expr="chromeos_ash or chromeos_lacros">
import {DestinationProvisionalType} from './destination.js';
// </if>
import {DestinationMatch, getPrinterTypeForDestination, originToType, PrinterType} from './destination_match.js';
import {DestinationMatch} from './destination_match.js';
import {ExtensionDestinationInfo, LocalDestinationInfo, parseDestination} from './local_parsers.js';
// <if expr="chromeos_ash or chromeos_lacros">
import {parseExtensionDestination} from './local_parsers.js';
Expand Down Expand Up @@ -302,6 +302,27 @@ export class DestinationStore extends EventTarget {
return !!destination && !!destination.id && !!destination.origin;
}

private getPrinterTypeForRecentDestination_(destination: RecentDestination):
PrinterType {
// <if expr="chromeos_ash or chromeos_lacros">
if (destination.id === GooglePromotedDestinationId.SAVE_TO_DRIVE_CROS) {
return PrinterType.PDF_PRINTER;
}
// </if>

if (destination.id === GooglePromotedDestinationId.SAVE_AS_PDF) {
return PrinterType.PDF_PRINTER;
}

if (destination.origin === DestinationOrigin.LOCAL ||
destination.origin === DestinationOrigin.CROS) {
return PrinterType.LOCAL_PRINTER;
}

assert(destination.origin === DestinationOrigin.EXTENSION);
return PrinterType.EXTENSION_PRINTER;
}

/**
* Initializes the destination store. Sets the initially selected
* destination. If any inserted destinations match this ID, that destination
Expand All @@ -328,27 +349,34 @@ export class DestinationStore extends EventTarget {
serializedDefaultDestinationSelectionRulesStr: string|null,
recentDestinations: RecentDestination[]) {
if (systemDefaultDestinationId) {
let systemDefaultVirtual = systemDefaultDestinationId ===
GooglePromotedDestinationId.SAVE_AS_PDF;
// <if expr="chromeos_ash or chromeos_lacros">
systemDefaultVirtual = systemDefaultVirtual ||
systemDefaultDestinationId ===
GooglePromotedDestinationId.SAVE_TO_DRIVE_CROS;
// </if>
const systemDefaultType = systemDefaultVirtual ?
PrinterType.PDF_PRINTER :
PrinterType.LOCAL_PRINTER;
const systemDefaultOrigin =
this.isDestinationLocal_(systemDefaultDestinationId) ?
DestinationOrigin.LOCAL :
this.platformOrigin_;
systemDefaultVirtual ? DestinationOrigin.LOCAL : this.platformOrigin_;
this.systemDefaultDestinationKey_ =
createDestinationKey(systemDefaultDestinationId, systemDefaultOrigin);
this.typesToSearch_.add(originToType(systemDefaultOrigin));
this.typesToSearch_.add(systemDefaultType);
}

this.recentDestinationKeys_ = recentDestinations.map(
destination => createRecentDestinationKey(destination));
for (const recent of recentDestinations) {
this.typesToSearch_.add(getPrinterTypeForDestination(recent));
this.typesToSearch_.add(this.getPrinterTypeForRecentDestination_(recent));
}

this.autoSelectMatchingDestination_ = this.convertToDestinationMatch_(
serializedDefaultDestinationSelectionRulesStr);
if (this.autoSelectMatchingDestination_) {
for (const type of this.autoSelectMatchingDestination_.getTypes()) {
this.typesToSearch_.add(type);
}
this.typesToSearch_.add(PrinterType.EXTENSION_PRINTER);
this.typesToSearch_.add(PrinterType.LOCAL_PRINTER);
}

this.pdfPrinterEnabled_ = !pdfPrinterDisabled;
Expand Down Expand Up @@ -550,12 +578,6 @@ export class DestinationStore extends EventTarget {
return null;
}

const origins = [
DestinationOrigin.LOCAL,
DestinationOrigin.EXTENSION,
DestinationOrigin.CROS,
];

let idRegExp = null;
try {
if (matchRules.idPattern) {
Expand All @@ -574,8 +596,7 @@ export class DestinationStore extends EventTarget {
console.warn('Failed to parse regexp for "name": ' + e);
}

return new DestinationMatch(
origins, idRegExp, displayNameRegExp, true /*skipVirtualDestinations*/);
return new DestinationMatch(idRegExp, displayNameRegExp);
}

/** @param Key identifying the destination to select */
Expand Down Expand Up @@ -610,8 +631,7 @@ export class DestinationStore extends EventTarget {
// Request destination capabilities from backend, since they are not
// known yet.
if (destination.capabilities === null) {
const type = getPrinterTypeForDestination(destination);
this.nativeLayer_.getPrinterCapabilities(destination.id, type)
this.nativeLayer_.getPrinterCapabilities(destination.id, destination.type)
.then(
(caps) => this.onCapabilitiesSet_(
destination.origin, destination.id, caps),
Expand Down Expand Up @@ -805,7 +825,7 @@ export class DestinationStore extends EventTarget {
assert(destination.constructor !== Array, 'Single printer expected');
assert(destination.capabilities);
destination.capabilities = localizeCapabilities(destination.capabilities);
if (originToType(destination.origin) !== PrinterType.LOCAL_PRINTER) {
if (destination.type !== PrinterType.LOCAL_PRINTER) {
destination.capabilities = sortMediaSizes(destination.capabilities);
}
const existingDestination = this.destinationMap_.get(destination.key);
Expand Down Expand Up @@ -907,7 +927,11 @@ export class DestinationStore extends EventTarget {
return;
}
assert(settingsInfo.printer);
dest = parseDestination(originToType(origin), settingsInfo.printer);
// PDF, CROS, and LOCAL printers all get parsed the same way.
const typeToParse = origin === DestinationOrigin.EXTENSION ?
PrinterType.EXTENSION_PRINTER :
PrinterType.LOCAL_PRINTER;
dest = parseDestination(typeToParse, settingsInfo.printer);
}
if (dest) {
if ((origin === DestinationOrigin.LOCAL ||
Expand Down
5 changes: 2 additions & 3 deletions chrome/browser/resources/print_preview/data/local_parsers.ts
Expand Up @@ -5,11 +5,10 @@
import {assertNotReached} from 'chrome://resources/js/assert_ts.js';
import {isChromeOS, isLacros} from 'chrome://resources/js/cr.m.js';

import {Destination, DestinationOptionalParams, DestinationOrigin} from './destination.js';
import {Destination, DestinationOptionalParams, DestinationOrigin, PrinterType} from './destination.js';
// <if expr="chromeos_ash or chromeos_lacros">
import {DestinationProvisionalType} from './destination.js';
// </if>
import {PrinterType} from './destination_match.js';

type ObjectMap = {
[k: string]: any,
Expand Down Expand Up @@ -43,7 +42,7 @@ export type ExtensionDestinationInfo = {
export function parseDestination(
type: PrinterType,
printer: (LocalDestinationInfo|ExtensionDestinationInfo)): Destination {
if (type === PrinterType.LOCAL_PRINTER) {
if (type === PrinterType.LOCAL_PRINTER || type === PrinterType.PDF_PRINTER) {
return parseLocalDestination(printer as LocalDestinationInfo);
}
if (type === PrinterType.EXTENSION_PRINTER) {
Expand Down
8 changes: 3 additions & 5 deletions chrome/browser/resources/print_preview/data/model.ts
Expand Up @@ -14,8 +14,7 @@ import {BackgroundGraphicsModeRestriction, Policies} from '../native_layer.js';
import {ColorModeRestriction, DuplexModeRestriction, PinModeRestriction} from '../native_layer.js';
// </if>
import {CapabilityWithReset, Cdd, CddCapabilities, ColorOption, DpiOption, DuplexOption, MediaSizeOption} from './cdd.js';
import {Destination, DestinationOrigin, GooglePromotedDestinationId, RecentDestination} from './destination.js';
import {getPrinterTypeForDestination, PrinterType} from './destination_match.js';
import {Destination, DestinationOrigin, GooglePromotedDestinationId, PrinterType, RecentDestination} from './destination.js';
import {DocumentSettings} from './document_info.js';
import {CustomMarginsOrientation, Margins, MarginsSetting, MarginsType} from './margins.js';
import {ScalingType} from './scaling.js';
Expand Down Expand Up @@ -757,8 +756,7 @@ export class PrintPreviewModelElement extends PolymerElement {
}

private updateSettingsAvailabilityFromDestinationAndDocumentSettings_() {
const isSaveAsPDF = getPrinterTypeForDestination(this.destination) ===
PrinterType.PDF_PRINTER;
const isSaveAsPDF = this.destination.type === PrinterType.PDF_PRINTER;
const knownSizeToSaveAsPdf = isSaveAsPDF &&
(!this.documentSettings.isModifiable ||
this.documentSettings.hasCssMediaStyles);
Expand Down Expand Up @@ -1547,7 +1545,7 @@ export class PrintPreviewModelElement extends PolymerElement {
shouldPrintBackgrounds: this.getSettingValue('cssBackground'),
shouldPrintSelectionOnly: false, // only used in print preview
previewModifiable: this.documentSettings.isModifiable,
printerType: getPrinterTypeForDestination(destination),
printerType: destination.type,
rasterizePDF: this.getSettingValue('rasterize'),
scaleFactor:
this.getSettingValue(scalingSettingKey) === ScalingType.CUSTOM ?
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/resources/print_preview/metrics.ts
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

import {assertNotReached} from 'chrome://resources/js/assert_ts.js';
import {PrinterType} from './data/destination_match.js';
import {PrinterType} from './data/destination.js';
import {NativeLayer, NativeLayerImpl} from './native_layer.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/resources/print_preview/native_layer.ts
Expand Up @@ -5,7 +5,7 @@
import {sendWithPromise} from 'chrome://resources/js/cr.m.js';

import {Cdd} from './data/cdd.js';
import {PrinterType} from './data/destination_match.js';
import {PrinterType} from './data/destination.js';
import {LocalDestinationInfo} from './data/local_parsers.js';
import {MeasurementSystemUnitType} from './data/measurement_system.js';

Expand Down

0 comments on commit d7df575

Please sign in to comment.