| // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| part of $LIBRARYNAME; |
| |
| $(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| /** |
| * Checks to see if Indexed DB is supported on the current platform. |
| */ |
| static bool get supported { |
| $if DARTIUM |
| return true; |
| $else |
| return JS('bool', |
| '!!(window.indexedDB || ' |
| 'window.webkitIndexedDB || ' |
| 'window.mozIndexedDB)'); |
| $endif |
| } |
| |
| @DomName('IDBFactory.open') |
| Future<Database> open(String name, |
| {int version, void onUpgradeNeeded(VersionChangeEvent), |
| void onBlocked(Event)}) { |
| if ((version == null) != (onUpgradeNeeded == null)) { |
| return new Future.error(new ArgumentError( |
| 'version and onUpgradeNeeded must be specified together')); |
| } |
| try { |
| var request; |
| if (version != null) { |
| request = $dom_open(name, version); |
| } else { |
| request = $dom_open(name); |
| } |
| |
| if (onUpgradeNeeded != null) { |
| request.onUpgradeNeeded.listen(onUpgradeNeeded); |
| } |
| if (onBlocked != null) { |
| request.onBlocked.listen(onBlocked); |
| } |
| return _completeRequest(request); |
| } catch (e, stacktrace) { |
| return new Future.error(e, stacktrace); |
| } |
| } |
| |
| @DomName('IDBFactory.deleteDatabase') |
| Future<IdbFactory> deleteDatabase(String name, |
| {void onBlocked(Event)}) { |
| try { |
| var request = $dom_deleteDatabase(name); |
| |
| if (onBlocked != null) { |
| request.onBlocked.listen(onBlocked); |
| } |
| var completer = new Completer.sync(); |
| request.onSuccess.listen((e) { |
| completer.complete(this); |
| }); |
| request.onError.listen((e) { |
| completer.completeError(e); |
| }); |
| return completer.future; |
| } catch (e, stacktrace) { |
| return new Future.error(e, stacktrace); |
| } |
| } |
| |
| @DomName('IDBFactory.getDatabaseNames') |
| @SupportedBrowser(SupportedBrowser.CHROME) |
| @Experimental() |
| Future<List<String>> getDatabaseNames() { |
| try { |
| var request = $dom_webkitGetDatabaseNames(); |
| |
| return _completeRequest(request); |
| } catch (e, stacktrace) { |
| return new Future.error(e, stacktrace); |
| } |
| } |
| |
| /** |
| * Checks to see if getDatabaseNames is supported by the current platform. |
| */ |
| bool get supportsDatabaseNames { |
| $if DART2JS |
| return supported && JS('bool', |
| '!!(#.getDatabaseNames || #.webkitGetDatabaseNames)', this, this); |
| $else |
| return true; |
| $endif |
| } |
| |
| $!MEMBERS |
| } |
| |
| |
| /** |
| * Ties a request to a completer, so the completer is completed when it succeeds |
| * and errors out when the request errors. |
| */ |
| Future _completeRequest(Request request) { |
| var completer = new Completer.sync(); |
| // TODO: make sure that completer.complete is synchronous as transactions |
| // may be committed if the result is not processed immediately. |
| request.onSuccess.listen((e) { |
| completer.complete(request.result); |
| }); |
| request.onError.listen((e) { |
| completer.completeError(e); |
| }); |
| return completer.future; |
| } |