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

Side by Side 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: Review feedback Created 9 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // Sets the base URL to be used for communicating with cloud print
10 // servers.
11 function setBaseURL(cloudPrintURL) {
12 cloudPrintBaseURL = cloudPrintURL;
13 }
14
15 // Gets the base URL to be used for communicating with cloud print
16 // servers.
17 function getBaseURL() {
18 return cloudPrintBaseURL;
19 }
20
21 // Makes a request to cloud print servers.
22 function sendCloudPrintRequest(method,
23 action,
24 headers,
25 body,
26 callback,
27 async) {
28 var xhr = new XMLHttpRequest();
29 if (callback != null) {
30 xhr.onreadystatechange = function() {
31 if (xhr.readyState == 4) {
32 callback.call(this, xhr);
33 }
34 };
35 }
36 var url = cloudPrintBaseURL + '/' + action
37 xhr.open(method, url, async);
38 xhr.setRequestHeader("Content-Type",
39 "multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i");
40 if (headers) {
41 for (var header in headers) {
42 if (headers.hasOwnProperty(header)) {
43 xhr.setRequestHeader(header, headers[header]);
44 }
45 }
46 }
47 xhr.send(body);
48 return xhr;
49 }
50
51 // Retrieve the list of printers available via cloud print.
52 function fetchPrinters(callback) {
53 try {
54 var xhr = sendCloudPrintRequest('POST',
55 'search',
56 {'Content-Type':
57 'application/x-www-form-urlencoded',
58 'X-CloudPrint-Proxy':
59 'ChromePrintPreview'},
60 '',
61 null,
62 false);
63 var searchResult = JSON.parse(xhr.responseText);
64 if (searchResult['success']) {
65 var printerList = searchResult['printers'];
66 callback.call(this, printerList);
67 }
68 } catch (err) {
69 callback.call(this, null);
70 }
71 }
72
73 // Handle the response from printing to cloud print.
74 function printToCloudResponse(callback, xhr) {
75 if (xhr.status == 200) {
76 var printResult = JSON.parse(xhr.responseText);
77 if (printResult['success']) {
78 callback.call();
79 }
80 }
81 // TODO(abodenha@chromium.org) Catch and handle failures.
82 }
83
84 // Send the current document to cloud print.
85 function printToCloud(data, callback) {
86 sendCloudPrintRequest('POST',
87 'submit',
88 {'X-CloudPrint-Proxy': 'ChromePrintPreview'},
89 data,
90 printToCloudResponse.bind(this, callback),
91 true);
92 }
93
94 // Gets the JSON string used to control the behavior of the current
95 // print job.
96 function getPrintTicketJSON(printer) {
97 return JSON.stringify({'capabilities':
98 [printer.cloudprintOptions.colorOption]});
99 }
100
101 // Process the response from cloud print containing the capabilities
102 // for the printer.
103 function updatePrinterCapsResponse(callback, printer, xhr) {
104 if (xhr.status == 200) {
105 var printResult = JSON.parse(xhr.responseText);
106 if (printResult['success']) {
107 printer.cloudprintOptions = new Object;
108 printer.cloudprintOptions.colorOption = null;
109 printer.cloudprintOptions.colorIsDefault = false;
110 var detailedCapabilities = printResult.printers[0].capabilities;
111 for (var capability in detailedCapabilities) {
112 var cap = detailedCapabilities[capability];
113 if (cap.name == 'ns1:Colors') {
114 printer.cloudprintOptions.colorOption = new Object;
115 printer.cloudprintOptions.colorOption.name = cap.name;
116 printer.cloudprintOptions.colorOption.type = cap.type;
117 for (var option in cap.options) {
118 var opt = cap.options[option];
119 if (opt.name == "Color") {
120 printer.cloudprintOptions.colorOnOption = opt;
121 }
122 if (opt.name == "Grey_K") {
123 printer.cloudprintOptions.colorOffOption = opt;
124 }
125 if (opt.default) {
126 printer.cloudprintOptions.colorOption.options = [opt];
127 printer.cloudprintOptions.colorIsDefault =
128 opt.name == printer.cloudprintOptions.colorOnOption.name;
129 }
130 }
131 }
132 }
133 callback.call(this, printer);
134 }
135 }
136 }
137
138 // Retrieve capabilities for a printer.
139 function updatePrinterCaps(printer, callback) {
140 sendCloudPrintRequest('GET',
141 'printer?printerid=' + printer.value + '&output=json',
142 {'X-CloudPrint-Proxy': 'ChromePrintPreview'},
sanjeevr 2011/06/10 18:13:48 You should probably define a constant for header v
Albert Bodenhamer 2011/06/10 23:13:45 Done.
143 null,
144 updatePrinterCapsResponse.bind(this,
145 callback,
146 printer),
147 true);
148 }
149
150 // Returns true if the printer supports color.
151 function supportsColor(printer) {
152 return printer.cloudprintOptions.colorOption != null;
153 }
154
155 // Returns true if the printer has color enabled by default.
156 function colorIsDefault(printer) {
157 return printer.cloudprintOptions.colorIsDefault;
158 }
159
160 // Turn color on or off for the specified printer.
161 function setColor(printer, color) {
162 if (color) {
163 printer.cloudprintOptions.colorOption.options =
164 [printer.cloudprintOptions.colorOnOption];
165 } else {
166 printer.cloudprintOptions.colorOption.options =
167 [printer.cloudprintOptions.colorOffOption];
168 }
169 }
170
171 return {
172 colorIsDefault: colorIsDefault,
173 fetchPrinters: fetchPrinters,
174 getPrintTicketJSON: getPrintTicketJSON,
175 getBaseURL: getBaseURL,
176 supportsColor: supportsColor,
177 printToCloud: printToCloud,
178 setBaseURL: setBaseURL,
179 setColor: setColor,
180 updatePrinterCaps: updatePrinterCaps
181 };
182 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698