OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 cr.define('cloudprint', function() { |
| 6 |
| 7 var cloudPrintBaseURL = ''; |
| 8 var xCloudPrintURLHeader = {'Content-Type': |
| 9 'application/x-www-form-urlencoded', |
| 10 'X-CloudPrint-Proxy': 'ChromePrintPreview'}; |
| 11 |
| 12 var xCloudPrintFormHeader = {'Content-Type': |
| 13 'multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i', |
| 14 'X-CloudPrint-Proxy': 'ChromePrintPreview'}; |
| 15 |
| 16 // Sets the base URL to be used for communicating with cloud print |
| 17 // servers. |
| 18 function setBaseURL(cloudPrintURL) { |
| 19 cloudPrintBaseURL = cloudPrintURL; |
| 20 } |
| 21 |
| 22 // Gets the base URL to be used for communicating with cloud print |
| 23 // servers. |
| 24 function getBaseURL() { |
| 25 return cloudPrintBaseURL; |
| 26 } |
| 27 |
| 28 // Makes a request to cloud print servers. |
| 29 function sendCloudPrintRequest(method, |
| 30 action, |
| 31 headers, |
| 32 body, |
| 33 callback, |
| 34 async) { |
| 35 var xhr = new XMLHttpRequest(); |
| 36 if (callback != null) { |
| 37 xhr.onreadystatechange = function() { |
| 38 if (xhr.readyState == 4) { |
| 39 callback.call(this, xhr); |
| 40 } |
| 41 }; |
| 42 } |
| 43 var url = cloudPrintBaseURL + '/' + action |
| 44 xhr.open(method, url, async); |
| 45 if (headers) { |
| 46 for (var header in headers) { |
| 47 if (headers.hasOwnProperty(header)) { |
| 48 xhr.setRequestHeader(header, headers[header]); |
| 49 } |
| 50 } |
| 51 } |
| 52 xhr.send(body); |
| 53 return xhr; |
| 54 } |
| 55 |
| 56 // Retrieve the list of printers available via cloud print. |
| 57 function fetchPrinters(callback) { |
| 58 try { |
| 59 var xhr = sendCloudPrintRequest('POST', |
| 60 'search', |
| 61 xCloudPrintURLHeader, |
| 62 '', |
| 63 null, |
| 64 false); |
| 65 var searchResult = JSON.parse(xhr.responseText); |
| 66 if (searchResult['success']) { |
| 67 var printerList = searchResult['printers']; |
| 68 callback.call(this, printerList); |
| 69 } |
| 70 } catch (err) { |
| 71 callback.call(this, null); |
| 72 } |
| 73 } |
| 74 |
| 75 // Handle the response from printing to cloud print. |
| 76 function printToCloudResponse(callback, xhr) { |
| 77 if (xhr.status == 200) { |
| 78 var printResult = JSON.parse(xhr.responseText); |
| 79 if (printResult['success']) { |
| 80 callback.call(); |
| 81 } |
| 82 } |
| 83 // TODO(abodenha@chromium.org) Catch and handle failures. |
| 84 } |
| 85 |
| 86 // Send the current document to cloud print. |
| 87 function printToCloud(data, callback) { |
| 88 sendCloudPrintRequest('POST', |
| 89 'submit', |
| 90 xCloudPrintFormHeader, |
| 91 data, |
| 92 printToCloudResponse.bind(this, callback), |
| 93 true); |
| 94 } |
| 95 |
| 96 // Gets the JSON string used to control the behavior of the current |
| 97 // print job. |
| 98 function getPrintTicketJSON(printer) { |
| 99 if (printer.isCloudPrint) { |
| 100 return JSON.stringify({'capabilities': |
| 101 [printer.cloudprintOptions.colorOption]}); |
| 102 } else { |
| 103 return null; |
| 104 } |
| 105 } |
| 106 |
| 107 // Process the response from cloud print containing the capabilities |
| 108 // for the printer. |
| 109 function updatePrinterCapsResponse(callback, printer, xhr) { |
| 110 if (xhr.status == 200) { |
| 111 var printResult = JSON.parse(xhr.responseText); |
| 112 if (printResult['success']) { |
| 113 printer.cloudprintOptions = new Object; |
| 114 printer.cloudprintOptions.colorOption = null; |
| 115 printer.cloudprintOptions.colorIsDefault = false; |
| 116 var detailedCapabilities = printResult.printers[0].capabilities; |
| 117 for (var capability in detailedCapabilities) { |
| 118 var cap = detailedCapabilities[capability]; |
| 119 if (cap.name == 'ns1:Colors') { |
| 120 printer.cloudprintOptions.colorOption = new Object; |
| 121 printer.cloudprintOptions.colorOption.name = cap.name; |
| 122 printer.cloudprintOptions.colorOption.type = cap.type; |
| 123 for (var option in cap.options) { |
| 124 var opt = cap.options[option]; |
| 125 if (opt.name == "Color") { |
| 126 printer.cloudprintOptions.colorOnOption = opt; |
| 127 } |
| 128 if (opt.name == "Grey_K") { |
| 129 printer.cloudprintOptions.colorOffOption = opt; |
| 130 } |
| 131 if (opt.default) { |
| 132 printer.cloudprintOptions.colorOption.options = [opt]; |
| 133 printer.cloudprintOptions.colorIsDefault = |
| 134 opt.name == printer.cloudprintOptions.colorOnOption.name; |
| 135 } |
| 136 } |
| 137 } |
| 138 } |
| 139 callback.call(this, printer); |
| 140 } |
| 141 } |
| 142 } |
| 143 |
| 144 // Retrieve capabilities for a printer. |
| 145 function updatePrinterCaps(printer, callback) { |
| 146 sendCloudPrintRequest('GET', |
| 147 'printer?printerid=' + printer.value + '&output=json', |
| 148 xCloudPrintURLHeader, |
| 149 null, |
| 150 updatePrinterCapsResponse.bind(this, |
| 151 callback, |
| 152 printer), |
| 153 true); |
| 154 } |
| 155 |
| 156 // Returns true if the printer supports color. |
| 157 function supportsColor(printer) { |
| 158 return printer.cloudprintOptions != null && |
| 159 printer.cloudprintOptions.colorOption != null; |
| 160 } |
| 161 |
| 162 // Returns true if the printer has color enabled by default. |
| 163 function colorIsDefault(printer) { |
| 164 return printer.cloudprintOptions.colorIsDefault; |
| 165 } |
| 166 |
| 167 // Turn color on or off for the specified printer. |
| 168 function setColor(printer, color) { |
| 169 if (printer.isCloudPrint && supportsColor(printer)) { |
| 170 if (color) { |
| 171 printer.cloudprintOptions.colorOption.options = |
| 172 [printer.cloudprintOptions.colorOnOption]; |
| 173 } else { |
| 174 printer.cloudprintOptions.colorOption.options = |
| 175 [printer.cloudprintOptions.colorOffOption]; |
| 176 } |
| 177 } |
| 178 } |
| 179 |
| 180 return { |
| 181 colorIsDefault: colorIsDefault, |
| 182 fetchPrinters: fetchPrinters, |
| 183 getPrintTicketJSON: getPrintTicketJSON, |
| 184 getBaseURL: getBaseURL, |
| 185 supportsColor: supportsColor, |
| 186 printToCloud: printToCloud, |
| 187 setBaseURL: setBaseURL, |
| 188 setColor: setColor, |
| 189 updatePrinterCaps: updatePrinterCaps |
| 190 }; |
| 191 }); |
OLD | NEW |