Chromium Code Reviews| 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..e567904c9c5402eef1f342efb451781547606a23 |
| --- /dev/null |
| +++ b/chrome/browser/resources/print_preview_cloud.js |
| @@ -0,0 +1,86 @@ |
| +// 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 = ''; |
| + |
| + function setBaseURL(cloudPrintURL) { |
| + cloudPrintBaseURL = cloudPrintURL; |
| + } |
| + |
| + 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); |
| + xhr.setRequestHeader("Content-Type", |
| + "application/x-www-form-urlencoded;charset=UTF-8"); |
| + if (headers) { |
| + for (var header in headers) { |
| + if (headers.hasOwnProperty(header)) { |
| + xhr.setRequestHeader(header, headers[header]); |
| + } |
| + } |
| + } |
| + xhr.send(body); |
| + return xhr; |
| + } |
| + |
| + function fetchPrinters(callback) { |
| + try { |
| + var xhr = sendCloudPrintRequest('POST', |
| + 'search', |
| + {'Content-Type': |
| + 'application/x-www-form-urlencoded', |
| + 'X-CloudPrint-Proxy': |
| + 'ChromePrintPreview'}, |
| + '', |
| + 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); |
| + } |
| + } |
| + |
| + function printToCloudResponse(callback, xhr) { |
| + 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
|
| + var printResult = JSON.parse(xhr.responseText); |
| + if (printResult['success']) { |
| + callback.call(); |
| + } |
| + } |
| + } |
| + |
| + function printToCloud(data, callback) { |
| + sendCloudPrintRequest('POST', |
| + 'submit', |
| + {'X-CloudPrint-Proxy': 'ChromePrintPreview'}, |
| + data, |
| + printToCloudResponse.bind(this, callback), |
| + true); |
| + } |
| + |
| + return { |
| + fetchPrinters: fetchPrinters, |
| + printToCloud: printToCloud, |
| + setBaseURL: setBaseURL |
| + }; |
| +}); |