| 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..13e55db97b43d38ad468bd64cc0659ab00d6698c
|
| --- /dev/null
|
| +++ b/chrome/browser/resources/print_preview_cloud.js
|
| @@ -0,0 +1,191 @@
|
| +// 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 = '';
|
| + var xCloudPrintURLHeader = {'Content-Type':
|
| + 'application/x-www-form-urlencoded',
|
| + 'X-CloudPrint-Proxy': 'ChromePrintPreview'};
|
| +
|
| + var xCloudPrintFormHeader = {'Content-Type':
|
| + 'multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i',
|
| + 'X-CloudPrint-Proxy': 'ChromePrintPreview'};
|
| +
|
| + // 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);
|
| + 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',
|
| + xCloudPrintURLHeader,
|
| + '',
|
| + 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',
|
| + xCloudPrintFormHeader,
|
| + data,
|
| + printToCloudResponse.bind(this, callback),
|
| + true);
|
| + }
|
| +
|
| + // Gets the JSON string used to control the behavior of the current
|
| + // print job.
|
| + function getPrintTicketJSON(printer) {
|
| + if (printer.isCloudPrint) {
|
| + return JSON.stringify({'capabilities':
|
| + [printer.cloudprintOptions.colorOption]});
|
| + } else {
|
| + return null;
|
| + }
|
| + }
|
| +
|
| + // 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',
|
| + xCloudPrintURLHeader,
|
| + null,
|
| + updatePrinterCapsResponse.bind(this,
|
| + callback,
|
| + printer),
|
| + true);
|
| + }
|
| +
|
| + // Returns true if the printer supports color.
|
| + function supportsColor(printer) {
|
| + return printer.cloudprintOptions != null &&
|
| + 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 (printer.isCloudPrint && supportsColor(printer)) {
|
| + 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
|
| + };
|
| +});
|
|
|