OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library usage_impl_io; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:convert' show JSON; | |
9 import 'dart:io'; | |
10 | |
11 import 'package:path/path.dart' as path; | |
12 | |
13 import 'usage_impl.dart'; | |
14 | |
15 String _createUserAgent() { | |
16 // Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) | |
17 // Dart/1.8.0-edge.41170 (macos; macos; macos; null) | |
18 String os = Platform.operatingSystem; | |
19 String locale = Platform.environment['LANG']; | |
20 return "Dart/${_dartVersion()} (${os}; ${os}; ${os}; ${locale})"; | |
21 } | |
22 | |
23 String _userHomeDir() { | |
24 String envKey = Platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME'; | |
25 String value = Platform.environment[envKey]; | |
26 return value == null ? '.' : value; | |
27 } | |
28 | |
29 String _dartVersion() { | |
30 String ver = Platform.version; | |
31 int index = ver.indexOf(' '); | |
32 if (index != -1) ver = ver.substring(0, index); | |
33 return ver; | |
34 } | |
35 | |
36 class IOPostHandler extends PostHandler { | |
37 final String _userAgent; | |
38 final HttpClient mockClient; | |
39 | |
40 IOPostHandler({HttpClient this.mockClient}) : _userAgent = _createUserAgent(); | |
41 | |
42 Future sendPost(String url, Map<String, String> parameters) { | |
43 // Add custom parameters for OS and the Dart version. | |
44 parameters['cd1'] = Platform.operatingSystem; | |
45 parameters['cd2'] = 'dart ${_dartVersion()}'; | |
46 | |
47 String data = postEncode(parameters); | |
48 | |
49 HttpClient client = mockClient != null ? mockClient : new HttpClient(); | |
50 client.userAgent = _userAgent; | |
51 return client.postUrl(Uri.parse(url)).then((HttpClientRequest req) { | |
52 req.write(data); | |
53 return req.close(); | |
54 }).then((HttpClientResponse response) { | |
55 response.drain(); | |
56 }).catchError((e) { | |
57 // Catch errors that can happen during a request, but that we can't do | |
58 // anything about, e.g. a missing internet conenction. | |
59 }); | |
60 } | |
61 } | |
62 | |
63 class IOPersistentProperties extends PersistentProperties { | |
64 File _file; | |
65 Map _map; | |
66 | |
67 IOPersistentProperties(String name) : super(name) { | |
68 String fileName = '.${name.replaceAll(' ', '_')}'; | |
69 _file = new File(path.join(_userHomeDir(), fileName)); | |
70 _file.createSync(); | |
71 String contents = _file.readAsStringSync(); | |
72 if (contents.isEmpty) contents = '{}'; | |
73 _map = JSON.decode(contents); | |
74 } | |
75 | |
76 dynamic operator[](String key) => _map[key]; | |
77 | |
78 void operator[]=(String key, dynamic value) { | |
79 if (value == null) { | |
80 _map.remove(key); | |
81 } else { | |
82 _map[key] = value; | |
83 } | |
84 | |
85 _file.writeAsStringSync(JSON.encode(_map) + '\n'); | |
86 } | |
87 } | |
OLD | NEW |