OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('cloudprint', function() { | 5 cr.define('cloudprint', function() { |
6 | 6 |
7 // The URL to use to access the cloud print servers. | 7 // The URL to use to access the cloud print servers. |
8 // Set by a call to setBaseURL. | 8 // Set by a call to setBaseURL. |
9 var cloudPrintBaseURL = ''; | 9 var cloudPrintBaseURL = ''; |
10 | 10 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 /** | 112 /** |
113 * Parse the response from the fetchPrinters call. | 113 * Parse the response from the fetchPrinters call. |
114 * @param {function} callback Function to be called to process response. | 114 * @param {function} callback Function to be called to process response. |
115 * @param {XMLHttpRequest} xhr The object used to make the request. | 115 * @param {XMLHttpRequest} xhr The object used to make the request. |
116 */ | 116 */ |
117 function fetchPrintersResponse(callback, xhr) { | 117 function fetchPrintersResponse(callback, xhr) { |
118 if (xhr.status == 200) { | 118 if (xhr.status == 200) { |
119 var searchResult = JSON.parse(xhr.responseText); | 119 var searchResult = JSON.parse(xhr.responseText); |
120 if (searchResult['success']) { | 120 if (searchResult['success']) { |
121 var printerList = searchResult['printers']; | 121 var printerList = searchResult['printers']; |
122 addCloudPrinters(printerList, callback); | 122 callback.call(this, printerList); |
123 return; | 123 } else { |
| 124 callback.call(this, null); |
124 } | 125 } |
| 126 } else { |
| 127 callback.call(this, null); |
125 } | 128 } |
126 addCloudPrinters(null, callback); | |
127 } | 129 } |
128 | 130 |
129 /** | 131 /** |
130 * Retrieve the list of printers available via cloud print. | 132 * Retrieve the list of printers available via cloud print. |
131 * @param {function} callback Function to be called to process response. | 133 * @param {function} callback Function to be called to process response. |
132 */ | 134 */ |
133 function fetchPrinters(callback, all) { | 135 function fetchPrinters(callback, all) { |
134 var query = 'q=^recent'; | 136 var query = 'q=^recent'; |
135 if (all) { | 137 if (all) { |
136 query = ''; | 138 query = ''; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 * @param {Object} cloud_print_data Data to be stored in cloudPrintOptions. | 299 * @param {Object} cloud_print_data Data to be stored in cloudPrintOptions. |
298 * @param {function} add_callback The callback to be called to add the new | 300 * @param {function} add_callback The callback to be called to add the new |
299 * printer to the print preview UI. | 301 * printer to the print preview UI. |
300 * @param {function} update_caps_callback The callback to be called to update | 302 * @param {function} update_caps_callback The callback to be called to update |
301 * capabilities on the new printer. | 303 * capabilities on the new printer. |
302 */ | 304 */ |
303 function setDefaultPrinter(printer_name, | 305 function setDefaultPrinter(printer_name, |
304 cloud_print_data, | 306 cloud_print_data, |
305 add_callback, | 307 add_callback, |
306 update_caps_callback) { | 308 update_caps_callback) { |
307 var printer = addCloudPrinters([JSON.parse(cloud_print_data)], | 309 var printer = add_callback([JSON.parse(cloud_print_data)]); |
308 add_callback); | |
309 if (printer) | 310 if (printer) |
310 update_caps_callback(printer); | 311 update_caps_callback(printer); |
311 } | 312 } |
312 | 313 |
313 /** | 314 /** |
314 * Returns the data necessary to serialize a cloud print printer. | 315 * Returns the data necessary to serialize a cloud print printer. |
315 * @param {Object} printer The printer object to get data for. | 316 * @param {Object} printer The printer object to get data for. |
316 * @return {string} A JSON string that can be used to recreate the | 317 * @return {string} A JSON string that can be used to recreate the |
317 * cloud print portion of the printer object, or and empty string if | 318 * cloud print portion of the printer object, or and empty string if |
318 * there is no data to save. | 319 * there is no data to save. |
(...skipping 23 matching lines...) Expand all Loading... |
342 * identify it. | 343 * identify it. |
343 */ | 344 */ |
344 function setCloudPrint(printer, name, id) { | 345 function setCloudPrint(printer, name, id) { |
345 if (!printer.cloudPrintOptions) { | 346 if (!printer.cloudPrintOptions) { |
346 printer.cloudPrintOptions = new Object; | 347 printer.cloudPrintOptions = new Object; |
347 } | 348 } |
348 printer.cloudPrintOptions.name = name; | 349 printer.cloudPrintOptions.name = name; |
349 printer.cloudPrintOptions.id = id; | 350 printer.cloudPrintOptions.id = id; |
350 } | 351 } |
351 | 352 |
352 /** | |
353 * Test if a particular cloud printer has already been added to the | |
354 * printer dropdown. | |
355 * @param {string} id A unique value to track this printer. | |
356 * @return {boolean} True if |id| has previously been passed to | |
357 * trackCloudPrinterAdded. | |
358 */ | |
359 function cloudPrinterAlreadyAdded(id) { | |
360 return addedCloudPrinters[id]; | |
361 } | |
362 | |
363 /** | |
364 * Test if a particular printer has already been added to the printers | |
365 * dropdown. Records it if not. | |
366 * @param {string} id A unique value to track this printer. | |
367 * @return {boolean} False if adding this printer would exceed | |
368 * |maxCloudPrinters|. | |
369 */ | |
370 function trackCloudPrinterAdded(id) { | |
371 if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) { | |
372 addedCloudPrinters[id] = true; | |
373 return true; | |
374 } else { | |
375 return false; | |
376 } | |
377 } | |
378 | |
379 /** | |
380 * Add cloud printers to the list drop down. | |
381 * Called from the cloudprint object on receipt of printer information from | |
382 * the cloud print server. | |
383 * @param {Array} printers Array of printer info objects. | |
384 * @return {Object} The currently selected printer. | |
385 */ | |
386 function addCloudPrinters(printers, addDestinationListOptionAtPosition) { | |
387 var isFirstPass = false; | |
388 var printerList = $('printer-list'); | |
389 | |
390 if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) { | |
391 isFirstPass = true; | |
392 // Remove empty entry added by setDefaultPrinter. | |
393 if (printerList[0] && printerList[0].textContent == '') | |
394 printerList.remove(0); | |
395 } | |
396 if (printers != null) { | |
397 for (var i = 0; i < printers.length; i++) { | |
398 if (!cloudPrinterAlreadyAdded(printers[i]['id'])) { | |
399 if (!trackCloudPrinterAdded(printers[i]['id'])) { | |
400 break; | |
401 } | |
402 var option = addDestinationListOptionAtPosition( | |
403 lastCloudPrintOptionPos++, | |
404 printers[i]['name'], | |
405 printers[i]['id'], | |
406 printers[i]['name'] == defaultOrLastUsedPrinterName, | |
407 false, | |
408 false); | |
409 cloudprint.setCloudPrint(option, | |
410 printers[i]['name'], | |
411 printers[i]['id']); | |
412 } | |
413 } | |
414 } else { | |
415 if (!cloudPrinterAlreadyAdded(SIGN_IN)) { | |
416 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, | |
417 localStrings.getString('signIn'), | |
418 SIGN_IN, | |
419 false, | |
420 false, | |
421 false); | |
422 trackCloudPrinterAdded(SIGN_IN); | |
423 } | |
424 } | |
425 var selectedPrinter = printerList.selectedIndex; | |
426 if (selectedPrinter < 0) | |
427 return null; | |
428 return printerList.options[selectedPrinter]; | |
429 } | |
430 | |
431 return { | 353 return { |
432 addCloudPrinters: addCloudPrinters, | |
433 colorIsDefault: colorIsDefault, | 354 colorIsDefault: colorIsDefault, |
434 fetchPrinters: fetchPrinters, | 355 fetchPrinters: fetchPrinters, |
435 getBaseURL: getBaseURL, | 356 getBaseURL: getBaseURL, |
436 getData: getData, | 357 getData: getData, |
437 getPrintTicketJSON: getPrintTicketJSON, | 358 getPrintTicketJSON: getPrintTicketJSON, |
438 isCloudPrint: isCloudPrint, | 359 isCloudPrint: isCloudPrint, |
439 printToCloud: printToCloud, | 360 printToCloud: printToCloud, |
440 setBaseURL: setBaseURL, | 361 setBaseURL: setBaseURL, |
441 setCloudPrint: setCloudPrint, | 362 setCloudPrint: setCloudPrint, |
442 setColor: setColor, | 363 setColor: setColor, |
443 setDefaultPrinter: setDefaultPrinter, | 364 setDefaultPrinter: setDefaultPrinter, |
444 supportsColor: supportsColor, | 365 supportsColor: supportsColor, |
445 updatePrinterCaps: updatePrinterCaps | 366 updatePrinterCaps: updatePrinterCaps |
446 }; | 367 }; |
447 }); | 368 }); |
OLD | NEW |