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 http.io; |
| 6 |
| 7 @MirrorsUsed(targets: const ['dart.io.HttpClient', 'dart.io.HttpException', |
| 8 'dart.io.File']) |
| 9 import 'dart:mirrors'; |
| 10 |
| 11 /// Whether `dart:io` is supported on this platform. |
| 12 bool get supported => _library != null; |
| 13 |
| 14 /// The `dart:io` library mirror, or `null` if it couldn't be loaded. |
| 15 final _library = _getLibrary(); |
| 16 |
| 17 /// The `dart:io` HttpClient class mirror. |
| 18 final ClassMirror _httpClient = |
| 19 _library.declarations[const Symbol('HttpClient')]; |
| 20 |
| 21 /// The `dart:io` HttpException class mirror. |
| 22 final ClassMirror _httpException = |
| 23 _library.declarations[const Symbol('HttpException')]; |
| 24 |
| 25 /// The `dart:io` File class mirror. |
| 26 final ClassMirror _file = _library.declarations[const Symbol('File')]; |
| 27 |
| 28 /// Asserts that the [name]d `dart:io` feature is supported on this platform. |
| 29 /// |
| 30 /// If `dart:io` doesn't work on this platform, this throws an |
| 31 /// [UnsupportedError]. |
| 32 void assertSupported(String name) { |
| 33 if (supported) return; |
| 34 throw new UnsupportedError("$name isn't supported on this platform."); |
| 35 } |
| 36 |
| 37 /// Creates a new `dart:io` HttpClient instance. |
| 38 newHttpClient() => _httpClient.newInstance(const Symbol(''), []).reflectee; |
| 39 |
| 40 /// Creates a new `dart:io` File instance with the given [path]. |
| 41 newFile(String path) => _file.newInstance(const Symbol(''), [path]).reflectee; |
| 42 |
| 43 /// Returns whether [error] is a `dart:io` HttpException. |
| 44 bool isHttpException(error) => reflect(error).type.isSubtypeOf(_httpException); |
| 45 |
| 46 /// Tries to load `dart:io` and returns `null` if it fails. |
| 47 LibraryMirror _getLibrary() { |
| 48 try { |
| 49 return currentMirrorSystem().findLibrary(const Symbol('dart.io')); |
| 50 } catch (_) { |
| 51 // TODO(nweiz): narrow the catch clause when issue 18532 is fixed. |
| 52 return null; |
| 53 } |
| 54 } |
OLD | NEW |