Commit 10c04142 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Change getPrinters to cr.sendWithPromise

Change one more native layer message to cr.sendWithPromise and adjust
tests to use browser proxy for this message instead of triggering an
event.

BUG=717296
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2919693002
Cr-Commit-Position: refs/heads/master@{#476503}
parent 2bfd0fd3
...@@ -1078,7 +1078,8 @@ cr.define('print_preview', function() { ...@@ -1078,7 +1078,8 @@ cr.define('print_preview', function() {
startLoadLocalDestinations: function() { startLoadLocalDestinations: function() {
if (!this.hasLoadedAllLocalDestinations_) { if (!this.hasLoadedAllLocalDestinations_) {
this.hasLoadedAllLocalDestinations_ = true; this.hasLoadedAllLocalDestinations_ = true;
this.nativeLayer_.startGetLocalDestinations(); this.nativeLayer_.getPrinters().then(
this.onLocalDestinationsSet_.bind(this));
this.isLocalDestinationSearchInProgress_ = true; this.isLocalDestinationSearchInProgress_ = true;
cr.dispatchSimpleEvent( cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
...@@ -1360,10 +1361,6 @@ cr.define('print_preview', function() { ...@@ -1360,10 +1361,6 @@ cr.define('print_preview', function() {
*/ */
addEventListeners_: function() { addEventListeners_: function() {
var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); var nativeLayerEventTarget = this.nativeLayer_.getEventTarget();
this.tracker_.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET,
this.onLocalDestinationsSet_.bind(this));
this.tracker_.add( this.tracker_.add(
nativeLayerEventTarget, nativeLayerEventTarget,
print_preview.NativeLayer.EventType.CAPABILITIES_SET, print_preview.NativeLayer.EventType.CAPABILITIES_SET,
...@@ -1437,11 +1434,12 @@ cr.define('print_preview', function() { ...@@ -1437,11 +1434,12 @@ cr.define('print_preview', function() {
/** /**
* Called when the local destinations have been got from the native layer. * Called when the local destinations have been got from the native layer.
* @param {Event} event Contains the local destinations. * @param {!Array<!print_preview.LocalDestinationInfo>} destinationInfos A
* list of the local destinations retrieved.
* @private * @private
*/ */
onLocalDestinationsSet_: function(event) { onLocalDestinationsSet_: function(destinationInfos) {
var localDestinations = event.destinationInfos.map(function(destInfo) { var localDestinations = destinationInfos.map(function(destInfo) {
return print_preview.LocalDestinationParser.parse(destInfo); return print_preview.LocalDestinationParser.parse(destInfo);
}); });
this.insertDestinations_(localDestinations); this.insertDestinations_(localDestinations);
......
...@@ -10,8 +10,8 @@ cr.define('print_preview', function() { ...@@ -10,8 +10,8 @@ cr.define('print_preview', function() {
/** /**
* Parses a local print destination. * Parses a local print destination.
* @param {!Object} destinationInfo Information describing a local print * @param {!print_preview.LocalDestinationInfo} destinationInfo Information
* destination. * describing a local print destination.
* @return {!print_preview.Destination} Parsed local print destination. * @return {!print_preview.Destination} Parsed local print destination.
*/ */
LocalDestinationParser.parse = function(destinationInfo) { LocalDestinationParser.parse = function(destinationInfo) {
......
...@@ -15,6 +15,17 @@ cr.exportPath('print_preview'); ...@@ -15,6 +15,17 @@ cr.exportPath('print_preview');
*/ */
print_preview.PreviewSettings; print_preview.PreviewSettings;
/**
* @typedef {{
* deviceName: string,
* printerName: string,
* printerDescription: (string | undefined),
* cupsEnterprisePrinter: (boolean | undefined),
* printerOptions: (Object | undefined),
* }}
*/
print_preview.LocalDestinationInfo;
/** /**
* @typedef {{ * @typedef {{
* printerId: string, * printerId: string,
...@@ -34,7 +45,6 @@ cr.define('print_preview', function() { ...@@ -34,7 +45,6 @@ cr.define('print_preview', function() {
function NativeLayer() { function NativeLayer() {
// Bind global handlers // Bind global handlers
global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this); global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this);
global.setPrinters = this.onSetPrinters_.bind(this);
global.updateWithPrinterCapabilities = global.updateWithPrinterCapabilities =
this.onUpdateWithPrinterCapabilities_.bind(this); this.onUpdateWithPrinterCapabilities_.bind(this);
global.failedToGetPrinterCapabilities = global.failedToGetPrinterCapabilities =
...@@ -220,9 +230,10 @@ cr.define('print_preview', function() { ...@@ -220,9 +230,10 @@ cr.define('print_preview', function() {
/** /**
* Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
* event will be dispatched in response. * event will be dispatched in response.
* @return {!Promise<!Array<print_preview.LocalDestinationInfo>>}
*/ */
startGetLocalDestinations: function() { getPrinters: function() {
chrome.send('getPrinters'); return cr.sendWithPromise('getPrinters');
}, },
/** /**
...@@ -549,19 +560,6 @@ cr.define('print_preview', function() { ...@@ -549,19 +560,6 @@ cr.define('print_preview', function() {
this.eventTarget_.dispatchEvent(cloudPrintEnableEvent); this.eventTarget_.dispatchEvent(cloudPrintEnableEvent);
}, },
/**
* Updates the print preview with local printers.
* Called from PrintPreviewHandler::SetupPrinterList().
* @param {Array} printers Array of printer info objects.
* @private
*/
onSetPrinters_: function(printers) {
var localDestsSetEvent = new Event(
NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
localDestsSetEvent.destinationInfos = printers;
this.eventTarget_.dispatchEvent(localDestsSetEvent);
},
/** /**
* Called when native layer gets settings information for a requested local * Called when native layer gets settings information for a requested local
* destination. * destination.
......
...@@ -661,10 +661,17 @@ printing::PrinterBackendProxy* PrintPreviewHandler::printer_backend_proxy() { ...@@ -661,10 +661,17 @@ printing::PrinterBackendProxy* PrintPreviewHandler::printer_backend_proxy() {
return printer_backend_proxy_.get(); return printer_backend_proxy_.get();
} }
void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) { void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* args) {
VLOG(1) << "Enumerate printers start"; VLOG(1) << "Enumerate printers start";
printer_backend_proxy()->EnumeratePrinters(base::Bind( std::string callback_id;
&PrintPreviewHandler::SetupPrinterList, weak_factory_.GetWeakPtr())); CHECK(args->GetString(0, &callback_id));
CHECK(!callback_id.empty());
AllowJavascript();
printer_backend_proxy()->EnumeratePrinters(
base::Bind(&PrintPreviewHandler::SetupPrinterList,
weak_factory_.GetWeakPtr(), callback_id));
} }
void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) { void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) {
...@@ -1325,6 +1332,7 @@ void PrintPreviewHandler::SendPrinterSetup( ...@@ -1325,6 +1332,7 @@ void PrintPreviewHandler::SendPrinterSetup(
} }
void PrintPreviewHandler::SetupPrinterList( void PrintPreviewHandler::SetupPrinterList(
const std::string& callback_id,
const printing::PrinterList& printer_list) { const printing::PrinterList& printer_list) {
base::ListValue printers; base::ListValue printers;
PrintersToValues(printer_list, &printers); PrintersToValues(printer_list, &printers);
...@@ -1337,7 +1345,7 @@ void PrintPreviewHandler::SetupPrinterList( ...@@ -1337,7 +1345,7 @@ void PrintPreviewHandler::SetupPrinterList(
has_logged_printers_count_ = true; has_logged_printers_count_ = true;
} }
web_ui()->CallJavascriptFunctionUnsafe("setPrinters", printers); ResolveJavascriptCallback(base::Value(callback_id), printers);
} }
void PrintPreviewHandler::SendCloudPrintEnabled() { void PrintPreviewHandler::SendCloudPrintEnabled() {
......
...@@ -242,7 +242,8 @@ class PrintPreviewHandler ...@@ -242,7 +242,8 @@ class PrintPreviewHandler
std::unique_ptr<base::DictionaryValue> settings_info); std::unique_ptr<base::DictionaryValue> settings_info);
// Send the list of printers to the Web UI. // Send the list of printers to the Web UI.
void SetupPrinterList(const printing::PrinterList& printer_list); void SetupPrinterList(const std::string& callback_id,
const printing::PrinterList& printer_list);
// Send whether cloud print integration should be enabled. // Send whether cloud print integration should be enabled.
void SendCloudPrintEnabled(); void SendCloudPrintEnabled();
......
...@@ -10,7 +10,7 @@ cr.define('print_preview', function() { ...@@ -10,7 +10,7 @@ cr.define('print_preview', function() {
*/ */
function NativeLayerStub() { function NativeLayerStub() {
settings.TestBrowserProxy.call(this, [ settings.TestBrowserProxy.call(this, [
'getInitialSettings', 'setupPrinter' ]); 'getInitialSettings', 'getPrinters', 'setupPrinter' ]);
/** /**
* @private {!cr.EventTarget} The event target used for dispatching and * @private {!cr.EventTarget} The event target used for dispatching and
...@@ -33,6 +33,13 @@ cr.define('print_preview', function() { ...@@ -33,6 +33,13 @@ cr.define('print_preview', function() {
*/ */
this.initialSettings_ = null; this.initialSettings_ = null;
/**
*
* @private {!Array<!print_preview.LocalDestinationInfo>} Local destination
* list to be used for the response to |getPrinters|.
*/
this.localDestinationInfos_ = [];
/** /**
* @private {!print_preview.PrinterSetupResponse} The response to be sent * @private {!print_preview.PrinterSetupResponse} The response to be sent
* on a |setupPrinter| call. * on a |setupPrinter| call.
...@@ -66,6 +73,12 @@ cr.define('print_preview', function() { ...@@ -66,6 +73,12 @@ cr.define('print_preview', function() {
return Promise.resolve(this.initialSettings_); return Promise.resolve(this.initialSettings_);
}, },
/** @override */
getPrinters: function() {
this.methodCalled('getPrinters');
return Promise.resolve(this.localDestinationInfos_);
},
/** @override */ /** @override */
setupPrinter: function(printerId) { setupPrinter: function(printerId) {
this.methodCalled('setupPrinter', printerId); this.methodCalled('setupPrinter', printerId);
...@@ -131,6 +144,14 @@ cr.define('print_preview', function() { ...@@ -131,6 +144,14 @@ cr.define('print_preview', function() {
this.initialSettings_ = settings; this.initialSettings_ = settings;
}, },
/**
* @param {!Array<!print_preview.LocalDestinationInfo>} localDestinations
* The local destinations to return as a response to |getPrinters|.
*/
setLocalDestinations: function(localDestinations) {
this.localDestinationInfos_ = localDestinations;
},
/** /**
* @param {boolean} reject Whether printSetup requests should be rejected. * @param {boolean} reject Whether printSetup requests should be rejected.
* @param {!print_preview.PrinterSetupResponse} The response to send when * @param {!print_preview.PrinterSetupResponse} The response to send when
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment