1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
// Copyright (c) 2012 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() {
'use strict';
/**
* API to the Google Cloud Print service.
* @param {string} baseUrl Base part of the Google Cloud Print service URL
* with no trailing slash. For example,
* 'https://www.google.com/cloudprint'.
* @constructor
* @extends {cr.EventTarget}
*/
function CloudPrintInterface(baseUrl) {
/**
* The base URL of the Google Cloud Print API.
* @type {string}
* @private
*/
this.baseURL_ = baseUrl;
/**
* Last received XSRF token. Sent as a parameter in every request.
* @type {string}
* @private
*/
this.xsrfToken_ = '';
};
/**
* Event types dispatched by the interface.
* @enum {string}
*/
CloudPrintInterface.EventType = {
ERROR: 'cloudprint.CloudPrintInterface.ERROR',
PRINTER_DONE: 'cloudprint.CloudPrintInterface.PRINTER_DONE',
SEARCH_DONE: 'cloudprint.CloudPrintInterface.SEARCH_DONE',
SUBMIT_DONE: 'cloudprint.CloudPrintInterface.SUBMIT_DONE'
};
/**
* Content type header value for a URL encoded HTTP request.
* @type {string}
* @private
*/
CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_ =
'application/x-www-form-urlencoded';
/**
* Content type header value for a multipart HTTP request.
* @type {string}
* @private
*/
CloudPrintInterface.MULTIPART_CONTENT_TYPE_ =
'multipart/form-data; boundary=----CloudPrintFormBoundaryjc9wuprokl8i';
/**
* Enumeration of JSON response fields from Google Cloud Print API.
* @enum {string}
* @private
*/
CloudPrintInterface.JsonFields_ = {
PRINTER: 'printer'
};
CloudPrintInterface.prototype = {
__proto__: cr.EventTarget.prototype,
/**
* Sends a Google Cloud Print search API request.
* @param {boolean} isRecent Whether to search for only recently used
* printers.
*/
search: function(isRecent) {
var params = {};
if (isRecent) {
params['q'] = '^recent';
}
params['connection_status'] = 'ALL';
params['client'] = 'chrome';
this.sendRequest_('GET', 'search', params, null, this.onSearchDone_);
},
/**
* Sends a Google Cloud Print submit API request.
* @param {string} body Body of the HTTP post request to send.
*/
submit: function(body) {
this.sendRequest_('POST', 'submit', null, body, this.onSubmitDone_);
},
/**
* Sends a Google Cloud Print printer API request.
* @param {string} printerId ID of the printer to lookup.
*/
printer: function(printerId) {
var params = {'printerid': printerId};
this.sendRequest_('GET', 'printer', params, null, this.onPrinterDone_);
},
/**
* Sends a Google Cloud Print update API request to accept (or reject) the
* terms-of-service of the given printer.
* @param {string} printerId ID of the printer to accept the
* terms-of-service for.
* @param {boolean} isAccepted Whether the user accepted the
* terms-of-service.
*/
updatePrinterTosAcceptance: function(printerId, isAccepted) {
var params = {
'printerid': printerId,
'is_tos_accepted': isAccepted
};
this.sendRequest_('POST', 'update', params, null,
this.onUpdatePrinterTosAcceptanceDone_);
},
/**
* Creates an object that represents a Google Cloud Print print ticket.
* @param {!print_preview.Destination} destination Destination to print to.
* @param {!print_preview.PrintTicketStore} printTicketStore Used to create
* the state of the print ticket.
* @return {!Object} Google Cloud Print print ticket.
*/
createPrintTicket: function(destination, printTicketStore) {
assert(!destination.isLocal,
'Trying to create a Google Cloud Print print ticket for a local ' +
'destination');
assert(destination.capabilities,
'Trying to create a Google Cloud Print print ticket for a ' +
'destination with no print capabilities');
var ticketItems = [];
if (destination.capabilities.collateCapability) {
var collateCap = destination.capabilities.collateCapability;
var ticketItem = {
'name': collateCap.id,
'type': collateCap.type,
'options': [{'name': printTicketStore.isCollateEnabled() ?
collateCap.collateOption : collateCap.noCollateOption}]
};
ticketItems.push(ticketItem);
}
if (destination.capabilities.colorCapability) {
var colorCap = destination.capabilities.colorCapability;
var ticketItem = {
'name': colorCap.id,
'type': colorCap.type,
'options': [{'name': printTicketStore.isColorEnabled() ?
colorCap.colorOption : colorCap.bwOption}]
};
ticketItems.push(ticketItem);
}
if (destination.capabilities.copiesCapability) {
var copiesCap = destination.capabilities.copiesCapability;
var ticketItem = {
'name': copiesCap.id,
'type': copiesCap.type,
'value': printTicketStore.getCopies()
};
ticketItems.push(ticketItem);
}
if (destination.capabilities.duplexCapability) {
var duplexCap = destination.capabilities.duplexCapability;
var ticketItem = {
'name': duplexCap.id,
'type': duplexCap.type,
'options': [{'name': printTicketStore.isDuplexEnabled() ?
duplexCap.longEdgeOption : duplexCap.simplexOption}]
};
ticketItems.push(ticketItem);
}
return {
'capabilities': ticketItems
};
},
/**
* Sends a request to the Google Cloud Print API.
* @param {string} method HTTP method of the request.
* @param {string} action Google Cloud Print action to perform.
* @param {Object} params HTTP parameters to include in the request.
* @param {string} body HTTP multi-part encoded body.
* @param {function(Object)} successCallback Callback to invoke when request
* completes successfully.
*/
sendRequest_: function(method, action, params, body, successCallback) {
if (!this.xsrfToken_) {
// TODO(rltoscano): Should throw an error if not a read-only action or
// issue an xsrf token request.
}
var url = this.baseURL_ + '/' + action + '?xsrf=' + this.xsrfToken_;
if (params) {
for (var paramName in params) {
url += '&' + paramName + '=' + encodeURIComponent(params[paramName]);
}
}
var headers = {};
headers['X-CloudPrint-Proxy'] = 'ChromePrintPreview';
if (method == 'GET') {
headers['Content-Type'] = CloudPrintInterface.URL_ENCODED_CONTENT_TYPE_;
} else if (method == 'POST') {
headers['Content-Type'] = CloudPrintInterface.MULTIPART_CONTENT_TYPE_;
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = this.onReadyStateChange_.bind(
this, xhr, successCallback.bind(this));
xhr.open(method, url, true);
xhr.withCredentials = true;
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
xhr.send(body);
},
/**
* Dispatches an ERROR event with the given error message.
* @param {string} message Error message to include in the ERROR event.
* @private
*/
dispatchErrorEvent_: function(message) {
var errorEvent = new cr.Event(CloudPrintInterface.EventType.ERROR);
errorEvent.message = message;
this.dispatchEvent(errorEvent);
},
/**
* Called when the ready-state of a XML http request changes.
* Calls the successCallback with the result or dispatches an ERROR event.
* @param {XMLHttpRequest} xhr XML http request that changed.
* @param {function(Object)} successCallback Callback to call if the request
* was successful.
* @private
*/
onReadyStateChange_: function(xhr, successCallback) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var result = JSON.parse(xhr.responseText);
if (result['success']) {
this.xsrfToken_ = result['xsrf_token'];
successCallback(result);
} else {
this.dispatchErrorEvent_(result['message']);
}
} else {
this.dispatchErrorEvent_(xhr.status + '');
}
}
},
/**
* Called when the search request completes successfully.
* @param {Object} result JSON response.
* @private
*/
onSearchDone_: function(result) {
var printerListJson = result['printers'] || [];
var printerList = [];
printerListJson.forEach(function(printerJson) {
try {
printerList.push(
cloudprint.CloudDestinationParser.parse(printerJson));
} catch (err) {
console.error('Unable to parse cloud print destination: ' + err);
}
});
var isRecent = result['request']['params']['q'] == '^recent';
var searchDoneEvent =
new cr.Event(CloudPrintInterface.EventType.SEARCH_DONE);
searchDoneEvent.printers = printerList;
searchDoneEvent.isRecent = isRecent;
searchDoneEvent.email = result['request']['user'];
this.dispatchEvent(searchDoneEvent);
},
/**
* Called when the submit request completes successfully.
* @param {Object} result JSON response.
* @private
*/
onSubmitDone_: function(result) {
var submitDoneEvent = new cr.Event(
CloudPrintInterface.EventType.SUBMIT_DONE);
submitDoneEvent.jobId = result['job']['id'];
this.dispatchEvent(submitDoneEvent);
},
/**
* Called when the printer request completes successfully.
* @param {Object} result JSON response.
* @private
*/
onPrinterDone_: function(result) {
// TODO(rltoscano): Better error handling here.
var printerJson = result['printers'][0];
var printer;
try {
printer = cloudprint.CloudDestinationParser.parse(printerJson);
} catch (err) {
console.error('Failed to parse cloud print destination: ' +
JSON.stringify(printerJson));
return;
}
var printerDoneEvent =
new cr.Event(CloudPrintInterface.EventType.PRINTER_DONE);
printerDoneEvent.printer = printer;
this.dispatchEvent(printerDoneEvent);
},
/**
* Called when the update printer TOS acceptance request completes
* successfully.
* @param {Object} result JSON response.
* @private
*/
onUpdatePrinterTosAcceptanceDone_: function(result) {
// Do nothing.
}
};
// Export
return {
CloudPrintInterface: CloudPrintInterface
};
});
|