Chromium Code Reviews| Index: chrome/browser/resources/print_preview_cloud.js |
| diff --git a/chrome/browser/resources/print_preview_cloud.js b/chrome/browser/resources/print_preview_cloud.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6f9c28a43420d32fb6504a572a86d3960b66f9d6 |
| --- /dev/null |
| +++ b/chrome/browser/resources/print_preview_cloud.js |
| @@ -0,0 +1,182 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +cr.define('cloudprint', function() { |
| + |
| + var cloudPrintBaseURL = ''; |
| + |
| + // Sets the base URL to be used for communicating with cloud print |
| + // servers. |
| + function setBaseURL(cloudPrintURL) { |
| + cloudPrintBaseURL = cloudPrintURL; |
| + } |
| + |
| + // Gets the base URL to be used for communicating with cloud print |
| + // servers. |
| + function getBaseURL() { |
| + return cloudPrintBaseURL; |
| + } |
| + |
| + // Makes a request to cloud print servers. |
| + function sendCloudPrintRequest(method, |
| + action, |
| + headers, |
| + body, |
| + callback, |
| + async) { |
| + var xhr = new XMLHttpRequest(); |
| + if (callback != null) { |
| + xhr.onreadystatechange = function() { |
| + if (xhr.readyState == 4) { |
| + callback.call(this, xhr); |
| + } |
| + }; |
| + } |
| + var url = cloudPrintBaseURL + '/' + action |
| + xhr.open(method, url, async); |
| + xhr.setRequestHeader("Content-Type", |
| + "multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i"); |
| + if (headers) { |
| + for (var header in headers) { |
| + if (headers.hasOwnProperty(header)) { |
| + xhr.setRequestHeader(header, headers[header]); |
| + } |
| + } |
| + } |
| + xhr.send(body); |
| + return xhr; |
| + } |
| + |
| + // Retrieve the list of printers available via cloud print. |
| + function fetchPrinters(callback) { |
| + try { |
| + var xhr = sendCloudPrintRequest('POST', |
| + 'search', |
| + {'Content-Type': |
| + 'application/x-www-form-urlencoded', |
| + 'X-CloudPrint-Proxy': |
| + 'ChromePrintPreview'}, |
| + '', |
| + null, |
| + false); |
| + var searchResult = JSON.parse(xhr.responseText); |
| + if (searchResult['success']) { |
| + var printerList = searchResult['printers']; |
| + callback.call(this, printerList); |
| + } |
| + } catch (err) { |
| + callback.call(this, null); |
| + } |
| + } |
| + |
| + // Handle the response from printing to cloud print. |
| + function printToCloudResponse(callback, xhr) { |
| + if (xhr.status == 200) { |
| + var printResult = JSON.parse(xhr.responseText); |
| + if (printResult['success']) { |
| + callback.call(); |
| + } |
| + } |
| + // TODO(abodenha@chromium.org) Catch and handle failures. |
| + } |
| + |
| + // Send the current document to cloud print. |
| + function printToCloud(data, callback) { |
| + sendCloudPrintRequest('POST', |
| + 'submit', |
| + {'X-CloudPrint-Proxy': 'ChromePrintPreview'}, |
| + data, |
| + printToCloudResponse.bind(this, callback), |
| + true); |
| + } |
| + |
| + // Gets the JSON string used to control the behavior of the current |
| + // print job. |
| + function getPrintTicketJSON(printer) { |
| + return JSON.stringify({'capabilities': |
| + [printer.cloudprintOptions.colorOption]}); |
| + } |
| + |
| + // Process the response from cloud print containing the capabilities |
| + // for the printer. |
| + function updatePrinterCapsResponse(callback, printer, xhr) { |
| + if (xhr.status == 200) { |
| + var printResult = JSON.parse(xhr.responseText); |
| + if (printResult['success']) { |
| + printer.cloudprintOptions = new Object; |
| + printer.cloudprintOptions.colorOption = null; |
| + printer.cloudprintOptions.colorIsDefault = false; |
| + var detailedCapabilities = printResult.printers[0].capabilities; |
| + for (var capability in detailedCapabilities) { |
| + var cap = detailedCapabilities[capability]; |
| + if (cap.name == 'ns1:Colors') { |
| + printer.cloudprintOptions.colorOption = new Object; |
| + printer.cloudprintOptions.colorOption.name = cap.name; |
| + printer.cloudprintOptions.colorOption.type = cap.type; |
| + for (var option in cap.options) { |
| + var opt = cap.options[option]; |
| + if (opt.name == "Color") { |
| + printer.cloudprintOptions.colorOnOption = opt; |
| + } |
| + if (opt.name == "Grey_K") { |
| + printer.cloudprintOptions.colorOffOption = opt; |
| + } |
| + if (opt.default) { |
| + printer.cloudprintOptions.colorOption.options = [opt]; |
| + printer.cloudprintOptions.colorIsDefault = |
| + opt.name == printer.cloudprintOptions.colorOnOption.name; |
| + } |
| + } |
| + } |
| + } |
| + callback.call(this, printer); |
| + } |
| + } |
| + } |
| + |
| + // Retrieve capabilities for a printer. |
| + function updatePrinterCaps(printer, callback) { |
| + sendCloudPrintRequest('GET', |
| + 'printer?printerid=' + printer.value + '&output=json', |
| + {'X-CloudPrint-Proxy': 'ChromePrintPreview'}, |
|
sanjeevr
2011/06/10 18:13:48
You should probably define a constant for header v
Albert Bodenhamer
2011/06/10 23:13:45
Done.
|
| + null, |
| + updatePrinterCapsResponse.bind(this, |
| + callback, |
| + printer), |
| + true); |
| + } |
| + |
| + // Returns true if the printer supports color. |
| + function supportsColor(printer) { |
| + return printer.cloudprintOptions.colorOption != null; |
| + } |
| + |
| + // Returns true if the printer has color enabled by default. |
| + function colorIsDefault(printer) { |
| + return printer.cloudprintOptions.colorIsDefault; |
| + } |
| + |
| + // Turn color on or off for the specified printer. |
| + function setColor(printer, color) { |
| + if (color) { |
| + printer.cloudprintOptions.colorOption.options = |
| + [printer.cloudprintOptions.colorOnOption]; |
| + } else { |
| + printer.cloudprintOptions.colorOption.options = |
| + [printer.cloudprintOptions.colorOffOption]; |
| + } |
| + } |
| + |
| + return { |
| + colorIsDefault: colorIsDefault, |
| + fetchPrinters: fetchPrinters, |
| + getPrintTicketJSON: getPrintTicketJSON, |
| + getBaseURL: getBaseURL, |
| + supportsColor: supportsColor, |
| + printToCloud: printToCloud, |
| + setBaseURL: setBaseURL, |
| + setColor: setColor, |
| + updatePrinterCaps: updatePrinterCaps |
| + }; |
| +}); |