Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(959)

Unified Diff: chrome/browser/resources/print_preview_cloud.js

Issue 7038028: Initial support for cloudprint in print preview (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix bad merge Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
+ };
+});

Powered by Google App Engine
This is Rietveld 408576698