| 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 // TODO(vsm): Unify with Dartium version. | 5 // TODO(vsm): Unify with Dartium version. |
| 6 class _DOMWindowCrossFrameImpl implements Window { | 6 class _DOMWindowCrossFrameImpl implements Window { |
| 7 // Private window. Note, this is a window in another frame, so it | 7 // Private window. Note, this is a window in another frame, so it |
| 8 // cannot be typed as "Window" as its prototype is not patched | 8 // cannot be typed as "Window" as its prototype is not patched |
| 9 // properly. Its fields and methods can only be accessed via JavaScript. | 9 // properly. Its fields and methods can only be accessed via JavaScript. |
| 10 var _window; | 10 var _window; |
| 11 | 11 |
| 12 // Fields. | 12 // Fields. |
| 13 // TODO(vsm): Implement history and location getters. | 13 History get history => |
| 14 _HistoryCrossFrameImpl._createSafe(JS('History', '#.history', _window)); |
| 15 Location get location => |
| 16 _LocationCrossFrameImpl._createSafe(JS('Location', '#.location', _window)); |
| 14 | 17 |
| 15 // TODO(vsm): Add frames to navigate subframes. See 2312. | 18 // TODO(vsm): Add frames to navigate subframes. See 2312. |
| 16 | 19 |
| 17 bool get closed => _closed(_window); | 20 bool get closed => JS('bool', '#.closed', _window); |
| 18 static bool _closed(win) native "return win.closed;"; | |
| 19 | 21 |
| 20 Window get opener => _createSafe(_opener(_window)); | 22 Window get opener => _createSafe(JS('Window', '#.opener', _window)); |
| 21 static Window _opener(win) native "return win.opener;"; | |
| 22 | 23 |
| 23 Window get parent => _createSafe(_parent(_window)); | 24 Window get parent => _createSafe(JS('Window', '#.parent', _window)); |
| 24 static Window _parent(win) native "return win.parent;"; | |
| 25 | 25 |
| 26 Window get top => _createSafe(_top(_window)); | 26 Window get top => _createSafe(JS('Window', '#.top', _window)); |
| 27 static Window _top(win) native "return win.top;"; | |
| 28 | 27 |
| 29 // Methods. | 28 // Methods. |
| 30 void focus() => _focus(_window); | 29 void focus() => JS('void', '#.focus()', _window); |
| 31 static void _focus(win) native "win.focus()"; | |
| 32 | 30 |
| 33 void blur() => _blur(_window); | 31 void blur() => JS('void', '#.blur()', _window); |
| 34 static void _blur(win) native "win.blur()"; | |
| 35 | 32 |
| 36 void close() => _close(_window); | 33 void close() => JS('void', '#.close()', _window); |
| 37 static void _close(win) native "win.close()"; | |
| 38 | 34 |
| 39 void postMessage(Dynamic message, | 35 void postMessage(Dynamic message, |
| 40 String targetOrigin, | 36 String targetOrigin, |
| 41 [List messagePorts = null]) { | 37 [List messagePorts = null]) { |
| 42 if (messagePorts == null) { | 38 if (messagePorts == null) { |
| 43 _postMessage2(_window, message, targetOrigin); | 39 JS('void', '#.postMessage(#,#)', _window, message, targetOrigin); |
| 44 } else { | 40 } else { |
| 45 _postMessage3(_window, message, targetOrigin, messagePorts); | 41 JS('void', '#.postMessage(#,#,#)', _window, message, targetOrigin, message
Ports); |
| 46 } | 42 } |
| 47 } | 43 } |
| 48 | 44 |
| 49 // TODO(vsm): This is a hack to workaround dartbug.com/3175. We | |
| 50 // need a more robust convention to invoke JS methods on the | |
| 51 // underlying window. | |
| 52 static void _postMessage2(win, message, targetOrigin) native """ | |
| 53 win.postMessage(message, targetOrigin); | |
| 54 """; | |
| 55 | |
| 56 static void _postMessage3(win, message, targetOrigin, messagePorts) native """ | |
| 57 win.postMessage(message, targetOrigin, messagePorts); | |
| 58 """; | |
| 59 | |
| 60 // Implementation support. | 45 // Implementation support. |
| 61 _DOMWindowCrossFrameImpl(this._window); | 46 _DOMWindowCrossFrameImpl(this._window); |
| 62 | 47 |
| 63 static Window _createSafe(w) { | 48 static Window _createSafe(w) { |
| 64 if (w === window) { | 49 if (w === window) { |
| 65 return w; | 50 return w; |
| 66 } else { | 51 } else { |
| 67 // TODO(vsm): Cache or implement equality. | 52 // TODO(vsm): Cache or implement equality. |
| 68 return new _DOMWindowCrossFrameImpl(w); | 53 return new _DOMWindowCrossFrameImpl(w); |
| 69 } | 54 } |
| 70 } | 55 } |
| 71 } | 56 } |
| 57 |
| 58 class _LocationCrossFrameImpl implements Location { |
| 59 // Private location. Note, this is a location object in another frame, so it |
| 60 // cannot be typed as "Location" as its prototype is not patched |
| 61 // properly. Its fields and methods can only be accessed via JavaScript. |
| 62 var _location; |
| 63 |
| 64 void set href(String val) => _setHref(_location, val); |
| 65 static void _setHref(location, val) { |
| 66 JS('void', '#.href = #', _location, val); |
| 67 } |
| 68 |
| 69 // Implementation support. |
| 70 _LocationCrossFrameImpl(this._location); |
| 71 |
| 72 static Location _createSafe(location) { |
| 73 if (location === window.location) { |
| 74 return location; |
| 75 } else { |
| 76 // TODO(vsm): Cache or implement equality. |
| 77 return new _LocationCrossFrameImpl(location); |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 class _HistoryCrossFrameImpl implements History { |
| 83 // Private history. Note, this is a history object in another frame, so it |
| 84 // cannot be typed as "History" as its prototype is not patched |
| 85 // properly. Its fields and methods can only be accessed via JavaScript. |
| 86 var _history; |
| 87 |
| 88 void back() => JS('void', '#.back()', _history); |
| 89 |
| 90 void forward() => JS('void', '#.forward()', _history); |
| 91 |
| 92 void go(int distance) => JS('void', '#.go(#)', _history, distance); |
| 93 |
| 94 // Implementation support. |
| 95 _HistoryCrossFrameImpl(this._history); |
| 96 |
| 97 static History _createSafe(h) { |
| 98 if (h === window.history) { |
| 99 return h; |
| 100 } else { |
| 101 // TODO(vsm): Cache or implement equality. |
| 102 return new _HistoryCrossFrameImpl(h); |
| 103 } |
| 104 } |
| 105 } |
| OLD | NEW |