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() {
startLoadLocalDestinations: function() {
if (!this.hasLoadedAllLocalDestinations_) {
this.hasLoadedAllLocalDestinations_ = true;
this.nativeLayer_.startGetLocalDestinations();
this.nativeLayer_.getPrinters().then(
this.onLocalDestinationsSet_.bind(this));
this.isLocalDestinationSearchInProgress_ = true;
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
......@@ -1360,10 +1361,6 @@ cr.define('print_preview', function() {
*/
addEventListeners_: function() {
var nativeLayerEventTarget = this.nativeLayer_.getEventTarget();
this.tracker_.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET,
this.onLocalDestinationsSet_.bind(this));
this.tracker_.add(
nativeLayerEventTarget,
print_preview.NativeLayer.EventType.CAPABILITIES_SET,
......@@ -1437,11 +1434,12 @@ cr.define('print_preview', function() {
/**
* 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
*/
onLocalDestinationsSet_: function(event) {
var localDestinations = event.destinationInfos.map(function(destInfo) {
onLocalDestinationsSet_: function(destinationInfos) {
var localDestinations = destinationInfos.map(function(destInfo) {
return print_preview.LocalDestinationParser.parse(destInfo);
});
this.insertDestinations_(localDestinations);
......
......@@ -10,8 +10,8 @@ cr.define('print_preview', function() {
/**
* Parses a local print destination.
* @param {!Object} destinationInfo Information describing a local print
* destination.
* @param {!print_preview.LocalDestinationInfo} destinationInfo Information
* describing a local print destination.
* @return {!print_preview.Destination} Parsed local print destination.
*/
LocalDestinationParser.parse = function(destinationInfo) {
......
......@@ -15,6 +15,17 @@ cr.exportPath('print_preview');
*/
print_preview.PreviewSettings;
/**
* @typedef {{
* deviceName: string,
* printerName: string,
* printerDescription: (string | undefined),
* cupsEnterprisePrinter: (boolean | undefined),
* printerOptions: (Object | undefined),
* }}
*/
print_preview.LocalDestinationInfo;
/**
* @typedef {{
* printerId: string,
......@@ -34,7 +45,6 @@ cr.define('print_preview', function() {
function NativeLayer() {
// Bind global handlers
global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this);
global.setPrinters = this.onSetPrinters_.bind(this);
global.updateWithPrinterCapabilities =
this.onUpdateWithPrinterCapabilities_.bind(this);
global.failedToGetPrinterCapabilities =
......@@ -220,9 +230,10 @@ cr.define('print_preview', function() {
/**
* Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
* event will be dispatched in response.
* @return {!Promise<!Array<print_preview.LocalDestinationInfo>>}
*/
startGetLocalDestinations: function() {
chrome.send('getPrinters');
getPrinters: function() {
return cr.sendWithPromise('getPrinters');
},
/**
......@@ -549,19 +560,6 @@ cr.define('print_preview', function() {
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
* destination.
......
......@@ -661,10 +661,17 @@ printing::PrinterBackendProxy* PrintPreviewHandler::printer_backend_proxy() {
return printer_backend_proxy_.get();
}
void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* /*args*/) {
void PrintPreviewHandler::HandleGetPrinters(const base::ListValue* args) {
VLOG(1) << "Enumerate printers start";
printer_backend_proxy()->EnumeratePrinters(base::Bind(
&PrintPreviewHandler::SetupPrinterList, weak_factory_.GetWeakPtr()));
std::string callback_id;
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) {
......@@ -1325,6 +1332,7 @@ void PrintPreviewHandler::SendPrinterSetup(
}
void PrintPreviewHandler::SetupPrinterList(
const std::string& callback_id,
const printing::PrinterList& printer_list) {
base::ListValue printers;
PrintersToValues(printer_list, &printers);
......@@ -1337,7 +1345,7 @@ void PrintPreviewHandler::SetupPrinterList(
has_logged_printers_count_ = true;
}
web_ui()->CallJavascriptFunctionUnsafe("setPrinters", printers);
ResolveJavascriptCallback(base::Value(callback_id), printers);
}
void PrintPreviewHandler::SendCloudPrintEnabled() {
......
......@@ -242,7 +242,8 @@ class PrintPreviewHandler
std::unique_ptr<base::DictionaryValue> settings_info);
// 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.
void SendCloudPrintEnabled();
......
......@@ -10,7 +10,7 @@ cr.define('print_preview', function() {
*/
function NativeLayerStub() {
settings.TestBrowserProxy.call(this, [
'getInitialSettings', 'setupPrinter' ]);
'getInitialSettings', 'getPrinters', 'setupPrinter' ]);
/**
* @private {!cr.EventTarget} The event target used for dispatching and
......@@ -33,6 +33,13 @@ cr.define('print_preview', function() {
*/
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
* on a |setupPrinter| call.
......@@ -66,6 +73,12 @@ cr.define('print_preview', function() {
return Promise.resolve(this.initialSettings_);
},
/** @override */
getPrinters: function() {
this.methodCalled('getPrinters');
return Promise.resolve(this.localDestinationInfos_);
},
/** @override */
setupPrinter: function(printerId) {
this.methodCalled('setupPrinter', printerId);
......@@ -131,6 +144,14 @@ cr.define('print_preview', function() {
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 {!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