Chromium Code Reviews| 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 | |
| 9 function setBaseURL(cloudPrintURL) { | |
| 10 cloudPrintBaseURL = cloudPrintURL; | |
| 11 } | |
| 12 | |
| 13 function sendCloudPrintRequest(method, | |
| 14 action, | |
| 15 headers, | |
| 16 body, | |
| 17 callback, | |
| 18 async) { | |
| 19 var xhr = new XMLHttpRequest(); | |
| 20 if (callback != null) { | |
| 21 xhr.onreadystatechange = function() { | |
| 22 if (xhr.readyState == 4) { | |
| 23 callback.call(this, xhr); | |
| 24 } | |
| 25 }; | |
| 26 } | |
| 27 var url = cloudPrintBaseURL + '/' + action | |
| 28 xhr.open(method, url, async); | |
| 29 xhr.setRequestHeader("Content-Type", | |
| 30 "application/x-www-form-urlencoded;charset=UTF-8"); | |
| 31 if (headers) { | |
| 32 for (var header in headers) { | |
| 33 if (headers.hasOwnProperty(header)) { | |
| 34 xhr.setRequestHeader(header, headers[header]); | |
| 35 } | |
| 36 } | |
| 37 } | |
| 38 xhr.send(body); | |
| 39 return xhr; | |
| 40 } | |
| 41 | |
| 42 function fetchPrinters(callback) { | |
| 43 try { | |
| 44 var xhr = sendCloudPrintRequest('POST', | |
| 45 'search', | |
| 46 {'Content-Type': | |
| 47 'application/x-www-form-urlencoded', | |
| 48 'X-CloudPrint-Proxy': | |
| 49 'ChromePrintPreview'}, | |
| 50 '', | |
| 51 null, | |
| 52 false); | |
| 53 var searchResult = JSON.parse(xhr.responseText); | |
| 54 if (searchResult['success']) { | |
| 55 var printerList = searchResult['printers']; | |
| 56 callback.call(this, printerList); | |
| 57 } | |
| 58 } catch (err) { | |
| 59 callback.call(this, null); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 function printToCloudResponse(callback, xhr) { | |
| 64 if (xhr.status == 200) { | |
|
sanjeevr
2011/05/24 23:21:25
Don't we need to update some error status if it fa
Albert Bodenhamer
2011/06/09 17:33:11
Some sort of feedback would definitely be good. I
| |
| 65 var printResult = JSON.parse(xhr.responseText); | |
| 66 if (printResult['success']) { | |
| 67 callback.call(); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 function printToCloud(data, callback) { | |
| 73 sendCloudPrintRequest('POST', | |
| 74 'submit', | |
| 75 {'X-CloudPrint-Proxy': 'ChromePrintPreview'}, | |
| 76 data, | |
| 77 printToCloudResponse.bind(this, callback), | |
| 78 true); | |
| 79 } | |
| 80 | |
| 81 return { | |
| 82 fetchPrinters: fetchPrinters, | |
| 83 printToCloud: printToCloud, | |
| 84 setBaseURL: setBaseURL | |
| 85 }; | |
| 86 }); | |
| OLD | NEW |