OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('cloudprint', function() { | 5 cr.define('cloudprint', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * API to the Google Cloud Print service. | 9 * API to the Google Cloud Print service. |
10 * @param {string} baseUrl Base part of the Google Cloud Print service URL | 10 * @param {string} baseUrl Base part of the Google Cloud Print service URL |
11 * with no trailing slash. For example, | 11 * with no trailing slash. For example, |
12 * 'https://www.google.com/cloudprint'. | 12 * 'https://www.google.com/cloudprint'. |
13 * @constructor | 13 * @constructor |
14 * @extends {cr.EventTarget} | 14 * @extends {cr.EventTarget} |
15 */ | 15 */ |
16 function CloudPrintInterface(baseUrl) { | 16 function CloudPrintInterface(baseUrl) { |
17 /** | 17 /** |
18 * The base URL of the Google Cloud Print API. | 18 * The base URL of the Google Cloud Print API. |
19 * @type {string} | 19 * @type {string} |
20 * @private | 20 * @private |
21 */ | 21 */ |
22 this.baseURL_ = baseUrl; | 22 this.baseUrl_ = baseUrl; |
23 | 23 |
24 /** | 24 /** |
25 * Last received XSRF token. Sent as a parameter in every request. | 25 * Last received XSRF token. Sent as a parameter in every request. |
26 * @type {string} | 26 * @type {string} |
27 * @private | 27 * @private |
28 */ | 28 */ |
29 this.xsrfToken_ = ''; | 29 this.xsrfToken_ = ''; |
30 }; | 30 }; |
31 | 31 |
32 /** | 32 /** |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 * @enum {string} | 86 * @enum {string} |
87 * @private | 87 * @private |
88 */ | 88 */ |
89 CloudPrintInterface.JsonFields_ = { | 89 CloudPrintInterface.JsonFields_ = { |
90 PRINTER: 'printer' | 90 PRINTER: 'printer' |
91 }; | 91 }; |
92 | 92 |
93 CloudPrintInterface.prototype = { | 93 CloudPrintInterface.prototype = { |
94 __proto__: cr.EventTarget.prototype, | 94 __proto__: cr.EventTarget.prototype, |
95 | 95 |
| 96 /** @return {string} Base URL of the Google Cloud Print service. */ |
| 97 get baseUrl() { |
| 98 return this.baseUrl_; |
| 99 }, |
| 100 |
96 /** | 101 /** |
97 * Sends a Google Cloud Print search API request. | 102 * Sends a Google Cloud Print search API request. |
98 * @param {boolean} isRecent Whether to search for only recently used | 103 * @param {boolean} isRecent Whether to search for only recently used |
99 * printers. | 104 * printers. |
100 */ | 105 */ |
101 search: function(isRecent) { | 106 search: function(isRecent) { |
102 var params = [ | 107 var params = [ |
103 new HttpParam('connection_status', 'ALL'), | 108 new HttpParam('connection_status', 'ALL'), |
104 new HttpParam('client', 'chrome') | 109 new HttpParam('client', 'chrome') |
105 ]; | 110 ]; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 * @param {Array.<!HttpParam>} params HTTP parameters to include in the | 245 * @param {Array.<!HttpParam>} params HTTP parameters to include in the |
241 * request. | 246 * request. |
242 * @param {function(number, Object)} callback Callback to invoke when | 247 * @param {function(number, Object)} callback Callback to invoke when |
243 * request completes. | 248 * request completes. |
244 */ | 249 */ |
245 sendRequest_: function(method, action, params, callback) { | 250 sendRequest_: function(method, action, params, callback) { |
246 if (!this.xsrfToken_) { | 251 if (!this.xsrfToken_) { |
247 // TODO(rltoscano): Should throw an error if not a read-only action or | 252 // TODO(rltoscano): Should throw an error if not a read-only action or |
248 // issue an xsrf token request. | 253 // issue an xsrf token request. |
249 } | 254 } |
250 var url = this.baseURL_ + '/' + action + '?xsrf=' + this.xsrfToken_; | 255 var url = this.baseUrl_ + '/' + action + '?xsrf=' + this.xsrfToken_; |
251 var body = null; | 256 var body = null; |
252 | 257 |
253 if (params) { | 258 if (params) { |
254 if (method == 'GET') { | 259 if (method == 'GET') { |
255 url = params.reduce(function(partialUrl, param) { | 260 url = params.reduce(function(partialUrl, param) { |
256 return partialUrl + '&' + param.name + '=' + | 261 return partialUrl + '&' + param.name + '=' + |
257 encodeURIComponent(param.value); | 262 encodeURIComponent(param.value); |
258 }, url); | 263 }, url); |
259 } else if (method == 'POST') { | 264 } else if (method == 'POST') { |
260 body = params.reduce(function(partialBody, param) { | 265 body = params.reduce(function(partialBody, param) { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 * @type {string} | 443 * @type {string} |
439 */ | 444 */ |
440 this.value = value; | 445 this.value = value; |
441 }; | 446 }; |
442 | 447 |
443 // Export | 448 // Export |
444 return { | 449 return { |
445 CloudPrintInterface: CloudPrintInterface | 450 CloudPrintInterface: CloudPrintInterface |
446 }; | 451 }; |
447 }); | 452 }); |
OLD | NEW |