Skip to content

Commit

Permalink
Printing: Listen for status updates in Printer settings
Browse files Browse the repository at this point in the history
Receive local printers from the 'local-printers-updated' and parse their
printer status to update the Saved printers section when the
"local-printer-observing flag" is enabled.

Bug: b:300136694
Change-Id: Id7426b1162ca92b4de4ed47d6ab4c0ef90f6c0a1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4977749
Reviewed-by: Ashley Prasad <ashleydp@google.com>
Reviewed-by: Jimmy Gong <jimmyxgong@chromium.org>
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1217196}
  • Loading branch information
Gavin Williams authored and Chromium LUCI CQ committed Oct 30, 2023
1 parent 771662c commit 152d3f8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import './cups_printers_entry.js';

import {WebUiListenerMixin} from 'chrome://resources/cr_elements/web_ui_listener_mixin.js';
import {assert} from 'chrome://resources/js/assert.js';
import {addWebUiListener, WebUiListener} from 'chrome://resources/js/cr.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.js';
import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

Expand Down Expand Up @@ -197,6 +198,17 @@ export class SettingsCupsSavedPrintersElement extends
readOnly: true,
reflectToAttribute: true,
},

/**
* True when the "local-printer-observing" feature flag is enabled.
*/
isLocalPrinterObservingEnabled_: {
type: Boolean,
value: () => {
return loadTimeData.getBoolean('isLocalPrinterObservingEnabled');
},
readOnly: true,
},
};
}

Expand Down Expand Up @@ -224,7 +236,9 @@ export class SettingsCupsSavedPrintersElement extends
private pageStartTime_: number;
private timeoutIds_: number[];
private onFocusListener_: () => void;
private localPrintersUpdatedListener_: WebUiListener;
private isPrinterSettingsPrinterStatusEnabled_: boolean;
private isLocalPrinterObservingEnabled_: boolean;

constructor() {
super();
Expand All @@ -237,6 +251,15 @@ export class SettingsCupsSavedPrintersElement extends
this.visiblePrinterCounter_ = MIN_VISIBLE_PRINTERS;

this.onFocusListener_ = () => this.resetPrinterStatusQueryTimers();

// Listen for updates of local printers from the 'local-printers-updated'
// event to consume their updated printer statuses.
if (this.isLocalPrinterObservingEnabled_) {
addWebUiListener(
'local-printers-updated',
(printers: CupsPrinterInfo[]) => printers.forEach(
printer => this.onPrinterStatusReceived_(printer.printerStatus)));
}
}

override ready(): void {
Expand All @@ -248,7 +271,11 @@ export class SettingsCupsSavedPrintersElement extends
this.onOpenActionMenu_(event);
});

if (this.isPrinterSettingsPrinterStatusEnabled_) {
// When `isLocalPrinterObservingEnabled_` is enabled printer statuses get
// pushed from the backend so printer statuses don't need to be
// individually requested.
if (this.isPrinterSettingsPrinterStatusEnabled_ &&
!this.isLocalPrinterObservingEnabled_) {
this.startPrinterStatusQueryTimer_(/*forErrorStatePrinters=*/ true);
this.startPrinterStatusQueryTimer_(/*forErrorStatePrinters=*/ false);
}
Expand Down Expand Up @@ -469,9 +496,10 @@ export class SettingsCupsSavedPrintersElement extends
* For each printer status received, add it to the printer status cache then
* notify its respective printer entry to update its status.
*/
private onPrinterStatusReceived_(printerStatus: PrinterStatus): void {
private onPrinterStatusReceived_(printerStatus: PrinterStatus|
undefined): void {
assert(this.isPrinterSettingsPrinterStatusEnabled_);
if (!printerStatus) {
if (!printerStatus?.printerId) {
return;
}

Expand Down Expand Up @@ -576,6 +604,10 @@ export class SettingsCupsSavedPrintersElement extends
getPrinterStatusReasonCacheForTesting(): Map<string, PrinterStatusReason> {
return this.printerStatusReasonCache_;
}

getTimeoutIdsForTesting(): number[] {
return this.timeoutIds_;
}
}

declare global {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ void PrintingSection::AddLoadTimeData(content::WebUIDataSource* html_source) {
features::IsPrinterSettingsPrinterStatusEnabled());
html_source->AddBoolean("isPrintPreviewDiscoveredPrintersEnabled",
features::IsPrintPreviewDiscoveredPrintersEnabled());
html_source->AddBoolean(
"isLocalPrinterObservingEnabled",
base::FeatureList::IsEnabled(::features::kLocalPrinterObserving));
}

void PrintingSection::AddHandlers(content::WebUI* web_ui) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,73 @@ suite('CupsSavedPrintersTests', () => {
assertFalse(printerStatusReasonCache.has('id2'));
});

// Verify the printer statuses received from the 'local-printers-updated'
// event are added to the printer status cache.
test('LocalPrintersUpdatedPrinterStatusCache', async () => {
loadTimeData.overrideValues({
isLocalPrinterObservingEnabled: true,
});
createCupsPrinterPage([]);
await flushTasks();
const element =
page.shadowRoot!.querySelector('settings-cups-saved-printers');
assertTrue(!!element);
savedPrintersElement = element;

const id1 = 'id1';
const printer1 = createCupsPrinterInfo('test1', '1', id1);
printer1.printerStatus = {
printerId: id1,
statusReasons: [
{
reason: PrinterStatusReason.PRINTER_UNREACHABLE,
severity: PrinterStatusSeverity.ERROR,
},
],
timestamp: 0,
};
const id2 = 'id2';
const printer2 = createCupsPrinterInfo('test2', '2', id2);
printer2.printerStatus = {
printerId: id2,
statusReasons: [
{
reason: PrinterStatusReason.LOW_ON_INK,
severity: PrinterStatusSeverity.ERROR,
},
],
timestamp: 0,
};
// Printer3 has an undefined printer status so it shouldn't be added to the
// cache.
const id3 = 'id3';
const printer3 = createCupsPrinterInfo('test3', '3', id3);
printer3.printerStatus = {
printerId: '',
statusReasons: [],
timestamp: 0,
};

// The printer status cache should initialize empty.
const printerStatusReasonCache =
savedPrintersElement.getPrinterStatusReasonCacheForTesting();
assertFalse(printerStatusReasonCache.has(id1));
assertFalse(printerStatusReasonCache.has(id2));
assertFalse(printerStatusReasonCache.has(id3));

// Trigger the observer and expect the printer statuses to be extracted then
// added to the cache.
webUIListenerCallback('local-printers-updated', [printer1, printer2]);
await flushTasks();
assertTrue(printerStatusReasonCache.has(id1));
assertTrue(printerStatusReasonCache.has(id2));
assertFalse(printerStatusReasonCache.has(id3));

// This verifies that no printer status query timers are scheduled when the
// "local-printer-observing" flag is enabled.
assertEquals(0, savedPrintersElement.getTimeoutIdsForTesting().length);
});

test('ShowMoreButtonIsInitiallyHiddenAndANewPrinterIsAdded', async () => {
createCupsPrinterPage([
createCupsPrinterInfo('google', '4', 'id4'),
Expand Down

0 comments on commit 152d3f8

Please sign in to comment.