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

Side by Side Diff: chrome/browser/resources/print_preview/print_preview_cloud.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix fit-to-page tests Created 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 // The URL to use to access the cloud print servers.
8 // Set by a call to setBaseURL.
9 var cloudPrintBaseURL = '';
10
11 // Headers to set for most cloud print API calls.
12 var xCloudPrintURLHeader = {'Content-Type':
13 'application/x-www-form-urlencoded',
14 'X-CloudPrint-Proxy': 'ChromePrintPreview'};
15
16 // Headers to set when sending multipart data to cloud print APIs.
17 // Currently only used when submitting a job.
18 var xCloudPrintFormHeader = {'Content-Type':
19 'multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i',
20 'X-CloudPrint-Proxy': 'ChromePrintPreview'};
21
22 // The last received XSRF token. This should be sent with each request
23 // to prevent XSRF.
24 var lastXSRFToken = '';
25
26 /**
27 * Sets the base URL to be used for communicating with cloud print
28 * servers.
29 * @param {string} cloudPrintURL The URL to use.
30 */
31 function setBaseURL(cloudPrintURL) {
32 cloudPrintBaseURL = cloudPrintURL;
33 }
34
35 /**
36 * Gets the base URL to be used for communicating with cloud print
37 * servers.
38 * @return {string} The URL.
39 */
40 function getBaseURL() {
41 return cloudPrintBaseURL;
42 }
43
44 /**
45 * Extracts the XSRF token from each response to be used in the next
46 * request.
47 * @param {XMLHttpRequest} xhr The object used to make the request.
48 * @return {string} The extracted XSRF token.
49 */
50 function extractXSRFtoken(xhr) {
51 if (xhr.status == 200) {
52 var result = JSON.parse(xhr.responseText);
53 return result['xsrf_token'];
54 } else {
55 return null;
56 }
57 }
58
59 /**
60 * Makes a request to cloud print servers.
61 * @param {string} method The HTTP method to be used.
62 * @param {string} action The cloud print API to call.
63 * @param {Array} headers Headers to send with the request.
64 * @param {string} body Body to be sent with POST requests.
65 * @param {function} callback Function to be called to process response.
66 * @param {boolean} async True if we want the request made asyncronously.
67 */
68 function sendCloudPrintRequest(method,
69 action,
70 headers,
71 params,
72 body,
73 callback) {
74 var xhr = new XMLHttpRequest();
75 if (callback != null) {
76 xhr.onreadystatechange = function() {
77 if (xhr.readyState == 4) {
78 var updatedXSRFToken = extractXSRFtoken(xhr);
79 if (updatedXSRFToken != null) {
80 lastXSRFToken = updatedXSRFToken;
81 }
82 callback.call(this, xhr);
83 }
84 };
85 }
86 var url = cloudPrintBaseURL + '/' + action;
87 if (params == null) {
88 params = new Array;
89 }
90 if (lastXSRFToken.length != 0) {
91 params.push('xsrf=' + lastXSRFToken);
92 }
93 if (params.length != 0) {
94 url = url + '?';
95 for (param in params) {
96 url = url + params[param] + '&';
97 }
98 }
99 xhr.open(method, url, true);
100 xhr.withCredentials = true;
101 if (headers) {
102 for (var header in headers) {
103 if (headers.hasOwnProperty(header)) {
104 xhr.setRequestHeader(header, headers[header]);
105 }
106 }
107 }
108 xhr.send(body);
109 return xhr;
110 }
111
112 /**
113 * Parse the response from the fetchPrinters call.
114 * @param {function} callback Function to be called to process response.
115 * @param {XMLHttpRequest} xhr The object used to make the request.
116 */
117 function fetchPrintersResponse(callback, xhr) {
118 if (xhr.status == 200) {
119 var searchResult = JSON.parse(xhr.responseText);
120 if (searchResult['success']) {
121 var printerList = searchResult['printers'];
122 addCloudPrinters(printerList, callback);
123 return;
124 }
125 }
126 addCloudPrinters(null, callback);
127 }
128
129 /**
130 * Retrieve the list of printers available via cloud print.
131 * @param {function} callback Function to be called to process response.
132 */
133 function fetchPrinters(callback, all) {
134 var query = 'q=^recent';
135 if (all) {
136 query = '';
137 }
138 sendCloudPrintRequest('GET',
139 'search',
140 xCloudPrintURLHeader,
141 [query],
142 null,
143 fetchPrintersResponse.bind(this, callback));
144 }
145
146 /**
147 * Handle the response from printing to cloud print.
148 * @param {function} callback Function to be called to process response.
149 * @param {XMLHttpRequest} xhr The object used to make the request.
150 */
151 function printToCloudResponse(callback, xhr) {
152 if (xhr.status == 200) {
153 var printResult = JSON.parse(xhr.responseText);
154 if (printResult['success']) {
155 callback.call();
156 }
157 }
158 // TODO(abodenha@chromium.org) Catch and handle failures.
159 }
160
161 /**
162 * Send the current document to cloud print.
163 * @param {string} data The document to be printed.
164 * @param {function} callback Function to be called to process response.
165 */
166 function printToCloud(data, callback) {
167 // TODO(abodenha@chromium.org) Make sure we have an XSRF token before
168 // sending a submit. Right now if the user clicks print before we
169 // complete any request we wont have an XSRF and the submit will fail.
170 sendCloudPrintRequest('POST',
171 'submit',
172 xCloudPrintFormHeader,
173 null,
174 data,
175 printToCloudResponse.bind(this, callback));
176 }
177
178 /**
179 * Gets the JSON string used to control the behavior of the current
180 * print job.
181 * @param {Object} printer The printer object to get the ticket for.
182 * @return {string} The print ticket or null if not a cloud printer.
183 */
184 function getPrintTicketJSON(printer) {
185 if (isCloudPrint(printer)) {
186 return JSON.stringify({'capabilities':
187 [printer.cloudPrintOptions.colorOption]});
188 } else {
189 return null;
190 }
191 }
192
193 /**
194 * Process the response from cloud print containing the capabilities
195 * for the printer.
196 * @param {function} callback Function to be called to process response.
197 * @param {Object} printer The printer object to get the capabilites for.
198 * @param {XMLHttpRequest} xhr The object used to make the request.
199 */
200 function updatePrinterCapsResponse(callback, printer, xhr) {
201 if (xhr.status == 200) {
202 var printResult = JSON.parse(xhr.responseText);
203 if (printResult['success']) {
204 if (!printer.cloudPrintOptions)
205 printer.cloudPrintOptions = new Object;
206 printer.cloudPrintOptions.capsDownloaded = true;
207 printer.cloudPrintOptions.colorOption = null;
208 printer.cloudPrintOptions.colorIsDefault = false;
209 var detailedCapabilities = printResult.printers[0].capabilities;
210 for (var capability in detailedCapabilities) {
211 var cap = detailedCapabilities[capability];
212 if (cap.name == 'ns1:Colors') {
213 printer.cloudPrintOptions.colorOption = new Object;
214 printer.cloudPrintOptions.colorOption.name = cap.name;
215 printer.cloudPrintOptions.colorOption.type = cap.type;
216 for (var option in cap.options) {
217 var opt = cap.options[option];
218 if (opt.name == 'Color') {
219 printer.cloudPrintOptions.colorOnOption = opt;
220 }
221 if (opt.name == 'Grey_K') {
222 printer.cloudPrintOptions.colorOffOption = opt;
223 }
224 if (opt.default) {
225 printer.cloudPrintOptions.colorOption.options = [opt];
226 printer.cloudPrintOptions.colorIsDefault =
227 opt.name == printer.cloudPrintOptions.colorOnOption.name;
228 }
229 }
230 }
231 }
232 callback.call(this, printer);
233 }
234 }
235 }
236
237 /**
238 * Retrieve capabilities for a printer.
239 * @param {Object} printer The printer object to get the capabilities for.
240 * @param {function} callback Function to be called to process response.
241 */
242 function updatePrinterCaps(printer, callback) {
243 if (isCloudPrint(printer) && !printer.cloudPrintOptions.capsDownloaded) {
244 sendCloudPrintRequest('GET',
245 'printer?printerid=' +
246 printer.value +
247 '&output=json',
248 xCloudPrintURLHeader,
249 null,
250 null,
251 updatePrinterCapsResponse.bind(this,
252 callback,
253 printer));
254 } else {
255 callback.call(this, printer);
256 }
257 }
258
259 /**
260 * @param {Object} printer The printer object to get the data for.
261 * @return {boolean} true if the printer supports color.
262 */
263 function supportsColor(printer) {
264 return isCloudPrint(printer) &&
265 printer.cloudPrintOptions.colorOption != null;
266 }
267
268 /**
269 * @param {Object} printer The printer object to get the data for.
270 * @return {boolean} true if the printer defaults to color.
271 */
272 function colorIsDefault(printer) {
273 // For now assume that unsupported color means we just don't know
274 // and assume color.
275 return !supportsColor(printer) ||
276 (isCloudPrint(printer) && printer.cloudPrintOptions.colorIsDefault);
277 }
278
279 /**
280 * Turn color on or off for the specified printer.
281 * @param {Object} printer The printer object to turn color on/off for.
282 * @param {boolean} color True to turn color on.
283 */
284 function setColor(printer, color) {
285 if (isCloudPrint(printer) && supportsColor(printer)) {
286 if (color) {
287 printer.cloudPrintOptions.colorOption.options =
288 [printer.cloudPrintOptions.colorOnOption];
289 } else {
290 printer.cloudPrintOptions.colorOption.options =
291 [printer.cloudPrintOptions.colorOffOption];
292 }
293 }
294 }
295
296 /**
297 * Creates a cloud print printer and sets it as the default printer.
298 * @param {string} printer_name The name of the printer to create.
299 * @param {Object} cloud_print_data Data to be stored in cloudPrintOptions.
300 * @param {function} add_callback The callback to be called to add the new
301 * printer to the print preview UI.
302 * @param {function} update_caps_callback The callback to be called to update
303 * capabilities on the new printer.
304 */
305 function setDefaultPrinter(printer_name,
306 cloud_print_data,
307 add_callback,
308 update_caps_callback) {
309 var printer = addCloudPrinters([JSON.parse(cloud_print_data)],
310 add_callback);
311 if (printer)
312 update_caps_callback(printer);
313 }
314
315 /**
316 * Returns the data necessary to serialize a cloud print printer.
317 * @param {Object} printer The printer object to get data for.
318 * @return {string} A JSON string that can be used to recreate the
319 * cloud print portion of the printer object, or and empty string if
320 * there is no data to save.
321 */
322 function getData(printer) {
323 if (isCloudPrint(printer)) {
324 return JSON.stringify(printer.cloudPrintOptions);
325 } else {
326 return '';
327 }
328 }
329
330 /**
331 * Test if a printer is a cloud print printer.
332 * @param {Object} printer The printer to test.
333 * @return {boolean} true iff the printer is a cloud print printer.
334 */
335 function isCloudPrint(printer) {
336 return printer && printer.cloudPrintOptions != null;
337 }
338
339 /**
340 * Mark a printer as a cloud print printer and record its name and id.
341 * @param {Object} printer The printer to mark.
342 * @param {string} name The user visible name of the printer.
343 * @param {string} id The id of the printer used by cloud print to
344 * identify it.
345 */
346 function setCloudPrint(printer, name, id) {
347 if (!printer.cloudPrintOptions) {
348 printer.cloudPrintOptions = new Object;
349 }
350 printer.cloudPrintOptions.name = name;
351 printer.cloudPrintOptions.id = id;
352 }
353
354 /**
355 * Test if a particular cloud printer has already been added to the
356 * printer dropdown.
357 * @param {string} id A unique value to track this printer.
358 * @return {boolean} True if |id| has previously been passed to
359 * trackCloudPrinterAdded.
360 */
361 function cloudPrinterAlreadyAdded(id) {
362 return addedCloudPrinters[id];
363 }
364
365 /**
366 * Test if a particular printer has already been added to the printers
367 * dropdown. Records it if not.
368 * @param {string} id A unique value to track this printer.
369 * @return {boolean} False if adding this printer would exceed
370 * |maxCloudPrinters|.
371 */
372 function trackCloudPrinterAdded(id) {
373 if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) {
374 addedCloudPrinters[id] = true;
375 return true;
376 } else {
377 return false;
378 }
379 }
380
381 /**
382 * Add cloud printers to the list drop down.
383 * Called from the cloudprint object on receipt of printer information from
384 * the cloud print server.
385 * @param {Array} printers Array of printer info objects.
386 * @return {Object} The currently selected printer.
387 */
388 function addCloudPrinters(printers, addDestinationListOptionAtPosition) {
389 var isFirstPass = false;
390 var printerList = $('printer-list');
391
392 if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) {
393 isFirstPass = true;
394 // Remove empty entry added by setDefaultPrinter.
395 if (printerList[0] && printerList[0].textContent == '')
396 printerList.remove(0);
397 }
398 if (printers != null) {
399 for (var i = 0; i < printers.length; i++) {
400 if (!cloudPrinterAlreadyAdded(printers[i]['id'])) {
401 if (!trackCloudPrinterAdded(printers[i]['id'])) {
402 break;
403 }
404 if (printers[i]['displayName'] && printers[i]['displayName'] != '')
405 var name = printers[i]['displayName'];
406 else
407 var name = printers[i]['name'];
408
409 var option = addDestinationListOptionAtPosition(
410 lastCloudPrintOptionPos++,
411 name,
412 printers[i]['id'],
413 name == defaultOrLastUsedPrinterName,
414 false,
415 false);
416 cloudprint.setCloudPrint(option,
417 name,
418 printers[i]['id']);
419 }
420 }
421 } else {
422 if (!cloudPrinterAlreadyAdded(SIGN_IN)) {
423 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
424 localStrings.getString('signIn'),
425 SIGN_IN,
426 false,
427 false,
428 false);
429 trackCloudPrinterAdded(SIGN_IN);
430 chrome.send('signIn');
431 }
432 }
433 var selectedPrinter = printerList.selectedIndex;
434 if (selectedPrinter < 0)
435 return null;
436 return printerList.options[selectedPrinter];
437 }
438
439 return {
440 addCloudPrinters: addCloudPrinters,
441 colorIsDefault: colorIsDefault,
442 fetchPrinters: fetchPrinters,
443 getBaseURL: getBaseURL,
444 getData: getData,
445 getPrintTicketJSON: getPrintTicketJSON,
446 isCloudPrint: isCloudPrint,
447 printToCloud: printToCloud,
448 setBaseURL: setBaseURL,
449 setCloudPrint: setCloudPrint,
450 setColor: setColor,
451 setDefaultPrinter: setDefaultPrinter,
452 supportsColor: supportsColor,
453 updatePrinterCaps: updatePrinterCaps
454 };
455 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698