| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 /** | 5 /** |
| 6 * @fileoverview Implements a simple XmlHttpRequest-based text document | 6 * @fileoverview Implements a simple XmlHttpRequest-based text document |
| 7 * fetcher. | 7 * fetcher. |
| 8 * | 8 * |
| 9 */ | 9 */ |
| 10 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * A fetcher of text files. | 13 * A fetcher of text files. |
| 14 * @interface | 14 * @interface |
| 15 */ | 15 */ |
| 16 function TextFetcher() {} | 16 function TextFetcher() {} |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * @param {string} url The URL to fetch. | 19 * @param {string} url The URL to fetch. |
| 20 * @param {string?} opt_method The HTTP method to use (default GET) | 20 * @param {string?} opt_method The HTTP method to use (default GET) |
| 21 * @param {string?} opt_body The request body | 21 * @param {string?} opt_body The request body |
| 22 * @return {!Promise.<string>} A promise for the fetched text. In case of an | 22 * @return {!Promise<string>} A promise for the fetched text. In case of an |
| 23 * error, this promise is rejected with an HTTP status code. | 23 * error, this promise is rejected with an HTTP status code. |
| 24 */ | 24 */ |
| 25 TextFetcher.prototype.fetch = function(url, opt_method, opt_body) {}; | 25 TextFetcher.prototype.fetch = function(url, opt_method, opt_body) {}; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * @constructor | 28 * @constructor |
| 29 * @implements {TextFetcher} | 29 * @implements {TextFetcher} |
| 30 */ | 30 */ |
| 31 function XhrTextFetcher() { | 31 function XhrTextFetcher() { |
| 32 } | 32 } |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * @param {string} url The URL to fetch. | 35 * @param {string} url The URL to fetch. |
| 36 * @param {string?} opt_method The HTTP method to use (default GET) | 36 * @param {string?} opt_method The HTTP method to use (default GET) |
| 37 * @param {string?} opt_body The request body | 37 * @param {string?} opt_body The request body |
| 38 * @return {!Promise.<string>} A promise for the fetched text. In case of an | 38 * @return {!Promise<string>} A promise for the fetched text. In case of an |
| 39 * error, this promise is rejected with an HTTP status code. | 39 * error, this promise is rejected with an HTTP status code. |
| 40 */ | 40 */ |
| 41 XhrTextFetcher.prototype.fetch = function(url, opt_method, opt_body) { | 41 XhrTextFetcher.prototype.fetch = function(url, opt_method, opt_body) { |
| 42 return new Promise(function(resolve, reject) { | 42 return new Promise(function(resolve, reject) { |
| 43 var xhr = new XMLHttpRequest(); | 43 var xhr = new XMLHttpRequest(); |
| 44 var method = opt_method || 'GET'; | 44 var method = opt_method || 'GET'; |
| 45 xhr.open(method, url, true); | 45 xhr.open(method, url, true); |
| 46 xhr.onloadend = function() { | 46 xhr.onloadend = function() { |
| 47 if (xhr.status != 200) { | 47 if (xhr.status != 200) { |
| 48 reject(xhr.status); | 48 reject(xhr.status); |
| 49 return; | 49 return; |
| 50 } | 50 } |
| 51 resolve(xhr.responseText); | 51 resolve(xhr.responseText); |
| 52 }; | 52 }; |
| 53 xhr.onerror = function() { | 53 xhr.onerror = function() { |
| 54 // Treat any network-level errors as though the page didn't exist. | 54 // Treat any network-level errors as though the page didn't exist. |
| 55 reject(404); | 55 reject(404); |
| 56 }; | 56 }; |
| 57 if (opt_body) | 57 if (opt_body) |
| 58 xhr.send(opt_body); | 58 xhr.send(opt_body); |
| 59 else | 59 else |
| 60 xhr.send(); | 60 xhr.send(); |
| 61 }); | 61 }); |
| 62 }; | 62 }; |
| OLD | NEW |