blob: 7e9ee41aaa11b51c96fcb5a961f021fb0fd2db69 [file] [log] [blame]
// 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.
// Conversions for Window. These check if the window is the local
// window, and if it's not, wraps or unwraps it with a secure wrapper.
// We need to test for EventTarget here as well as it's a base type.
// We omit an unwrapper for Window as no methods take a non-local
// window as a parameter.
part of html;
DateTime _convertNativeToDart_DateTime(date) {
var millisSinceEpoch = JS('int', '#.getTime()', date);
return new DateTime.fromMillisecondsSinceEpoch(millisSinceEpoch, isUtc: true);
}
_convertDartToNative_DateTime(DateTime date) {
return JS('', 'new Date(#)', date.millisecondsSinceEpoch);
}
WindowBase _convertNativeToDart_Window(win) {
return _DOMWindowCrossFrame._createSafe(win);
}
EventTarget _convertNativeToDart_EventTarget(e) {
// Assume it's a Window if it contains the setInterval property. It may be
// from a different frame - without a patched prototype - so we cannot
// rely on Dart type checking.
if (JS('bool', r'"setInterval" in #', e))
return _DOMWindowCrossFrame._createSafe(e);
else
return e;
}
EventTarget _convertDartToNative_EventTarget(e) {
if (e is _DOMWindowCrossFrame) {
return e._window;
} else {
return e;
}
}
// Conversions for ImageData
//
// On Firefox, the returned ImageData is a plain object.
class _TypedImageData implements ImageData {
final Uint8ClampedArray data;
final int height;
final int width;
_TypedImageData(this.data, this.height, this.width);
}
ImageData _convertNativeToDart_ImageData(nativeImageData) {
// None of the native getters that return ImageData have the type ImageData
// since that is incorrect for FireFox (which returns a plain Object). So we
// need something that tells the compiler that the ImageData class has been
// instantiated.
// TODO(sra): Remove this when all the ImageData returning APIs have been
// annotated as returning the union ImageData + Object.
JS('ImageData', '0');
if (nativeImageData is ImageData) return nativeImageData;
// On Firefox the above test fails because imagedata is a plain object.
// So we create a _TypedImageData.
return new _TypedImageData(
JS('var', '#.data', nativeImageData),
JS('var', '#.height', nativeImageData),
JS('var', '#.width', nativeImageData));
}
// We can get rid of this conversion if _TypedImageData implements the fields
// with native names.
_convertDartToNative_ImageData(ImageData imageData) {
if (imageData is _TypedImageData) {
return JS('', '{data: #, height: #, width: #}',
imageData.data, imageData.height, imageData.width);
}
return imageData;
}