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 // The URL to use to access the cloud print servers. |
| 8 // Set by a call to setBaseURL. |
| 9 var cloudPrintBaseURL = ''; |
| 10 |
| 11 // Headers to set for most cloud print API calls. |
| 12 var xCloudPrintURLHeader = {'Content-Type': |
| 13 'application/x-www-form-urlencoded', |
| 14 'X-CloudPrint-Proxy': 'ChromePrintPreview'}; |
| 15 |
| 16 // Headers to set when sending multipart data to cloud print APIs. |
| 17 // Currently only used when submitting a job. |
| 18 var xCloudPrintFormHeader = {'Content-Type': |
| 19 'multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i', |
| 20 'X-CloudPrint-Proxy': 'ChromePrintPreview'}; |
| 21 |
| 22 // The last received XSRF token. This should be sent with each request |
| 23 // to prevent XSRF. |
| 24 var lastXSRFToken = ''; |
| 25 |
| 26 /** |
| 27 * Sets the base URL to be used for communicating with cloud print |
| 28 * servers. |
| 29 * @param {string} cloudPrintURL The URL to use. |
| 30 */ |
| 31 function setBaseURL(cloudPrintURL) { |
| 32 cloudPrintBaseURL = cloudPrintURL; |
| 33 } |
| 34 |
| 35 /** |
| 36 * Gets the base URL to be used for communicating with cloud print |
| 37 * servers. |
| 38 * @return {string} The URL. |
| 39 */ |
| 40 function getBaseURL() { |
| 41 return cloudPrintBaseURL; |
| 42 } |
| 43 |
| 44 /** |
| 45 * Extracts the XSRF token from each response to be used in the next |
| 46 * request. |
| 47 * @param {XMLHttpRequest} xhr The object used to make the request. |
| 48 * @return {string} The extracted XSRF token. |
| 49 */ |
| 50 function extractXSRFtoken(xhr) { |
| 51 if (xhr.status == 200) { |
| 52 var result = JSON.parse(xhr.responseText); |
| 53 return result['xsrf_token']; |
| 54 } else { |
| 55 return null; |
| 56 } |
| 57 } |
| 58 |
| 59 /** |
| 60 * Makes a request to cloud print servers. |
| 61 * @param {string} method The HTTP method to be used. |
| 62 * @param {string} action The cloud print API to call. |
| 63 * @param {Array} headers Headers to send with the request. |
| 64 * @param {string} body Body to be sent with POST requests. |
| 65 * @param {function} callback Function to be called to process response. |
| 66 * @param {boolean} async True if we want the request made asyncronously. |
| 67 */ |
| 68 function sendCloudPrintRequest(method, |
| 69 action, |
| 70 headers, |
| 71 params, |
| 72 body, |
| 73 callback) { |
| 74 var xhr = new XMLHttpRequest(); |
| 75 if (callback != null) { |
| 76 xhr.onreadystatechange = function() { |
| 77 if (xhr.readyState == 4) { |
| 78 var updatedXSRFToken = extractXSRFtoken(xhr); |
| 79 if (updatedXSRFToken != null) { |
| 80 lastXSRFToken = updatedXSRFToken; |
| 81 } |
| 82 callback.call(this, xhr); |
| 83 } |
| 84 }; |
| 85 } |
| 86 var url = cloudPrintBaseURL + '/' + action; |
| 87 if (params == null) { |
| 88 params = new Array; |
| 89 } |
| 90 if (lastXSRFToken.length != 0) { |
| 91 params.push("xsrf=" + lastXSRFToken); |
| 92 } |
| 93 if (params.length != 0) { |
| 94 url = url + "?"; |
| 95 for (param in params) { |
| 96 url = url + params[param] + "&"; |
| 97 } |
| 98 } |
| 99 xhr.open(method, url, true); |
| 100 xhr.withCredentials = true; |
| 101 if (headers) { |
| 102 for (var header in headers) { |
| 103 if (headers.hasOwnProperty(header)) { |
| 104 xhr.setRequestHeader(header, headers[header]); |
| 105 } |
| 106 } |
| 107 } |
| 108 xhr.send(body); |
| 109 return xhr; |
| 110 } |
| 111 |
| 112 /** |
| 113 * Parse the response from the fetchPrinters call. |
| 114 * @param {function} callback Function to be called to process response. |
| 115 * @param {XMLHttpRequest} xhr The object used to make the request. |
| 116 */ |
| 117 function fetchPrintersResponse(callback, xhr) { |
| 118 if (xhr.status == 200) { |
| 119 var searchResult = JSON.parse(xhr.responseText); |
| 120 if (searchResult['success']) { |
| 121 var printerList = searchResult['printers']; |
| 122 callback.call(this, printerList, false); |
| 123 } else { |
| 124 callback.call(this, null, false); |
| 125 } |
| 126 } else { |
| 127 callback.call(this, null, false); |
| 128 } |
| 129 } |
| 130 |
| 131 /** |
| 132 * Retrieve the list of printers available via cloud print. |
| 133 * @param {function} callback Function to be called to process response. |
| 134 */ |
| 135 function fetchPrinters(callback, all) { |
| 136 var query = 'q=^recent'; |
| 137 if (all) { |
| 138 query = ''; |
| 139 } |
| 140 sendCloudPrintRequest('GET', |
| 141 'search', |
| 142 xCloudPrintURLHeader, |
| 143 [query], |
| 144 null, |
| 145 fetchPrintersResponse.bind(this, callback)); |
| 146 } |
| 147 |
| 148 /** |
| 149 * Handle the response from printing to cloud print. |
| 150 * @param {function} callback Function to be called to process response. |
| 151 * @param {XMLHttpRequest} xhr The object used to make the request. |
| 152 */ |
| 153 function printToCloudResponse(callback, xhr) { |
| 154 if (xhr.status == 200) { |
| 155 var printResult = JSON.parse(xhr.responseText); |
| 156 if (printResult['success']) { |
| 157 callback.call(); |
| 158 } |
| 159 } |
| 160 // TODO(abodenha@chromium.org) Catch and handle failures. |
| 161 } |
| 162 |
| 163 /** |
| 164 * Send the current document to cloud print. |
| 165 * @param {string} data The document to be printed. |
| 166 * @param {function} callback Function to be called to process response. |
| 167 */ |
| 168 function printToCloud(data, callback) { |
| 169 // TODO(abodenha@chromium.org) Make sure we have an XSRF token before |
| 170 // sending a submit. Right now if the user clicks print before we |
| 171 // complete any request we wont have an XSRF and the submit will fail. |
| 172 sendCloudPrintRequest('POST', |
| 173 'submit', |
| 174 xCloudPrintFormHeader, |
| 175 null, |
| 176 data, |
| 177 printToCloudResponse.bind(this, callback)); |
| 178 } |
| 179 |
| 180 /** |
| 181 * Gets the JSON string used to control the behavior of the current |
| 182 * print job. |
| 183 * @param {Object} printer The printer object to get the ticket for. |
| 184 * @return {string} The print ticket or null if not a cloud printer. |
| 185 */ |
| 186 function getPrintTicketJSON(printer) { |
| 187 if (isCloudPrint(printer)) { |
| 188 return JSON.stringify({'capabilities': |
| 189 [printer.cloudPrintOptions.colorOption]}); |
| 190 } else { |
| 191 return null; |
| 192 } |
| 193 } |
| 194 |
| 195 /** |
| 196 * Process the response from cloud print containing the capabilities |
| 197 * for the printer. |
| 198 * @param {function} callback Function to be called to process response. |
| 199 * @param {Object} printer The printer object to get the capabilites for. |
| 200 * @param {XMLHttpRequest} xhr The object used to make the request. |
| 201 */ |
| 202 function updatePrinterCapsResponse(callback, printer, xhr) { |
| 203 if (xhr.status == 200) { |
| 204 var printResult = JSON.parse(xhr.responseText); |
| 205 if (printResult['success']) { |
| 206 if (!printer.cloudPrintOptions) |
| 207 printer.cloudPrintOptions = new Object; |
| 208 printer.cloudPrintOptions.capsDownloaded = true; |
| 209 printer.cloudPrintOptions.colorOption = null; |
| 210 printer.cloudPrintOptions.colorIsDefault = false; |
| 211 var detailedCapabilities = printResult.printers[0].capabilities; |
| 212 for (var capability in detailedCapabilities) { |
| 213 var cap = detailedCapabilities[capability]; |
| 214 if (cap.name == 'ns1:Colors') { |
| 215 printer.cloudPrintOptions.colorOption = new Object; |
| 216 printer.cloudPrintOptions.colorOption.name = cap.name; |
| 217 printer.cloudPrintOptions.colorOption.type = cap.type; |
| 218 for (var option in cap.options) { |
| 219 var opt = cap.options[option]; |
| 220 if (opt.name == "Color") { |
| 221 printer.cloudPrintOptions.colorOnOption = opt; |
| 222 } |
| 223 if (opt.name == "Grey_K") { |
| 224 printer.cloudPrintOptions.colorOffOption = opt; |
| 225 } |
| 226 if (opt.default) { |
| 227 printer.cloudPrintOptions.colorOption.options = [opt]; |
| 228 printer.cloudPrintOptions.colorIsDefault = |
| 229 opt.name == printer.cloudPrintOptions.colorOnOption.name; |
| 230 } |
| 231 } |
| 232 } |
| 233 } |
| 234 callback.call(this, printer); |
| 235 } |
| 236 } |
| 237 } |
| 238 |
| 239 /** |
| 240 * Retrieve capabilities for a printer. |
| 241 * @param {Object} printer The printer object to get the capabilities for. |
| 242 * @param {function} callback Function to be called to process response. |
| 243 */ |
| 244 function updatePrinterCaps(printer, callback) { |
| 245 if (isCloudPrint(printer) && !printer.cloudPrintOptions.capsDownloaded) { |
| 246 sendCloudPrintRequest('GET', |
| 247 'printer?printerid=' + |
| 248 printer.value + |
| 249 '&output=json', |
| 250 xCloudPrintURLHeader, |
| 251 null, |
| 252 null, |
| 253 updatePrinterCapsResponse.bind(this, |
| 254 callback, |
| 255 printer)); |
| 256 } else { |
| 257 callback.call(this, printer); |
| 258 } |
| 259 } |
| 260 |
| 261 /** |
| 262 * @param {Object} printer The printer object to get the data for. |
| 263 * @return {boolean} true if the printer supports color. |
| 264 */ |
| 265 function supportsColor(printer) { |
| 266 return isCloudPrint(printer) && |
| 267 printer.cloudPrintOptions.colorOption != null; |
| 268 } |
| 269 |
| 270 /** |
| 271 * @param {Object} printer The printer object to get the data for. |
| 272 * @return {boolean} true if the printer defaults to color. |
| 273 */ |
| 274 function colorIsDefault(printer) { |
| 275 return isCloudPrint(printer) && |
| 276 printer.cloudPrintOptions.colorIsDefault; |
| 277 } |
| 278 |
| 279 /** |
| 280 * Turn color on or off for the specified printer. |
| 281 * @param {Object} printer The printer object to turn color on/off for. |
| 282 * @param {boolean} color True to turn color on. |
| 283 */ |
| 284 function setColor(printer, color) { |
| 285 if (isCloudPrint(printer) && supportsColor(printer)) { |
| 286 if (color) { |
| 287 printer.cloudPrintOptions.colorOption.options = |
| 288 [printer.cloudPrintOptions.colorOnOption]; |
| 289 } else { |
| 290 printer.cloudPrintOptions.colorOption.options = |
| 291 [printer.cloudPrintOptions.colorOffOption]; |
| 292 } |
| 293 } |
| 294 } |
| 295 |
| 296 /** Creates a cloud print printer and sets it as the default printer. |
| 297 * @param {string} printer_name The name of the printer to create. |
| 298 * @param {Object} cloud_print_data Data to be stored in cloudPrintOptions. |
| 299 * @param {function} add_callback The callback to be called to add the new |
| 300 * printer to the print preview UI. |
| 301 * @param {function} update_caps_callback The callback to be called to update |
| 302 * capabilities on the new printer. |
| 303 */ |
| 304 function setDefaultPrinter(printer_name, |
| 305 cloud_print_data, |
| 306 add_callback, |
| 307 update_caps_callback) { |
| 308 var printer = add_callback([JSON.parse(cloud_print_data)], false); |
| 309 if (printer) |
| 310 update_caps_callback(printer); |
| 311 } |
| 312 |
| 313 /** Returns the data necessary to serialize a cloud print printer. |
| 314 * @param {Object} printer The printer object to get data for. |
| 315 * @return {string} A JSON string that can be used to recreate the |
| 316 * cloud print portion of the printer object, or |
| 317 * an empty string if there is no data to save. |
| 318 */ |
| 319 function getData(printer) { |
| 320 if (isCloudPrint(printer)) { |
| 321 return JSON.stringify(printer.cloudPrintOptions); |
| 322 } else { |
| 323 return ""; |
| 324 } |
| 325 } |
| 326 |
| 327 /** Test if a printer is a cloud print printer. |
| 328 * @param {Object} printer The printer to test. |
| 329 * @return {boolean} true iff the printer is a cloud print printer. |
| 330 */ |
| 331 function isCloudPrint(printer) { |
| 332 return printer && printer.cloudPrintOptions != null; |
| 333 } |
| 334 |
| 335 /** Mark a printer as a cloud print printer and record its name and id. |
| 336 * @param {Object} printer The printer to mark. |
| 337 * @param {string} name The user visible name of the printer. |
| 338 * @param {string} id The id of the printer used by cloud print to |
| 339 * identify it. |
| 340 */ |
| 341 function setCloudPrint(printer, name, id) { |
| 342 if (!printer.cloudPrintOptions) { |
| 343 printer.cloudPrintOptions = new Object; |
| 344 } |
| 345 printer.cloudPrintOptions.name = name; |
| 346 printer.cloudPrintOptions.id = id; |
| 347 } |
| 348 |
| 349 return { |
| 350 colorIsDefault: colorIsDefault, |
| 351 fetchPrinters: fetchPrinters, |
| 352 getBaseURL: getBaseURL, |
| 353 getData: getData, |
| 354 getPrintTicketJSON: getPrintTicketJSON, |
| 355 isCloudPrint: isCloudPrint, |
| 356 printToCloud: printToCloud, |
| 357 setBaseURL: setBaseURL, |
| 358 setCloudPrint: setCloudPrint, |
| 359 setColor: setColor, |
| 360 setDefaultPrinter: setDefaultPrinter, |
| 361 supportsColor: supportsColor, |
| 362 updatePrinterCaps: updatePrinterCaps |
| 363 }; |
| 364 }); |
OLD | NEW |