OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /// This contains a reader that accesses data using the HttpRequest |
6 * This contains a reader that accesses data using the HttpRequest | 6 /// facility, and thus works only in the web browser. |
7 * facility, and thus works only in the web browser. | |
8 */ | |
9 | 7 |
10 library http_request_data_reader; | 8 library http_request_data_reader; |
11 | 9 |
12 import 'dart:async'; | 10 import 'dart:async'; |
13 import 'dart:html'; | 11 import 'dart:html'; |
14 import 'intl_helpers.dart'; | 12 import 'intl_helpers.dart'; |
15 | 13 |
16 class HttpRequestDataReader implements LocaleDataReader { | 14 class HttpRequestDataReader implements LocaleDataReader { |
17 | 15 |
18 /** The base url from which we read the data. */ | 16 /// The base url from which we read the data. |
19 String url; | 17 String url; |
20 HttpRequestDataReader(this.url); | 18 HttpRequestDataReader(this.url); |
21 | 19 |
22 Future read(String locale) { | 20 Future read(String locale) { |
23 // TODO(alanknight): Remove this once it's not necessary for Chrome. | |
24 // Without it, the tests will be flaky on Chrome. Issue 11834. | |
25 var someNumber = new DateTime.now().millisecondsSinceEpoch; | |
26 var request = new HttpRequest(); | 21 var request = new HttpRequest(); |
27 request.timeout = 5000; | 22 request.timeout = 5000; |
28 return _getString('$url$locale.json?cacheBlocker=$someNumber', request) | 23 return _getString('$url$locale.json', request) |
29 .then((r) => r.responseText); | 24 .then((r) => r.responseText); |
30 } | 25 } |
31 | 26 |
32 /// Read a string with the given request. This is a stripped down copy | 27 /// Read a string with the given request. This is a stripped down copy |
33 /// of HttpRequest getString, but was the simplest way I could find to | 28 /// of HttpRequest getString, but was the simplest way I could find to |
34 /// issue a request with a timeout. | 29 /// issue a request with a timeout. |
35 Future<HttpRequest> _getString(String url, HttpRequest xhr) { | 30 Future<HttpRequest> _getString(String url, HttpRequest xhr) { |
36 var completer = new Completer<HttpRequest>(); | 31 var completer = new Completer<HttpRequest>(); |
37 xhr.open('GET', url, async: true); | 32 xhr.open('GET', url, async: true); |
38 xhr.onLoad.listen((e) { | 33 xhr.onLoad.listen((e) { |
39 // Note: file:// URIs have status of 0. | 34 // Note: file:// URIs have status of 0. |
40 if ((xhr.status >= 200 && xhr.status < 300) || | 35 if ((xhr.status >= 200 && xhr.status < 300) || |
41 xhr.status == 0 || | 36 xhr.status == 0 || |
42 xhr.status == 304) { | 37 xhr.status == 304) { |
43 completer.complete(xhr); | 38 completer.complete(xhr); |
44 } else { | 39 } else { |
45 completer.completeError(e); | 40 completer.completeError(e); |
46 } | 41 } |
47 }); | 42 }); |
48 | 43 |
49 xhr.onError.listen(completer.completeError); | 44 xhr.onError.listen(completer.completeError); |
50 xhr.send(); | 45 xhr.send(); |
51 | 46 |
52 return completer.future; | 47 return completer.future; |
53 } | 48 } |
54 } | 49 } |
OLD | NEW |